Moving tests to d4science-iam-client

This commit is contained in:
Luca Frosini 2024-11-12 17:02:15 +01:00
parent 642a98f76f
commit fec6dadcd2
2 changed files with 54 additions and 37 deletions

View File

@ -112,5 +112,11 @@
<version>[2.1.0-SNAPSHOT, 3.0.0-SNAPSHOT)</version> <version>[2.1.0-SNAPSHOT, 3.0.0-SNAPSHOT)</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.gcube.common</groupId>
<artifactId>d4science-iam-client</artifactId>
<version>[1.0.0-SNAPSHOT, 2.0.0)</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -12,9 +12,9 @@ import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
import org.gcube.common.authorization.utils.secret.JWTSecret; import org.gcube.common.authorization.utils.secret.JWTSecret;
import org.gcube.common.authorization.utils.secret.Secret; import org.gcube.common.authorization.utils.secret.Secret;
import org.gcube.common.authorization.utils.secret.SecretUtility; import org.gcube.common.authorization.utils.secret.SecretUtility;
import org.gcube.common.keycloak.KeycloakClientFactory; import org.gcube.common.iam.D4ScienceIAMClient;
import org.gcube.common.keycloak.KeycloakClientHelper; import org.gcube.common.iam.D4ScienceIAMClientAuthn;
import org.gcube.common.keycloak.model.TokenResponse; import org.gcube.informationsystem.model.reference.properties.Metadata;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -29,9 +29,15 @@ public class ContextTest {
protected static final String CONFIG_INI_FILENAME = "config.ini"; protected static final String CONFIG_INI_FILENAME = "config.ini";
public static final String ROOT_DEV; public static final String PARENT_DEFAULT_TEST_SCOPE;
public static final String ROOT_PREPROD; public static final String DEFAULT_TEST_SCOPE;
public static final String ROOT_PROD; public static final String ALTERNATIVE_TEST_SCOPE;
public static final String GCUBE;
public static final String DEVNEXT;
public static final String NEXTNEXT;
public static final String DEVSEC;
public static final String DEVVRE;
protected static final Properties properties; protected static final Properties properties;
@ -40,24 +46,31 @@ public class ContextTest {
public static final String PASSWORD_PROPERTY_KEY = "password"; public static final String PASSWORD_PROPERTY_KEY = "password";
public static final String CLIENT_ID_PROPERTY_KEY = "clientId"; public static final String CLIENT_ID_PROPERTY_KEY = "clientId";
public static final String RESOURCE_REGISTRY_URL_PROPERTY = "RESOURCE_REGISTRY_URL";
static { static {
ROOT_DEV = "/gcube"; GCUBE = "/gcube";
ROOT_PREPROD = "/pred4s"; DEVNEXT = GCUBE + "/devNext";
ROOT_PROD = "/d4science.research-infrastructures.eu"; NEXTNEXT = DEVNEXT + "/NextNext";
DEVSEC = GCUBE + "/devsec";
DEVVRE = DEVSEC + "/devVRE";
PARENT_DEFAULT_TEST_SCOPE = GCUBE;
DEFAULT_TEST_SCOPE = DEVNEXT;
ALTERNATIVE_TEST_SCOPE = NEXTNEXT;
properties = new Properties(); properties = new Properties();
InputStream input = ContextTest.class.getClassLoader().getResourceAsStream(CONFIG_INI_FILENAME); InputStream input = ContextTest.class.getClassLoader().getResourceAsStream(CONFIG_INI_FILENAME);
try { try {
// load the properties file // load the properties file
properties.load(input); properties.load(input);
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
public enum Type{ private enum Type{
USER, CLIENT_ID USER, CLIENT_ID
}; };
@ -76,20 +89,22 @@ public class ContextTest {
} }
private static TokenResponse getJWTAccessToken(String context) throws Exception { protected static String getJWTAccessToken(String context) throws Exception {
Type type = Type.valueOf(properties.get(TYPE_PROPERTY_KEY).toString()); Type type = Type.valueOf(properties.get(TYPE_PROPERTY_KEY).toString());
TokenResponse tr = null; String accessToken = null;
int index = context.indexOf('/', 1); int index = context.indexOf('/', 1);
String root = context.substring(0, index == -1 ? context.length() : index); String root = context.substring(0, index == -1 ? context.length() : index);
D4ScienceIAMClient iamClient = D4ScienceIAMClient.newInstance(root);
D4ScienceIAMClientAuthn d4ScienceIAMClientAuthn = null;
switch (type) { switch (type) {
case CLIENT_ID: case CLIENT_ID:
String clientId = properties.getProperty(CLIENT_ID_PROPERTY_KEY); String clientId = properties.getProperty(CLIENT_ID_PROPERTY_KEY);
String clientSecret = properties.getProperty(root); String clientSecret = properties.getProperty(root);
tr = KeycloakClientFactory.newInstance().queryUMAToken(context, clientId, clientSecret, context, null); d4ScienceIAMClientAuthn = iamClient.authenticate(clientId, clientSecret, context);
break; break;
case USER: case USER:
@ -97,34 +112,20 @@ public class ContextTest {
String username = properties.getProperty(USERNAME_PROPERTY_KEY); String username = properties.getProperty(USERNAME_PROPERTY_KEY);
String password = properties.getProperty(PASSWORD_PROPERTY_KEY); String password = properties.getProperty(PASSWORD_PROPERTY_KEY);
switch (root) { d4ScienceIAMClientAuthn = iamClient.authenticateUser(username, password, context);
case "/gcube":
default:
clientId = "next.d4science.org";
break;
case "/pred4s":
clientId = "pre.d4science.org";
break;
case "/d4science.research-infrastructures.eu":
clientId = "services.d4science.org";
break;
}
clientSecret = null;
tr = KeycloakClientHelper.getTokenForUser(context, username, password);
break; break;
} }
accessToken = d4ScienceIAMClientAuthn.getAccessTokenString();
return tr; logger.trace("Generated Access Token is {}", accessToken);
return accessToken;
} }
public static Secret getSecretByContextName(String context) throws Exception { public static Secret getSecretByContextName(String context) throws Exception {
TokenResponse tr = getJWTAccessToken(context); String accessToken = getJWTAccessToken(context);
Secret secret = new JWTSecret(tr.getAccessToken()); Secret secret = new JWTSecret(accessToken);
return secret; return secret;
} }
@ -138,9 +139,19 @@ public class ContextTest {
return secret; return secret;
} }
public static String getUser() {
String user = Metadata.UNKNOWN_USER;
try {
user = SecretManagerProvider.instance.get().getUser().getUsername();
} catch(Exception e) {
logger.error("Unable to retrieve user. {} will be used", user);
}
return user;
}
@BeforeClass @BeforeClass
public static void beforeClass() throws Exception { public static void beforeClass() throws Exception {
setContextByName(ROOT_DEV); setContextByName(DEFAULT_TEST_SCOPE);
} }
@AfterClass @AfterClass