token ora scope made mandatory

This commit is contained in:
lucio 2019-11-06 15:31:21 +01:00
parent 858c935b6c
commit eb40fd0eeb
4 changed files with 32 additions and 30 deletions

View File

@ -21,7 +21,7 @@
<groupId>org.gcube.resources</groupId> <groupId>org.gcube.resources</groupId>
<artifactId>registry-publisher</artifactId> <artifactId>registry-publisher</artifactId>
<version>1.3.0-SNAPSHOT</version> <version>1.3.1-SNAPSHOT</version>
<dependencies> <dependencies>

View File

@ -40,7 +40,7 @@ public class CollectorStubs implements RegistryStub {
StringBuilder callUrl = new StringBuilder(endopoint).append("/").append(type); StringBuilder callUrl = new StringBuilder(endopoint).append("/").append(type);
try { try {
URL url = new URL(callUrl.toString()); URL url = new URL(callUrl.toString());
HttpURLConnection connection = makeRequest(url, "POST", false); HttpURLConnection connection = makeRequest(url, "POST");
connection.setDoOutput(true); connection.setDoOutput(true);
connection.setRequestProperty("Content-type", "text/xml"); connection.setRequestProperty("Content-type", "text/xml");
@ -63,7 +63,7 @@ public class CollectorStubs implements RegistryStub {
StringBuilder callUrl = new StringBuilder(endopoint).append("/").append(type).append("/").append(id); StringBuilder callUrl = new StringBuilder(endopoint).append("/").append(type).append("/").append(id);
try { try {
URL url = new URL(callUrl.toString()); URL url = new URL(callUrl.toString());
HttpURLConnection connection = makeRequest(url, "PUT", false); HttpURLConnection connection = makeRequest(url, "PUT");
connection.setDoOutput(true); connection.setDoOutput(true);
connection.setRequestProperty("Content-type", "text/xml"); connection.setRequestProperty("Content-type", "text/xml");
@ -85,7 +85,7 @@ public class CollectorStubs implements RegistryStub {
StringBuilder callUrl = new StringBuilder(endopoint).append("/").append(type).append("/").append(id); StringBuilder callUrl = new StringBuilder(endopoint).append("/").append(type).append("/").append(id);
try { try {
URL url = new URL(callUrl.toString()); URL url = new URL(callUrl.toString());
HttpURLConnection connection = makeRequest(url, "DELETE", false); HttpURLConnection connection = makeRequest(url, "DELETE");
connection.setDoInput(true); connection.setDoInput(true);
if (connection.getResponseCode()!=200){ if (connection.getResponseCode()!=200){
log.info("response code is not 200"); log.info("response code is not 200");
@ -99,19 +99,18 @@ public class CollectorStubs implements RegistryStub {
private HttpURLConnection makeRequest(URL url, String method, boolean includeTokenInHeader) throws Exception{ private HttpURLConnection makeRequest(URL url, String method) throws Exception{
HttpURLConnection connection; HttpURLConnection connection;
if (url.toString().startsWith("https://")) if (url.toString().startsWith("https://"))
connection = (HttpsURLConnection)url.openConnection(); connection = (HttpsURLConnection)url.openConnection();
else connection = (HttpURLConnection)url.openConnection(); else connection = (HttpURLConnection)url.openConnection();
if (includeTokenInHeader){ if (SecurityTokenProvider.instance.get()!=null)
if (SecurityTokenProvider.instance.get()==null) throw new RuntimeException("null token passed");
connection.setRequestProperty(TOKEN_HEADER_ENTRY,SecurityTokenProvider.instance.get()); connection.setRequestProperty(TOKEN_HEADER_ENTRY,SecurityTokenProvider.instance.get());
} else { else if (ScopeProvider.instance.get()!=null)
if (ScopeProvider.instance.get()==null) throw new RuntimeException("null scope passed");
connection.setRequestProperty(SCOPE_HEADER_ENTRY,ScopeProvider.instance.get()); connection.setRequestProperty(SCOPE_HEADER_ENTRY,ScopeProvider.instance.get());
} else throw new RuntimeException("Collector requires authorization (via token or scope)");
connection.setRequestMethod(method); connection.setRequestMethod(method);
return connection; return connection;
} }

View File

@ -2,6 +2,8 @@ package org.gcube.informationsystem.publisher;
import static org.gcube.common.resources.gcore.Resources.print; import static org.gcube.common.resources.gcore.Resources.print;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.resources.gcore.GCoreEndpoint; import org.gcube.common.resources.gcore.GCoreEndpoint;
import org.gcube.common.resources.gcore.Resource; import org.gcube.common.resources.gcore.Resource;
import org.gcube.common.resources.gcore.Resource.Type; import org.gcube.common.resources.gcore.Resource.Type;
@ -14,38 +16,39 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
public class GCoreEndpointPublisherTests { public class GCoreEndpointPublisherTests {
private static final Logger log = LoggerFactory.getLogger(RegistryPublisherTests.class); private static final Logger log = LoggerFactory.getLogger(RegistryPublisherTests.class);
static GCoreEndpoint running; static GCoreEndpoint running;
static RegistryPublisher rp; static RegistryPublisher rp;
static Resource r; static Resource r;
@BeforeClass @BeforeClass
public static void init(){ public static void init(){
// ScopeProvider.instance.set("/d4science.research-infrastructures.eu/EUBrazilOpenBio"); // ScopeProvider.instance.set("/d4science.research-infrastructures.eu/EUBrazilOpenBio");
ScopeProvider.instance.set("/gcube/devNext"); ScopeProvider.instance.set("/gcube/devNext");
running = Resources.unmarshal(GCoreEndpoint.class, PublisherTest.class.getClassLoader().getResourceAsStream("gCoreEndpoint.xml")); SecurityTokenProvider.instance.set("52b59669-ccde-46d2-a4da-108b9e941f7c-98187548");
rp=RegistryPublisherFactory.create(); rp=RegistryPublisherFactory.create();
} }
@Test @Test
public void printTest(){ public void printTest(){
print(running); //print(running);
//resource-specific tests //resource-specific tests
assertEquals(Type.GCOREENDPOINT,running.type()); assertEquals(Type.GCOREENDPOINT,running.type());
} }
@Test @Test
public void registerCreate(){ public void registerCreate(){
r=rp.create(running);
System.out.println("new resource created: "); for (int i = 0; i<10; i++) {
if(r!=null) running = Resources.unmarshal(GCoreEndpoint.class, PublisherTest.class.getClassLoader().getResourceAsStream("gCoreEndpoint.xml"));
print(r); r=rp.create(running);
assertEquals(running,r); System.out.println("new resource created: "+r.id());
}
} }
@AfterClass /*@AfterClass
public static void forceDeleteResource(){ public static void forceDeleteResource(){
try { try {
Thread.sleep(3000); Thread.sleep(3000);
@ -59,8 +62,8 @@ public class GCoreEndpointPublisherTests {
advancedPublisher.forceRemove(r); advancedPublisher.forceRemove(r);
ScopeProvider.instance.set(currentScope); ScopeProvider.instance.set(currentScope);
} }*/
} }

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Resource version="0.4.x"> <Resource version="0.4.x">
<ID>5d244ab0-9d79-11e3-af18-dd5904b11dd6</ID> <ID></ID>
<Type>RunningInstance</Type> <Type>RunningInstance</Type>
<Scopes> <Scopes>
</Scopes> </Scopes>