code formatting

This commit is contained in:
Alfredo Oliviero 2024-03-26 17:41:10 +01:00
parent c4786572ab
commit 9508b7329c
9 changed files with 78 additions and 74 deletions

View File

@ -11,6 +11,8 @@ This service allows any client to publish on the gCube Catalogue.
[Identity Manager Service](https://wiki.gcube-system.org/gcube/SmartGears) [Identity Manager Service](https://wiki.gcube-system.org/gcube/SmartGears)
configuration: inserte
## Change log ## Change log
See [CHANGELOG.md](CHANGELOG.md). See [CHANGELOG.md](CHANGELOG.md).
@ -18,7 +20,6 @@ See [CHANGELOG.md](CHANGELOG.md).
## Authors ## Authors
* **Alfredo Oliviero** [ISTI-CNR Infrascience Group](http://nemis.isti.cnr.it/groups/infrascience) * **Alfredo Oliviero** [ISTI-CNR Infrascience Group](http://nemis.isti.cnr.it/groups/infrascience)
* **Luca Frosini** ([ORCID](https://orcid.org/0000-0003-3183-2291)) - [ISTI-CNR Infrascience Group](http://nemis.isti.cnr.it/groups/infrascience)
## How to Cite this Software ## How to Cite this Software

28
pom.xml
View File

@ -92,23 +92,23 @@
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/com.auth0/java-jwt --> <!-- https://mvnrepository.com/artifact/com.auth0/java-jwt -->
<dependency> <dependency>
<groupId>com.auth0</groupId> <groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId> <artifactId>java-jwt</artifactId>
<version>4.4.0</version> <version>4.4.0</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.fasterxml.jackson.datatype</groupId> <groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId> <artifactId>jackson-datatype-jsr310</artifactId>
<version>${jackson.version}</version> <version>${jackson.version}</version>
</dependency> </dependency>
<!-- <!--
usermanagement-core depends on older httpcommon and gives this error on keycloak client: usermanagement-core depends on older httpcommon and gives this error on keycloak client:
java.lang.NoClassDefFoundError: org/apache/http/ssl/TrustStrategy java.lang.NoClassDefFoundError: org/apache/http/ssl/TrustStrategy
https://stackoverflow.com/questions/44750124/classnotfoundexception-on-org-apache-http-ssl-truststrategy https://stackoverflow.com/questions/44750124/classnotfoundexception-on-org-apache-http-ssl-truststrategy
solution: bind version, or exclude them in usermanagement-core solution: bind version, or exclude them in usermanagement-core
@ -238,10 +238,10 @@ solution: bind version, or exclude them in usermanagement-core
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<!-- <!--
usermanagement-core depends on older httpcommon and gives this error on keycloak client: usermanagement-core depends on older httpcommon and gives this error on keycloak client:
java.lang.NoClassDefFoundError: org/apache/http/ssl/TrustStrategy java.lang.NoClassDefFoundError: org/apache/http/ssl/TrustStrategy
https://stackoverflow.com/questions/44750124/classnotfoundexception-on-org-apache-http-ssl-truststrategy https://stackoverflow.com/questions/44750124/classnotfoundexception-on-org-apache-http-ssl-truststrategy
--> -->
<dependency> <dependency>
<groupId>org.gcube.dvos</groupId> <groupId>org.gcube.dvos</groupId>
@ -253,7 +253,7 @@ https://stackoverflow.com/questions/44750124/classnotfoundexception-on-org-apach
<groupId>org.apache.httpcomponents</groupId> <groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId> <artifactId>httpclient</artifactId>
</exclusion> </exclusion>
<exclusion> <exclusion>
<groupId>org.apache.httpcomponents</groupId> <groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId> <artifactId>httpcore</artifactId>

View File

@ -6,20 +6,19 @@ public class ErrorMessages {
protected static final String CANNOT_RETRIEVE_SERVICE_ENDPOINT_INFORMATION = "Unable to retrieve such service endpoint information"; protected static final String CANNOT_RETRIEVE_SERVICE_ENDPOINT_INFORMATION = "Unable to retrieve such service endpoint information";
private static final String NO_RUNTIME_RESOURCE_TEMPLATE_NAME_CATEGORY = "There is no Runtime Resource having name %s and Category %s in this scope"; private static final String NO_RUNTIME_RESOURCE_TEMPLATE_NAME_CATEGORY = "There is no Runtime Resource having name %s and Category %s in this scope";
public static final String MISSING_TOKEN = "Missing token."; public static final String MISSING_TOKEN = "Missing token.";
public static final String MISSING_PARAMETERS = "Missing request parameters."; public static final String MISSING_PARAMETERS = "Missing request parameters.";
public static final String INVALID_TOKEN = "Invalid token."; public static final String INVALID_TOKEN = "Invalid token.";
public static final String TOKEN_GENERATION_APP_FAILED = "Token generation failed."; public static final String TOKEN_GENERATION_APP_FAILED = "Token generation failed.";
public static final String NOT_APP_TOKEN = "Invalid token: not belonging to an application."; public static final String NOT_APP_TOKEN = "Invalid token: not belonging to an application.";
public static final String NOT_APP_ID = "Invalid application id: it doesn't belong to an application."; public static final String NOT_APP_ID = "Invalid application id: it doesn't belong to an application.";
public static final String NO_APP_PROFILE_FOUND = "There is no application profile for this app id/scope."; public static final String NO_APP_PROFILE_FOUND = "There is no application profile for this app id/scope.";
public static final String BAD_REQUEST = "Please check the parameter you passed, it seems a bad request"; public static final String BAD_REQUEST = "Please check the parameter you passed, it seems a bad request";
public static final String ERROR_IN_API_RESULT = "The error is reported into the 'message' field of the returned object"; public static final String ERROR_IN_API_RESULT = "The error is reported into the 'message' field of the returned object";
public static final String POST_OUTSIDE_VRE = "A post cannot be written into a context that is not a VRE"; public static final String POST_OUTSIDE_VRE = "A post cannot be written into a context that is not a VRE";
public static final String DEPRECATED_METHOD = "This method is deprecated, must use version 2"; public static final String DEPRECATED_METHOD = "This method is deprecated, must use version 2";
protected static final String no_runtime_category(String runtime, String category) { protected static final String no_runtime_category(String runtime, String category) {
return String.format(NO_RUNTIME_RESOURCE_TEMPLATE_NAME_CATEGORY, runtime, category); return String.format(NO_RUNTIME_RESOURCE_TEMPLATE_NAME_CATEGORY, runtime, category);
} }

View File

@ -8,17 +8,17 @@ public class KeycloackApiClient {
public String clientIdContext; public String clientIdContext;
public String context; public String context;
public static String getClientIdContext(String context){ public static String getClientIdContext(String context) {
return context.replace("/", "%2F"); return context.replace("/", "%2F");
} }
public KeycloackApiClient(Keycloak kclient, String realmName, String context) { public KeycloackApiClient(Keycloak kclient, String realmName, String context) {
this.clientIdContext = getClientIdContext(context); this.clientIdContext = getClientIdContext(context);
this.context = context; this.context = context;
this.kclient = kclient; this.kclient = kclient;
this.realmName = realmName; this.realmName = realmName;
//ClientsResource clients = kclient.realm(realmName).clients().get* // ClientsResource clients = kclient.realm(realmName).clients().get*
//clients.get(context); // clients.get(context);
} }
} }

View File

@ -18,7 +18,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.gcube.common.authorization.utils.secret.Secret; import org.gcube.common.authorization.utils.secret.Secret;
/*** /***
* from gcat implementation * from gcat implementation
* *
@ -61,7 +60,7 @@ public class KeycloackClientGcatFactory {
} catch (Exception e) { } catch (Exception e) {
throw new InternalServerErrorException( throw new InternalServerErrorException(
"Unable to retrieve Application Token for context " "Unable to retrieve Application Token for context "
/*+ SecretManagerProvider.instance.get().getContext() */, /* + SecretManagerProvider.instance.get().getContext() */,
e); e);
} }
} }
@ -76,7 +75,7 @@ public class KeycloackClientGcatFactory {
} }
public URL getServerURL() { public URL getServerURL() {
try { try {
return this.getRealmBaseURL(); return this.getRealmBaseURL();
} catch (KeycloakClientException e) { } catch (KeycloakClientException e) {
// That should be almost impossible // That should be almost impossible
@ -85,19 +84,19 @@ public class KeycloackClientGcatFactory {
} }
} }
public String getClientid() { public String getClientid() {
return clientId; return clientId;
} }
// TODO: serve? implementare // TODO: serve? implementare
public String getPassword() { public String getPassword() {
return null; return null;
} }
// TODO: VERIFICARE // TODO: VERIFICARE
public String getRealm() { public String getRealm() {
return this.context; return this.context;
} }
public KeycloackClientGcatFactory(String context) { public KeycloackClientGcatFactory(String context) {
this.context = context; this.context = context;
@ -108,17 +107,18 @@ public class KeycloackClientGcatFactory {
this.gcubeKeycloakClient = new DefaultKeycloakClient(); this.gcubeKeycloakClient = new DefaultKeycloakClient();
} }
private static TokenResponse getJWTAccessToken() throws Exception { private static TokenResponse getJWTAccessToken() throws Exception {
String context = SecretManagerProvider.get().getContext(); String context = SecretManagerProvider.get().getContext();
Entry<String,String> entry = getClientIdAndClientSecret(context); Entry<String, String> entry = getClientIdAndClientSecret(context);
TokenResponse tr = KeycloakClientFactory.newInstance().queryUMAToken(context, entry.getKey(), entry.getValue(), context, null); TokenResponse tr = KeycloakClientFactory.newInstance().queryUMAToken(context, entry.getKey(), entry.getValue(),
return tr; context, null);
} return tr;
}
public static Secret getCatalogueSecret() throws Exception {
TokenResponse tr = getJWTAccessToken(); public static Secret getCatalogueSecret() throws Exception {
Secret secret = new JWTSecret(tr.getAccessToken()); TokenResponse tr = getJWTAccessToken();
return secret; Secret secret = new JWTSecret(tr.getAccessToken());
} return secret;
}
} }

View File

@ -22,9 +22,6 @@ import org.keycloak.admin.client.KeycloakBuilder;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import jakarta.ws.rs.DefaultValue;
import jakarta.ws.rs.QueryParam;
public class KeycloakClientFactory { public class KeycloakClientFactory {
private static final Logger logger = LoggerFactory.getLogger(KeycloakClientFactory.class); private static final Logger logger = LoggerFactory.getLogger(KeycloakClientFactory.class);
@ -141,16 +138,16 @@ public class KeycloakClientFactory {
public KeycloackApiClient createtKeycloakInstance(String context) { public KeycloackApiClient createtKeycloakInstance(String context) {
return defaultKeycloackInstance(context); return defaultKeycloackInstance(context);
// // String clientIdContext = KeycloackUtils.getClientIdContext(context); // // String clientIdContext = KeycloackUtils.getClientIdContext(context);
// String realm = this.getRealm(); // String realm = this.getRealm();
// Keycloak keycloak = KeycloakBuilder.builder() // Keycloak keycloak = KeycloakBuilder.builder()
// .serverUrl(this.getServerURL()) // .serverUrl(this.getServerURL())
// .realm(realm) // .realm(realm)
// .grantType(OAuth2Constants.CLIENT_CREDENTIALS) // .grantType(OAuth2Constants.CLIENT_CREDENTIALS)
// .clientId(this.getClientid()) // // .clientId(this.getClientid()) //
// .clientSecret(this.getPassword()).build(); // .clientSecret(this.getPassword()).build();
// return // return
} }
public KeycloackApiClient defaultKeycloackInstance(String context) { public KeycloackApiClient defaultKeycloackInstance(String context) {

View File

@ -30,7 +30,6 @@ import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam; import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.Response; import jakarta.ws.rs.core.Response;
@ManagedBy(IdMManager.class) @ManagedBy(IdMManager.class)
@Path("") @Path("")
public class KeycloakTestService { public class KeycloakTestService {
@ -51,8 +50,7 @@ public class KeycloakTestService {
@QueryParam("realm") @DefaultValue("d4science") String realm, @QueryParam("realm") @DefaultValue("d4science") String realm,
@QueryParam("role-name") @DefaultValue("Member") String roleName, @QueryParam("role-name") @DefaultValue("Member") String roleName,
@QueryParam("client_id") @DefaultValue("id.d4science.org") String clientId, @QueryParam("client_id") @DefaultValue("id.d4science.org") String clientId,
@QueryParam("client_secret") @DefaultValue(CLIENT_SECRET) String client_secret @QueryParam("client_secret") @DefaultValue(CLIENT_SECRET) String client_secret) {
) {
String client_contenxt = "/gcube"; String client_contenxt = "/gcube";
@ -139,4 +137,3 @@ public class KeycloakTestService {
return users_repr; return users_repr;
} }
} }

View File

@ -25,13 +25,15 @@ import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
public class ContextSerializator { public class ContextSerializator {
private static ObjectMapper serializer = null; private static ObjectMapper serializer = null;
protected static TypeReference<HashMap<String,Object>> typeRefHashmap = new TypeReference<HashMap<String,Object>>() {}; protected static TypeReference<HashMap<String, Object>> typeRefHashmap = new TypeReference<HashMap<String, Object>>() {
};
public static String decodeBase64String(String encodedString){ public static String decodeBase64String(String encodedString) {
return new String(Base64.getUrlDecoder().decode(encodedString), StandardCharsets.UTF_8); return new String(Base64.getUrlDecoder().decode(encodedString), StandardCharsets.UTF_8);
} }
public static HashMap<String, Object> jsonStringToHasmap(String jsonString) throws JsonMappingException, JsonProcessingException { public static HashMap<String, Object> jsonStringToHasmap(String jsonString)
throws JsonMappingException, JsonProcessingException {
return getSerializer().readValue(jsonString, typeRefHashmap); return getSerializer().readValue(jsonString, typeRefHashmap);
} }
@ -46,7 +48,7 @@ public class ContextSerializator {
module.addSerializer(SimpleCredentials.class, new SimpleCredentialsSerializer()); module.addSerializer(SimpleCredentials.class, new SimpleCredentialsSerializer());
// DecodedJWT serialization // DecodedJWT serialization
om.registerModule(new JavaTimeModule()); om.registerModule(new JavaTimeModule());
serializer = om; serializer = om;
} }
return serializer; return serializer;

View File

@ -4,29 +4,31 @@ import org.gcube.common.authorization.library.ClientType;
import org.gcube.common.authorization.library.utils.Caller; import org.gcube.common.authorization.library.utils.Caller;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
/** /**
* Tokens utils methods * Tokens utils methods
*/ */
public class TokensUtils { public class TokensUtils {
// a user context token (not qualified) has as qualifier the word "TOKEN" // a user context token (not qualified) has as qualifier the word "TOKEN"
private static final String DEFAULT_QUALIFIER_USER_TOKEN = "TOKEN"; private static final String DEFAULT_QUALIFIER_USER_TOKEN = "TOKEN";
// Logger // Logger
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(TokensUtils.class); private static final org.slf4j.Logger logger = LoggerFactory.getLogger(TokensUtils.class);
/** /**
* Check if it is a service token * Check if it is a service token
*
* @return a boolean value * @return a boolean value
*/ */
public static boolean isServiceToken(Caller caller){ public static boolean isServiceToken(Caller caller) {
return caller.getClient().getType().equals(ClientType.SERVICE); return caller.getClient().getType().equals(ClientType.SERVICE);
} }
/** /**
* Check if it is an application token * Check if it is an application token
*
* @return a boolean value * @return a boolean value
*/ */
public static boolean isApplicationToken(Caller caller){ public static boolean isApplicationToken(Caller caller) {
String username = caller.getClient().getId(); String username = caller.getClient().getId();
if (username.startsWith("service-account-")) { if (username.startsWith("service-account-")) {
return true; return true;
@ -37,9 +39,10 @@ public class TokensUtils {
/** /**
* Check if it is a container token * Check if it is a container token
*
* @return a boolean value * @return a boolean value
*/ */
public static boolean isContainerToken(Caller caller){ public static boolean isContainerToken(Caller caller) {
return caller.getClient().getType().equals(ClientType.CONTAINER); return caller.getClient().getType().equals(ClientType.CONTAINER);
@ -47,10 +50,12 @@ public class TokensUtils {
/** /**
* Check if it is a user token * Check if it is a user token
*
* @return a boolean value * @return a boolean value
*/ */
public static boolean isUserToken(Caller caller) { public static boolean isUserToken(Caller caller) {
logger.debug("\n ****** \n isUserToken: caller.getClient().getType().equals(ClientType.USER) => " + caller.getClient().getType().equals(ClientType.USER)); logger.debug("\n ****** \n isUserToken: caller.getClient().getType().equals(ClientType.USER) => "
+ caller.getClient().getType().equals(ClientType.USER));
String username = caller.getClient().getId(); String username = caller.getClient().getId();
if (username.startsWith("service-account-")) { if (username.startsWith("service-account-")) {
return false; return false;
@ -61,19 +66,22 @@ public class TokensUtils {
/** /**
* Check if it is a user token (not qualified) * Check if it is a user token (not qualified)
*
* @return a boolean value * @return a boolean value
*/ */
public static boolean isUserTokenDefault(Caller caller){ public static boolean isUserTokenDefault(Caller caller) {
return isUserToken(caller); return isUserToken(caller);
} }
/** /**
* Check if it is a user token (qualified) * Check if it is a user token (qualified)
*
* @return a boolean value * @return a boolean value
*/ */
public static boolean isUserTokenQualified(Caller caller){ public static boolean isUserTokenQualified(Caller caller) {
return caller.getClient().getType().equals(ClientType.USER) && !caller.getTokenQualifier().equals(DEFAULT_QUALIFIER_USER_TOKEN); return caller.getClient().getType().equals(ClientType.USER)
&& !caller.getTokenQualifier().equals(DEFAULT_QUALIFIER_USER_TOKEN);
} }