Added missing API and test

This commit is contained in:
Luca Frosini 2022-03-08 10:16:57 +01:00
parent e714b058df
commit cf1e6f0602
6 changed files with 148 additions and 1 deletions

13
pom.xml
View File

@ -64,6 +64,19 @@
<artifactId>keycloak-client</artifactId>
<version>[1.0.0,2.0.0-SNAPSHOT)</version>
</dependency>
<!-- Test Dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -34,7 +34,7 @@ public class SecretHolder {
}
}
public void addAuthorizationSecrets(Collection<Secret> secrets) {
public void addSecrets(Collection<Secret> secrets) {
for(Secret secret : secrets){
addSecret(secret);
}

View File

@ -1,9 +1,12 @@
package org.gcube.common.authorization.utils.manager;
import java.util.Collection;
import java.util.SortedSet;
import java.util.TreeSet;
import org.gcube.common.authorization.utils.provider.SecretProvider;
import org.gcube.common.authorization.utils.secret.Secret;
import org.gcube.common.authorization.utils.secret.SecretUtility;
import org.gcube.common.authorization.utils.user.User;
/**
@ -91,4 +94,19 @@ public class SecretManager {
return currentSecretHolder.getUser();
}
/**
* @return a copy of the current secret holder
* to avoid modification to the original
*/
public synchronized SecretHolder getCurrentSecretHolder() {
SecretHolder secretHolder = new SecretHolder();
SortedSet<Secret> secrets = new TreeSet<>();
SortedSet<Secret> originalSecrets = currentSecretHolder.getSecrets();
for(Secret s : originalSecrets) {
Secret secret = SecretUtility.getSecretByTokenString(s.getToken());
secrets.add(secret);
}
secretHolder.addSecrets(secrets);
return secretHolder;
}
}

View File

@ -0,0 +1,86 @@
/**
*
*/
package org.gcube.common.authorization.utils;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.gcube.common.authorization.utils.manager.SecretManager;
import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
import org.gcube.common.authorization.utils.secret.Secret;
import org.gcube.common.authorization.utils.secret.SecretUtility;
import org.junit.AfterClass;
import org.junit.BeforeClass;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class ContextTest {
protected static Properties properties;
protected static final String PROPERTIES_FILENAME = "token.properties";
public static final String ROOT;
public static final String VO;
public static final String VRE;
static {
properties = new Properties();
InputStream input = ContextTest.class.getClassLoader().getResourceAsStream(PROPERTIES_FILENAME);
try {
// load the properties file
properties.load(input);
} catch(IOException e) {
throw new RuntimeException(e);
}
ROOT = "/gcube";
// VO = ROOT + "/devsec";
// VRE = VO + "/devVRE";
VO = ROOT + "/devNext";
VRE = VO + "/NextNext";
}
public static void set(Secret secret) throws Exception {
SecretManagerProvider.instance.reset();
SecretManager secretManager = new SecretManager();
SecretManagerProvider.instance.set(secretManager);
secretManager.addSecret(secret);
secretManager.set();
}
public static void setContext(String token) throws Exception {
Secret secret = getSecret(token);
set(secret);
}
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);
return getSecret(token);
}
@BeforeClass
public static void beforeClass() throws Exception {
setContextByName(VRE);
}
@AfterClass
public static void afterClass() throws Exception {
SecretManagerProvider.instance.reset();
}
}

View File

@ -0,0 +1,28 @@
package org.gcube.common.authorization.utils.manager;
import java.util.Set;
import org.gcube.common.authorization.utils.ContextTest;
import org.gcube.common.authorization.utils.secret.Secret;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class SecretManagerTest extends ContextTest {
private static final Logger logger = LoggerFactory.getLogger(SecretManagerTest.class);
@Test
public void test() throws Exception {
SecretManager secretManager = SecretManagerProvider.instance.get();
SecretHolder secretHolder = secretManager.getCurrentSecretHolder();
Set<Secret> secrets = secretHolder.getSecrets();
for(Secret s : secrets) {
logger.debug("{}: token={}", s.getClass().getSimpleName(), s.getToken());
}
}
}

2
src/test/resources/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/logback-test.xml
/token.properties