From 702f109cdeeb45209eb3a88857ecedf6f217204b Mon Sep 17 00:00:00 2001 From: Roberto Cirillo Date: Fri, 7 May 2021 10:53:57 +0200 Subject: [PATCH] get System ServiceEndpoint from IS --- CHANGELOG.md | 4 +- .../consumer/UserAccountingConsumer.java | 1 - .../storageserver/startup/Configuration.java | 50 +++++++++++++++++-- .../storageserver/startup/Startup.java | 24 +++++++-- 4 files changed, 67 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8bb9dfc..913855d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,4 +5,6 @@ * upgrade mongo-java-driver to 3.12.0 version * switch from document-store-lib-couchbase to document-sore-lib-accounting-service * upgrade accounting libraries to 2.0.0 versions - * update JUnit to 4.12 \ No newline at end of file + * update JUnit to 4.12 + * add oidc-library dep + * switch to UMAToken \ No newline at end of file diff --git a/src/main/java/org/gcube/contentmanager/storageserver/consumer/UserAccountingConsumer.java b/src/main/java/org/gcube/contentmanager/storageserver/consumer/UserAccountingConsumer.java index dda8e0e..5de74ba 100644 --- a/src/main/java/org/gcube/contentmanager/storageserver/consumer/UserAccountingConsumer.java +++ b/src/main/java/org/gcube/contentmanager/storageserver/consumer/UserAccountingConsumer.java @@ -167,7 +167,6 @@ public class UserAccountingConsumer extends Thread{ } }catch(Exception e){ -// logger.error(" CATCHED EXCEPTION "+e.getCause().getLocalizedMessage()); logger.error("ERROR Processing record: "+x+" Exception throws: "+e.getMessage()); logger.info("skip to next record "); if(mongo!=null) diff --git a/src/main/java/org/gcube/contentmanager/storageserver/startup/Configuration.java b/src/main/java/org/gcube/contentmanager/storageserver/startup/Configuration.java index 85d9b5e..2a10cec 100644 --- a/src/main/java/org/gcube/contentmanager/storageserver/startup/Configuration.java +++ b/src/main/java/org/gcube/contentmanager/storageserver/startup/Configuration.java @@ -8,7 +8,7 @@ import java.util.Iterator; import java.util.List; import java.util.Map; -import org.gcube.common.encryption.StringEncrypter; +import org.gcube.common.encryption.encrypter.StringEncrypter; import org.gcube.common.resources.gcore.GCoreEndpoint; import org.gcube.common.resources.gcore.GCoreEndpoint.Profile.Endpoint; import org.gcube.common.resources.gcore.ServiceEndpoint; @@ -31,10 +31,15 @@ public class Configuration { private String backendType; private ArrayList dtsHosts; private boolean activeDTSFilter; - private static final String SE_CATEGORY="DataStorage"; - private static final String SE_NAME="StorageManager"; + private static final String STORAGE_SE_CATEGORY="DataStorage"; + private static final String STORAGE_SE_NAME="StorageManager"; + private static final String SYSTEM_SE_CATEGORY="DataStorage"; + private static final String SYSTEM_SE_NAME="StorageManager"; private static final String ACCOUNTING_USERNAME="accounting_user"; private static final String ACCOUNTING_PASSWORDNAME="accounting_pwd"; + protected String clientId; + protected String secret; + Logger logger= LoggerFactory.getLogger(Configuration.class); public Configuration(String scope, String user, String password, boolean dtsFilter){ @@ -103,6 +108,14 @@ public class Configuration { } return null; } + + + protected void setSecrets(ServiceEndpoint se) throws Exception { + for (AccessPoint ap:se.profile().accessPoints()) { + setClientId(ap.username()); + setSecret(StringEncrypter.getEncrypter().decrypt(ap.password())); + } + } /** * The accounting password is retrieved from the first serviceEndpoint found in the scope and the first accessPoint inside the serviceEndpoint @@ -129,13 +142,25 @@ public class Configuration { protected List getStorageServiceEndpoint() { logger.debug("query for serviceEndpoint ongoing..."); SimpleQuery query = queryFor(ServiceEndpoint.class); - query.addCondition("$resource/Profile/Category/text() eq '"+SE_CATEGORY+"' and $resource/Profile/Name eq '"+SE_NAME+"' "); + query.addCondition("$resource/Profile/Category/text() eq '"+STORAGE_SE_CATEGORY+"' and $resource/Profile/Name eq '"+STORAGE_SE_NAME+"' "); DiscoveryClient client = clientFor(ServiceEndpoint.class); List resources = client.submit(query); if (resources.size() > 0) logger.debug("resource found on IS"); return resources; } + + protected ServiceEndpoint getSystemServiceEndpoint() { + logger.debug("query for serviceEndpoint ongoing..."); + SimpleQuery query = queryFor(ServiceEndpoint.class); + query.addCondition("$resource/Profile/Category/text() eq '"+SYSTEM_SE_CATEGORY+"' and $resource/Profile/Name eq '"+SYSTEM_SE_NAME+"' "); + DiscoveryClient client = clientFor(ServiceEndpoint.class); + List resources = client.submit(query); + if (resources.size() > 0) + return resources.get(0); + else + throw new RuntimeException("System ServiceEndpoint not found"); + } private String[] getServers(ServiceEndpoint res) { server=new String[res.profile().accessPoints().size()]; @@ -329,7 +354,22 @@ public class Configuration { this.password = password; } - + public String getClientId() { + return clientId; + } + + public void setClientId(String clientId) { + this.clientId = clientId; + } + + public String getSecret() { + return secret; + } + + public void setSecret(String secret) { + this.secret = secret; + } + } diff --git a/src/main/java/org/gcube/contentmanager/storageserver/startup/Startup.java b/src/main/java/org/gcube/contentmanager/storageserver/startup/Startup.java index c1456c0..eff2e24 100644 --- a/src/main/java/org/gcube/contentmanager/storageserver/startup/Startup.java +++ b/src/main/java/org/gcube/contentmanager/storageserver/startup/Startup.java @@ -16,7 +16,10 @@ public class Startup { private static String accountingUser; private static String accountingPassword; private static String scope; - private static List se; + private static List storage_se; + private static ServiceEndpoint system_se; + private static String clientId; + private static String secret; public static void main(String[] args) { @@ -41,9 +44,19 @@ public class Startup { // user=args[2]; // password=args[3]; Configuration cfg=new Configuration(scope, false); - se=cfg.getStorageServiceEndpoint(); - accountingUser=cfg.getAccountingUser(se); - accountingPassword=cfg.getAccountingPassword(se); + storage_se=cfg.getStorageServiceEndpoint(); + accountingUser=cfg.getAccountingUser(storage_se); + accountingPassword=cfg.getAccountingPassword(storage_se); + system_se=cfg.getSystemServiceEndpoint(); + try { + cfg.setSecrets(system_se); + } catch (Exception e) { + System.err.println("problem retrieving credentials from ServiceEnpoint"); + e.printStackTrace(); + + } + clientId=cfg.getClientId(); + secret=cfg.getSecret(); String[] server=retrieveServerConfiguration(cfg); List dtsHosts=null;//retrieveDTSConfiguration(cfg); @@ -54,6 +67,7 @@ public class Startup { // startFolderAccountingConsumer(args, server, c2); } + @Deprecated private static void startFolderAccountingConsumer(String[] args, String[] server, CubbyHole c2) { FolderAccountingConsumer fsConsumer=null; @@ -92,7 +106,7 @@ public class Startup { } private static String[] retrieveServerConfiguration(Configuration c) { - String[] server= c.getServerAccess(se); + String[] server= c.getServerAccess(storage_se); if(user == null) user=c.getUsername(); if(password == null)