diff --git a/mail/src/main/java/org/assimbly/mail/component/mail/MailConfiguration.java b/mail/src/main/java/org/assimbly/mail/component/mail/MailConfiguration.java index 213f941e..9a453e8f 100644 --- a/mail/src/main/java/org/assimbly/mail/component/mail/MailConfiguration.java +++ b/mail/src/main/java/org/assimbly/mail/component/mail/MailConfiguration.java @@ -34,6 +34,7 @@ import org.apache.camel.support.jsse.SSLContextParameters; import org.apache.camel.util.ObjectHelper; +import org.assimbly.tenantvariables.domain.TenantVariable; import org.assimbly.tenantvariables.mongo.MongoDao; import javax.net.ssl.SSLContext; @@ -455,7 +456,7 @@ public void setAuthenticationType(String authenticationType) { * The accessToken for login */ public String getAccessToken() { - return MongoDao.interpolatePossibleTenantVariable(accessToken, getTenant()); + return MongoDao.interpolatePossibleTenantVariable(accessToken, getTenant(), TenantVariable.TenantVarType.StaticTenantVariable); } public void setAccessToken(String accessToken) { diff --git a/oauth2token/src/main/java/org/assimbly/oauth2token/OAuth2TokenProcessor.java b/oauth2token/src/main/java/org/assimbly/oauth2token/OAuth2TokenProcessor.java index 2708047c..5bff0fcd 100644 --- a/oauth2token/src/main/java/org/assimbly/oauth2token/OAuth2TokenProcessor.java +++ b/oauth2token/src/main/java/org/assimbly/oauth2token/OAuth2TokenProcessor.java @@ -3,7 +3,7 @@ import org.apache.camel.Exchange; import org.apache.camel.Processor; import org.apache.commons.lang3.StringUtils; -import org.assimbly.tenantvariables.mongo.MongoDao; +import org.assimbly.oauth2token.tenant.TenantVariableManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.assimbly.tenantvariables.TenantVariablesProcessor; @@ -39,10 +39,10 @@ public void process(Exchange exchange) { String refreshFlagVarName = TokenService.OAUTH2_PREFIX + id + TokenService.OAUTH2_REFRESH_FLAG_SUFFIX; // check if there's a tenant variable inside tenantVar, and return real value - String expireDate = MongoDao.getTenantVariableValue(expireDateVarName, tenant, environment); - String accessToken = MongoDao.getTenantVariableValue(accessTokenVarName, tenant, environment); - String refreshFlag = MongoDao.getTenantVariableValue(refreshFlagVarName, tenant, environment); - String tokenTenantVarValue = MongoDao.getTenantVariableValue(tokenName, tenant, environment); + String expireDate = TenantVariableManager.getTenantVariableValue(expireDateVarName, tenant, environment); + String accessToken = TenantVariableManager.getTenantVariableValue(accessTokenVarName, tenant, environment); + String refreshFlag = TenantVariableManager.getTenantVariableValue(refreshFlagVarName, tenant, environment); + String tokenTenantVarValue = TenantVariableManager.discoverAndGetTenantVariableValue(tokenName, tenant, environment); Calendar expireCal = Calendar.getInstance(); Calendar expireDelayCal = Calendar.getInstance(); @@ -73,11 +73,14 @@ public void process(Exchange exchange) { accessToken = TokenService.refreshTokenInfo(id, environment, tenant); if(accessToken!=null && (!accessToken.equals(accessTokenOld) || StringUtils.isEmpty(tokenTenantVarValue))) { // add token to tenant variable - MongoDao.saveTenantVariable(tokenName, accessToken, tenant, environment); + TenantVariableManager.discoverAndSaveTenantVariable(tokenName, accessToken, tenant, environment); } } - // add token to the header - exchange.getOut().setHeader(tokenName, accessToken); + if(!TenantVariableManager.isStaticTenantVariable(tokenName)) { + // add token to the header + exchange.getOut().setHeader(tokenName, accessToken); + } } + } diff --git a/oauth2token/src/main/java/org/assimbly/oauth2token/service/TokenService.java b/oauth2token/src/main/java/org/assimbly/oauth2token/service/TokenService.java index dc46f8c1..413d8a0f 100644 --- a/oauth2token/src/main/java/org/assimbly/oauth2token/service/TokenService.java +++ b/oauth2token/src/main/java/org/assimbly/oauth2token/service/TokenService.java @@ -2,7 +2,7 @@ import org.apache.commons.lang3.StringUtils; import org.apache.log4j.Logger; -import org.assimbly.tenantvariables.mongo.MongoDao; +import org.assimbly.oauth2token.tenant.TenantVariableManager; import org.assimbly.util.exception.OAuth2TokenException; import org.json.JSONObject; import org.assimbly.auth.endpoint.annotation.Secured; @@ -73,13 +73,13 @@ public Map tokenInfo( String tokenTenantVarName = OAUTH2_PREFIX + id + OAUTH2_TOKEN_TENANT_VAR_SUFFIX; // check if there's a tenant variable inside tenantVar, and return real value - String scope = MongoDao.getTenantVariableValue(scopeVarName, tenant, environment); - String clientId = MongoDao.getTenantVariableValue(clientIdVarName, tenant, environment); - String clientSecret = MongoDao.getTenantVariableValue(clientSecretVarName, tenant, environment); - String redirectUri = MongoDao.getTenantVariableValue(redirectUriVarName, tenant, environment); - String uriToken = MongoDao.getTenantVariableValue(uriTokenVarName, tenant, environment); - String credentialsType = MongoDao.getTenantVariableValue(credentialsTypeVarName, tenant, environment); - String tokenTenantVar = MongoDao.getTenantVariableValue(tokenTenantVarName, tenant, environment); + String scope = TenantVariableManager.getTenantVariableValue(scopeVarName, tenant, environment); + String clientId = TenantVariableManager.getTenantVariableValue(clientIdVarName, tenant, environment); + String clientSecret = TenantVariableManager.getTenantVariableValue(clientSecretVarName, tenant, environment); + String redirectUri = TenantVariableManager.getTenantVariableValue(redirectUriVarName, tenant, environment); + String uriToken = TenantVariableManager.getTenantVariableValue(uriTokenVarName, tenant, environment); + String credentialsType = TenantVariableManager.getTenantVariableValue(credentialsTypeVarName, tenant, environment); + String tokenTenantVar = TenantVariableManager.getTenantVariableValue(tokenTenantVarName, tenant, environment); boolean customCredentialsType = StringUtils.isEmpty(credentialsType) || credentialsType.equals(CREDENTIALS_TYPE_CUSTOM); @@ -98,20 +98,20 @@ public Map tokenInfo( // save token info into tenant vars String expiresIn = tokenInfoMap.get(SERVICE_PARAM_EXPIRES_IN); if(expiresIn!=null && !expiresIn.isEmpty()) { - MongoDao.saveTenantVariable(expireDateVarName, expiresIn, tenant, environment); + TenantVariableManager.saveTenantVariable(expireDateVarName, expiresIn, tenant, environment); } String accessToken = tokenInfoMap.get(SERVICE_PARAM_ACCESS_TOKEN); if(accessToken!=null && !accessToken.isEmpty()) { - MongoDao.saveTenantVariable(accessTokenVarName, accessToken, tenant, environment); - MongoDao.saveTenantVariable(tokenTenantVar, accessToken, tenant, environment); + TenantVariableManager.saveTenantVariable(accessTokenVarName, accessToken, tenant, environment); + TenantVariableManager.discoverAndSaveTenantVariable(tokenTenantVar, accessToken, tenant, environment); } String refreshToken = tokenInfoMap.get(SERVICE_PARAM_REFRESH_TOKEN); if(refreshToken!=null && !refreshToken.isEmpty()) { - MongoDao.saveTenantVariable(refreshTokenVarName, refreshToken, tenant, environment); + TenantVariableManager.saveTenantVariable(refreshTokenVarName, refreshToken, tenant, environment); } // set refresh flag to inactive - MongoDao.saveTenantVariable(refreshFlagVarName, "0", tenant, environment); + TenantVariableManager.saveTenantVariable(refreshFlagVarName, "0", tenant, environment); // return token info hashmap return tokenInfoMap; @@ -136,16 +136,16 @@ public static String refreshTokenInfo(String id, String environment, String tena try { // set refresh flag to active - MongoDao.saveTenantVariable(refreshFlagVarName, "1", tenant, environment); + TenantVariableManager.saveTenantVariable(refreshFlagVarName, "1", tenant, environment); // check if there's a tenant variable inside tenantVar, and return real value - String scope = MongoDao.getTenantVariableValue(scopeVarName, tenant, environment); - String clientId = MongoDao.getTenantVariableValue(clientIdVarName, tenant, environment); - String clientSecret = MongoDao.getTenantVariableValue(clientSecretVarName, tenant, environment); - String redirectUri = MongoDao.getTenantVariableValue(redirectUriVarName, tenant, environment); - String refreshToken = MongoDao.getTenantVariableValue(refreshTokenVarName, tenant, environment); - String uriToken = MongoDao.getTenantVariableValue(uriTokenVarName, tenant, environment); - String credentialsType = MongoDao.getTenantVariableValue(credentialsTypeVarName, tenant, environment); + String scope = TenantVariableManager.getTenantVariableValue(scopeVarName, tenant, environment); + String clientId = TenantVariableManager.getTenantVariableValue(clientIdVarName, tenant, environment); + String clientSecret = TenantVariableManager.getTenantVariableValue(clientSecretVarName, tenant, environment); + String redirectUri = TenantVariableManager.getTenantVariableValue(redirectUriVarName, tenant, environment); + String refreshToken = TenantVariableManager.getTenantVariableValue(refreshTokenVarName, tenant, environment); + String uriToken = TenantVariableManager.getTenantVariableValue(uriTokenVarName, tenant, environment); + String credentialsType = TenantVariableManager.getTenantVariableValue(credentialsTypeVarName, tenant, environment); boolean customCredentialsType = StringUtils.isEmpty(credentialsType) || credentialsType.equals(CREDENTIALS_TYPE_CUSTOM); @@ -164,23 +164,23 @@ public static String refreshTokenInfo(String id, String environment, String tena // save token info into tenant vars String expiresInResp = tokenInfoMap.get(SERVICE_PARAM_EXPIRES_IN); if(expiresInResp!=null && !expiresInResp.isEmpty()) { - MongoDao.saveTenantVariable(expireDateVarName, expiresInResp, tenant, environment); + TenantVariableManager.saveTenantVariable(expireDateVarName, expiresInResp, tenant, environment); } String accessTokenResp = tokenInfoMap.get(SERVICE_PARAM_ACCESS_TOKEN); if(accessTokenResp!=null && !accessTokenResp.isEmpty()) { - MongoDao.saveTenantVariable(accessTokenVarName, accessTokenResp, tenant, environment); + TenantVariableManager.saveTenantVariable(accessTokenVarName, accessTokenResp, tenant, environment); accessToken = accessTokenResp; } String refreshTokenResp = tokenInfoMap.get(SERVICE_PARAM_REFRESH_TOKEN); if(refreshTokenResp!=null && !refreshTokenResp.isEmpty()) { - MongoDao.saveTenantVariable(refreshTokenVarName, refreshTokenResp, tenant, environment); + TenantVariableManager.saveTenantVariable(refreshTokenVarName, refreshTokenResp, tenant, environment); } } catch (Exception e) { e.printStackTrace(); } finally { // set refresh flag to inactive - MongoDao.saveTenantVariable(refreshFlagVarName, "0", tenant, environment); + TenantVariableManager.saveTenantVariable(refreshFlagVarName, "0", tenant, environment); } // return new access token diff --git a/oauth2token/src/main/java/org/assimbly/oauth2token/tenant/TenantVariableManager.java b/oauth2token/src/main/java/org/assimbly/oauth2token/tenant/TenantVariableManager.java new file mode 100644 index 00000000..de43a538 --- /dev/null +++ b/oauth2token/src/main/java/org/assimbly/oauth2token/tenant/TenantVariableManager.java @@ -0,0 +1,49 @@ +package org.assimbly.oauth2token.tenant; + +import org.assimbly.tenantvariables.domain.TenantVariable; +import org.assimbly.tenantvariables.mongo.MongoDao; + +public class TenantVariableManager { + + private static final String STATIC_TENANT_VARIABLE_SUFFIX = "_"+ TenantVariable.TenantVarType.StaticTenantVariable.name(); + + public static String getTenantVariableValue(String tokenName, String tenant, String environment) { + return MongoDao.getTenantVariableValue(tokenName, tenant, environment); + } + + public static String discoverAndGetTenantVariableValue(String tokenName, String tenant, String environment) { + TenantVariable.TenantVarType tenantVarType = TenantVariable.TenantVarType.TenantVariable; + + if(isStaticTenantVariable(tokenName)) { + tokenName = getStaticTenantVariableName(tokenName); + tenantVarType = TenantVariable.TenantVarType.StaticTenantVariable; + } + + return MongoDao.getTenantVariableValue(tokenName, tenant, environment, tenantVarType); + } + + public static void saveTenantVariable(String tokenName, String accessToken, String tenant, String environment) { + MongoDao.saveTenantVariable(tokenName, accessToken, tenant, environment); + } + + public static void discoverAndSaveTenantVariable(String tokenName, String accessToken, String tenant, String environment) { + TenantVariable.TenantVarType tenantVarType = TenantVariable.TenantVarType.TenantVariable; + + if(TenantVariableManager.isStaticTenantVariable(tokenName)) { + tokenName = getStaticTenantVariableName(tokenName); + tenantVarType = TenantVariable.TenantVarType.StaticTenantVariable; + } + + MongoDao.saveTenantVariable(tokenName, accessToken, tenant, environment, tenantVarType); + } + + public static boolean isStaticTenantVariable(String tokenName) { + return tokenName.endsWith(STATIC_TENANT_VARIABLE_SUFFIX); + } + + private static String getStaticTenantVariableName(String tokenName) { + int index = tokenName.indexOf(STATIC_TENANT_VARIABLE_SUFFIX); + return tokenName.substring(0, index); + } + +} diff --git a/pom.xml b/pom.xml index 49c74c51..92a546f2 100644 --- a/pom.xml +++ b/pom.xml @@ -84,12 +84,11 @@ 2.1.3 2.1.3 2.1.39 - 0.8.0 1.0.5 2.5.2.Final 0.8.11 3.3.3 - 5.13.0 + 5.14.1 5.2.0 4.7.2 6.0.0 diff --git a/tenantvariables/pom.xml b/tenantvariables/pom.xml index 3da4343d..179b1310 100644 --- a/tenantvariables/pom.xml +++ b/tenantvariables/pom.xml @@ -24,12 +24,6 @@ - - org.abstractj.kalium - kalium - ${kalium.version} - - org.mongodb mongodb-driver-sync diff --git a/tenantvariables/src/main/java/org/assimbly/tenantvariables/TenantVariablesProcessor.java b/tenantvariables/src/main/java/org/assimbly/tenantvariables/TenantVariablesProcessor.java index 46222063..72a256fd 100644 --- a/tenantvariables/src/main/java/org/assimbly/tenantvariables/TenantVariablesProcessor.java +++ b/tenantvariables/src/main/java/org/assimbly/tenantvariables/TenantVariablesProcessor.java @@ -1,14 +1,13 @@ package org.assimbly.tenantvariables; import com.jayway.jsonpath.JsonPath; -import org.abstractj.kalium.crypto.Random; -import org.abstractj.kalium.crypto.SecretBox; import org.apache.camel.Exchange; import org.apache.camel.Processor; import org.apache.camel.language.groovy.GroovyExpression; import org.apache.camel.language.xpath.XPathBuilder; import org.apache.camel.language.simple.SimpleLanguage; import org.apache.camel.model.language.SimpleExpression; +import org.assimbly.util.EncryptionUtil; import org.assimbly.util.exception.EnvironmentException; import org.assimbly.util.exception.TenantVariableNotFoundException; import org.assimbly.util.helper.Base64Helper; @@ -25,13 +24,15 @@ import java.util.regex.Pattern; import static java.nio.charset.StandardCharsets.UTF_8; -import static org.abstractj.kalium.NaCl.Sodium.CRYPTO_SECRETBOX_XSALSA20POLY1305_NONCEBYTES; public class TenantVariablesProcessor implements Processor { private static final Logger LOG = LoggerFactory.getLogger(TenantVariablesProcessor.class); - private static final String ASSIMBLY_ENCRYPTION_SECRET = "ASSIMBLY_ENCRYPTION_SECRET"; + private static final String ALGORITHM = "PBEWithHMACSHA512AndAES_256"; + private static final String ASSIMBLY_ENCRYPTION_SECRET = System.getenv("ASSIMBLY_ENCRYPTION_SECRET"); + + private EncryptionUtil encryptionUtil = new EncryptionUtil(ASSIMBLY_ENCRYPTION_SECRET, ALGORITHM); private final String DEFAULT_TENANT_NAME = "default"; private static final String ASSIMBLY_ENV = "ASSIMBLY_ENV"; @@ -62,41 +63,24 @@ public void process(Exchange exchange) { } } - public String decrypt(byte[] value, byte[] nonce) { - SecretBox box = new SecretBox(System.getenv(ASSIMBLY_ENCRYPTION_SECRET).getBytes()); - byte[] decrypted = box.decrypt(nonce, value); - return new String(decrypted); + public String decrypt(String encryptedValue) { + String value = encryptionUtil.decrypt(encryptedValue); + return value; } - byte[] encrypt(String value, byte[] nonce) { - SecretBox box = new SecretBox(System.getenv(ASSIMBLY_ENCRYPTION_SECRET).getBytes()); - byte[] encriptedValue = box.encrypt(nonce, value.getBytes()); - return encriptedValue; + public String encrypt(String value) { + String encryptedValue = encryptionUtil.encrypt(value); + return encryptedValue; } public String getValueByEnvironmentValue(EnvironmentValue environmentVar) { - if(environmentVar.isEncrypted()) { - return decrypt( - Base64Helper.unmarshal(environmentVar.getValue()), - Base64Helper.unmarshal(environmentVar.getNonce()) - ); - } else { - return environmentVar.getValue(); - } + return (environmentVar.isEncrypted() ? decrypt(environmentVar.getValue()) : environmentVar.getValue()); } private void getTenantVariable(Exchange exchange) { String name = endpoint.getConfiguration().getName(); - String tenant = ( - endpoint.getConfiguration().getTenant()!=null ? - endpoint.getConfiguration().getTenant() : - DEFAULT_TENANT_NAME - ); - String environment = ( - endpoint.getConfiguration().getEnvironment()!=null ? - endpoint.getConfiguration().getEnvironment() : - getEnvironment() - ); + String tenant = (endpoint.getConfiguration().getTenant()!=null ? endpoint.getConfiguration().getTenant() : DEFAULT_TENANT_NAME); + String environment = (endpoint.getConfiguration().getEnvironment()!=null ? endpoint.getConfiguration().getEnvironment() : getEnvironment()); name = interpolateVar(name, exchange, false); @@ -108,19 +92,12 @@ private void getTenantVariable(Exchange exchange) { ); } - if(!gVariable.find(environment).isPresent()) { - throw new TenantVariableNotFoundException( - String.format("The Tenant Variable \"%s\" has no values for the %s environment.", name, environment) - ); - } + if(!gVariable.find(environment).isPresent()) + throw new TenantVariableNotFoundException("The Tenant Variable \"" + name + "\" has no values for the "+ environment +" environment."); EnvironmentValue variable = gVariable.find(environment).get(); - String value = ( - variable.isEncrypted() ? - decrypt(Base64Helper.unmarshal(variable.getValue()), Base64Helper.unmarshal(variable.getNonce())) : - variable.getValue() - ); + String value = (variable.isEncrypted() ? decrypt(variable.getValue()) : variable.getValue()); String header = interpolateVar(endpoint.getConfiguration().getHeader(), exchange, false); @@ -132,16 +109,8 @@ private void setTenantVariable(Exchange exchange) { String value = endpoint.getConfiguration().getValue(); String name = endpoint.getConfiguration().getName(); String modifier = endpoint.getConfiguration().getModifier(); - String tenant = ( - endpoint.getConfiguration().getTenant()!=null ? - endpoint.getConfiguration().getTenant() : - DEFAULT_TENANT_NAME - ); - String environment = ( - endpoint.getConfiguration().getEnvironment()!=null ? - endpoint.getConfiguration().getEnvironment() : - getEnvironment() - ); + String tenant = (endpoint.getConfiguration().getTenant()!=null ? endpoint.getConfiguration().getTenant() : DEFAULT_TENANT_NAME); + String environment = (endpoint.getConfiguration().getEnvironment()!=null ? endpoint.getConfiguration().getEnvironment() : getEnvironment()); long modifyDate = System.currentTimeMillis(); name = interpolateVar(name, exchange, false); @@ -170,9 +139,7 @@ private void setTenantVariable(Exchange exchange) { boolean encrypt = endpoint.getConfiguration().isEncrypt(); if(encrypt) { - byte[] nonce = new Random().randomBytes(CRYPTO_SECRETBOX_XSALSA20POLY1305_NONCEBYTES); - value = Base64Helper.marshal(encrypt(value, nonce)); - variable.setNonce(Base64Helper.marshal(nonce)); + value = encrypt(value); } else { variable.setNonce(null); } @@ -187,20 +154,15 @@ private void setTenantVariable(Exchange exchange) { private void deleteTenantVariable(Exchange exchange) { String name = endpoint.getConfiguration().getName(); - String tenant = ( - endpoint.getConfiguration().getTenant()!=null ? endpoint.getConfiguration().getTenant() : DEFAULT_TENANT_NAME - ); + String tenant = (endpoint.getConfiguration().getTenant()!=null ? endpoint.getConfiguration().getTenant() : DEFAULT_TENANT_NAME); if(ExchangeHelper.hasVariables(name)) name = ExchangeHelper.interpolate(name, exchange); TenantVariable variable = MongoDao.findTenantVariableByName(name, tenant); - if(variable == null) { - throw new TenantVariableNotFoundException( - String.format("The Tenant Variable \"%s\" was not found in the database.", name) - ); - } + if(variable == null) + throw new TenantVariableNotFoundException("The Tenant Variable \"" + name + "\" was not found in the database."); MongoDao.deleteTenantVariable(variable, tenant); } diff --git a/tenantvariables/src/main/java/org/assimbly/tenantvariables/domain/TenantVariable.java b/tenantvariables/src/main/java/org/assimbly/tenantvariables/domain/TenantVariable.java index a71aed46..787d6c4f 100644 --- a/tenantvariables/src/main/java/org/assimbly/tenantvariables/domain/TenantVariable.java +++ b/tenantvariables/src/main/java/org/assimbly/tenantvariables/domain/TenantVariable.java @@ -10,13 +10,20 @@ public class TenantVariable { public static final String ID_FIELD = "_id"; + public static final String TYPE_FIELD = "_type"; public static final String NAME_FIELD = "name"; public static final String CREATED_AT_FIELD = "createdAt"; public static final String CREATED_BY_FIELD = "createdBy"; public static final String VALUES_FIELD = "values"; public static final String TAG_IDS_FIELD = "tag_ids"; + public enum TenantVarType { + TenantVariable, + StaticTenantVariable; + } + private ObjectId _id; + private String _type; private String name; private long createdAt; private String createdBy; @@ -27,17 +34,26 @@ public class TenantVariable { public TenantVariable(){ this._id = new ObjectId(); + this._type = TenantVarType.TenantVariable.name(); this.values = new ArrayList<>(); this.tagIds = new ArrayList<>(); } public TenantVariable(String name){ this._id = new ObjectId(); + this._type = TenantVarType.TenantVariable.name(); this.name = name; this.values = new ArrayList<>(); this.tagIds = new ArrayList<>(); } + public TenantVariable(String name, TenantVarType tenantVarType){ + this._id = new ObjectId(); + this._type = tenantVarType.name(); + this.name = name; + this.values = new ArrayList<>(); + } + public List getValues() { return values; } @@ -55,10 +71,21 @@ public void put(EnvironmentValue environmentValue) { public static TenantVariable fromDocument(Document document) { TenantVariable tenantVariable = new TenantVariable(); tenantVariable.set_id(document.getObjectId(ID_FIELD)); + if(document.getString(TYPE_FIELD) != null) { + tenantVariable.set_type(document.getString(TYPE_FIELD)); + } tenantVariable.setName(document.getString(NAME_FIELD)); - if(document.getLong(CREATED_AT_FIELD) != null) { - tenantVariable.setCreatedAt(document.getLong(CREATED_AT_FIELD)); + + Object createdAtField = document.get(CREATED_AT_FIELD); + if (createdAtField != null) { + if (createdAtField instanceof Long) { + tenantVariable.setCreatedAt((Long) createdAtField); + } else if (createdAtField instanceof Integer) { + // Convert Integer to Long + tenantVariable.setCreatedAt(((Integer) createdAtField).longValue()); + } } + tenantVariable.setCreatedBy(document.getString(CREATED_BY_FIELD)); List valuesList = (List) document.get(VALUES_FIELD); @@ -84,6 +111,7 @@ public static TenantVariable fromDocument(Document document) { public Document toDocument() { Document document = new Document(); document.append(ID_FIELD, this.get_id()); + document.append(TYPE_FIELD, this.get_type()); document.append(NAME_FIELD, this.getName()); document.append(CREATED_AT_FIELD, this.getCreatedAt()); document.append(CREATED_BY_FIELD, this.getCreatedBy()); @@ -115,6 +143,14 @@ public void set_id(ObjectId _id) { this._id = _id; } + public String get_type() { + return _type; + } + + public void set_type(String _type) { + this._type = _type; + } + public String getName() { return name; } diff --git a/tenantvariables/src/main/java/org/assimbly/tenantvariables/mongo/MongoDao.java b/tenantvariables/src/main/java/org/assimbly/tenantvariables/mongo/MongoDao.java index 770315c7..df0ecf79 100644 --- a/tenantvariables/src/main/java/org/assimbly/tenantvariables/mongo/MongoDao.java +++ b/tenantvariables/src/main/java/org/assimbly/tenantvariables/mongo/MongoDao.java @@ -3,6 +3,7 @@ import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoCursor; import com.mongodb.client.MongoDatabase; +import org.apache.commons.lang3.StringUtils; import org.assimbly.tenantvariables.TenantVariablesProcessor; import org.assimbly.tenantvariables.domain.EnvironmentValue; import org.assimbly.tenantvariables.domain.TenantVariable; @@ -28,10 +29,18 @@ public class MongoDao { private static final String TENANT_VARIABLE_EXPRESSION = "@\\{(.*?)}"; public static TenantVariable findTenantVariableByName(String variableName, String tenant) { + return findTenantVariableByName(variableName, tenant, TenantVariable.TenantVarType.TenantVariable); + } + + public static TenantVariable findTenantVariableByName(String variableName, String tenant, TenantVariable.TenantVarType tenantVarType) { MongoDatabase database = MongoClientProvider.getInstance().getDatabase(tenant); MongoCollection collection = database.getCollection(TENANT_VARIABLES_COLLECTION_NAME); - Document document = collection.find(new Document(NAME_FIELD, variableName)).first(); + Document query = new Document() + .append(TenantVariable.NAME_FIELD, variableName) + .append(TenantVariable.TYPE_FIELD, tenantVarType.name()); + + Document document = collection.find(query).first(); if (document != null) { return TenantVariable.fromDocument(document); } @@ -53,11 +62,15 @@ public static List findAll(String tenant) { return tenantVariables; } - public static String getTenantVariableValue(String tenantVarName, String tenant, String environment) { + static public String getTenantVariableValue(String tenantVarName, String tenant, String environment) { + return getTenantVariableValue(tenantVarName, tenant, environment, TenantVariable.TenantVarType.TenantVariable); + } + + public static String getTenantVariableValue(String tenantVarName, String tenant, String environment, TenantVariable.TenantVarType tenantVarType) { - TenantVariable tenantVar = MongoDao.findTenantVariableByName(tenantVarName, tenant); + TenantVariable tenantVar = MongoDao.findTenantVariableByName(tenantVarName, tenant, tenantVarType); if(tenantVar==null) { - LOG.info(String.format("tenantVar %s is NULL", tenantVarName)); + LOG.info(String.format("tenantVar %s of type %s is NULL", tenantVarType.name(), tenantVarName)); return null; } @@ -94,6 +107,10 @@ public static String getTenantVariableValue(String tenantVarName, String tenant, } public static String interpolatePossibleTenantVariable(String value, String tenant) { + return interpolatePossibleTenantVariable(value, tenant, TenantVariable.TenantVarType.TenantVariable); + } + + public static String interpolatePossibleTenantVariable(String value, String tenant, TenantVariable.TenantVarType tenantVarType) { StringBuffer valueBuf = new StringBuffer(); String environment = TenantVariablesProcessor.getEnvironment(); Pattern pattern = Pattern.compile(TENANT_VARIABLE_EXPRESSION); @@ -104,7 +121,7 @@ public static String interpolatePossibleTenantVariable(String value, String tena TenantVariablesProcessor tenantVarProcessor = new TenantVariablesProcessor(); String environmentVarValue = ""; - TenantVariable tenantVar = MongoDao.findTenantVariableByName(varName, tenant); + TenantVariable tenantVar = MongoDao.findTenantVariableByName(varName, tenant, tenantVarType); Optional environmentVar = tenantVar.find(environment); if(environmentVar.isPresent()) { environmentVarValue = tenantVarProcessor.getValueByEnvironmentValue(environmentVar.get()); @@ -123,20 +140,19 @@ public static boolean isTenantVar(String value) { return matcher.find(); } - public static void saveTenantVariable( + static public void saveTenantVariable( String tenantVarName, String tenantVarValue, String tenant, String environment ) { - TenantVariable tenantVariable = findTenantVariableByName(tenantVarName, tenant); - boolean tenantVariableExist = !Objects.isNull(tenantVariable); + saveTenantVariable(tenantVarName, tenantVarValue, tenant, environment, TenantVariable.TenantVarType.TenantVariable); + } - if(!tenantVariableExist) { - tenantVariable = new TenantVariable(tenantVarName); - tenantVariable.setCreatedAt(new Date().getTime()); - tenantVariable.setCreatedBy(CREATED_BY_SYSTEM); - } + public static void saveTenantVariable( + String tenantVarName, String tenantVarValue, String tenant, String environment, TenantVariable.TenantVarType tenantVarType + ) { + TenantVariable tenantVariable = findTenantVariableByName(tenantVarName, tenant, tenantVarType); + boolean tenantVariableExist = !Objects.isNull(tenantVariable); - if(!tenantVariable.find(environment).isPresent()) - tenantVariable.put(new EnvironmentValue(environment)); + tenantVariable = initTenantVariable(tenantVariable, tenantVarType, tenantVarName, environment, tenantVariableExist); EnvironmentValue variable = tenantVariable.find(environment).get(); @@ -148,6 +164,24 @@ public static void saveTenantVariable( updateTenantVariable(tenantVariable, tenant, tenantVariableExist); } + private static TenantVariable initTenantVariable(TenantVariable tenantVariable, TenantVariable.TenantVarType tenantVarType, String tenantVarName, String environment, boolean tenantVariableExist) { + if(!tenantVariableExist) { + tenantVariable = new TenantVariable(tenantVarName); + tenantVariable.set_type(tenantVarType.name()); + } + if(StringUtils.isEmpty(tenantVariable.getCreatedBy())) { + tenantVariable.setCreatedBy(CREATED_BY_SYSTEM); + } + if(tenantVariable.getCreatedAt() == 0) { + tenantVariable.setCreatedAt(new Date().getTime()); + } + if(!tenantVariable.find(environment).isPresent()) { + tenantVariable.put(new EnvironmentValue(environment)); + } + + return tenantVariable; + } + public static void updateTenantVariable(TenantVariable tenantVariable, String tenant, boolean tenantVariableExist){ MongoDatabase database = MongoClientProvider.getInstance().getDatabase(tenant); MongoCollection collection = database.getCollection(TENANT_VARIABLES_COLLECTION_NAME); diff --git a/tenantvariables/src/test/java/org/assimbly/tenantvariables/TenantVariablesEncryptionTest.java b/tenantvariables/src/test/java/org/assimbly/tenantvariables/TenantVariablesEncryptionTest.java index ea8ee4ef..fa423bcf 100644 --- a/tenantvariables/src/test/java/org/assimbly/tenantvariables/TenantVariablesEncryptionTest.java +++ b/tenantvariables/src/test/java/org/assimbly/tenantvariables/TenantVariablesEncryptionTest.java @@ -1,18 +1,12 @@ package org.assimbly.tenantvariables; -import org.abstractj.kalium.crypto.Random; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; -import org.mockito.MockedStatic; -import org.mockito.Mockito; -import org.powermock.core.classloader.annotations.PrepareForTest; import java.io.IOException; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.abstractj.kalium.NaCl.Sodium.CRYPTO_SECRETBOX_XSALSA20POLY1305_NONCEBYTES; -@PrepareForTest(TenantVariablesProcessor.class) public class TenantVariablesEncryptionTest { private static TenantVariablesProcessor processor; @@ -21,17 +15,14 @@ public class TenantVariablesEncryptionTest { @BeforeAll public static void setup() { processor = new TenantVariablesProcessor(); - try (MockedStatic mockedEnv = Mockito.mockStatic(EnvironmentVariables.class)) { - mockedEnv.when(() -> EnvironmentVariables.getEnv("ASSIMBLY_ENCRYPTION_SECRET")).thenReturn("assimblyassimblyassimblyassimbly"); - } } @Test public void runTest() throws IOException { - byte[] nonce = new Random().randomBytes(CRYPTO_SECRETBOX_XSALSA20POLY1305_NONCEBYTES); - byte[] encrypted = processor.encrypt("Value 1", nonce); - String decrypted = processor.decrypt(encrypted, nonce); + String encrypted = processor.encrypt("Value 1"); + + String decrypted = processor.decrypt(encrypted); assertEquals("Value 1", decrypted); } diff --git a/tenantvariables/src/test/java/org/assimbly/tenantvariables/TenantVariablesTest.java b/tenantvariables/src/test/java/org/assimbly/tenantvariables/TenantVariablesTest.java index 40139307..f47de035 100644 --- a/tenantvariables/src/test/java/org/assimbly/tenantvariables/TenantVariablesTest.java +++ b/tenantvariables/src/test/java/org/assimbly/tenantvariables/TenantVariablesTest.java @@ -1,6 +1,5 @@ package org.assimbly.tenantvariables; -import org.abstractj.kalium.crypto.Random; import org.apache.camel.CamelExecutionException; import org.apache.camel.Exchange; import org.apache.camel.builder.RouteBuilder; @@ -10,7 +9,6 @@ import org.apache.camel.test.junit5.CamelTestSupport; import org.assimbly.util.exception.TenantVariableNotFoundException; -import org.assimbly.util.helper.Base64Helper; import org.junit.jupiter.api.*; import org.assimbly.tenantvariables.domain.EnvironmentValue; import org.assimbly.tenantvariables.domain.TenantVariable; @@ -22,7 +20,6 @@ import java.util.Optional; import static org.junit.jupiter.api.Assertions.*; -import static org.abstractj.kalium.NaCl.Sodium.CRYPTO_SECRETBOX_XSALSA20POLY1305_NONCEBYTES; public class TenantVariablesTest extends CamelTestSupport { @@ -457,13 +454,12 @@ private TenantVariable createVariable() { private TenantVariable createEncryptedVariable() { TenantVariable variable = new TenantVariable(ENCRYPTED_VARIABLE_NAME); - byte[] nonce = new Random().randomBytes(CRYPTO_SECRETBOX_XSALSA20POLY1305_NONCEBYTES); - String encryptedVariableValue = Base64Helper.marshal(PROCESSOR.encrypt(VARIABLE_VALUE, nonce)); + String encryptedVariableValue = PROCESSOR.encrypt(VARIABLE_VALUE); EnvironmentValue environmentValue = new EnvironmentValue("test"); environmentValue.setValue(encryptedVariableValue); - environmentValue.setNonce(Base64Helper.marshal(nonce)); + environmentValue.setNonce(null); environmentValue.setEncrypted(true); variable.put(environmentValue); diff --git a/xmltojsonlegacy/pom.xml b/xmltojsonlegacy/pom.xml index b124b97c..b26722d7 100644 --- a/xmltojsonlegacy/pom.xml +++ b/xmltojsonlegacy/pom.xml @@ -23,11 +23,6 @@ - - org.abstractj.kalium - kalium - 0.8.0 -