This commit is contained in:
Lucio Lelii 2016-09-30 16:27:04 +00:00
parent e5c5e37594
commit fb3f20124f
3 changed files with 127 additions and 71 deletions

View File

@ -35,6 +35,10 @@ public interface AuthorizationProxy {
String requestActivation(ContainerInfo container) throws Exception;
public Map<String, String> retrieveApiKeys() throws Exception;
String requestActivation(ContainerInfo container, String context) throws Exception;
Map<String, String> retrieveApiKeys() throws Exception;
//File getSymmKey(String filePath) throws Exception;
}

View File

@ -184,18 +184,20 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy {
}
@Override
public String requestActivation(ContainerInfo container) throws Exception {
public String requestActivation(ContainerInfo container, String context) throws Exception {
String methodPath = "/token/node";
int infrastructureHash = Utils.getInfrastructureHashFromToken(SecurityTokenProvider.instance.get(), endpoints.getDefaultInfrastructure());
StringBuilder callUrl =
new StringBuilder(getInternalEnpoint(infrastructureHash)).append(methodPath);
StringBuilder callUrl;
callUrl = new StringBuilder(getInternalEnpoint(infrastructureHash)).append(methodPath);
if (context!=null) callUrl.append("?context=").append(context);
URL url = new URL(callUrl.toString());
HttpURLConnection connection = makeRequest(url, "PUT", false);
HttpURLConnection connection = makeRequest(url, "PUT", true);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("Content-type", "application/xml");
@ -218,6 +220,13 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy {
return Utils.addInfrastructureHashToToken(token, infrastructureHash);
}
@Override
public String requestActivation(ContainerInfo container) throws Exception {
return requestActivation(container, null);
}
@Override
public AuthorizationEntry get(String token) throws ObjectNotFound, Exception{
String realToken = Utils.getRealToken(token);
@ -234,7 +243,7 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy {
final String methodPath = "/token/";
StringBuilder callUrl = new StringBuilder(getInternalEnpoint(infrastructureHashFromToken))
.append(methodPath).append(realToken);
.append(methodPath).append(realToken);
URL url = new URL(callUrl.toString());
HttpURLConnection connection = makeRequest(url, "GET", false);
@ -303,10 +312,42 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy {
}
}
/*
@Override
public File getSymmKey(String filePath) throws Exception{
final String methodPath = "/symmKey/";
StringBuilder callUrl = new StringBuilder(getInternalEnpoint(Utils.getInfrastructureHashFromToken(SecurityTokenProvider.instance.get(), endpoints.getDefaultInfrastructure())))
.append(methodPath);
URL url = new URL(callUrl.toString());
HttpURLConnection connection = makeRequest(url, "GET", true);
connection.setDoInput(true);
if (connection.getResponseCode()!=200) throw new Exception("error retrieving policies");
if (connection.getContentLengthLong()<=0) return null;
String resourceName = (String)connection.getHeaderField("resource-name");
File toReturnFile = new File(filePath+"/"+resourceName);
toReturnFile.createNewFile();
try(InputStream stream = (InputStream)connection.getContent();
OutputStream os = new FileOutputStream(filePath)){
int read = 0;
byte[] bytes = new byte[1024];
while ((read = stream.read(bytes)) != -1) {
os.write(bytes, 0, read);
}
}
return toReturnFile;
}*/
private HttpURLConnection makeRequest(URL url, String method, boolean includeTokenInHeader) throws Exception{
HttpURLConnection connection;
if (url.toString().startsWith("https://"))
connection = (HttpsURLConnection)url.openConnection();
connection = (HttpsURLConnection)url.openConnection();
else connection = (HttpURLConnection)url.openConnection();
if (includeTokenInHeader){

View File

@ -25,9 +25,7 @@ public class CallTest {
@Test
public void requestNodeToken() throws Exception {
SecurityTokenProvider.instance.set(requestTestToken("/gcube/devNext/NextNext"));
String token = authorizationService().requestActivation(new ContainerInfo("node11.d.d4science.research-infrastructures.eu",8080));
System.out.println(token);
System.out.println(_requestNodeToken());
}
@Test
@ -55,12 +53,19 @@ public class CallTest {
public void createKeyWithError() throws Exception {
authorizationService().generateApiKey("TEST");
}
/*
@Test
public void getSymmKey() throws Exception{
SecurityTokenProvider.instance.set(_requestNodeToken());
authorizationService().getSymmKey("/tmp");
}*/
@Test
public void createKey() throws Exception {
String token = requestTestToken("/gcube/devNext/NextNext");
String token = requestTestToken("/gcube");
SecurityTokenProvider.instance.set(token);
String key = authorizationService().generateApiKey("TEST");
String key = authorizationService().generateApiKey("PIPPO");
System.out.println("key : "+key);
System.out.println(resolveToken(key));
}
@ -74,6 +79,12 @@ public class CallTest {
}
public String _requestNodeToken() throws Exception {
SecurityTokenProvider.instance.set(requestTestToken("/gcube/devNext/NextNext"));
String token = authorizationService().requestActivation(new ContainerInfo("mynode",8080));
return token;
}
@Test
public void createTestToken() throws Exception {
System.out.println(requestTestToken("/gcube/devNext/NextNext"));