request for bunch token creation for service and container added

master
lucio 4 years ago
parent 94d9901b11
commit 9ed284e2d1

@ -22,6 +22,7 @@
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="org.eclipse.jst.component.nondependency" value=""/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">

@ -5,6 +5,11 @@
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.wst.common.project.facet.core.builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
@ -15,9 +20,17 @@
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
</natures>
</projectDescription>

@ -24,7 +24,7 @@
</fileSets>
<files>
<file>
<source>target/${build.finalName}.war</source>
<source>target/${build.finalName}.jar</source>
<outputDirectory>/${artifactId}</outputDirectory>
</file>
</files>

@ -77,7 +77,7 @@
<outputDirectory>target</outputDirectory>
<resources>
<resource>
<directory>${distroDirectory}</directory>
<directory>.</directory>
<filtering>true</filtering>
<includes>
<include>profile.xml</include>

@ -12,6 +12,7 @@ import org.gcube.common.authorization.library.policies.Policy;
import org.gcube.common.authorization.library.provider.ClientInfo;
import org.gcube.common.authorization.library.provider.ServiceInfo;
import org.gcube.common.authorization.library.provider.UserInfo;
import org.gcube.common.authorization.library.utils.AuthorizationEntryList;
import org.gcube.common.authorization.library.utils.ListMapper;
public class Binder {
@ -21,7 +22,7 @@ public class Binder {
public static JAXBContext getContext() throws JAXBException{
if (context==null)
context = JAXBContext.newInstance(ExternalServiceList.class, QualifiersList.class, AuthorizationEntry.class, ClientInfo.class, UserInfo.class,
ServiceInfo.class, Policies.class, Policy.class, ListMapper.class);
ServiceInfo.class, Policies.class, Policy.class, ListMapper.class, AuthorizationEntryList.class);
return context;
}

@ -12,6 +12,7 @@ import org.gcube.common.authorization.library.policies.Policy;
import org.gcube.common.authorization.library.provider.ContainerInfo;
import org.gcube.common.authorization.library.provider.ServiceInfo;
import org.gcube.common.authorization.library.provider.UserInfo;
import org.gcube.common.authorization.library.utils.MultiServiceTokenRequest;
public interface AuthorizationProxy {
@ -20,6 +21,8 @@ public interface AuthorizationProxy {
void setEndpoint(EndpointsContainer endpoints);
AuthorizationEntry get(String token) throws ObjectNotFound, Exception;
List<AuthorizationEntry> get(List<String> tokens) throws ObjectNotFound, Exception;
void addPolicies(List<Policy> policies) throws Exception;
@ -30,6 +33,9 @@ public interface AuthorizationProxy {
String generateApiKey(String apiQualifier) throws Exception;
String generateServiceToken(ServiceInfo client) throws Exception;
List<String> generateServiceToken(ServiceInfo client, List<String> containerTokens) throws Exception;
String generateUserToken(UserInfo client, String context)
throws Exception;

@ -36,7 +36,9 @@ import org.gcube.common.authorization.library.provider.ContainerInfo;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.authorization.library.provider.ServiceInfo;
import org.gcube.common.authorization.library.provider.UserInfo;
import org.gcube.common.authorization.library.utils.AuthorizationEntryList;
import org.gcube.common.authorization.library.utils.ListMapper;
import org.gcube.common.authorization.library.utils.MultiServiceTokenRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -55,10 +57,15 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy {
private String getInternalEnpoint(int infrastructureHash){
AuthorizationEndpoint ae = getEndpoint(infrastructureHash);
return getInternalEnpoint(ae);
}
private String getInternalEnpoint(AuthorizationEndpoint ae){
StringBuilder endpoint = new StringBuilder(ae.isSecureConnection()?"https://":"http://").append(ae.getHost()).append(":")
.append(ae.getPort()).append("/authorization-service/gcube/service");
return endpoint.toString();
}
@Override
public String generateServiceToken(ServiceInfo client) throws Exception {
@ -74,7 +81,7 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy {
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("Content-type", "application/xml");
try(OutputStream os = new BufferedOutputStream(connection.getOutputStream())){
Binder.getContext().createMarshaller().marshal(client, os);
}
@ -94,6 +101,44 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy {
return Utils.addInfrastructureHashToToken(token, infrastructureHash);
}
@Override
public List<String> generateServiceToken(ServiceInfo client, List<String> containerTokens) throws Exception {
String methodPath = "/token/service/bunch";
int infrastructureHash = Utils.getInfrastructureHashFromToken(SecurityTokenProvider.instance.get(), endpoints.getDefaultInfrastructure());
StringBuilder callUrl = new StringBuilder(getInternalEnpoint(infrastructureHash)).append(methodPath);
URL url = new URL(callUrl.toString());
HttpURLConnection connection = makeRequest(url, "PUT", true);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("Content-type", "application/xml");
try(OutputStream os = new BufferedOutputStream(connection.getOutputStream())){
Binder.getContext().createMarshaller().marshal(new MultiServiceTokenRequest(containerTokens, client), os);
}
log.debug("response code for "+callUrl.toString()+" is "+connection.getResponseCode()+" "+connection.getResponseMessage());
if (connection.getResponseCode()!=200) throw new Exception("error contacting authorization service");
List<String> tokensToReturn = new ArrayList<String>();
try(InputStream stream = (InputStream)connection.getContent();){
ListMapper entries = (ListMapper)Binder.getContext().createUnmarshaller().unmarshal(stream);
for (String token: entries.getList())
tokensToReturn.add(Utils.addInfrastructureHashToToken(token, infrastructureHash));
return tokensToReturn;
}
}
@Override
public String generateExternalServiceToken(String serviceId) throws Exception {
@ -122,7 +167,7 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy {
return Utils.addInfrastructureHashToToken(token, infrastructureHash);
}
@Override
public String resolveTokenByUserAndContext(String user, String context) throws ObjectNotFound, Exception {
@ -134,7 +179,7 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy {
URL url = new URL(callUrl.toString());
HttpURLConnection connection = makeRequest(url, "GET", false);
log.debug("response code for "+callUrl.toString()+" is "+connection.getResponseCode()+" "+connection.getResponseMessage());
if (connection.getResponseCode()==404) throw new ObjectNotFound("token not found");
@ -151,7 +196,7 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy {
return Utils.addInfrastructureHashToToken(token, infrastructureHash);
}
@Override
public String generateUserToken(UserInfo client, String context) throws Exception {
@ -186,7 +231,7 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy {
return Utils.addInfrastructureHashToToken(token, infrastructureHash);
}
@Override
public void setTokenRoles(String token, List<String> roles) throws Exception {
@ -194,7 +239,7 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy {
String methodPath = String.format("/token/user/%s/roles",realToken);
int infrastructureHash = Utils.getInfrastructureHashFromToken(token, endpoints.getDefaultInfrastructure());
StringBuilder callUrl = new StringBuilder(getInternalEnpoint(infrastructureHash)).append(methodPath);
URL url = new URL(callUrl.toString());
@ -202,9 +247,8 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy {
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("Content-type", "application/xml");
ListMapper listmapper = new ListMapper();
listmapper.setList(roles);
ListMapper listmapper = new ListMapper(roles);
try(OutputStream os = new BufferedOutputStream(connection.getOutputStream())){
Binder.getContext().createMarshaller().marshal(listmapper, os);
}
@ -212,17 +256,17 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy {
log.debug("response code for "+callUrl.toString()+" is "+connection.getResponseCode()+" "+connection.getResponseMessage());
if (connection.getResponseCode()!=200) throw new Exception("error contacting authorization service");
}
@Override
public void removeAllReleatedToken(String clientId, String context) throws Exception{
String methodPath = "/token/user";
int infrastructureHash = Utils.getInfrastructureHashfromContext(context);
StringBuilder callUrl = new StringBuilder(getInternalEnpoint(infrastructureHash)).append(methodPath).append("?")
.append(CONTEXT_PARAM).append("=").append(context).append("&").append(CLIENT_ID_PARAM).append("=").append(clientId);
URL url = new URL(callUrl.toString());
HttpURLConnection connection = makeRequest(url, "DELETE", false);
//connection.setDoOutput(false);
@ -231,7 +275,7 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy {
log.debug("response code for "+callUrl.toString()+" is "+connection.getResponseCode()+" "+connection.getResponseMessage());
if (connection.getResponseCode()!=200 && connection.getResponseCode()!=204) throw new Exception("error contacting authorization service");
}
@Override
@ -292,16 +336,16 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy {
QualifiersList entries = (QualifiersList)Binder.getContext().createUnmarshaller().unmarshal(stream);
tokensQulifiersMap = entries.getQualifiers();
}
if (tokensQulifiersMap!=null && !tokensQulifiersMap.isEmpty()){
Map<String, String> toReturnMap = new HashMap<String, String>();
for (Entry<String, String> entry: tokensQulifiersMap.entrySet())
toReturnMap.put(entry.getKey(), Utils.addInfrastructureHashToToken(entry.getValue(), infrastructureHash));
return toReturnMap;
} else return Collections.emptyMap();
}
@Override
/**
* return a map with key external service id and value token
@ -327,14 +371,14 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy {
ExternalServiceList entries = (ExternalServiceList)Binder.getContext().createUnmarshaller().unmarshal(stream);
externalServiceMap = entries.getExternalServiceMap();
}
if (externalServiceMap!=null && !externalServiceMap.isEmpty()){
Map<String, String> toReturnMap = new HashMap<String, String>();
for (Entry<String, String> entry: externalServiceMap.entrySet())
toReturnMap.put(entry.getKey(), Utils.addInfrastructureHashToToken(entry.getValue(), infrastructureHash));
return toReturnMap;
} else return Collections.emptyMap();
}
@Override
@ -424,7 +468,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);
@ -441,6 +485,50 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy {
}
}
@Override
public List<AuthorizationEntry> get(List<String> tokens) throws ObjectNotFound, Exception {
List<String> realTokens = new ArrayList<String>();
List<AuthorizationEntry> toReturn = new ArrayList<AuthorizationEntry>();
AuthorizationEndpoint endpoint = null;
for (String token : tokens) {
String realToken = Utils.getRealToken(token);
if (cache.containsKey(realToken) && cache.get(realToken).isValid(endpoint.getClientCacheValidity()))
toReturn.add(cache.get(realToken).getEntry());
else realTokens.add(realToken);
if (endpoint==null) {
int infrastructureHashFromToken = Utils.getInfrastructureHashFromToken(token, endpoints.getDefaultInfrastructure());
endpoint = getEndpoint(infrastructureHashFromToken);
}
}
final String methodPath = "/resolve/?";
StringBuilder callUrl = new StringBuilder(getInternalEnpoint(endpoint))
.append(methodPath);
for (String toAppend : realTokens)
callUrl= callUrl.append("token=").append(toAppend);
URL url = new URL(callUrl.toString());
HttpURLConnection connection = makeRequest(url, "GET", false);
connection.setDoInput(true);
if (connection.getResponseCode()==404) throw new ObjectNotFound("token not found");
if (connection.getResponseCode()!=200) throw new Exception("error contacting authorization service (error code is "+connection.getResponseCode()+")");
if (connection.getContentLengthLong()==0) return null;
try(InputStream stream = (InputStream)connection.getContent();){
AuthorizationEntryList entries = (AuthorizationEntryList)Binder.getContext().createUnmarshaller().unmarshal(stream);
return entries.getEntries();
}
}
@Override
public void addPolicies(List<Policy> policies) throws Exception {
@ -503,7 +591,7 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy {
final String methodPath = "/symmKey/";
StringBuilder callUrl = new StringBuilder(getInternalEnpoint(Utils.getInfrastructureHashFromToken(SecurityTokenProvider.instance.get(), endpoints.getDefaultInfrastructure())))
.append(methodPath);
.append(methodPath);
URL url = new URL(callUrl.toString());
HttpURLConnection connection = makeRequest(url, "GET", true);

@ -28,7 +28,6 @@ import org.gcube.common.authorization.library.provider.UserInfo;
import org.junit.Ignore;
import org.junit.Test;
@Ignore
public class CallTest {
@Test
@ -36,12 +35,19 @@ public class CallTest {
System.out.println("pred4s".hashCode() & 0xfffffff);
}
@Test
public void requestActivation() throws Exception {
SecurityTokenProvider.instance.set("b653566c-2983-4a0e-a0a9-b913636469a8-98187548");
//ic-test.dev.int.d4science.net b653566c-2983-4a0e-a0a9-b913636469a8-98187548 80 /home/gcube/.containerxml/2-container.xml /gcube /gcube/devNext
String token = authorizationService().requestActivation(new ContainerInfo("ic-test.dev.int.d4science.net", 80), "/gcube/devNext") ;
System.out.println(token);
}
@Test
public void requestUserTokenViaUserNameAndScope() throws Exception {
String token = authorizationService().resolveTokenByUserAndContext("valentina.marioli", "/gcube");
authorizationService().setTokenRoles(token, Arrays.asList("VOManager"));
AuthorizationEntry authEntry = authorizationService().get(token);
System.out.println(authEntry.getClientInfo().toString());
AuthorizationEntry token = authorizationService().get("d9431600-9fef-41a7-946d-a5b402de30d6-98187548");
System.out.println(token);
}
@Test

Loading…
Cancel
Save