Skip to content

Commit 9c9b390

Browse files
authored
Improved Support of Storage Services on Containerized Environments (#521)
1 parent 400a24d commit 9c9b390

File tree

6 files changed

+109
-31
lines changed

6 files changed

+109
-31
lines changed

common/src/main/java/com/genexus/util/Encryption.java

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,40 @@ public static String decrypt16(String value, String key)
148148
return "";
149149
}
150150

151-
public static String decrypt64(String value){
152-
value= decrypt64(value, SpecificImplementation.Application.getModelContext().getServerKey());
153-
return value.substring(0, value.length()-CHECKSUM_LENGTH);
154-
}
151+
public static String decrypt64(String value){
152+
value = decrypt64(value, SpecificImplementation.Application.getModelContext().getServerKey());
153+
return value.substring(0, value.length()-CHECKSUM_LENGTH);
154+
}
155+
156+
/**
157+
* Returns decrpyted value if the checksum verification succedes. Otherwise, original value is returned
158+
* @param encryptedOrDecryptedValue
159+
* @return Decrypted Value
160+
*/
161+
public static String tryDecrypt64(String encryptedOrDecryptedValue) {
162+
return tryDecrypt64(encryptedOrDecryptedValue, SpecificImplementation.Application.getModelContext().getServerKey());
163+
}
164+
165+
public static String tryDecrypt64(String encryptedOrDecryptedValue, String key) {
166+
if (encryptedOrDecryptedValue == null) {
167+
return null;
168+
}
169+
170+
int checkSumLength = Encryption.getCheckSumLength();
171+
if (encryptedOrDecryptedValue.length() > checkSumLength) {
172+
String dec = Encryption.decrypt64(encryptedOrDecryptedValue, key);
173+
// Ojo, el = de aca es porque sino no me deja tener passwords vacias, dado que el length queda igual al length del checksum
174+
if (dec.length() >= checkSumLength) {
175+
String checksum = CommonUtil.right(dec, checkSumLength);
176+
String decryptedValue = CommonUtil.left(dec, dec.length() - checkSumLength);
177+
if (checksum.equals(Encryption.checksum(decryptedValue, Encryption.getCheckSumLength()))) {
178+
return decryptedValue;
179+
}
180+
}
181+
}
182+
return encryptedOrDecryptedValue;
183+
}
184+
155185

156186
public static String decrypt64(String value, String key)
157187
{
@@ -193,7 +223,9 @@ public static String decrypt64(String value, String key, boolean safeEncoding)
193223
return "";
194224
}
195225
}
196-
226+
227+
228+
197229
private static final int CHECKSUM_LENGTH = 6;
198230

199231
public static int getCheckSumLength()

common/src/main/java/com/genexus/util/GXService.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,13 @@ public class GXService
66
private String type;
77
private String className;
88
private boolean allowMultiple;
9+
private boolean allowOverrideWithEnvVarSettings;
910
private GXProperties properties;
10-
11+
12+
public GXService() {
13+
this.allowOverrideWithEnvVarSettings = true;
14+
}
15+
1116
public String getName()
1217
{
1318
return name;
@@ -42,12 +47,22 @@ public boolean getAllowMultiple()
4247
{
4348
return allowMultiple;
4449
}
45-
50+
4651
public void setAllowMultiple(boolean allowMultiple)
4752
{
4853
this.allowMultiple = allowMultiple;
4954
}
50-
55+
56+
public void setAllowOverrideWithEnvVarSettings(boolean allowOverride)
57+
{
58+
this.allowOverrideWithEnvVarSettings = allowOverride;
59+
}
60+
61+
public boolean getAllowOverrideWithEnvVarSettings()
62+
{
63+
return allowOverrideWithEnvVarSettings;
64+
}
65+
5166
public GXProperties getProperties()
5267
{
5368
return properties;

gxexternalproviders/src/main/java/com/genexus/db/driver/ExternalProviderBase.java

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ public ExternalProviderBase() {
2727
init();
2828
}
2929

30-
3130
public ExternalProviderBase(GXService s) {
3231
this.service = s;
3332
init();
@@ -51,16 +50,18 @@ public String getEncryptedPropertyValue(String propertyName, String alternativeP
5150
}
5251

5352
public String getEncryptedPropertyValue(String propertyName, String alternativePropertyName, String defaultValue) {
54-
String value = getPropertyValue(propertyName, alternativePropertyName, defaultValue);
55-
if (value != null && value.length() > 0) {
53+
String encryptedOrUnEncryptedValue = getPropertyValue(propertyName, alternativePropertyName, defaultValue);
54+
String decryptedValue = encryptedOrUnEncryptedValue;
55+
if (encryptedOrUnEncryptedValue != null && encryptedOrUnEncryptedValue.length() > 0) {
5656
try {
57-
value = Encryption.decrypt64(value);
57+
String decryptedTemp = Encryption.tryDecrypt64(encryptedOrUnEncryptedValue);
58+
decryptedValue = (decryptedTemp != null) ? decryptedTemp: encryptedOrUnEncryptedValue;
5859
}
5960
catch (Exception e) {
6061
logger.warn("Could not decrypt property name: " + resolvePropertyName(propertyName));
6162
}
6263
}
63-
return value;
64+
return decryptedValue;
6465
}
6566

6667
public String getPropertyValue(String propertyName, String alternativePropertyName) throws Exception{
@@ -74,20 +75,32 @@ public String getPropertyValue(String propertyName, String alternativePropertyNa
7475
}
7576

7677
public String getPropertyValue(String propertyName, String alternativePropertyName, String defaultValue) {
77-
propertyName = resolvePropertyName(propertyName);
78-
String value = System.getenv(propertyName);
79-
if (value == null || value.length() == 0){
80-
value = System.getenv(alternativePropertyName);
78+
String value = readFromEnvVars(propertyName, alternativePropertyName);
79+
if (value != null) {
80+
return value;
8181
}
82-
if (this.service != null) {
83-
value = this.service.getProperties().get(propertyName);
82+
String resolvedPtyName = resolvePropertyName(propertyName);
83+
if (service != null) {
84+
value = this.service.getProperties().get(resolvedPtyName);
8485
if (value == null || value.length() == 0) {
8586
value = this.service.getProperties().get(alternativePropertyName);
8687
}
8788
}
8889
return value != null? value: defaultValue;
8990
}
9091

92+
private String readFromEnvVars(String propertyName, String alternativePropertyName) {
93+
if (service != null && !service.getAllowOverrideWithEnvVarSettings()){
94+
return null;
95+
}
96+
97+
String value = System.getenv(resolvePropertyName(propertyName));
98+
if (value == null){
99+
value = System.getenv(alternativePropertyName);
100+
}
101+
return value;
102+
}
103+
91104
private String resolvePropertyName(String propertyName) {
92105
return String.format("STORAGE_%s_%s", getName(), propertyName);
93106
}

gxexternalproviders/src/main/java/com/genexus/db/driver/ExternalProviderS3.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.genexus.db.driver;
22

3+
import com.amazonaws.auth.*;
34
import com.amazonaws.client.builder.AwsClientBuilder;
45
import com.amazonaws.services.s3.model.*;
56
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
@@ -14,9 +15,7 @@
1415
import java.io.File;
1516
import java.io.InputStream;
1617
import java.io.ByteArrayInputStream;
17-
import com.amazonaws.auth.AWSCredentials;
18-
import com.amazonaws.auth.BasicAWSCredentials;
19-
import com.amazonaws.auth.AWSStaticCredentialsProvider;
18+
2019
import com.amazonaws.services.s3.AmazonS3;
2120
import com.amazonaws.services.s3.AmazonS3Client;
2221
import com.amazonaws.util.IOUtils;
@@ -41,6 +40,7 @@ public class ExternalProviderS3 extends ExternalProviderBase implements External
4140
static final String STORAGE_ENDPOINT = "ENDPOINT";
4241
static final String BUCKET = "BUCKET_NAME";
4342
static final String REGION = "REGION";
43+
static final String USE_IAM = "USE_IAM";
4444

4545
//Keep it for compatibility reasons
4646
@Deprecated
@@ -90,8 +90,8 @@ public ExternalProviderS3(GXService providerService) throws Exception{
9090
}
9191

9292
private void initialize() throws Exception{
93-
String accessKey = getEncryptedPropertyValue(ACCESS_KEY, ACCESS_KEY_ID_DEPRECATED);
94-
String secretKey = getEncryptedPropertyValue(SECRET_ACCESS_KEY, SECRET_ACCESS_KEY_DEPRECATED);
93+
String accessKey = getEncryptedPropertyValue(ACCESS_KEY, ACCESS_KEY_ID_DEPRECATED, "");
94+
String secretKey = getEncryptedPropertyValue(SECRET_ACCESS_KEY, SECRET_ACCESS_KEY_DEPRECATED, "");
9595
String bucket = getEncryptedPropertyValue(BUCKET, BUCKET_DEPRECATED);
9696
String folder = getPropertyValue(FOLDER, FOLDER_DEPRECATED, "");
9797
String region = getPropertyValue(REGION, REGION_DEPRECATED, DEFAULT_REGION);
@@ -109,19 +109,28 @@ private void initialize() throws Exception{
109109
if (region.length() == 0) {
110110
region = DEFAULT_REGION;
111111
}
112+
112113
this.bucket = bucket;
113114
this.folder = folder;
114-
this.client = buildS3Client(accessKey, secretKey, endpointValue, region);
115115

116+
this.client = buildS3Client(accessKey, secretKey, endpointValue, region);
116117
bucketExists();
117118
ensureFolder(folder);
118119
}
119120
}
120121

121122
private AmazonS3 buildS3Client(String accessKey, String secretKey, String endpoint, String region) {
122123
AmazonS3 s3Client;
123-
AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
124-
AmazonS3ClientBuilder builder = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(credentials));
124+
125+
boolean bUseIAM = !getPropertyValue(USE_IAM, "", "").isEmpty() || (accessKey.equals("") && secretKey.equals(""));
126+
127+
AmazonS3ClientBuilder builder = bUseIAM ?
128+
AmazonS3ClientBuilder.standard():
129+
AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey)));
130+
131+
if (bUseIAM) {
132+
logger.debug("Using IAM Credentials");
133+
}
125134

126135
if (endpoint.length() > 0 && !endpoint.contains(".amazonaws.com")) {
127136
pathStyleUrls = true;

java/src/main/java/com/genexus/Application.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,15 +177,23 @@ private static ExternalProvider getExternalProviderImpl(String service)
177177
GXService providerService = getGXServices().get(service);
178178
if (providerService != null)
179179
{
180+
Class providerClass;
180181
try
181182
{
182-
Class providerClass = Class.forName(providerService.getClassName());
183+
providerClass = Class.forName(providerService.getClassName());
184+
}
185+
catch (ClassNotFoundException e)
186+
{
187+
logger.fatal("Unrecognized External Provider class (ClassNotFound) : " + providerService.getName() + " / " + providerService.getClassName(), e);
188+
throw new InternalError("Unrecognized External Provider class (ClassNotFound) : " + providerService.getName() + " / " + providerService.getClassName());
189+
}
190+
try {
183191
externalProviderImpl = (ExternalProvider) providerClass.getConstructor(String.class).newInstance(service);
184192
}
185193
catch (Exception e)
186194
{
187-
logger.error("Unrecognized External Provider class : " + providerService.getName() + " / " + providerService.getClassName(), e);
188-
throw new InternalError("Unrecognized External Provider class : " + providerService.getName() + " / " + providerService.getClassName());
195+
logger.fatal("Unable to Initialize External Provider Class: " + providerService.getClassName(), e);
196+
throw new InternalError("Unable to Initialize External Provider Class: " + providerService.getClassName(), e);
189197
}
190198
}
191199
return externalProviderImpl;

java/src/main/java/com/genexus/configuration/ExternalStorage.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public boolean create(String name, GXProperties properties, GXStorageProvider[]
3737

3838
if (isNullOrEmpty(name))
3939
{
40-
GXutil.ErrorToMessages("Unsopported", "Provider cannot be empty", messages[0]);
40+
GXutil.ErrorToMessages("Unsupported", "Provider name cannot be empty", messages[0]);
4141
return false;
4242
}
4343

@@ -49,6 +49,7 @@ public boolean create(String name, GXProperties properties, GXStorageProvider[]
4949
providerService.setType(GXServices.STORAGE_SERVICE);
5050
providerService.setName(name);
5151
providerService.setAllowMultiple(false);
52+
providerService.setAllowOverrideWithEnvVarSettings(false);
5253
providerService.setProperties(new GXProperties());
5354
}
5455

0 commit comments

Comments
 (0)