moved to common library

This commit is contained in:
Alfredo Oliviero 2024-05-08 17:59:00 +02:00
parent 881a26c5e7
commit 4ea0762f7f
14 changed files with 150 additions and 139 deletions

View File

@ -1,5 +1,6 @@
package org.gcube.service.idm.controller; package org.gcube.service.idm.controller;
import java.rmi.ServerException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@ -19,6 +20,7 @@ import org.keycloak.representations.idm.RoleRepresentation;
import org.keycloak.representations.idm.UserRepresentation; import org.keycloak.representations.idm.UserRepresentation;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import jakarta.ws.rs.NotFoundException;
import jakarta.ws.rs.WebApplicationException; import jakarta.ws.rs.WebApplicationException;
public class AdminKeycloakController { public class AdminKeycloakController {
@ -28,7 +30,8 @@ public class AdminKeycloakController {
// level) // level)
// https://gist.github.com/thomasdarimont/c4e739c5a319cf78a4cff3b87173a84b // https://gist.github.com/thomasdarimont/c4e739c5a319cf78a4cff3b87173a84b
public static UserRepresentation createUser(String username, String email, String password, String firstName, public static UserRepresentation createUser(String username, String email, String password, String firstName,
String lastName, Map<String, List<String>> attributes, List<String> roles) throws WebApplicationException { String lastName, Map<String, List<String>> attributes, List<String> roles)
throws WebApplicationException, ServerException {
UserRepresentation newUser = new UserRepresentation(); UserRepresentation newUser = new UserRepresentation();
newUser.setEnabled(true); newUser.setEnabled(true);
newUser.setUsername(username); newUser.setUsername(username);

View File

@ -1,5 +1,6 @@
package org.gcube.service.idm.controller; package org.gcube.service.idm.controller;
import java.rmi.ServerException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -112,6 +113,8 @@ public class KCClientsController {
* the context * the context
* *
* @param clientId null for current context * @param clientId null for current context
* @throws ServerException
* @throws NotFoundException
*/ */
public static List<UserRepresentation> getMemberGroupUsersCurrent() { public static List<UserRepresentation> getMemberGroupUsersCurrent() {
@ -126,6 +129,8 @@ public class KCClientsController {
* @param clientId null for current context * @param clientId null for current context
* @param firstResult * @param firstResult
* @param maxResults * @param maxResults
* @throws ServerException
* @throws NotFoundException
*/ */
public static List<UserRepresentation> getMemberGroupUsers(String clientId, Integer firstResult, public static List<UserRepresentation> getMemberGroupUsers(String clientId, Integer firstResult,
Integer maxResults) { Integer maxResults) {

View File

@ -1,5 +1,6 @@
package org.gcube.service.idm.controller; package org.gcube.service.idm.controller;
import java.rmi.ServerException;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.stream.Collectors; import java.util.stream.Collectors;

View File

@ -1,5 +1,6 @@
package org.gcube.service.idm.controller; package org.gcube.service.idm.controller;
import java.rmi.ServerException;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;

View File

@ -27,7 +27,7 @@ public class LiferayProfileClient {
} }
return user; return user;
} catch (NotFoundException | ServerException | UserManagementSystemException e) { } catch (UserManagementSystemException e) {
e.printStackTrace(); e.printStackTrace();
throw new ServiceUnavailableException(errormsg); throw new ServiceUnavailableException(errormsg);
} catch (UserRetrievalFault e) { } catch (UserRetrievalFault e) {

View File

@ -4,6 +4,7 @@ import java.rmi.ServerException;
import java.util.List; import java.util.List;
import org.gcube.common.security.providers.SecretManagerProvider; import org.gcube.common.security.providers.SecretManagerProvider;
import org.gcube.common.security.secrets.Secret;
import org.gcube.idm.common.is.InfrastrctureServiceClient; import org.gcube.idm.common.is.InfrastrctureServiceClient;
import org.gcube.idm.common.is.IsServerConfig; import org.gcube.idm.common.is.IsServerConfig;
import org.keycloak.admin.client.Keycloak; import org.keycloak.admin.client.Keycloak;
@ -14,6 +15,7 @@ import org.keycloak.representations.idm.ClientRepresentation;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import jakarta.ws.rs.InternalServerErrorException;
import jakarta.ws.rs.NotFoundException; import jakarta.ws.rs.NotFoundException;
public class KkClientFactory { public class KkClientFactory {
@ -25,9 +27,11 @@ public class KkClientFactory {
private final static boolean IS_ROOT_SERVICE = true; private final static boolean IS_ROOT_SERVICE = true;
// the singleton obj // the singleton obj
private IsServerConfig config;
private Secret secret;
private static KkClientFactory singleton = new KkClientFactory(); private static KkClientFactory singleton = new KkClientFactory();
public static KkClientFactory getSingleton() { public static KkClientFactory getSingleton() {
if (singleton == null) if (singleton == null)
singleton = new KkClientFactory(); singleton = new KkClientFactory();
@ -39,65 +43,43 @@ public class KkClientFactory {
* using the singleton pattern, it's retrieved from IS only for the first * using the singleton pattern, it's retrieved from IS only for the first
* access, then kept in the singleton object * access, then kept in the singleton object
*/ */
private IsServerConfig config;
public Secret getSecret() {
return secret;
}
public void setSecret(Secret secret) {
this.secret = secret;
this.config = fetchIsConfig(this.secret);
}
// allows to configure the factory // allows to configure the factory
// e.g. from external configuration file // e.g. from external configuration file
public void setConfig(IsServerConfig configuration) { public void setConfig(IsServerConfig configuration) {
config = configuration; config = configuration;
} }
public IsServerConfig fetchIsConfig(Secret secret) throws InternalServerErrorException {
/**
* Private constructor
* obtains the config from IS
*/
private KkClientFactory() {
logger.info("Building KeycloakAPICredentials object");
try { try {
if (this.config == null) { if (this.secret == null)
this.config = fetchIsConfig(); this.secret = InfrastrctureServiceClient.getSecretForInfrastructure();
}
logger.info("KeycloakAPICredentials object built {} - {}", config.getServerUrl(), config.getName());
IsServerConfig cfg = InfrastrctureServiceClient.serviceConfigFromIS(RUNTIME_RESOURCE_NAME, CATEGORY,
END_POINT_NAME, IS_ROOT_SERVICE, secret);
logger.info("KeycloakAPICredentials object built {} - {}", cfg.getServerUrl(), cfg.getName());
return cfg;
} catch (Exception e) { } catch (Exception e) {
logger.error("error obtaining IAM configuration from IS {} ", e); e.printStackTrace();
throw new InternalServerErrorException(e);
} }
} }
// public IsServerConfig configFromINI() throws NotFoundException,
// ServerException{
// ApplicationContext appContext = ContextProvider.get();
// SimpleCredentials credentials =
// (SimpleCredentials)appContext.authorizationProvider().getCredentials();
// IsServerConfig cfg = fetchIsConfig();
// IsServerConfig newConfig = new IsServerConfig(
// cfg.getServerUrl(),
// cfg.getName(),
// credentials.getClientID(), // cfg.getClientId(),
// credentials.getSecret(), // cfg.getClientSecrxet(),
// cfg.getProperties()
// );
// return newConfig;s
// }
public IsServerConfig fetchIsConfig() throws NotFoundException, ServerException {
IsServerConfig cfg = InfrastrctureServiceClient.serviceConfigFromIS(RUNTIME_RESOURCE_NAME, CATEGORY,
END_POINT_NAME, IS_ROOT_SERVICE);
return cfg;
}
public static String encodeClientIdContext(String context) {
return context.replace("/", "%2F");
}
public KeycloackApiClient createtKeycloakInstance(String context) { public KeycloackApiClient createtKeycloakInstance(String context) {
if (this.config == null) {
this.config = fetchIsConfig(this.secret);
}
return createtKeycloakInstance(this.config, context); return createtKeycloakInstance(this.config, context);
} }
@ -151,6 +133,8 @@ public class KkClientFactory {
* *
* @param clientId * @param clientId
* @return * @return
* @throws ServerException
* @throws NotFoundException
*/ */
public ClientResource getKKClientById(String clientId) { public ClientResource getKKClientById(String clientId) {
@ -179,4 +163,7 @@ public class KkClientFactory {
// return new IsServerConfig(serverUrl, realm, clientId, clientSecret); // return new IsServerConfig(serverUrl, realm, clientId, clientSecret);
// } // }
public static String encodeClientIdContext(String context) {
return context.replace("/", "%2F");
}
} }

View File

@ -1,14 +1,13 @@
package org.gcube.service.idm.liferay; package org.gcube.service.idm.liferay;
import java.rmi.ServerException; import org.gcube.common.security.secrets.Secret;
import org.gcube.idm.common.is.InfrastrctureServiceClient; import org.gcube.idm.common.is.InfrastrctureServiceClient;
import org.gcube.idm.common.is.IsServerConfig; import org.gcube.idm.common.is.IsServerConfig;
import org.gcube.vomanagement.usermanagement.impl.ws.LiferayWSUserManager; import org.gcube.vomanagement.usermanagement.impl.ws.LiferayWSUserManager;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import jakarta.ws.rs.NotFoundException; import jakarta.ws.rs.InternalServerErrorException;
public class LiferayClientFactory { public class LiferayClientFactory {
private static final Logger logger = LoggerFactory.getLogger(LoggerFactory.class); private static final Logger logger = LoggerFactory.getLogger(LoggerFactory.class);
@ -22,49 +21,7 @@ public class LiferayClientFactory {
LiferayWSUserManager client = null; LiferayWSUserManager client = null;
private IsServerConfig config; private IsServerConfig config;
private Secret secret;
public static LiferayWSUserManager createtLiferayClientInstance(IsServerConfig config)
throws NotFoundException, ServerException {
String host = config.getServerUrl();
String schema = config.getProperty("schema");
String user = config.getProperty("username");
String password = config.getProperty("password");
Integer port = Integer.valueOf(config.getProperty("port"));
LiferayWSUserManager client = null;
try {
client = new LiferayWSUserManager(user, password, host, schema, port);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new ServerException("cannot create Liferay client");
}
if (client == null) {
throw new ServerException("cannot create Liferay client");
}
logger.info("Liferay object built {} - {}", config.getServerUrl(), config.getName());
return client;
}
public IsServerConfig getConfig() throws NotFoundException, ServerException {
if (this.config == null) {
this.config = fetchIsConfig();
}
return this.config;
}
public LiferayWSUserManager getClient() throws NotFoundException, ServerException {
if (this.client == null) {
IsServerConfig configuration = this.getConfig();
this.client = createtLiferayClientInstance(configuration);
}
return this.client;
}
private static LiferayClientFactory singleton = new LiferayClientFactory(); private static LiferayClientFactory singleton = new LiferayClientFactory();
@ -74,12 +31,75 @@ public class LiferayClientFactory {
return singleton; return singleton;
} }
public IsServerConfig fetchIsConfig() throws NotFoundException, ServerException { public Secret getSecret() {
IsServerConfig cfg = InfrastrctureServiceClient.serviceConfigFromIS(RUNTIME_RESOURCE_NAME, CATEGORY, return secret;
END_POINT_NAME, }
IS_ROOT_SERVICE);
return cfg; public void setSecret(Secret secret) {
this.secret = secret;
this.config = fetchIsConfig(this.secret);
}
public IsServerConfig fetchIsConfig(Secret secret) throws InternalServerErrorException {
try {
if (this.secret == null)
this.secret = InfrastrctureServiceClient.getSecretForInfrastructure();
IsServerConfig cfg = InfrastrctureServiceClient.serviceConfigFromIS(RUNTIME_RESOURCE_NAME, CATEGORY,
END_POINT_NAME, IS_ROOT_SERVICE, secret);
logger.info("KeycloakAPICredentials object built {} - {}", cfg.getServerUrl(), cfg.getName());
return cfg;
} catch (Exception e) {
e.printStackTrace();
throw new InternalServerErrorException(e);
}
}
public LiferayWSUserManager createtLiferayClientInstance() {
if (this.config == null) {
this.config = fetchIsConfig(this.secret);
}
return createtLiferayClientInstance(this.config);
}
public static LiferayWSUserManager createtLiferayClientInstance(IsServerConfig config) {
String host = config.getServerUrl();
String schema = config.getProperty("schema");
String user = config.getProperty("username");
String password = config.getProperty("password");
Integer port = Integer.valueOf(config.getProperty("port"));
LiferayWSUserManager client = null;
try {
client = new LiferayWSUserManager(user, password, host, schema, port);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new InternalServerErrorException("cannot create Liferay client");
}
if (client == null) {
throw new InternalServerErrorException("cannot create Liferay client");
}
logger.info("Liferay object built {} - {}", config.getServerUrl(), config.getName());
return client;
}
// public IsServerConfig getConfig() {
// if (this.config == null) {
// this.config = fetchIsConfig();
// }
// return this.config;
// }
public LiferayWSUserManager getClient() {
if (this.client == null) {
this.client = createtLiferayClientInstance();
}
return this.client;
} }
} }

View File

@ -5,10 +5,9 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.gcube.idm.common.IdmUserInterface; import org.gcube.idm.common.models.IdmProfileInterface;
public class IdmUser implements IdmProfileInterface {
public class IdmUser implements IdmUserInterface {
protected String self; // link protected String self; // link
protected String id; protected String id;
@ -109,12 +108,12 @@ public class IdmUser implements IdmUserInterface {
// @Deprecated // @Deprecated
// public Boolean isTotp() { // public Boolean isTotp() {
// return totp; // return totp;
// } // }
// @Deprecated // @Deprecated
// public void setTotp(Boolean totp) { // public void setTotp(Boolean totp) {
// this.totp = totp; // this.totp = totp;
// } // }
public Boolean isEmailVerified() { public Boolean isEmailVerified() {
@ -134,16 +133,18 @@ public class IdmUser implements IdmUserInterface {
} }
// public UserRepresentation singleAttribute(String name, String value) { // public UserRepresentation singleAttribute(String name, String value) {
// if (this.attributes == null) // if (this.attributes == null)
// this.attributes = new HashMap<>(); // this.attributes = new HashMap<>();
// attributes.put(name, (value == null ? new ArrayList<String>() : Arrays.asList(value))); // attributes.put(name, (value == null ? new ArrayList<String>() :
// return this; // Arrays.asList(value)));
// return this;
// } // }
// public String firstAttribute(String key) { // public String firstAttribute(String key) {
// return this.attributes == null ? null // return this.attributes == null ? null
// : this.attributes.get(key) == null ? null // : this.attributes.get(key) == null ? null
// : this.attributes.get(key).isEmpty() ? null : this.attributes.get(key).get(0); // : this.attributes.get(key).isEmpty() ? null :
// this.attributes.get(key).get(0);
// } // }
// public List<CredentialRepresentation> getCredentials() { // public List<CredentialRepresentation> getCredentials() {
@ -214,7 +215,7 @@ public class IdmUser implements IdmUserInterface {
// @Deprecated // @Deprecated
// public Map<String, List<String>> getApplicationRoles() { // public Map<String, List<String>> getApplicationRoles() {
// return applicationRoles; // return applicationRoles;
// } // }
public String getFederationLink() { public String getFederationLink() {
@ -242,24 +243,25 @@ public class IdmUser implements IdmUserInterface {
} }
// /** // /**
// * Returns id of UserStorageProvider that loaded this user // * Returns id of UserStorageProvider that loaded this user
// * // *
// * @return NULL if user stored locally // * @return NULL if user stored locally
// */ // */
// public String getOrigin() { // public String getOrigin() {
// return origin; // return origin;
// } // }
// public void setOrigin(String origin) { // public void setOrigin(String origin) {
// this.origin = origin; // this.origin = origin;
// } // }
// public Set<String> getDisableableCredentialTypes() { // public Set<String> getDisableableCredentialTypes() {
// return disableableCredentialTypes; // return disableableCredentialTypes;
// } // }
// public void setDisableableCredentialTypes(Set<String> disableableCredentialTypes) { // public void setDisableableCredentialTypes(Set<String>
// this.disableableCredentialTypes = disableableCredentialTypes; // disableableCredentialTypes) {
// this.disableableCredentialTypes = disableableCredentialTypes;
// } // }
public Map<String, Boolean> getAccess() { public Map<String, Boolean> getAccess() {

View File

@ -235,8 +235,6 @@ public class ClientsAPI {
AuthController.checkIsRealmAdmin(ErrorMessages.RESERVED_PARAMETER + "client_id"); AuthController.checkIsRealmAdmin(ErrorMessages.RESERVED_PARAMETER + "client_id");
} }
// String role_name = "Member"; // String role_name = "Member";
boolean show_groups = !format_group.equals(KCGroupsController.REPR.none); boolean show_groups = !format_group.equals(KCGroupsController.REPR.none);

View File

@ -27,8 +27,8 @@ import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.Response.Status; import jakarta.ws.rs.core.Response.Status;
@RequestHeaders({ @RequestHeaders({
@RequestHeader(name = "Authorization", description = "Bearer token, see https://dev.d4science.org/how-to-access-resources"), @RequestHeader(name = "Authorization", description = "Bearer token, see https://dev.d4science.org/how-to-access-resources"),
@RequestHeader(name = "Content-Type", description = "application/json") @RequestHeader(name = "Content-Type", description = "application/json")
}) })
@Path("jwt") @Path("jwt")
public class JwtAPI { public class JwtAPI {

View File

@ -200,7 +200,6 @@ public class SocialUsersAPI {
@QueryParam("max") @DefaultValue("100") int max, @QueryParam("max") @DefaultValue("100") int max,
@QueryParam("firstResult") @DefaultValue("0") int firstResult, @QueryParam("firstResult") @DefaultValue("0") int firstResult,
@QueryParam("maxResults") @DefaultValue("100") int maxResults) { @QueryParam("maxResults") @DefaultValue("100") int maxResults) {
if (firstResult > 0) { if (firstResult > 0) {
first = firstResult; first = firstResult;

View File

@ -4,8 +4,6 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import jakarta.ws.rs.ServiceUnavailableException;
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;

View File

@ -11,11 +11,6 @@ public class ErrorMessages {
public static final String RESERVED_PARAMETER = "The parameter can be used only by realm administrators: "; public static final String RESERVED_PARAMETER = "The parameter can be used only by realm administrators: ";
// //
// protected static final String CANNOT_RETRIEVE_SERVICE_ENDPOINT_INFORMATION = // protected static final String CANNOT_RETRIEVE_SERVICE_ENDPOINT_INFORMATION =
// "Unable to retrieve such service endpoint information"; // "Unable to retrieve such service endpoint information";

10
todo.md
View File

@ -1,9 +1,13 @@
# OPENSTACK
* [ ] deploy su openstack
* [ ] aggiungere accounting
# CONFIGURAZIONE # CONFIGURAZIONE
* [ ] leggere parametri da file di container.ini * [ ] leggere parametri da file di container.ini
* [ ] rivedere gestione errori * [ ] rivedere gestione errori
*
# MASSI # MASSI
* API REST social * API REST social
@ -24,5 +28,3 @@
* [ ] implementare client (rif. gcat-client) * [ ] implementare client (rif. gcat-client)
* [ ] verificare controllo diritti * [ ] verificare controllo diritti
* [ ] aggiungere accounting
* [ ] mettere su openstack