Migrating tests to new SecretManagerProvider

This commit is contained in:
Luca Frosini 2022-03-07 14:14:42 +01:00
parent a5eda58293
commit 3359295adf
5 changed files with 43 additions and 67 deletions

View File

@ -50,6 +50,11 @@
<artifactId>information-system-model</artifactId> <artifactId>information-system-model</artifactId>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>org.gcube.common</groupId>
<artifactId>authorization-utils</artifactId>
<scope>provided</scope>
</dependency>
<dependency> <dependency>
<groupId>com.orientechnologies</groupId> <groupId>com.orientechnologies</groupId>
<!-- <!--

View File

@ -7,14 +7,11 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.Properties; import java.util.Properties;
import org.gcube.common.authorization.client.Constants; import org.gcube.common.authorization.utils.manager.SecretManager;
import org.gcube.common.authorization.client.exceptions.ObjectNotFound; import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
import org.gcube.common.authorization.library.AuthorizationEntry; import org.gcube.common.authorization.utils.secret.Secret;
import org.gcube.common.authorization.library.provider.AuthorizationProvider; import org.gcube.common.authorization.utils.secret.SecretUtility;
import org.gcube.common.authorization.library.provider.ClientInfo; import org.gcube.informationsystem.model.reference.properties.Header;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.authorization.library.utils.Caller;
import org.gcube.common.scope.api.ScopeProvider;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -34,8 +31,6 @@ public class ContextTest {
public static final String DEFAULT_TEST_SCOPE; public static final String DEFAULT_TEST_SCOPE;
public static final String ALTERNATIVE_TEST_SCOPE; public static final String ALTERNATIVE_TEST_SCOPE;
public static final String DEFAULT_TEST_SCOPE_ANOTHER_USER;
public static final String GCUBE; public static final String GCUBE;
public static final String DEVNEXT; public static final String DEVNEXT;
public static final String NEXTNEXT; public static final String NEXTNEXT;
@ -67,40 +62,43 @@ public class ContextTest {
DEFAULT_TEST_SCOPE = PARENT_DEFAULT_TEST_SCOPE + "/devNext"; DEFAULT_TEST_SCOPE = PARENT_DEFAULT_TEST_SCOPE + "/devNext";
ALTERNATIVE_TEST_SCOPE = DEFAULT_TEST_SCOPE + "/NextNext"; ALTERNATIVE_TEST_SCOPE = DEFAULT_TEST_SCOPE + "/NextNext";
DEFAULT_TEST_SCOPE_ANOTHER_USER = "lucio.lelii_" + DEFAULT_TEST_SCOPE;
try {
setContextByName(DEFAULT_TEST_SCOPE);
} catch(Exception e) {
throw new RuntimeException(e);
}
} }
public static String getCurrentContextFullName() throws ObjectNotFound, Exception { public static void set(Secret secret) throws Exception {
return getContextFullNameByToken(SecurityTokenProvider.instance.get()); SecretManagerProvider.instance.reset();
SecretManager secretManager = SecretManagerProvider.instance.get();
secretManager.addSecret(secret);
secretManager.set();
} }
public static String getContextFullNameByToken(String token) throws ObjectNotFound, Exception { public static void setContext(String token) throws Exception {
AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token); Secret secret = getSecret(token);
String context = authorizationEntry.getContext(); set(secret);
logger.info("Context of token {} is {}", token, context);
return context;
} }
public static void setContextByName(String fullContextName) throws ObjectNotFound, Exception { public static void setContextByName(String fullContextName) throws Exception {
Secret secret = getSecretByContextName(fullContextName);
set(secret);
}
private static Secret getSecret(String token) throws Exception {
Secret secret = SecretUtility.getSecretByTokenString(token);
return secret;
}
private static Secret getSecretByContextName(String fullContextName) throws Exception {
String token = ContextTest.properties.getProperty(fullContextName); String token = ContextTest.properties.getProperty(fullContextName);
setContext(token); return getSecret(token);
} }
private static void setContext(String token) throws ObjectNotFound, Exception { public static String getUser() {
SecurityTokenProvider.instance.set(token); String user = Header.UNKNOWN_USER;
AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token); try {
ClientInfo clientInfo = authorizationEntry.getClientInfo(); user = SecretManagerProvider.instance.get().getUser().getUsername();
logger.debug("User : {} - Type : {}", clientInfo.getId(), clientInfo.getType().name()); } catch(Exception e) {
String qualifier = authorizationEntry.getQualifier(); logger.error("Unable to retrieve user. {} will be used", user);
Caller caller = new Caller(clientInfo, qualifier); }
AuthorizationProvider.instance.set(caller); return user;
ScopeProvider.instance.set(getContextFullNameByToken(token));
} }
@BeforeClass @BeforeClass
@ -110,8 +108,7 @@ public class ContextTest {
@AfterClass @AfterClass
public static void afterClass() throws Exception { public static void afterClass() throws Exception {
SecurityTokenProvider.instance.reset(); SecretManagerProvider.instance.reset();
ScopeProvider.instance.reset();
} }
} }

View File

@ -12,7 +12,6 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
import org.gcube.common.authorization.library.provider.AuthorizationProvider;
import org.gcube.informationsystem.base.reference.Element; import org.gcube.informationsystem.base.reference.Element;
import org.gcube.informationsystem.base.reference.IdentifiableElement; import org.gcube.informationsystem.base.reference.IdentifiableElement;
import org.gcube.informationsystem.model.impl.properties.EncryptedImpl; import org.gcube.informationsystem.model.impl.properties.EncryptedImpl;
@ -525,7 +524,7 @@ public class ERManagementTest extends ContextTest {
Assert.assertTrue(((String) updatedCpuFacet.getAdditionalProperty(additionPropertyKey)) Assert.assertTrue(((String) updatedCpuFacet.getAdditionalProperty(additionPropertyKey))
.compareTo((String) readCpuFacet.getAdditionalProperty(additionPropertyKey)) == 0); .compareTo((String) readCpuFacet.getAdditionalProperty(additionPropertyKey)) == 0);
Assert.assertTrue(uuid.compareTo(updatedCpuFacet.getHeader().getUUID()) == 0); Assert.assertTrue(uuid.compareTo(updatedCpuFacet.getHeader().getUUID()) == 0);
String user = AuthorizationProvider.instance.get().getClient().getId(); String user = ContextTest.getUser();
Assert.assertTrue(updatedCpuFacet.getHeader().getLastUpdateBy().compareTo(user) == 0); Assert.assertTrue(updatedCpuFacet.getHeader().getLastUpdateBy().compareTo(user) == 0);
facetManagement = new FacetManagement(); facetManagement = new FacetManagement();

View File

@ -9,6 +9,7 @@ import java.util.TreeSet;
import java.util.UUID; import java.util.UUID;
import org.gcube.com.fasterxml.jackson.databind.JsonNode; import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
import org.gcube.informationsystem.base.reference.Element; import org.gcube.informationsystem.base.reference.Element;
import org.gcube.informationsystem.contexts.reference.entities.Context; import org.gcube.informationsystem.contexts.reference.entities.Context;
import org.gcube.informationsystem.model.reference.entities.Facet; import org.gcube.informationsystem.model.reference.entities.Facet;
@ -229,7 +230,7 @@ public class MultiContextTest extends ERManagementTest {
// checkAffectedInstances(expectedInstances, affectedInstances); // checkAffectedInstances(expectedInstances, affectedInstances);
// //
if(!dryRun) { if(!dryRun) {
String currentContext = ContextTest.getCurrentContextFullName(); String currentContext = SecretManagerProvider.instance.get().getContext();
ContextTest.setContextByName(targetContextFullName); ContextTest.setContextByName(targetContextFullName);
resourceManagement = ERManagementTest.getResourceManagement(r); resourceManagement = ERManagementTest.getResourceManagement(r);
String json = resourceManagement.read(); String json = resourceManagement.read();

View File

@ -10,10 +10,6 @@ import org.gcube.com.fasterxml.jackson.databind.JavaType;
import org.gcube.com.fasterxml.jackson.databind.JsonNode; import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper; import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode; import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode;
import org.gcube.common.authorization.client.Constants;
import org.gcube.common.authorization.library.AuthorizationEntry;
import org.gcube.common.authorization.library.provider.ClientInfo;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.informationsystem.base.reference.IdentifiableElement; import org.gcube.informationsystem.base.reference.IdentifiableElement;
import org.gcube.informationsystem.model.reference.properties.Header; import org.gcube.informationsystem.model.reference.properties.Header;
import org.gcube.informationsystem.queries.templates.impl.entities.QueryTemplateImpl; import org.gcube.informationsystem.queries.templates.impl.entities.QueryTemplateImpl;
@ -41,28 +37,6 @@ public class QueryTemplateManagementTest extends ContextTest {
public static final String NAME_VARIABLE_NAME = "$name"; public static final String NAME_VARIABLE_NAME = "$name";
public static final String GROUP_VARIABLE_NAME = "$group"; public static final String GROUP_VARIABLE_NAME = "$group";
public static String getUser() {
String user = Header.UNKNOWN_USER;
try {
String token = SecurityTokenProvider.instance.get();
if(token != null) {
AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token);
if(authorizationEntry != null) {
ClientInfo clientInfo = authorizationEntry.getClientInfo();
String clientId = clientInfo.getId();
if(clientId != null && clientId.compareTo("") != 0) {
user = clientId;
} else {
throw new Exception("Username null or empty");
}
}
}
} catch(Exception e) {
logger.error("Unable to retrieve user. {} will be used", user);
}
return user;
}
public static void checkHeader(IdentifiableElement previous, IdentifiableElement got) { public static void checkHeader(IdentifiableElement previous, IdentifiableElement got) {
Header gotHeader = got.getHeader(); Header gotHeader = got.getHeader();
Header previousHeader = previous.getHeader(); Header previousHeader = previous.getHeader();
@ -70,7 +44,7 @@ public class QueryTemplateManagementTest extends ContextTest {
Assert.assertTrue(gotHeader != null); Assert.assertTrue(gotHeader != null);
Assert.assertTrue(gotHeader.getUUID() != null); Assert.assertTrue(gotHeader.getUUID() != null);
String user = getUser(); String user = ContextTest.getUser();
Assert.assertTrue(gotHeader.getLastUpdateBy().compareTo(user) == 0); Assert.assertTrue(gotHeader.getLastUpdateBy().compareTo(user) == 0);
if(previousHeader != null) { if(previousHeader != null) {