Skip to content

Commit

Permalink
Added support for getting guest tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
vthglyk committed Jul 16, 2018
1 parent 7bc288c commit aa737dd
Show file tree
Hide file tree
Showing 10 changed files with 356 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,61 @@ public static AbstractSymbIoTeClientFactory getFactory(Config config)

public abstract IAAMClient getAAMClient();

/**
* Get a configuration for home token symbIoTeClient factory. In this configuration, getting guest token is also possible
*
* @param coreAddress the base address of the symbIoTe core
* @param keystorePath the keystore path
* @param keystorePassword the keystore password
* @param type the type of factory
* @return the factory configuration
*/
public static Config getGuestTokenConfiguration(String coreAddress, String keystorePath, String keystorePassword, Type type) {
return new Config(
coreAddress,
keystorePath,
keystorePassword,
null,
null,
null,
null,
type
);
}

/**
* Get a configuration for guest token symbIoTeClient factory. In this configuration, getting home token is not possible
*
* @param coreAddress the base address of the symbIoTe core
* @param keystorePath the keystore path
* @param keystorePassword the keystore password
* @param homePlatformId the home Platform Id
* @param username the username in the home platform
* @param password the password in the home platform
* @param clientId the client id
* @param type the type of factory
* @return the factory configuration
*/
public static Config getHomeTokenConfiguration(String coreAddress,
String keystorePath,
String keystorePassword,
String homePlatformId,
String username,
String password,
String clientId,
Type type) {
return new Config(
coreAddress,
keystorePath,
keystorePassword,
homePlatformId,
username,
password,
clientId,
type
);
}

/**
* The type of factory. For now there is just one type but we followed the abstract factory pattern to facilitate
* future extension
Expand Down
50 changes: 42 additions & 8 deletions src/main/java/eu/h2020/symbiote/client/feign/FeignCRAMClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ public class FeignCRAMClient implements CRAMClient {
private static final String GET_RESOURCE_URLS_BASE_PATH = CORE_INTERFACE_PATH + "/resourceUrls?";

private final CRAMI cramClient;
private final CRAMI cramClientAsGuest;
private final CRAMI cramClientWithoutValidation;
private final CRAMI cramClientAsGuestWithoutValidation;

/**
*
Expand Down Expand Up @@ -63,6 +65,18 @@ public FeignCRAMClient(ISecurityHandler securityHandler,
true))
.target(CRAMI.class, coreAddress);

this.cramClientAsGuest = Feign.builder()
.decoder(new JacksonDecoder())
.encoder(new JacksonEncoder())
.logger(new ApacheCommonsLogger4Feign(logger))
.logLevel(Logger.Level.FULL)
.client(new SymbIoTeSecurityHandlerFeignClient(
securityHandler,
ComponentIdentifiers.CORE_RESOURCE_ACCESS_MONITOR,
SecurityConstants.CORE_AAM_INSTANCE_ID,
true))
.target(CRAMI.class, coreAddress);

this.cramClientWithoutValidation = Feign.builder()
.decoder(new JacksonDecoder())
.encoder(new JacksonEncoder())
Expand All @@ -79,26 +93,46 @@ public FeignCRAMClient(ISecurityHandler securityHandler,
false))
.target(CRAMI.class, coreAddress);

this.cramClientAsGuestWithoutValidation = Feign.builder()
.decoder(new JacksonDecoder())
.encoder(new JacksonEncoder())
.logger(new ApacheCommonsLogger4Feign(logger))
.logLevel(Logger.Level.FULL)
.client(new SymbIoTeSecurityHandlerFeignClient(
securityHandler,
ComponentIdentifiers.CORE_RESOURCE_ACCESS_MONITOR,
SecurityConstants.CORE_AAM_INSTANCE_ID,
false))
.target(CRAMI.class, coreAddress);

}

@Override
public ResourceUrlsResponse getResourceUrl(String resourceId) {
return cramClient.getResourceUrls(new ArrayList<>(Collections.singleton(resourceId)));
public ResourceUrlsResponse getResourceUrl(String resourceId, boolean serverValidation) {
return serverValidation ?
cramClient.getResourceUrls(new ArrayList<>(Collections.singleton(resourceId))) :
cramClientWithoutValidation.getResourceUrls(new ArrayList<>(Collections.singleton(resourceId)));
}

@Override
public ResourceUrlsResponse getResourceUrl(List<String> resourceIds) {
return cramClient.getResourceUrls(resourceIds);
public ResourceUrlsResponse getResourceUrl(List<String> resourceIds, boolean serverValidation) {
return serverValidation ?
cramClient.getResourceUrls(resourceIds) :
cramClientWithoutValidation.getResourceUrls(resourceIds);
}

@Override
public ResourceUrlsResponse getResourceUrlWithoutValidation(String resourceId) {
return cramClientWithoutValidation.getResourceUrls(new ArrayList<>(Collections.singleton(resourceId)));
public ResourceUrlsResponse getResourceUrlAsGuest(String resourceId, boolean serverValidation) {
return serverValidation ?
cramClientAsGuest.getResourceUrls(new ArrayList<>(Collections.singleton(resourceId))) :
cramClientAsGuestWithoutValidation.getResourceUrls(new ArrayList<>(Collections.singleton(resourceId)));
}

@Override
public ResourceUrlsResponse getResourceUrlWithoutValidation(List<String> resourceIds) {
return cramClientWithoutValidation.getResourceUrls(resourceIds);
public ResourceUrlsResponse getResourceUrlAsGuest(List<String> resourceIds, boolean serverValidation) {
return serverValidation ?
cramClientAsGuest.getResourceUrls(resourceIds) :
cramClientAsGuestWithoutValidation.getResourceUrls(resourceIds);
}

private interface CRAMI {
Expand Down
38 changes: 34 additions & 4 deletions src/main/java/eu/h2020/symbiote/client/feign/FeignPRClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ public class FeignPRClient implements PRClient {
private static final Log logger = LogFactory.getLog(FeignPRClient.class);

private PlatformRegistryI prClient;
private PlatformRegistryI prClientAsGuest;
private PlatformRegistryI prClientWithoutValidation;
private PlatformRegistryI prClientAsGuestWithoutValidation;

/**
*
Expand Down Expand Up @@ -62,6 +64,18 @@ public FeignPRClient(ISecurityHandler securityHandler,
true))
.target(PlatformRegistryI.class, prUrl);

this.prClientAsGuest = Feign.builder()
.decoder(new JacksonDecoder())
.encoder(new JacksonEncoder())
.logger(new ApacheCommonsLogger4Feign(logger))
.logLevel(Logger.Level.FULL)
.client(new SymbIoTeSecurityHandlerFeignClient(
securityHandler,
ComponentIdentifiers.PLATFORM_REGISTRY,
homePlatformId,
true))
.target(PlatformRegistryI.class, prUrl);

this.prClientWithoutValidation = Feign.builder()
.decoder(new JacksonDecoder())
.encoder(new JacksonEncoder())
Expand All @@ -77,20 +91,36 @@ public FeignPRClient(ISecurityHandler securityHandler,
homePlatformId,
false))
.target(PlatformRegistryI.class, prUrl);

this.prClientAsGuestWithoutValidation = Feign.builder()
.decoder(new JacksonDecoder())
.encoder(new JacksonEncoder())
.logger(new ApacheCommonsLogger4Feign(logger))
.logLevel(Logger.Level.FULL)
.client(new SymbIoTeSecurityHandlerFeignClient(
securityHandler,
ComponentIdentifiers.PLATFORM_REGISTRY,
homePlatformId,
false))
.target(PlatformRegistryI.class, prUrl);
} catch (SecurityHandlerException e) {
logger.error("Could not create FeignPRClient", e);
}

}

@Override
public FederationSearchResult search(PlatformRegistryQuery query) {
return prClient.query(query.buildRequestParametersMap());
public FederationSearchResult search(PlatformRegistryQuery query, boolean serverValidation) {
return serverValidation ?
prClient.query(query.buildRequestParametersMap()) :
prClientWithoutValidation.query(query.buildRequestParametersMap());
}

@Override
public FederationSearchResult searchWithoutValidation(PlatformRegistryQuery query) {
return prClientWithoutValidation.query(query.buildRequestParametersMap());
public FederationSearchResult searchAsGuest(PlatformRegistryQuery query, boolean serverValidation) {
return serverValidation ?
prClientAsGuest.query(query.buildRequestParametersMap()) :
prClientAsGuestWithoutValidation.query(query.buildRequestParametersMap());
}

private interface PlatformRegistryI {
Expand Down
110 changes: 84 additions & 26 deletions src/main/java/eu/h2020/symbiote/client/feign/FeignRAPClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import eu.h2020.symbiote.client.interfaces.RAPClient;
import eu.h2020.symbiote.model.cim.Observation;
import eu.h2020.symbiote.security.commons.ComponentIdentifiers;
import eu.h2020.symbiote.security.commons.exceptions.custom.SecurityHandlerException;
import eu.h2020.symbiote.security.communication.ApacheCommonsLogger4Feign;
import eu.h2020.symbiote.security.communication.payloads.AAM;
import eu.h2020.symbiote.security.handler.ISecurityHandler;
import feign.*;
import feign.jackson.JacksonDecoder;
Expand All @@ -12,6 +14,7 @@
import org.apache.commons.logging.LogFactory;

import java.util.List;
import java.util.stream.Collectors;

/**
* symbIoTe RAP client based on Feign
Expand Down Expand Up @@ -51,43 +54,81 @@ public FeignRAPClient(ISecurityHandler securityHandler,
}

@Override
public Observation getLatestObservation(String resourceUrl, String targetPlatformId) {
return getClient(resourceUrl, targetPlatformId).getTopObservations(1).get(0);
public Observation getLatestObservation(String resourceUrl, boolean serverValidation) {
try {
return getClient(resourceUrl, serverValidation).getTopObservations(1).get(0);
} catch (SecurityHandlerException e) {
logger.error("Could not get latest Observation", e);
return null;
}
}

@Override
public List<Observation> getTopObservations(String resourceUrl, int top, String targetPlatformId) {
return getClient(resourceUrl, targetPlatformId).getTopObservations(top);
public List<Observation> getTopObservations(String resourceUrl, int top, boolean serverValidation) {
try {
return getClient(resourceUrl, serverValidation).getTopObservations(top);
} catch (SecurityHandlerException e) {
logger.error("Could not get Observations", e);
return null;
}
}

@Override
public void actuate(String resourceUrl, String body, String targetPlatformId) {
getClient(resourceUrl, targetPlatformId).actuate(body);
public void actuate(String resourceUrl, String body, boolean serverValidation) {
try {
getClient(resourceUrl, serverValidation).actuate(body);
} catch (SecurityHandlerException e) {
logger.error("Could not send actuation request", e);
}
}

@Override
public String invokeService(String resourceUrl, String body, String targetPlatformId) {
return getClient(resourceUrl, targetPlatformId).invokeService(body);
public String invokeService(String resourceUrl, String body, boolean serverValidation) {
try {
return getClient(resourceUrl, serverValidation).invokeService(body);
} catch (SecurityHandlerException e) {
logger.error("Could not invoke service", e);
return "Could not invoke service : " + e.getMessage();
}
}

@Override
public Observation getLatestObservationWithoutValidation(String resourceUrl) {
return getClientWithoutValidation(resourceUrl).getTopObservations(1).get(0);
public Observation getLatestObservationAsGuest(String resourceUrl, boolean serverValidation) {
try {
return getGuestClient(resourceUrl, serverValidation).getTopObservations(1).get(0);
} catch (SecurityHandlerException e) {
logger.error("Could not get latest Observation", e);
return null;
}
}

@Override
public List<Observation> getTopObservationsWithoutValidation(String resourceUrl, int top) {
return getClientWithoutValidation(resourceUrl).getTopObservations(top);
public List<Observation> getTopObservationsAsGuest(String resourceUrl, int top, boolean serverValidation) {
try {
return getGuestClient(resourceUrl, serverValidation).getTopObservations(top);
} catch (SecurityHandlerException e) {
logger.error("Could not get Observations", e);
return null;
}
}

@Override
public void actuateWithoutValidation(String resourceUrl, String body) {
getClientWithoutValidation(resourceUrl).actuate(body);
public void actuateAsGuest(String resourceUrl, String body, boolean serverValidation) {
try {
getGuestClient(resourceUrl, serverValidation).actuate(body);
} catch (SecurityHandlerException e) {
logger.error("Could not send actuation request", e);
}
}

@Override
public String invokeServiceWithoutValidation(String resourceUrl, String body) {
return getClientWithoutValidation(resourceUrl).invokeService(body);
public String invokeServiceAsGuest(String resourceUrl, String body, boolean serverValidation) {
try {
return getGuestClient(resourceUrl, serverValidation).invokeService(body);
} catch (SecurityHandlerException e) {
logger.error("Could not invoke service", e);
return "Could not invoke service : " + e.getMessage();
}
}

private interface RAPI {
Expand All @@ -107,7 +148,10 @@ private interface RAPI {
String invokeService(@Param("body") String body);
}

private RAPI getClient(String resourceUrl, String targetPlatformId) {
private RAPI getClient(String resourceUrl, boolean serverValidation) throws SecurityHandlerException {

List<AAM> filteredAAMs = findAAMS(resourceUrl);

return Feign.builder()
.decoder(new JacksonDecoder())
.encoder(new JacksonEncoder())
Expand All @@ -120,26 +164,40 @@ private RAPI getClient(String resourceUrl, String targetPlatformId) {
password,
clientId,
ComponentIdentifiers.RESOURCE_ACCESS_PROXY,
targetPlatformId,
true))
filteredAAMs.get(0).getAamInstanceId(),
serverValidation))
.target(RAPI.class, resourceUrl);
}

private RAPI getClientWithoutValidation(String resourceUrl) {
private RAPI getGuestClient(String resourceUrl, boolean serverValidation) throws SecurityHandlerException {

List<AAM> filteredAAMs = findAAMS(resourceUrl);

return Feign.builder()
.decoder(new JacksonDecoder())
.encoder(new JacksonEncoder())
.logger(new ApacheCommonsLogger4Feign(logger))
.logLevel(Logger.Level.FULL)
.client(new SymbIoTeSecurityHandlerFeignClient(
securityHandler,
homePlatformId,
username,
password,
clientId,
ComponentIdentifiers.RESOURCE_ACCESS_PROXY,
"",
false))
filteredAAMs.get(0).getAamInstanceId(),
serverValidation))
.target(RAPI.class, resourceUrl);
}

private List<AAM> findAAMS(String resourceUrl) throws SecurityHandlerException {
String aamUrl = resourceUrl.replaceAll("/rap.*", "/paam");
List<AAM> filteredAAMs;

filteredAAMs = securityHandler.getAvailableAAMs().values().stream()
.filter(aam -> aam.getAamAddress().equals(aamUrl))
.collect(Collectors.toList());

if (filteredAAMs.size() != 1) {
throw new SecurityHandlerException(String.format("Found %d possible targets instead of only 1", filteredAAMs.size()));
}

return filteredAAMs;
}
}
Loading

0 comments on commit aa737dd

Please sign in to comment.