Compare commits

...

4 Commits

Author SHA1 Message Date
Alfredo Oliviero d89f456247 starter e config 2024-02-27 16:07:27 +01:00
Alfredo Oliviero e64b19bddf documentazione 2024-02-27 12:45:01 +01:00
Alfredo Oliviero c7f147d1c9 moved manager 2024-02-27 12:42:24 +01:00
Alfredo Oliviero 914197e3f2 code format 2024-02-26 10:05:14 +01:00
16 changed files with 178 additions and 91 deletions

4
.vscode/launch.json vendored
View File

@ -7,10 +7,10 @@
{ {
"type": "java", "type": "java",
"name": "Debug (Attach)", "name": "Debug (Attach)",
"projectName": "identity-manager", "projectName": "helloword",
"request": "attach", "request": "attach",
"hostName": "localhost", "hostName": "localhost",
"port": 5005 "port": 5001
} }
] ]
} }

View File

@ -1,3 +1,9 @@
{ {
"java.configuration.updateBuildConfiguration": "automatic" "java.configuration.updateBuildConfiguration": "automatic",
"Workspace_Formatter.excludePattern": [
"**/build",
"**/.*",
"**/.vscode",
"**/target/*"
]
} }

72
Instructions.md Normal file
View File

@ -0,0 +1,72 @@
# instructions
## project structure
├── .gitignore
├── pom.xml
├── src
│ └── ***
├── gcube/
│ └── extra-resources/
│ └── WEB-INF/
│ ├── application.yaml
│ └── web.xml
# optional, for local develop in docker containers
├── docker/
│ ├── ontainer.ini
│ └── logback.xml
├── docker-compose.yaml
└── Dockerfile
### file details
* gitignore: gitignore file for java projects
* pom.xml
* <parent>: maven-parent configuration. currently 1.2.0 version
* <properties><webappDirectory> : path for web.xml
* <scm> ???
* <dependencyManagement> gcube dependency manager
* docker/container.ini: configurations for the docker container.
* mode: [online/offline]: register the service on IS. use offline for local docker instances
# EXCECUTION
start the docker container
```./buildImageAndStart.sh```
test urls (replace {{TOKEN}} with your token):
http://localhost:8081/helloworld/guest
http://localhost:8080/helloworld/details?gcube-token={{TOKEN}}
http://localhost:8080/helloworld/hello/details?gcube-token={{TOKEN}}
http://localhost:8080/helloworld/auth?gcube-token={{TOKEN}} (checks for myRole role)
http://localhost:8080/helloworld/auth/orm_member?gcube-token={{TOKEN}} (checks for OrganizationMember role)
### Authentication:
#### GCUBE-TOKEN param
obtain personal token at https://next.dev.d4science.org/group/gcube/home
add
### DEBUG
start the docker container in debug Mode
```./buildImageAndStartWithDebug.sh```
connect the JAVA debugger to port 5005
* VSCODE: press the green play button and start "Debug (Attach)

View File

@ -14,40 +14,6 @@ HelloWorld service for smartgears4
## Documentation ## Documentation
start the docker container
```./buildImageAndStart.sh```
test urls (replace {{TOKEN}} with your token):
http://localhost:8080/helloworld/guest
http://localhost:8080/helloworld/details?gcube-token={{TOKEN}}
http://localhost:8080/helloworld/hello/details?gcube-token={{TOKEN}}
http://localhost:8080/helloworld/auth?gcube-token={{TOKEN}} (checks for myRole role)
http://localhost:8080/helloworld/auth/orm_member?gcube-token={{TOKEN}} (checks for OrganizationMember role)
### Authentication:
#### GCUBE-TOKEN param
obtain personal token at https://next.dev.d4science.org/group/gcube/home
add
### DEBUG
start the docker container in debug Mode
```./buildImageAndStartWithDebug.sh```
connect the JAVA debugger to port 5005
* VSCODE: press the green play button and start "Debug (Attach)
## Change log ## Change log
See [Releases](https://code-repo.d4science.org/gCubeSystem/helloworld/releases). See [Releases](https://code-repo.d4science.org/gCubeSystem/helloworld/releases).

View File

@ -1,3 +1,8 @@
mvn clean package mvn clean package
docker build -t smartgears-helloworld .
docker run -p 8080:8080 smartgears-helloworld NAME=smartgears-helloworld
PORT=8081
DEBUG_PORT=5001
docker build -t $NAME .
docker run -p $PORT:8080 $NAME

View File

@ -1,3 +1,9 @@
mvn package mvn clean package
docker build -t smartgears-helloworld .
docker run -p 8080:8080 -p 5005:5005 -e JAVA_TOOL_OPTIONS="-agentlib:jdwp=transport=dt_socket,address=*:5005,server=y,suspend=y" smartgears-helloworld NAME=smartgears-helloworld
PORT=8081
DEBUG_PORT=5001
docker build -t $NAME .
docker run -p $PORT:8080 -p $DEBUG_PORT:5005 -e JAVA_TOOL_OPTIONS="-agentlib:jdwp=transport=dt_socket,address=*:5005,server=y,suspend=y" $NAME

View File

@ -1,18 +1,16 @@
<configuration scan="true" debug="true"> <configuration scan="true" debug="true">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>Ï <encoder>Ï <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder>
</encoder>
</appender> </appender>
<logger name="org.gcube.service.helloworld" level="DEBUG" /> <logger name="org.gcube.service.helloworld" level="DEBUG" />
<logger name="org.gcube.smartgears" level="DEBUG" /> <logger name="org.gcube.smartgears" level="DEBUG" />
<root level="WARN"> <root level="WARN">
<appender-ref ref="STDOUT" /> <appender-ref ref="STDOUT" />
</root> </root>
</configuration> </configuration>

View File

@ -311,7 +311,7 @@
"variable": [ "variable": [
{ {
"key": "base_url", "key": "base_url",
"value": "http://localhost:8080", "value": "http://localhost:8081",
"type": "string" "type": "string"
}, },
{ {

View File

@ -5,4 +5,4 @@ description: HelloWorld Service
excludes: excludes:
- path: /excluded - path: /excluded
- path: /guest - path: /guest
- path: /api-docs/* - path: /api-docs/*

View File

@ -1,11 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0">
version="4.0"> <display-name>Hello World</display-name>
<display-name>Hello World</display-name> <description>
<description>
A gcube HelloWorld service - smartgears 4 A gcube HelloWorld service - smartgears 4
</description> </description>
<servlet> <servlet>
@ -15,4 +14,4 @@
<servlet-name>org.gcube.service.helloworld.HelloWorld</servlet-name> <servlet-name>org.gcube.service.helloworld.HelloWorld</servlet-name>
<url-pattern>/*</url-pattern> <url-pattern>/*</url-pattern>
</servlet-mapping> </servlet-mapping>
</web-app> </web-app>

41
pom.xml
View File

@ -2,23 +2,30 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.gcube.tools</groupId>
<artifactId>maven-parent</artifactId>
<version>1.2.0</version>
</parent>
<groupId>org.gcube.service</groupId> <groupId>org.gcube.service</groupId>
<artifactId>helloworld</artifactId> <artifactId>helloworld</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
<name>Smartgears HelloWorld Service</name> <name>Smartgears HelloWorld Service</name>
<packaging>war</packaging> <packaging>war</packaging>
<parent>
<groupId>org.gcube.tools</groupId>
<artifactId>maven-parent</artifactId>
<version>1.2.0</version>
</parent>
<properties> <properties>
<java.version>11</java.version> <java.version>11</java.version>
<aspectj-plugin.version>1.14.0</aspectj-plugin.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>${java.version}</maven.compiler.source> <maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target> <maven.compiler.target>${java.version}</maven.compiler.target>
<!-- OPTIONAL. for authorization-control-library -->
<aspectj-plugin.version>1.14.0</aspectj-plugin.version>
</properties> </properties>
<scm> <scm>
<connection> <connection>
scm:git:https://code-repo.d4science.org/gCubeSystem/hello-world-sg4-service.git</connection> scm:git:https://code-repo.d4science.org/gCubeSystem/hello-world-sg4-service.git</connection>
@ -26,6 +33,7 @@
scm:git:https://code-repo.d4science.org/gCubeSystem/hello-world-sg4-service.git</developerConnection> scm:git:https://code-repo.d4science.org/gCubeSystem/hello-world-sg4-service.git</developerConnection>
<url>https://code-repo.d4science.org/gCubeSystem/hello-world-sg4-service.git</url> <url>https://code-repo.d4science.org/gCubeSystem/hello-world-sg4-service.git</url>
</scm> </scm>
<dependencyManagement> <dependencyManagement>
<dependencies> <dependencies>
<dependency> <dependency>
@ -37,6 +45,7 @@
</dependency> </dependency>
</dependencies> </dependencies>
</dependencyManagement> </dependencyManagement>
<dependencies> <dependencies>
<!-- smartgears --> <!-- smartgears -->
<dependency> <dependency>
@ -55,6 +64,7 @@
<groupId>org.gcube.core</groupId> <groupId>org.gcube.core</groupId>
<artifactId>common-smartgears-app</artifactId> <artifactId>common-smartgears-app</artifactId>
</dependency> </dependency>
<!-- jersey --> <!-- jersey -->
<dependency> <dependency>
<groupId>javax.ws.rs</groupId> <groupId>javax.ws.rs</groupId>
@ -64,6 +74,7 @@
<groupId>org.glassfish.jersey.containers</groupId> <groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId> <artifactId>jersey-container-servlet</artifactId>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.inject/jersey-cdi2-se --> <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.inject/jersey-cdi2-se -->
<dependency> <dependency>
<groupId>org.glassfish.jersey.inject</groupId> <groupId>org.glassfish.jersey.inject</groupId>
@ -74,6 +85,7 @@
<artifactId>javax.servlet-api</artifactId> <artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version> <version>3.0.1</version>
</dependency> </dependency>
<!-- add jackson as json provider --> <!-- add jackson as json provider -->
<dependency> <dependency>
<groupId>org.glassfish.jersey.media</groupId> <groupId>org.glassfish.jersey.media</groupId>
@ -84,10 +96,25 @@
<artifactId>slf4j-api</artifactId> <artifactId>slf4j-api</artifactId>
</dependency> </dependency>
</dependencies> </dependencies>
<build> <build>
<finalName>${project.artifactId}</finalName> <finalName>${project.artifactId}</finalName>
<plugins> <plugins>
<!-- add this plugin if you want to use gcube authorization control funzionalities -->
<!-- OPTIONAL generate the war in a different folder, that will be mounted on docker
container -->
<!-- <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<warName>identity-manager</warName>
<outputDirectory>tomcat/webapps</outputDirectory>
</configuration>
</plugin> -->
<!-- OPTIONAL. authorization-control-library: add this plugin if you want to use gcube
authorization control funzionalities -->
<plugin> <plugin>
<groupId>org.codehaus.mojo</groupId> <groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId> <artifactId>aspectj-maven-plugin</artifactId>

View File

@ -1,4 +1,4 @@
package org.gcube.service.helloworld.manager; package org.gcube.service.helloworld;
import org.gcube.common.security.providers.SecretManagerProvider; import org.gcube.common.security.providers.SecretManagerProvider;
import org.gcube.common.security.secrets.Secret; import org.gcube.common.security.secrets.Secret;
@ -23,8 +23,11 @@ public class HelloWorldManager implements ApplicationManager {
Logger logger = LoggerFactory.getLogger(HelloWorldManager.class); Logger logger = LoggerFactory.getLogger(HelloWorldManager.class);
/**
* {@inheritDoc}
*/
@Override @Override
public void onInit() { public void onInit() {
if (ContextProvider.get().container().configuration().mode() == Mode.offline) { if (ContextProvider.get().container().configuration().mode() == Mode.offline) {
logger.debug("init called in offline mode"); logger.debug("init called in offline mode");
} else { } else {
@ -37,6 +40,9 @@ public class HelloWorldManager implements ApplicationManager {
} }
} }
/**
* {@inheritDoc}
*/
@Override @Override
public void onShutdown() { public void onShutdown() {
if (ContextProvider.get().container().configuration().mode() == Mode.offline) { if (ContextProvider.get().container().configuration().mode() == Mode.offline) {

View File

@ -9,9 +9,10 @@ import org.gcube.common.health.api.response.HealthCheckResponse;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
/** /**
* *
* this class is used to add checks about components used by the service (DB, external FS, other services etc. ) * 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 * and automatically exposed using the REST method webapp/gcube/resources/health
* *
* *
@ -20,11 +21,10 @@ import org.slf4j.LoggerFactory;
*/ */
@ReadinessChecker @ReadinessChecker
public class CreateTempFileCheck implements HealthCheck{ public class CreateTempFileCheck implements HealthCheck {
private static Logger log = LoggerFactory.getLogger(CreateTempFileCheck.class); private static Logger log = LoggerFactory.getLogger(CreateTempFileCheck.class);
@Override @Override
public String getName() { public String getName() {
return "create temp file"; return "create temp file";
@ -33,13 +33,13 @@ public class CreateTempFileCheck implements HealthCheck{
@Override @Override
public HealthCheckResponse check() { public HealthCheckResponse check() {
try { try {
File.createTempFile("exampleTest","txt"); File.createTempFile("exampleTest", "txt");
return HealthCheckResponse.builder(getName()).up().info("health check example").build(); return HealthCheckResponse.builder(getName()).up().info("health check example").build();
} catch (IOException e) { } catch (IOException e) {
log.error("error checking defaultStorage",e); log.error("error checking defaultStorage", e);
return HealthCheckResponse.builder(getName()).down().error(e.getMessage()).build(); return HealthCheckResponse.builder(getName()).down().error(e.getMessage()).build();
} }
} }
} }

View File

@ -32,7 +32,8 @@ public class AuthenticatedService {
String userId = secret.getOwner().getId(); String userId = secret.getOwner().getId();
String context = secret.getContext(); String context = secret.getContext();
return String.format( 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); "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 }) @AuthorizationControl(allowedRoles = { ALLOWED_ROLE_MEMBER })
@ -45,10 +46,10 @@ public class AuthenticatedService {
String userId = secret.getOwner().getId(); String userId = secret.getOwner().getId();
String context = secret.getContext(); String context = secret.getContext();
return String.format( 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); "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 }) @AuthorizationControl(allowedRoles = { ALLOWED_ROLE })
@GET @GET
@Path("") @Path("")
@ -59,7 +60,8 @@ public class AuthenticatedService {
String userId = secret.getOwner().getId(); String userId = secret.getOwner().getId();
String context = secret.getContext(); String context = secret.getContext();
return String.format( return String.format(
"User %s in context %s is authorized to execute this method because he has the role %s", userId, context, ALLOWED_ROLE); "User %s in context %s is authorized to execute this method because he has the role %s", userId,
context, ALLOWED_ROLE);
} }
} }

View File

@ -8,20 +8,20 @@ import org.slf4j.LoggerFactory;
@Path("guest") @Path("guest")
public class ExcludeAuthorizationService { public class ExcludeAuthorizationService {
private final Logger logger = LoggerFactory.getLogger(ExcludeAuthorizationService.class);
private final Logger logger = LoggerFactory.getLogger(ExcludeAuthorizationService.class);
/** /**
* this method doesn't need authorization and the SecretManagerProvider is null * this method doesn't need authorization and the SecretManagerProvider is null
* see to implement this behavior add to excludes section in your application.yaml * 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
* *
* - path: /{path-to-your-method-path}
*
* example for this method
*
* - path: /excluded
*
*/ */
@GET @GET
public String exludedMethod() { public String exludedMethod() {

View File

@ -12,7 +12,7 @@ import javax.ws.rs.core.Response;
import org.gcube.common.security.Owner; import org.gcube.common.security.Owner;
import org.gcube.common.security.providers.SecretManagerProvider; import org.gcube.common.security.providers.SecretManagerProvider;
import org.gcube.common.security.secrets.Secret; import org.gcube.common.security.secrets.Secret;
import org.gcube.service.helloworld.manager.HelloWorldManager; import org.gcube.service.helloworld.HelloWorldManager;
import org.gcube.service.helloworld.serializers.ContextSerializator; import org.gcube.service.helloworld.serializers.ContextSerializator;
import org.gcube.smartgears.ContextProvider; import org.gcube.smartgears.ContextProvider;
import org.gcube.smartgears.annotations.ManagedBy; import org.gcube.smartgears.annotations.ManagedBy;