|
|
|
@ -3,6 +3,7 @@ package org.gcube.contentmanager.storageclient.wrapper;
|
|
|
|
|
import java.util.Iterator;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.Objects;
|
|
|
|
|
import java.util.TreeSet;
|
|
|
|
|
|
|
|
|
|
import org.gcube.common.resources.gcore.ServiceEndpoint;
|
|
|
|
@ -28,6 +29,7 @@ public class Configuration {
|
|
|
|
|
private String sn;
|
|
|
|
|
private String user;
|
|
|
|
|
private String password;
|
|
|
|
|
// if the backend is mongodb, this field is used for crypt/decrypt. If the backend is S3, this field is a token.
|
|
|
|
|
private String passPhrase;
|
|
|
|
|
private ISClientConnector isclient;
|
|
|
|
|
// the scope used for discovering the runtimeResource
|
|
|
|
@ -36,12 +38,17 @@ public class Configuration {
|
|
|
|
|
private String[] volatileHost;
|
|
|
|
|
private String[] persistentHosts;
|
|
|
|
|
private String resolverHost;
|
|
|
|
|
private String region;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(Configuration.class);
|
|
|
|
|
private static final String DEFAULT_BACKEND_TYPE="MongoDB";
|
|
|
|
|
protected static final String DEFAULT_BACKEND_TYPE="MongoDB";
|
|
|
|
|
private static final String WRITE_CONCERN_PROPERTY_NAME="write_concern";
|
|
|
|
|
private static final String READ_PREFERENCE_PROPERTY_NAME="read_preference";
|
|
|
|
|
private static final String DB_PROPERTY_NAME="collection";
|
|
|
|
|
private static final String DB_STRING_SEPARATOR=";";
|
|
|
|
|
private static final String PASS_PHRASE_FIELD="PassPhrase";
|
|
|
|
|
private static final String TOKEN_FIELD="token";
|
|
|
|
|
/**
|
|
|
|
|
* home library need a special configuration
|
|
|
|
|
*/
|
|
|
|
@ -73,34 +80,49 @@ public class Configuration {
|
|
|
|
|
setScopeString(scopeString);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Loads all the configuration parameters in a java object for a specific backend
|
|
|
|
|
*
|
|
|
|
|
* @param sc: service class of the remote resource
|
|
|
|
|
* @param sn service name of the remote resource
|
|
|
|
|
* @param scopeString string that identifies the scope
|
|
|
|
|
* @param owner user of the client library
|
|
|
|
|
* @param clientID
|
|
|
|
|
* @param accessType indicates the type of access to the storage
|
|
|
|
|
* @param memory indicates the type of memory used by the storage: Persistent or Volatile
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
public Configuration(String sc, String sn, String scopeString, String owner, String clientID, String accessType, String memory, String backend){
|
|
|
|
|
this.sc=sc;
|
|
|
|
|
this.sn=sn;
|
|
|
|
|
this.owner=owner;
|
|
|
|
|
this.clientID=clientID;
|
|
|
|
|
this.typeAccess=accessType;
|
|
|
|
|
this.memoryType=memory;
|
|
|
|
|
setScopeString(scopeString);
|
|
|
|
|
setBackendType(backend);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Retrieve a valid configuration from IS for instantiating the engine
|
|
|
|
|
*/
|
|
|
|
|
public void getConfiguration(){
|
|
|
|
|
String[] newServer=null;
|
|
|
|
|
// ISClientConnector isclient=getISClient();
|
|
|
|
|
String currentScope=ScopeProvider.instance.get();
|
|
|
|
|
logger.debug("Scope found on ScopeProvider instance is "+currentScope);
|
|
|
|
|
|
|
|
|
|
/*COMMENTED THE FOLLOWING LINES 20181214*/
|
|
|
|
|
|
|
|
|
|
if(RRScope == null){
|
|
|
|
|
// if(new ScopeBean(currentScope).is(Type.VRE)){
|
|
|
|
|
// logger.debug("If ScopeProvider scope is VRE scope RR scope became VO scope");
|
|
|
|
|
// RRScope=new ScopeBean(currentScope).enclosingScope().toString();
|
|
|
|
|
// }else{
|
|
|
|
|
// logger.debug("If ScopeProvider scope is not a VRE scope RR scope is ScopeProvider scope");
|
|
|
|
|
RRScope=currentScope;
|
|
|
|
|
// }
|
|
|
|
|
}
|
|
|
|
|
/*END*/
|
|
|
|
|
logger.debug("RuntimeResource scope "+RRScope);
|
|
|
|
|
ServiceEndpoint resource=getISClient().getStorageEndpoint(RRScope);
|
|
|
|
|
//if a specific backend is not passed as input param then take the default one
|
|
|
|
|
if (Objects.isNull(getBackendType()))
|
|
|
|
|
setBackendType(DEFAULT_BACKEND_TYPE);
|
|
|
|
|
ServiceEndpoint resource=getISClient(getBackendType()).getStorageEndpoint(RRScope);
|
|
|
|
|
if(resource ==null )
|
|
|
|
|
throw new IllegalStateException("the storage resource is not present on IS in scope: "+RRScope);
|
|
|
|
|
List<ServiceEndpoint> resolverResource =getISClient().getServiceEndpoint(Utils.URI_RESOLVER_RESOURCE_CATEGORY, Utils.URI_RESOLVER_RESOURCE_NAME);
|
|
|
|
|
List<ServiceEndpoint> resolverResource =getISClient(getBackendType()).getServiceEndpoint(Utils.URI_RESOLVER_RESOURCE_CATEGORY, Utils.URI_RESOLVER_RESOURCE_NAME);
|
|
|
|
|
if(resolverResource !=null && resolverResource.size()> 0)
|
|
|
|
|
setResolverHost(getISClient().getResolverHost(resolverResource.get(0)));
|
|
|
|
|
setResolverHost(getISClient(getBackendType()).getResolverHost(resolverResource.get(0)));
|
|
|
|
|
else
|
|
|
|
|
throw new IllegalStateException("the uri resolver resource is not present on IS in scope: "+currentScope);
|
|
|
|
|
// old method for retrieve hostedOn field in storage ServiceEndpoint resource
|
|
|
|
@ -108,13 +130,17 @@ public class Configuration {
|
|
|
|
|
logger.debug("server not set. Try to query IS in scope: "+scopeString);
|
|
|
|
|
String[] serverFound=checkVarEnvMongo();
|
|
|
|
|
if(serverFound==null){
|
|
|
|
|
serverFound=getISClient().retrieveConnectionInfo(resource);
|
|
|
|
|
setUser(getISClient().getUsername());
|
|
|
|
|
setPassword(getISClient().password);
|
|
|
|
|
setBackendType(getISClient().getBackendType(resource));
|
|
|
|
|
serverFound=getISClient(getBackendType()).retrieveConnectionInfo(resource);
|
|
|
|
|
setUser(getISClient(getBackendType()).getUsername());
|
|
|
|
|
setPassword(getISClient(getBackendType()).password);
|
|
|
|
|
setRegion(getISClient(getBackendType()).getRegion());
|
|
|
|
|
if (Objects.isNull(getBackendType()))
|
|
|
|
|
setBackendType(getISClient(getBackendType()).getBackendType(resource));
|
|
|
|
|
}else{
|
|
|
|
|
setBackendType(checkVarEnvBackendType());
|
|
|
|
|
if(getBackendType() == null) setBackendType(DEFAULT_BACKEND_TYPE);
|
|
|
|
|
if (!Objects.isNull(getBackendType()))
|
|
|
|
|
setBackendType(checkVarEnvBackendType());
|
|
|
|
|
else
|
|
|
|
|
setBackendType(DEFAULT_BACKEND_TYPE);
|
|
|
|
|
setUser(checkVarEnvUser());
|
|
|
|
|
setPassword(checkVarEnvPassword());
|
|
|
|
|
}
|
|
|
|
@ -128,15 +154,46 @@ public class Configuration {
|
|
|
|
|
if(newServer==null){
|
|
|
|
|
throw new IllegalStateException("Resource not found on Information System");
|
|
|
|
|
}else{
|
|
|
|
|
setEnvironment(setAreaStorage(getSc(), getSn()));
|
|
|
|
|
setServerHosts(newServer, isclient, resource);
|
|
|
|
|
try {
|
|
|
|
|
setPassPhrase(retrieveEncryptionPhrase());
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
setEnvironment(setAreaStorage(getSc(), getSn()));
|
|
|
|
|
// if the backend is mongodb we should set a separated server for volatile area
|
|
|
|
|
if(getBackendType().equals("MongoDB")) {
|
|
|
|
|
setServerHosts(newServer, isclient, resource);
|
|
|
|
|
try {
|
|
|
|
|
setPassPhrase(retrieveEncryptionField(PASS_PHRASE_FIELD));
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}else {
|
|
|
|
|
// if the backend isn't mongodb, the passPhrase field will be filled with a token if it is present on ServiceEndpoint
|
|
|
|
|
try {
|
|
|
|
|
setPassPhrase(retrieveEncryptionField(TOKEN_FIELD));
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
// TODO Auto-generated catch block
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
setPersistentHosts(newServer);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger.debug("Configuration parameters retrieved: host: "+getPersistentHosts()+" user: "+getUser());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// /**
|
|
|
|
|
// * Retrieve a valid configuration from IS for instantiating the engine
|
|
|
|
|
// */
|
|
|
|
|
// public void getConfiguration2(){
|
|
|
|
|
// String currentScope=ScopeProvider.instance.get();
|
|
|
|
|
// ServiceEndpoint resource=getISClient().getStorageEndpoint(currentScope);
|
|
|
|
|
// if((getMemoryType() != null) && ((getMemoryType().equals(MemoryType.VOLATILE.toString()) || (getMemoryType().equals(MemoryType.BOTH.toString()))))){
|
|
|
|
|
// setVolatileHost(isclient.getVolatileHost(resource));
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void setServerHosts(String[] newServer, ISClientConnector isclient, ServiceEndpoint resource) {
|
|
|
|
|
if((getMemoryType() != null) && ((getMemoryType().equals(MemoryType.VOLATILE.toString()) || (getMemoryType().equals(MemoryType.BOTH.toString()))))){
|
|
|
|
@ -192,11 +249,11 @@ public class Configuration {
|
|
|
|
|
String dbString=null;
|
|
|
|
|
// check optional properties only if it is not a volatile storage instance
|
|
|
|
|
if((getMemoryType() != null) && (!(getMemoryType().equals(MemoryType.VOLATILE.toString())))){
|
|
|
|
|
write=getISClient().retrievePropertyValue(WRITE_CONCERN_PROPERTY_NAME, currentScope);
|
|
|
|
|
write=getISClient(getBackendType()).retrievePropertyValue(WRITE_CONCERN_PROPERTY_NAME, currentScope);
|
|
|
|
|
logger.debug("read preference: read from service endpoint");
|
|
|
|
|
read=getISClient().retrievePropertyValue(READ_PREFERENCE_PROPERTY_NAME, currentScope);
|
|
|
|
|
read=getISClient(getBackendType()).retrievePropertyValue(READ_PREFERENCE_PROPERTY_NAME, currentScope);
|
|
|
|
|
logger.debug(" write preference: read from service endpoint");
|
|
|
|
|
dbString=getISClient().retrievePropertyValue(DB_PROPERTY_NAME, currentScope);
|
|
|
|
|
dbString=getISClient(getBackendType()).retrievePropertyValue(DB_PROPERTY_NAME, currentScope);
|
|
|
|
|
if((write!=null) && (read!=null)){
|
|
|
|
|
engine.setWriteConcern(write);
|
|
|
|
|
engine.setReadConcern(read);
|
|
|
|
@ -210,11 +267,12 @@ public class Configuration {
|
|
|
|
|
}
|
|
|
|
|
if (dbNames.length > 1)
|
|
|
|
|
logger.debug("multiple collection discovered");
|
|
|
|
|
engine.setDbNames(dbNames);
|
|
|
|
|
if (Objects.isNull(engine.getDbNames()))
|
|
|
|
|
engine.setDbNames(dbNames);
|
|
|
|
|
}
|
|
|
|
|
// added db check also on volatile are
|
|
|
|
|
}else if((getMemoryType().equals(MemoryType.VOLATILE.toString()))){
|
|
|
|
|
dbString=getISClient().retrievePropertyValue(DB_PROPERTY_NAME, currentScope);
|
|
|
|
|
dbString=getISClient(getBackendType()).retrievePropertyValue(DB_PROPERTY_NAME, currentScope);
|
|
|
|
|
if(dbString!=null){
|
|
|
|
|
if (dbString.contains(DB_STRING_SEPARATOR)){
|
|
|
|
|
logger.debug("more than one collection read from ServiceEnpoint");
|
|
|
|
@ -326,19 +384,31 @@ public class Configuration {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Deprecated
|
|
|
|
|
public String retrieveEncryptionPhrase() throws Exception {
|
|
|
|
|
String currentScope=ScopeProvider.instance.get();
|
|
|
|
|
logger.debug("retrieve encryption prhase on scope: "+currentScope);
|
|
|
|
|
String encryptedKey=null;
|
|
|
|
|
// ISClientConnector isclient=getISClient();
|
|
|
|
|
logger.info("retrieve encryption phrase from scope "+currentScope);
|
|
|
|
|
encryptedKey=getISClient().retrievePropertyValue("PassPhrase", currentScope);
|
|
|
|
|
encryptedKey=getISClient(getBackendType()).retrievePropertyValue("PassPhrase", currentScope);
|
|
|
|
|
logger.info("encrypted prhase is "+encryptedKey);
|
|
|
|
|
String decryptString=org.gcube.common.encryption.StringEncrypter.getEncrypter().decrypt(encryptedKey);
|
|
|
|
|
return decryptString;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String retrieveEncryptionField(String fieldName) throws Exception {
|
|
|
|
|
String currentScope=ScopeProvider.instance.get();
|
|
|
|
|
logger.debug("retrieve encryption prhase on scope: "+currentScope);
|
|
|
|
|
String encryptedKey=null;
|
|
|
|
|
// ISClientConnector isclient=getISClient();
|
|
|
|
|
logger.info("retrieve encryption phrase from scope "+currentScope);
|
|
|
|
|
encryptedKey=getISClient(getBackendType()).retrievePropertyValue(fieldName, currentScope);
|
|
|
|
|
logger.info("encrypted prhase is "+encryptedKey);
|
|
|
|
|
String decryptString=(!Objects.isNull(encryptedKey))?org.gcube.common.encryption.StringEncrypter.getEncrypter().decrypt(encryptedKey):null;
|
|
|
|
|
return decryptString;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public String getClientID() {
|
|
|
|
|
return clientID;
|
|
|
|
@ -460,10 +530,14 @@ public class Configuration {
|
|
|
|
|
this.resolverHost = resolverHost;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ISClientConnector getISClient(){
|
|
|
|
|
if (isclient == null)
|
|
|
|
|
isclient=new ISClientConnector();
|
|
|
|
|
return isclient;
|
|
|
|
|
private ISClientConnector getISClient(String backendType){
|
|
|
|
|
// if ((isclient == null) || (isclient.getBackendType().equals(backendType)))
|
|
|
|
|
// isclient=new ISClientConnector(backendType);
|
|
|
|
|
//
|
|
|
|
|
// return isclient;
|
|
|
|
|
|
|
|
|
|
return isclient=((isclient == null) || (!isclient.getBackendType().equals(backendType)))?new ISClientConnector(backendType):isclient;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String getSc() {
|
|
|
|
@ -481,5 +555,13 @@ public class Configuration {
|
|
|
|
|
public void setSn(String sn) {
|
|
|
|
|
this.sn = sn;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String getRegion() {
|
|
|
|
|
return region;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setRegion(String region) {
|
|
|
|
|
this.region = region;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|