Merge pull request '#23805' (#1) from #23805 into master

Reviewed-on: #1
This commit is contained in:
Fabio Sinibaldi 2022-09-12 10:59:11 +02:00
commit f5c95ace16
7 changed files with 74 additions and 54 deletions

View File

@ -2,7 +2,9 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
# Changelog for accounting-summary-access # Changelog for accounting-summary-access
## [v1.0.3-SNAPSHOT] - 2020-09-03 ## [v1.0.3] - 2020-09-03
Integrated auth-utils
### Fixes ### Fixes

10
pom.xml
View File

@ -8,7 +8,7 @@
</parent> </parent>
<groupId>org.gcube.accounting</groupId> <groupId>org.gcube.accounting</groupId>
<artifactId>accounting-summary-access</artifactId> <artifactId>accounting-summary-access</artifactId>
<version>1.0.3-SNAPSHOT</version> <version>1.0.3</version>
<name>Accounting Summary Access</name> <name>Accounting Summary Access</name>
<description>Java library for accessing accounting summary data</description> <description>Java library for accessing accounting summary data</description>
@ -17,7 +17,7 @@
<dependency> <dependency>
<groupId>org.gcube.distribution</groupId> <groupId>org.gcube.distribution</groupId>
<artifactId>gcube-bom</artifactId> <artifactId>gcube-bom</artifactId>
<version>2.0.0-SNAPSHOT</version> <version>2.0.2</version>
<type>pom</type> <type>pom</type>
<scope>import</scope> <scope>import</scope>
</dependency> </dependency>
@ -56,6 +56,12 @@
</dependency> </dependency>
<dependency>
<groupId>org.gcube.common</groupId>
<artifactId>authorization-utils</artifactId>
<version>[2.0.0, 3.0.0-SNAPSHOT)</version>
</dependency>
<!-- IS --> <!-- IS -->
<dependency> <dependency>
<groupId>org.gcube.resources.discovery</groupId> <groupId>org.gcube.resources.discovery</groupId>

View File

@ -10,6 +10,9 @@ import java.util.function.Function;
import org.gcube.common.authorization.library.AuthorizationEntry; import org.gcube.common.authorization.library.AuthorizationEntry;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider; import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.authorization.utils.manager.SecretManager;
import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
import org.gcube.common.authorization.utils.user.User;
import org.gcube.common.encryption.StringEncrypter; import org.gcube.common.encryption.StringEncrypter;
import org.gcube.common.resources.gcore.ServiceEndpoint; import org.gcube.common.resources.gcore.ServiceEndpoint;
import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint; import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint;
@ -43,16 +46,22 @@ public class BasicConnectionManager implements ConnectionManager {
private synchronized DataBaseDescriptor getDatabase() { private synchronized DataBaseDescriptor getDatabase() {
try { try {
final String currentToken=SecurityTokenProvider.instance.get(); log.info("Looking for database, checking caller context..");
if(currentToken==null) throw new Exception("Unauthorized request. No gcube token found."); SecretManager cm = SecretManagerProvider.instance.get();
AuthorizationEntry entry = authorizationService().get(currentToken); final String context = cm.getContext();
final String context=entry.getContext(); User user = cm.getUser();
log.info("Identified caller {} in context {}",user.getUsername(),context);
return cache.computeIfAbsent(context, new Function<String, DataBaseDescriptor>(){ return cache.computeIfAbsent(context, new Function<String, DataBaseDescriptor>(){
@Override @Override
public DataBaseDescriptor apply(String t) { public DataBaseDescriptor apply(String t) {
try{ try{
log.debug("Querying for database in context {} ",context); log.info("Database for {} not found in cache. Querying IS .. ",context);
SecurityTokenProvider.instance.set(currentToken); SecretManager cm = SecretManagerProvider.instance.get();
final String context = cm.getContext();
User user = cm.getUser();
log.debug("CACHE FUNCTION : Identified caller {} in context {}",user.getUsername(),context);
SimpleQuery query = ICFactory.queryFor(ServiceEndpoint.class); SimpleQuery query = ICFactory.queryFor(ServiceEndpoint.class);
query.addCondition("$resource/Profile/Category/text() eq 'Database'") query.addCondition("$resource/Profile/Category/text() eq 'Database'")
.addCondition("$resource/Profile/Name/text() eq 'AccountingDashboard'"); .addCondition("$resource/Profile/Name/text() eq 'AccountingDashboard'");

View File

@ -1,20 +1,17 @@
package org.gcube.accounting.accounting.summary.access.impl; package org.gcube.accounting.accounting.summary.access.impl;
import static org.gcube.common.authorization.client.Constants.authorizationService;
import org.gcube.accounting.accounting.summary.access.model.ScopeDescriptor; import org.gcube.accounting.accounting.summary.access.model.ScopeDescriptor;
import org.gcube.common.authorization.library.AuthorizationEntry; import org.gcube.common.authorization.utils.manager.SecretManager;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider; import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
public class BasicContextTreeProvider implements ContextTreeProvider { public class BasicContextTreeProvider implements ContextTreeProvider {
@Override @Override
public ScopeDescriptor getTree(Object unuseful) throws Exception { public ScopeDescriptor getTree(Object unuseful) throws Exception {
final String currentToken=SecurityTokenProvider.instance.get(); SecretManager cm = SecretManagerProvider.instance.get();
if(currentToken==null) throw new Exception("Unauthorized request. No gcube token found."); final String context = cm.getContext();
AuthorizationEntry entry = authorizationService().get(currentToken);
final String context=entry.getContext();
return new ScopeDescriptor(context.substring(context.lastIndexOf("/")+1, context.length()),context); return new ScopeDescriptor(context.substring(context.lastIndexOf("/")+1, context.length()),context);

View File

@ -56,7 +56,7 @@ public class ReportElement implements Serializable {
@Override @Override
public String toString() { public String toString() {
StringBuilder builder=new StringBuilder("REPORT "+label); StringBuilder builder=new StringBuilder("REPORT "+"{cat : "+category+"}"+label);
builder.append(" Y : "+yAxis+" X : "+xAxis+"\n"); builder.append(" Y : "+yAxis+" X : "+xAxis+"\n");
for(Series s:serieses){ for(Series s:serieses){
builder.append(s.toString()+"\n"); builder.append(s.toString()+"\n");

View File

@ -17,9 +17,11 @@ public class SimpleTest {
public static void main(String[] args) throws ParameterException, Exception { public static void main(String[] args) throws ParameterException, Exception {
TokenSetter.set("/gcube/devNext"); TokenSetter.set("/d4science.research-infrastructures.eu/D4OS/GeoNA-Prototype");
// AccountingDao dao=AccountingDao.get(); // TokenSetter.set("/gcube/devsec/devVRE");
AccountingDao dao=AccountingDao.get(new DummyContextTreeProvider()); // TokenSetter.set("/pred4s/preprod/preVRE");
AccountingDao dao=AccountingDao.get();
// AccountingDao dao=AccountingDao.get(new DummyContextTreeProvider());
ScopeDescriptor desc=dao.getTree(null); ScopeDescriptor desc=dao.getTree(null);
@ -49,6 +51,7 @@ public class SimpleTest {
System.out.println("**********************************************************************************************************"); System.out.println("**********************************************************************************************************");
System.out.println(desc); System.out.println(desc);
System.out.println(dao.getReportByScope(desc, from, to, MeasureResolution.MONTHLY)); System.out.println(dao.getReportByScope(desc, from, to, MeasureResolution.MONTHLY));
// if(desc.hasChildren()) { // if(desc.hasChildren()) {
// for(ScopeDescriptor child:desc.getChildren()) // for(ScopeDescriptor child:desc.getChildren())
// scan(child,from,to,dao); // scan(child,from,to,dao);

View File

@ -2,54 +2,57 @@ package org.gcube.data.access.accounting.summary.access.test;
import static org.gcube.common.authorization.client.Constants.authorizationService; import static org.gcube.common.authorization.client.Constants.authorizationService;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Properties; import java.util.Properties;
import org.gcube.common.authorization.library.AuthorizationEntry; import org.gcube.common.authorization.library.AuthorizationEntry;
import org.gcube.common.authorization.library.provider.AccessTokenProvider;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider; import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.authorization.utils.manager.SecretManager;
import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
import org.gcube.common.authorization.utils.secret.GCubeSecret;
import org.gcube.common.authorization.utils.secret.Secret;
import org.gcube.common.scope.api.ScopeProvider; import org.gcube.common.scope.api.ScopeProvider;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@Slf4j @Slf4j
public class TokenSetter{ public class TokenSetter {
private static Properties props=null;
static{
}
public static synchronized void set(String scope){ private static Properties props=new Properties();
try{
if(props==null) { static{
props=new Properties(); try {
try { props.load(TokenSetter.class.getResourceAsStream("/tokens.properties"));
props.load(TokenSetter.class.getResourceAsStream("/tokens.properties")); } catch (Exception e) {
} catch (Exception e) { throw new RuntimeException("YOU NEED TO SET TOKEN FILE IN CONFIGURATION",e);
throw new RuntimeException("YOU NEED TO SET TOKEN FILE IN CONFIGURATION");
}
} }
if(!props.containsKey(scope)) throw new Exception("No token found for scope : "+scope);
SecurityTokenProvider.instance.set(props.getProperty(scope));
}catch(Throwable e){
log.trace("Unable to set token for scope "+scope,e);
} }
ScopeProvider.instance.set(scope);
}
public static void setToken(String token){ public static void set(String scope){
try{ SecretManagerProvider.instance.set(new SecretManager());
AuthorizationEntry entry = authorizationService().get(token); if(!props.containsKey(scope)) throw new RuntimeException("No token found for scope : "+scope);
ScopeProvider.instance.set(entry.getContext()); // Secret secret = SecretUtility. getSecretByTokenString(token); // se non sai con che token hai a che fare;
SecurityTokenProvider.instance.set(token); // oppure
}catch(Throwable t) { Secret secret = new GCubeSecret(props.getProperty(scope)); // se vecchio token
throw new RuntimeException("Unable to set token "+token,t); // oppure
// Secret secret = new JWTSecret(token); // se nuovo token
SecretManagerProvider.instance.get().addSecret(secret);
try{
SecretManagerProvider.instance.get().set();
}catch(Exception e ){throw new RuntimeException("Unable to set secret for context "+scope,e);}
} }
} //
// public static void setUma() throws IOException {
// File umaFile = new File("uma.json");
// String uma= Files.readFileAsString(umaFile.getAbsolutePath(), Charset.defaultCharset());
// AccessTokenProvider.instance.set(uma);
//
// }
public static String getCurrentToken() {
return SecurityTokenProvider.instance.get();
}
} }