Switched code to authorization-utils in place of ad-hoc code

This commit is contained in:
Luca Frosini 2021-12-07 14:31:30 +01:00
parent fb35c970b7
commit d00e899ddb
5 changed files with 30 additions and 155 deletions

View File

@ -55,7 +55,8 @@
</dependency>
<dependency>
<groupId>org.gcube.common</groupId>
<artifactId>gxHTTP</artifactId>
<artifactId>authorization-utils</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<!-- Test libraries -->
<dependency>

View File

@ -1,54 +0,0 @@
package org.gcube.storagehub;
import javax.ws.rs.InternalServerErrorException;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class ApplicationMode {
private static final Logger logger = LoggerFactory.getLogger(ApplicationMode.class);
private final String applicationToken;
private final String originalToken;
public ApplicationMode(String applicationToken) {
this.applicationToken = applicationToken;
String currentToken = SecurityTokenProvider.instance.get();
if(applicationToken.compareTo(currentToken)!=0) {
this.originalToken = currentToken;
}else {
logger.warn("You are already in application Mode. Operation on this instance will not have any effect.");
this.originalToken = null;
}
}
public synchronized void start() {
if(originalToken!=null) {
try {
ContextUtility.setContext(applicationToken);
}catch (Exception e) {
throw new InternalServerErrorException(e);
}
}else {
logger.warn("You are already in application Mode. start() does not provide any effect.");
}
}
public synchronized void end() {
if(originalToken!=null) {
try {
ContextUtility.setContext(originalToken);
}catch (Exception e) {
throw new InternalServerErrorException(e);
}
}else {
logger.warn("You are already in application Mode. end() does not provide any effect.");
}
}
}

View File

@ -1,83 +0,0 @@
package org.gcube.storagehub;
import javax.ws.rs.InternalServerErrorException;
import org.gcube.common.authorization.client.Constants;
import org.gcube.common.authorization.client.exceptions.ObjectNotFound;
import org.gcube.common.authorization.library.AuthorizationEntry;
import org.gcube.common.authorization.library.ClientType;
import org.gcube.common.authorization.library.provider.AuthorizationProvider;
import org.gcube.common.authorization.library.provider.ClientInfo;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class ContextUtility {
private static Logger logger = LoggerFactory.getLogger(ContextUtility.class);
public static void setContext(String token) throws ObjectNotFound, Exception{
SecurityTokenProvider.instance.set(token);
AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token);
ClientInfo clientInfo = authorizationEntry.getClientInfo();
logger.debug("User : {} - Type : {}", clientInfo.getId(), clientInfo.getType().name());
String qualifier = authorizationEntry.getQualifier();
Caller caller = new Caller(clientInfo, qualifier);
AuthorizationProvider.instance.set(caller);
ScopeProvider.instance.set(getCurrentContext());
}
public static String getCurrentContext() {
try {
String token = SecurityTokenProvider.instance.get();
return Constants.authorizationService().get(token).getContext();
}catch (Exception e) {
String context = ScopeProvider.instance.get();
if(context!=null) {
return context;
}
throw new InternalServerErrorException(e);
}
}
public static ClientInfo getClientInfo() {
try {
Caller caller = AuthorizationProvider.instance.get();
if(caller!=null){
return caller.getClient();
}else{
String token = SecurityTokenProvider.instance.get();
AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token);
return authorizationEntry.getClientInfo();
}
}catch (Exception e) {
throw new InternalServerErrorException(e);
}
}
private static final String GET_USERNAME_ERROR = "Unable to retrieve user";
public static String getUsername() {
try {
return getClientInfo().getId();
} catch (Exception e) {
logger.error(GET_USERNAME_ERROR);
throw new InternalServerErrorException(GET_USERNAME_ERROR, e);
}
}
public static boolean isApplication() {
try {
ClientInfo clientInfo = getClientInfo();
return clientInfo.getType() == ClientType.EXTERNALSERVICE ;
}catch (Exception e) {
throw new InternalServerErrorException(e);
}
}
}

View File

@ -5,6 +5,7 @@ import java.io.StringWriter;
import java.net.URL;
import java.util.List;
import org.gcube.common.authorization.utils.manager.SecretManager;
import org.gcube.common.scope.impl.ScopeBean;
import org.gcube.common.scope.impl.ScopeBean.Type;
import org.gcube.common.storagehub.client.dsl.ContainerType;
@ -105,7 +106,7 @@ public class StorageHubManagement {
protected FolderContainer getContextFolder() throws Exception {
FolderContainer destinationFolder = getWorkspaceRoot();
String currentContext = ContextUtility.getCurrentContext();
String currentContext = SecretManager.instance.get().getContext();
ScopeBean scopeBean = new ScopeBean(currentContext);
switch(scopeBean.type()) {
case INFRASTRUCTURE:
@ -127,10 +128,10 @@ public class StorageHubManagement {
public FolderContainer getApplicationFolder() throws Exception {
FolderContainer destinationFolder = getContextFolder();
String currentContext = ContextUtility.getCurrentContext();
String currentContext = SecretManager.instance.get().getContext();
ScopeBean scopeBean = new ScopeBean(currentContext);
if(scopeBean.is(Type.VRE)) {
String username = ContextUtility.getUsername();
String username = SecretManager.instance.get().getUser().getUsername();
destinationFolder = getOrCreateFolder(destinationFolder, username, "Folder Created for user/application", true);
}
return destinationFolder;

View File

@ -3,6 +3,8 @@ package org.gcube.storagehub;
import java.util.List;
import java.util.Map;
import org.gcube.common.authorization.utils.manager.SecretManager;
import org.gcube.common.authorization.utils.secret.GCubeSecret;
import org.gcube.common.storagehub.client.dsl.ContainerType;
import org.gcube.common.storagehub.client.dsl.FileContainer;
import org.gcube.common.storagehub.client.dsl.FolderContainer;
@ -23,18 +25,22 @@ public class StorageHubManagementTest extends ContextTest {
@Test
public void myTest() throws Exception {
ApplicationMode applicationMode = new ApplicationMode(ContextTest.properties.getProperty(DEFAULT_TEST_SCOPE_NAME));
applicationMode.start();
SecretManager secretManager = SecretManager.instance.get();
GCubeSecret gCubeSecret = new GCubeSecret(ContextTest.properties.getProperty(DEFAULT_TEST_SCOPE_NAME));
secretManager.startSession(gCubeSecret);
StorageHubManagement storageHubManagement = new StorageHubManagement();
FolderContainer contextFolder = storageHubManagement.getContextFolder();
logger.debug("Context Folder ID : {} - Name : {}", contextFolder.getId(), contextFolder.get().getName());
secretManager.endSession();
}
@Test
public void test() throws Exception {
ApplicationMode applicationMode = new ApplicationMode(ContextTest.properties.getProperty(DEFAULT_TEST_SCOPE_NAME));
applicationMode.start();
SecretManager secretManager = SecretManager.instance.get();
GCubeSecret gCubeSecret = new GCubeSecret(ContextTest.properties.getProperty(DEFAULT_TEST_SCOPE_NAME));
secretManager.startSession(gCubeSecret);
StorageHubManagement storageHubManagement = new StorageHubManagement();
@SuppressWarnings("unused")
OpenResolver openResolver = storageHubManagement.storageHubClient.open("");
// openResolver.asItem().delete();
/*
@ -43,14 +49,16 @@ public class StorageHubManagementTest extends ContextTest {
openResolver = storageHubManagement.storageHubClient.open("bd44d81e-0e2f-4527-b634-2e26e8908f36");
openResolver.asItem().delete();
*/
applicationMode.end();
secretManager.endSession();
}
@Test
public void listFolders() throws Exception {
ApplicationMode applicationMode = new ApplicationMode(ContextTest.properties.getProperty(DEFAULT_TEST_SCOPE_NAME));
applicationMode.start();
SecretManager secretManager = SecretManager.instance.get();
GCubeSecret gCubeSecret = new GCubeSecret(ContextTest.properties.getProperty(DEFAULT_TEST_SCOPE_NAME));
secretManager.startSession(gCubeSecret);
StorageHubManagement storageHubManagement = new StorageHubManagement();
@SuppressWarnings("unused")
FolderContainer root = storageHubManagement.getWorkspaceRoot();
FolderContainer contextFolder = storageHubManagement.getContextFolder();
ListResolverTyped listResolverTyped = contextFolder.list();
@ -67,13 +75,14 @@ public class StorageHubManagementTest extends ContextTest {
// storageHubManagement.tree(root);
// storageHubManagement.tree(contextFolder);
// storageHubManagement.tree(dstFolder);
applicationMode.end();
secretManager.endSession();
}
@Test
public void getFileInfo() throws Exception {
ApplicationMode applicationMode = new ApplicationMode(ContextTest.properties.getProperty(DEFAULT_TEST_SCOPE_NAME));
applicationMode.start();
SecretManager secretManager = SecretManager.instance.get();
GCubeSecret gCubeSecret = new GCubeSecret(ContextTest.properties.getProperty(DEFAULT_TEST_SCOPE_NAME));
secretManager.startSession(gCubeSecret);
StorageHubManagement storageHubManagement = new StorageHubManagement();
String id = "3daf465b-b84e-4d1c-9786-a388a267382c";
OpenResolver openResolver = storageHubManagement.storageHubClient.open(id);
@ -96,13 +105,14 @@ public class StorageHubManagementTest extends ContextTest {
for(Version version : versions){
logger.debug("Version {} {}", version.getId(), version.getName());
}
applicationMode.end();
secretManager.endSession();
}
@Test
public void getFileInfoViaDirectoryListing() throws Exception {
ApplicationMode applicationMode = new ApplicationMode(ContextTest.properties.getProperty(DEFAULT_TEST_SCOPE_NAME));
applicationMode.start();
SecretManager secretManager = SecretManager.instance.get();
GCubeSecret gCubeSecret = new GCubeSecret(ContextTest.properties.getProperty(DEFAULT_TEST_SCOPE_NAME));
secretManager.startSession(gCubeSecret);
StorageHubManagement storageHubManagement = new StorageHubManagement();
String id = "22bd9034-1da0-45ac-868f-91d1e5438344";
OpenResolver openResolver = storageHubManagement.storageHubClient.open(id);
@ -133,8 +143,8 @@ public class StorageHubManagementTest extends ContextTest {
for(Version version : versions){
logger.debug("Version {} {}", version.getId(), version.getName());
}
applicationMode.end();
}
secretManager.endSession();
}
}