From d6257cf520800cac2845971866a41691023ae3b1 Mon Sep 17 00:00:00 2001 From: Alfredo Oliviero Date: Mon, 26 Feb 2024 17:40:13 +0100 Subject: [PATCH] reorganized packages --- .../gcube/idm/IdentityManagerApplication.java | 28 ------ .../IdentityManagerApplicationManager.java | 65 -------------- src/main/java/org/gcube/service/idm/IdM.java | 28 ++++++ .../org/gcube/service/idm/IdMManager.java | 58 +++++++++++++ .../idm/health/CreateTempFileCheck.java | 45 ++++++++++ .../{ => service}/idm/rest/TestRest.java | 24 ++--- .../{ => service}/idm/rest/UsersRest.java | 2 +- .../ContainerConfigurationSerializer.java | 45 ++++++++++ .../ContainerContextSerializer.java | 42 +++++++++ .../idm/serializers/ContextSerializator.java | 36 ++++++++ .../idm/serializers/OwnerSerializer.java | 47 ++++++++++ .../SimpleCredentialsSerializer.java | 36 ++++++++ .../test/services/AuthenticatedService.java | 67 ++++++++++++++ .../services/ExcludeAuthorizationService.java | 31 +++++++ .../idm/test/services/HelloService.java | 87 +++++++++++++++++++ .../{ => service}/rest/ResponseBean.java | 2 +- .../{ => service}/rest/annotation/PATCH.java | 2 +- .../{ => service}/rest/annotation/PURGE.java | 2 +- .../{ => service}/rest/utils/HTTPUtility.java | 2 +- 19 files changed, 539 insertions(+), 110 deletions(-) delete mode 100644 src/main/java/org/gcube/idm/IdentityManagerApplication.java delete mode 100644 src/main/java/org/gcube/idm/IdentityManagerApplicationManager.java create mode 100644 src/main/java/org/gcube/service/idm/IdM.java create mode 100644 src/main/java/org/gcube/service/idm/IdMManager.java create mode 100644 src/main/java/org/gcube/service/idm/health/CreateTempFileCheck.java rename src/main/java/org/gcube/{ => service}/idm/rest/TestRest.java (65%) rename src/main/java/org/gcube/{ => service}/idm/rest/UsersRest.java (99%) create mode 100644 src/main/java/org/gcube/service/idm/serializers/ContainerConfigurationSerializer.java create mode 100644 src/main/java/org/gcube/service/idm/serializers/ContainerContextSerializer.java create mode 100644 src/main/java/org/gcube/service/idm/serializers/ContextSerializator.java create mode 100644 src/main/java/org/gcube/service/idm/serializers/OwnerSerializer.java create mode 100644 src/main/java/org/gcube/service/idm/serializers/SimpleCredentialsSerializer.java create mode 100644 src/main/java/org/gcube/service/idm/test/services/AuthenticatedService.java create mode 100644 src/main/java/org/gcube/service/idm/test/services/ExcludeAuthorizationService.java create mode 100644 src/main/java/org/gcube/service/idm/test/services/HelloService.java rename src/main/java/org/gcube/{ => service}/rest/ResponseBean.java (97%) rename src/main/java/org/gcube/{ => service}/rest/annotation/PATCH.java (88%) rename src/main/java/org/gcube/{ => service}/rest/annotation/PURGE.java (88%) rename src/main/java/org/gcube/{ => service}/rest/utils/HTTPUtility.java (97%) diff --git a/src/main/java/org/gcube/idm/IdentityManagerApplication.java b/src/main/java/org/gcube/idm/IdentityManagerApplication.java deleted file mode 100644 index bd4b288..0000000 --- a/src/main/java/org/gcube/idm/IdentityManagerApplication.java +++ /dev/null @@ -1,28 +0,0 @@ -package org.gcube.idm; - -import java.util.HashSet; -import java.util.Set; - -import javax.ws.rs.Path; -import javax.ws.rs.core.Application; - -import org.gcube.idm.rest.TestRest; - -/** - * @author Alfredo Oliviero (ISTI - CNR) - */ - -// SMARTGEARS -// legge i parametri del service da application.yaml - -@Path("idm") -public class IdentityManagerApplication extends Application { - - public Set> getClasses() { - final Set> classes = new HashSet>(); - // register resources and features - classes.add(TestRest.class); - return classes; - } - -} diff --git a/src/main/java/org/gcube/idm/IdentityManagerApplicationManager.java b/src/main/java/org/gcube/idm/IdentityManagerApplicationManager.java deleted file mode 100644 index 8a85a71..0000000 --- a/src/main/java/org/gcube/idm/IdentityManagerApplicationManager.java +++ /dev/null @@ -1,65 +0,0 @@ -package org.gcube.idm; - -import org.gcube.common.security.providers.SecretManagerProvider; -import org.gcube.smartgears.ApplicationManager; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * @author Alfredo gOliviero (ISTI - CNR) - */ -public class IdentityManagerApplicationManager implements ApplicationManager { - - /** - * Logger - */ - private static Logger logger = LoggerFactory.getLogger(IdentityManagerApplicationManager.class); - - public static boolean initialised; - - /** - * {@inheritDoc} - */ - @Override - public synchronized void onInit() { - - String context = SecretManagerProvider.get().getContext(); - - logger.trace( - "\n-------------------------------------------------------\n" - + "Identity Manager Service is Starting on context {}\n" - + "-------------------------------------------------------", - context); - - // ApplicationContext applicationContext = ContextProvider.get(); - // String helloWorldEServiceID = applicationContext.id(); - - logger.trace( - "\n-------------------------------------------------------\n" - + "Identity Manager Service Started Successfully on context {}\n" - + "-------------------------------------------------------", - context); - - } - - /** - * {@inheritDoc} - */ - @Override - public synchronized void onShutdown() { - - String context = SecretManagerProvider.get().getContext(); - - logger.trace( - "\n-------------------------------------------------------\n" - + "Identity Manager Service is Stopping on context {}\n" - + "-------------------------------------------------------", - context); - - logger.trace( - "\n-------------------------------------------------------\n" - + "Identity Manager Service Stopped Successfully on context {}\n" - + "-------------------------------------------------------", - context); - } -} diff --git a/src/main/java/org/gcube/service/idm/IdM.java b/src/main/java/org/gcube/service/idm/IdM.java new file mode 100644 index 0000000..1314b12 --- /dev/null +++ b/src/main/java/org/gcube/service/idm/IdM.java @@ -0,0 +1,28 @@ +package org.gcube.service.idm; + +import java.util.HashSet; +import java.util.Set; + +import javax.ws.rs.ApplicationPath; +import javax.ws.rs.core.Application; + +import org.gcube.service.idm.test.services.AuthenticatedService; +import org.gcube.service.idm.test.services.ExcludeAuthorizationService; +import org.gcube.service.idm.test.services.HelloService; + +/** + * @author Alfredo Oliviero (ISTI - CNR) + */ +@ApplicationPath("") +public class IdM extends Application { + + public Set> getClasses() { + final Set> classes = new HashSet>(); + // register resources and features + classes.add(HelloService.class); + classes.add(ExcludeAuthorizationService.class); + classes.add(AuthenticatedService.class); + return classes; + } + +} diff --git a/src/main/java/org/gcube/service/idm/IdMManager.java b/src/main/java/org/gcube/service/idm/IdMManager.java new file mode 100644 index 0000000..d85672c --- /dev/null +++ b/src/main/java/org/gcube/service/idm/IdMManager.java @@ -0,0 +1,58 @@ +package org.gcube.service.idm; + +import org.gcube.common.security.providers.SecretManagerProvider; +import org.gcube.common.security.secrets.Secret; +import org.gcube.smartgears.ApplicationManager; +import org.gcube.smartgears.ContextProvider; +import org.gcube.smartgears.configuration.Mode; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * + * This class is use to Manage the application initialization and shutdown per + * context; The init and shutdown methods are called one per context in which + * the app is running respectively at init and a shutdown time. It is connected + * to the app declaring it via the @ManagedBy annotation. (@see HelloWorld + * class) + * + * @author Alfredo gOliviero (ISTI - CNR) + */ + +public class IdMManager implements ApplicationManager { + Logger logger = LoggerFactory.getLogger(IdMManager.class); + + /** + * {@inheritDoc} + */ + @Override + public void onInit() { + if (ContextProvider.get().container().configuration().mode() == Mode.offline) { + logger.debug("init called in offline mode"); + } else { + Secret secret = SecretManagerProvider.get(); + if (secret != null) { + logger.debug("init called in context {}", secret.getContext()); + } else { + logger.debug("init called in null context"); + } + } + } + + /** + * {@inheritDoc} + */ + @Override + public void onShutdown() { + if (ContextProvider.get().container().configuration().mode() == Mode.offline) { + logger.debug("shutDown called in offline mode"); + } else { + Secret secret = SecretManagerProvider.get(); + if (secret != null) { + logger.debug("shutDown called in context {}", secret.getContext()); + } else { + logger.debug("shutDown called in null context"); + } + } + } +} diff --git a/src/main/java/org/gcube/service/idm/health/CreateTempFileCheck.java b/src/main/java/org/gcube/service/idm/health/CreateTempFileCheck.java new file mode 100644 index 0000000..9c26ebc --- /dev/null +++ b/src/main/java/org/gcube/service/idm/health/CreateTempFileCheck.java @@ -0,0 +1,45 @@ +package org.gcube.service.idm.health; + +import java.io.File; +import java.io.IOException; + +import org.gcube.common.health.api.HealthCheck; +import org.gcube.common.health.api.ReadinessChecker; +import org.gcube.common.health.api.response.HealthCheckResponse; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * + * this class is used to add checks about components used by the service (DB, + * external FS, other services etc. ) + * and automatically exposed using the REST method webapp/gcube/resources/health + * + * + * @author lucio + * + */ + +@ReadinessChecker +public class CreateTempFileCheck implements HealthCheck { + + private static Logger log = LoggerFactory.getLogger(CreateTempFileCheck.class); + + @Override + public String getName() { + return "create temp file"; + } + + @Override + public HealthCheckResponse check() { + try { + File.createTempFile("exampleTest", "txt"); + return HealthCheckResponse.builder(getName()).up().info("health check example").build(); + } catch (IOException e) { + log.error("error checking defaultStorage", e); + return HealthCheckResponse.builder(getName()).down().error(e.getMessage()).build(); + } + + } + +} \ No newline at end of file diff --git a/src/main/java/org/gcube/idm/rest/TestRest.java b/src/main/java/org/gcube/service/idm/rest/TestRest.java similarity index 65% rename from src/main/java/org/gcube/idm/rest/TestRest.java rename to src/main/java/org/gcube/service/idm/rest/TestRest.java index 6ecc547..7ad4aa3 100644 --- a/src/main/java/org/gcube/idm/rest/TestRest.java +++ b/src/main/java/org/gcube/service/idm/rest/TestRest.java @@ -1,4 +1,4 @@ -package org.gcube.idm.rest; +package org.gcube.service.idm.rest; import javax.ws.rs.GET; import javax.ws.rs.Path; @@ -7,24 +7,24 @@ import javax.ws.rs.core.MediaType; import org.gcube.common.security.providers.SecretManagerProvider; import org.gcube.common.security.secrets.Secret; -import org.gcube.idm.IdentityManagerApplicationManager; +import org.gcube.service.idm.IdMManager; import org.gcube.smartgears.annotations.ManagedBy; import org.gcube.smartgears.utils.InnerMethodName; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.webcohesion.enunciate.metadata.rs.RequestHeader; -import com.webcohesion.enunciate.metadata.rs.RequestHeaders; -import com.webcohesion.enunciate.metadata.rs.ResourceGroup; -import com.webcohesion.enunciate.metadata.rs.ResourceLabel; +// import com.webcohesion.enunciate.metadata.rs.RequestHeader; +// import com.webcohesion.enunciate.metadata.rs.RequestHeaders; +// import com.webcohesion.enunciate.metadata.rs.ResourceGroup; +// import com.webcohesion.enunciate.metadata.rs.ResourceLabel; -@ManagedBy(IdentityManagerApplicationManager.class) +@ManagedBy(IdMManager.class) @Path("/test") -@ResourceGroup("test") -@ResourceLabel("test") -@RequestHeaders({ - @RequestHeader(name = "Authorization", description = "Bearer token, see https://dev.d4science.org/how-to-access-resources") -}) +// @ResourceGroup("test") +// @ResourceLabel("test") +// @RequestHeaders({ +// @RequestHeader(name = "Authorization", description = "Bearer token, see https://dev.d4science.org/how-to-access-resources") +// }) public class TestRest { private final Logger logger = LoggerFactory.getLogger(TestRest.class); diff --git a/src/main/java/org/gcube/idm/rest/UsersRest.java b/src/main/java/org/gcube/service/idm/rest/UsersRest.java similarity index 99% rename from src/main/java/org/gcube/idm/rest/UsersRest.java rename to src/main/java/org/gcube/service/idm/rest/UsersRest.java index 346811f..0c82769 100644 --- a/src/main/java/org/gcube/idm/rest/UsersRest.java +++ b/src/main/java/org/gcube/service/idm/rest/UsersRest.java @@ -1,4 +1,4 @@ -package org.gcube.idm.rest; +package org.gcube.service.idm.rest; import org.gcube.common.authorization.library.policies.Users; import org.slf4j.LoggerFactory; diff --git a/src/main/java/org/gcube/service/idm/serializers/ContainerConfigurationSerializer.java b/src/main/java/org/gcube/service/idm/serializers/ContainerConfigurationSerializer.java new file mode 100644 index 0000000..28f0bd6 --- /dev/null +++ b/src/main/java/org/gcube/service/idm/serializers/ContainerConfigurationSerializer.java @@ -0,0 +1,45 @@ +package org.gcube.service.idm.serializers; + +import java.io.IOException; + +import org.gcube.smartgears.configuration.container.ContainerConfiguration; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.ser.std.StdSerializer; + +/** + * Jackson serializer for ContainerConfiguration + * + * @author Alfredo Oliviero (ISTI-CNR) + * + */ + +public class ContainerConfigurationSerializer extends StdSerializer { + protected ContainerConfigurationSerializer(Class t) { + super(t); + } + + public ContainerConfigurationSerializer() { + super(ContainerConfiguration.class, true); + } + + @Override + public void serialize(ContainerConfiguration configuration, JsonGenerator jgen, SerializerProvider provider) + throws IOException { + jgen.writeStartObject(); + jgen.writeObjectField("mode", configuration.mode()); + jgen.writeObjectField("app", configuration.apps()); + jgen.writeObjectField("site", configuration.site()); + jgen.writeObjectField("infrastructure", configuration.infrastructure()); + jgen.writeObjectField("hostname", configuration.hostname()); + jgen.writeObjectField("port", configuration.port()); + jgen.writeObjectField("protocol", configuration.protocol()); + jgen.writeObjectField("authorizeChildrenContext", configuration.authorizeChildrenContext()); + jgen.writeObjectField("proxy", configuration.proxy()); + + jgen.writeObjectField("desc", configuration.toString()); + + jgen.writeEndObject(); + } +} diff --git a/src/main/java/org/gcube/service/idm/serializers/ContainerContextSerializer.java b/src/main/java/org/gcube/service/idm/serializers/ContainerContextSerializer.java new file mode 100644 index 0000000..c9b47e1 --- /dev/null +++ b/src/main/java/org/gcube/service/idm/serializers/ContainerContextSerializer.java @@ -0,0 +1,42 @@ +package org.gcube.service.idm.serializers; + +import java.io.IOException; + +import org.gcube.smartgears.context.container.ContainerContext; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.ser.std.StdSerializer; + +/** + * Jackson serializer for ContainerContext + * + * @author Alfredo Oliviero (ISTI-CNR) + * + */ + +public class ContainerContextSerializer extends StdSerializer { + protected ContainerContextSerializer(Class t) { + super(t); + } + + public ContainerContextSerializer() { + super(ContainerContext.class, true); + } + + @Override + public void serialize(ContainerContext ccontext, JsonGenerator jgen, SerializerProvider provider) + throws IOException { + jgen.writeStartObject(); + jgen.writeStringField("id", ccontext.id()); + // jgen.writeObjectField("configuration.site", ccontext.configuration().site()); + // jgen.writeObjectField("configuration", ccontext.configuration()); + jgen.writeObjectField("properties", ccontext.properties()); + jgen.writeObjectField("authorizationProvider", ccontext.authorizationProvider()); + + jgen.writeObjectField("configuration", ccontext.configuration()); + jgen.writeObjectField("desc", ccontext.toString()); + + jgen.writeEndObject(); + } +} diff --git a/src/main/java/org/gcube/service/idm/serializers/ContextSerializator.java b/src/main/java/org/gcube/service/idm/serializers/ContextSerializator.java new file mode 100644 index 0000000..5e4e4e7 --- /dev/null +++ b/src/main/java/org/gcube/service/idm/serializers/ContextSerializator.java @@ -0,0 +1,36 @@ +package org.gcube.service.idm.serializers; + +import org.gcube.smartgears.configuration.container.ContainerConfiguration; +import org.gcube.smartgears.context.container.ContainerContext; +import org.gcube.smartgears.security.SimpleCredentials; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.module.SimpleModule; + +/** + * Jackson Serialization utils for Smartgear Context classes + * + * @author Alfredo Oliviero (ISTI-CNR) + * + */ + +public class ContextSerializator { + private static ObjectMapper serializer = null; + + public static ObjectMapper getSerializer() { + if (serializer == null) { + ObjectMapper om = new ObjectMapper(); + SimpleModule module = new SimpleModule(); + // module.addSerializer(Owner.class, new OwnerSerializer()); + + module.addSerializer(ContainerConfiguration.class, new ContainerConfigurationSerializer()); + module.addSerializer(ContainerContext.class, new ContainerContextSerializer()); + module.addSerializer(SimpleCredentials.class, new SimpleCredentialsSerializer()); + + om.registerModule(module); + serializer = om; + } + return serializer; + } + +} diff --git a/src/main/java/org/gcube/service/idm/serializers/OwnerSerializer.java b/src/main/java/org/gcube/service/idm/serializers/OwnerSerializer.java new file mode 100644 index 0000000..12f1163 --- /dev/null +++ b/src/main/java/org/gcube/service/idm/serializers/OwnerSerializer.java @@ -0,0 +1,47 @@ +package org.gcube.service.idm.serializers; + +import java.io.IOException; + +import org.gcube.common.security.Owner; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.ser.std.StdSerializer; + +/** + * Jackson serializer for Owner + * + * @author Alfredo Oliviero (ISTI-CNR) + * + */ + +public class OwnerSerializer extends StdSerializer { + + protected OwnerSerializer(Class t) { + super(t); + } + + public OwnerSerializer() { + super(Owner.class, true); + } + + @Override + public void serialize(Owner owner, JsonGenerator jgen, SerializerProvider provider) throws IOException { + jgen.writeStartObject(); + jgen.writeStringField("ownerId", owner.getId()); + jgen.writeStringField("clientName", owner.getClientName()); + jgen.writeArrayFieldStart("roles"); + for (String role : owner.getRoles()) { + jgen.writeString(role); + } + jgen.writeEndArray(); + jgen.writeStringField("email", owner.getEmail()); + jgen.writeStringField("firstName", owner.getFirstName()); + jgen.writeStringField("lastName", owner.getLastName()); + jgen.writeBooleanField("externalClient", owner.isExternalClient()); + jgen.writeStringField("contactPerson", owner.getClientName()); + jgen.writeStringField("contactOrganisation", owner.getContactOrganisation()); + + jgen.writeEndObject(); + } +} diff --git a/src/main/java/org/gcube/service/idm/serializers/SimpleCredentialsSerializer.java b/src/main/java/org/gcube/service/idm/serializers/SimpleCredentialsSerializer.java new file mode 100644 index 0000000..9a7f5d6 --- /dev/null +++ b/src/main/java/org/gcube/service/idm/serializers/SimpleCredentialsSerializer.java @@ -0,0 +1,36 @@ +package org.gcube.service.idm.serializers; + +import java.io.IOException; + +import org.gcube.smartgears.security.SimpleCredentials; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.ser.std.StdSerializer; + +/** + * Jackson serializer for SimpleCredentials + * + * @author Alfredo Oliviero (ISTI-CNR) + * + */ + +public class SimpleCredentialsSerializer extends StdSerializer { + + protected SimpleCredentialsSerializer(Class t) { + super(t); + } + + public SimpleCredentialsSerializer() { + super(SimpleCredentials.class, true); + } + + @Override + public void serialize(SimpleCredentials credentials, JsonGenerator jgen, SerializerProvider provider) + throws IOException { + jgen.writeStartObject(); + jgen.writeStringField("clientId", credentials.getClientID()); + jgen.writeStringField("secret", "[*****]"); + jgen.writeEndObject(); + } +} diff --git a/src/main/java/org/gcube/service/idm/test/services/AuthenticatedService.java b/src/main/java/org/gcube/service/idm/test/services/AuthenticatedService.java new file mode 100644 index 0000000..a804668 --- /dev/null +++ b/src/main/java/org/gcube/service/idm/test/services/AuthenticatedService.java @@ -0,0 +1,67 @@ +package org.gcube.service.idm.test.services; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; + +import org.gcube.common.authorization.control.annotations.AuthorizationControl; +import org.gcube.common.security.providers.SecretManagerProvider; +import org.gcube.common.security.secrets.Secret; +import org.gcube.smartgears.utils.InnerMethodName; + +/** + * @author Lucio Le (ISTI - CNR) + * @author Alfredo Oliviero (ISTI - CNR) + */ + +@Path("auth") +public class AuthenticatedService { + + private static final String ALLOWED_ROLE = "myRole"; + private static final String ALLOWED_ROLE_ORG = "OrganizationMember"; + private static final String ALLOWED_ROLE_MEMBER = "Member"; + + @AuthorizationControl(allowedRoles = { ALLOWED_ROLE_ORG }) + @GET + @Path("org_member") + @Produces(MediaType.TEXT_PLAIN) + public String authorizedOrg() { + InnerMethodName.set("auth"); + Secret secret = SecretManagerProvider.get(); + String userId = secret.getOwner().getId(); + String context = secret.getContext(); + return String.format( + "User %s in context %s is authorized to execute this method because he has the role %s", userId, + context, ALLOWED_ROLE_ORG); + } + + @AuthorizationControl(allowedRoles = { ALLOWED_ROLE_MEMBER }) + @GET + @Path("member") + @Produces(MediaType.TEXT_PLAIN) + public String authorizedMember() { + InnerMethodName.set("auth"); + Secret secret = SecretManagerProvider.get(); + String userId = secret.getOwner().getId(); + String context = secret.getContext(); + return String.format( + "User %s in context %s is authorized to execute this method because he has the role %s", userId, + context, ALLOWED_ROLE_MEMBER); + } + + @AuthorizationControl(allowedRoles = { ALLOWED_ROLE }) + @GET + @Path("") + @Produces(MediaType.TEXT_PLAIN) + public String authorized() { + InnerMethodName.set("auth"); + Secret secret = SecretManagerProvider.get(); + String userId = secret.getOwner().getId(); + String context = secret.getContext(); + return String.format( + "User %s in context %s is authorized to execute this method because he has the role %s", userId, + context, ALLOWED_ROLE); + } + +} diff --git a/src/main/java/org/gcube/service/idm/test/services/ExcludeAuthorizationService.java b/src/main/java/org/gcube/service/idm/test/services/ExcludeAuthorizationService.java new file mode 100644 index 0000000..726298b --- /dev/null +++ b/src/main/java/org/gcube/service/idm/test/services/ExcludeAuthorizationService.java @@ -0,0 +1,31 @@ +package org.gcube.service.idm.test.services; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@Path("guest") +public class ExcludeAuthorizationService { + + private final Logger logger = LoggerFactory.getLogger(ExcludeAuthorizationService.class); + + /** + * this method doesn't need authorization and the SecretManagerProvider is null + * see to implement this behavior add to excludes section in your + * application.yaml + * + * - path: /{path-to-your-method-path} + * + * example for this method + * + * - path: /excluded + * + */ + @GET + public String exludedMethod() { + logger.info("executed whithout any authorization"); + return "executed whithout any authorization"; + } +} diff --git a/src/main/java/org/gcube/service/idm/test/services/HelloService.java b/src/main/java/org/gcube/service/idm/test/services/HelloService.java new file mode 100644 index 0000000..46965e6 --- /dev/null +++ b/src/main/java/org/gcube/service/idm/test/services/HelloService.java @@ -0,0 +1,87 @@ +package org.gcube.service.idm.test.services; + + +import java.util.HashMap; +import java.util.Map; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +import org.gcube.common.security.Owner; +import org.gcube.common.security.providers.SecretManagerProvider; +import org.gcube.common.security.secrets.Secret; +import org.gcube.service.idm.IdMManager; +import org.gcube.service.idm.serializers.ContextSerializator; +import org.gcube.smartgears.ContextProvider; +import org.gcube.smartgears.annotations.ManagedBy; +import org.gcube.smartgears.context.application.ApplicationContext; +import org.gcube.smartgears.context.container.ContainerContext; +import org.gcube.smartgears.utils.InnerMethodName; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +/** + * + * @author Lucio Lelii (ISTI-CNR) + * @author Alfredo Oliviero (ISTI-CNR) + */ + +@ManagedBy(IdMManager.class) +@Path("") +public class HelloService { + + private final Logger logger = LoggerFactory.getLogger(HelloService.class); + + @GET + @Path("hello") + @Produces(MediaType.TEXT_PLAIN) + public String hello() { + InnerMethodName.set("hello"); + Secret secret = SecretManagerProvider.get(); + String userId = secret.getOwner().getId(); + String context = secret.getContext(); + + String infrastructureName = ContextProvider.get().container().configuration().infrastructure(); + logger.info("caller id is {}", userId); + return String.format("Hello %s in context %s in infastructure %s -roles %s", userId, context, + infrastructureName, secret.getOwner().getRoles()); + } + + @GET + @Path("details") + @Produces({ MediaType.APPLICATION_JSON }) + public Response details() { + InnerMethodName.set("details"); + + Map data = new HashMap<>(); + + Secret secret = SecretManagerProvider.get(); + + String context = secret.getContext(); + data.put("context", context); + + Owner owner = secret.getOwner(); + data.put("owner", owner); + + ContainerContext container = ContextProvider.get().container(); + data.put("container", container); + + ObjectMapper objectMapper = ContextSerializator.getSerializer(); + + try { + String jsonData = objectMapper.writeValueAsString(data); + return Response.ok(jsonData).build(); + + } catch (JsonProcessingException e) { + e.printStackTrace(); + return Response.serverError().build(); + } + } + +} \ No newline at end of file diff --git a/src/main/java/org/gcube/rest/ResponseBean.java b/src/main/java/org/gcube/service/rest/ResponseBean.java similarity index 97% rename from src/main/java/org/gcube/rest/ResponseBean.java rename to src/main/java/org/gcube/service/rest/ResponseBean.java index 2e5fea8..b908a8d 100644 --- a/src/main/java/org/gcube/rest/ResponseBean.java +++ b/src/main/java/org/gcube/service/rest/ResponseBean.java @@ -1,4 +1,4 @@ -package org.gcube.rest; +package org.gcube.service.rest; import java.io.Serializable; diff --git a/src/main/java/org/gcube/rest/annotation/PATCH.java b/src/main/java/org/gcube/service/rest/annotation/PATCH.java similarity index 88% rename from src/main/java/org/gcube/rest/annotation/PATCH.java rename to src/main/java/org/gcube/service/rest/annotation/PATCH.java index 72dd40e..7cf6741 100644 --- a/src/main/java/org/gcube/rest/annotation/PATCH.java +++ b/src/main/java/org/gcube/service/rest/annotation/PATCH.java @@ -1,4 +1,4 @@ -package org.gcube.rest.annotation; +package org.gcube.service.rest.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; diff --git a/src/main/java/org/gcube/rest/annotation/PURGE.java b/src/main/java/org/gcube/service/rest/annotation/PURGE.java similarity index 88% rename from src/main/java/org/gcube/rest/annotation/PURGE.java rename to src/main/java/org/gcube/service/rest/annotation/PURGE.java index 928cfe3..804e40e 100644 --- a/src/main/java/org/gcube/rest/annotation/PURGE.java +++ b/src/main/java/org/gcube/service/rest/annotation/PURGE.java @@ -1,4 +1,4 @@ -package org.gcube.rest.annotation; +package org.gcube.service.rest.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; diff --git a/src/main/java/org/gcube/rest/utils/HTTPUtility.java b/src/main/java/org/gcube/service/rest/utils/HTTPUtility.java similarity index 97% rename from src/main/java/org/gcube/rest/utils/HTTPUtility.java rename to src/main/java/org/gcube/service/rest/utils/HTTPUtility.java index 1318545..eb42086 100644 --- a/src/main/java/org/gcube/rest/utils/HTTPUtility.java +++ b/src/main/java/org/gcube/service/rest/utils/HTTPUtility.java @@ -1,4 +1,4 @@ -package org.gcube.rest.utils; +package org.gcube.service.rest.utils; import java.io.BufferedReader; import java.io.IOException;