Skip to content

Commit

Permalink
CAMEL-21276: camel-core: Lookup ENV variable should use IOHelper to b…
Browse files Browse the repository at this point in the history
…e consistent, and also support camelCase style to have more human readable keys and still match ENV variables that are UPPER_CASE with underscores only. (apache#15744)

(cherry picked from commit 171a84b)
  • Loading branch information
davsclaus authored and christophd committed Oct 11, 2024
1 parent 395f116 commit d550864
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import org.apache.camel.support.MessageHelper;
import org.apache.camel.support.service.ServiceHelper;
import org.apache.camel.support.service.ServiceSupport;
import org.apache.camel.util.IOHelper;
import org.apache.camel.util.StopWatch;
import org.apache.camel.util.json.JsonObject;
import org.slf4j.Logger;
Expand Down Expand Up @@ -258,7 +259,7 @@ public void detach() {
* the value of the system property {@link #SUSPEND_MODE_SYSTEM_PROP_NAME}, {@code false} by default.
*/
private static boolean resolveSuspendMode() {
final String value = System.getenv(SUSPEND_MODE_ENV_VAR_NAME);
final String value = IOHelper.lookupEnvironmentVariable(SUSPEND_MODE_ENV_VAR_NAME);
return value == null ? Boolean.getBoolean(SUSPEND_MODE_SYSTEM_PROP_NAME) : Boolean.parseBoolean(value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.camel.component.properties;

import org.apache.camel.spi.PropertiesFunction;
import org.apache.camel.util.IOHelper;
import org.apache.camel.util.StringHelper;

/**
Expand All @@ -39,15 +40,7 @@ public String apply(String remainder) {
defaultValue = StringHelper.after(remainder, ":");
}

// lookup OS environment variable using upper case key
key = key.toUpperCase();

String value = System.getenv(key);
// some OS do not support dashes in keys, so replace with underscore
if (value == null) {
String noDashKey = key.replace('-', '_');
value = System.getenv(noDashKey);
}
String value = IOHelper.lookupEnvironmentVariable(key);
return value != null ? value : defaultValue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@
*/
package org.apache.camel.component.properties;

import java.util.Locale;

import org.apache.camel.spi.PropertiesFunction;
import org.apache.camel.util.IOHelper;
import org.apache.camel.util.StringHelper;

/**
Expand Down Expand Up @@ -57,12 +56,8 @@ public String apply(String remainder) {

// make sure to use upper case
if (key != null) {
// make sure to use underscore as dash is not supported as ENV variables
key = key.toUpperCase(Locale.ENGLISH).replace('-', '_');

// a service should have both the host and port defined
String host = System.getenv(key + HOST_PREFIX);

String host = IOHelper.lookupEnvironmentVariable(key + HOST_PREFIX);
if (host != null) {
return host;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@
*/
package org.apache.camel.component.properties;

import java.util.Locale;

import org.apache.camel.spi.PropertiesFunction;
import org.apache.camel.util.IOHelper;
import org.apache.camel.util.StringHelper;

/**
Expand Down Expand Up @@ -57,12 +56,8 @@ public String apply(String remainder) {

// make sure to use upper case
if (key != null) {
// make sure to use underscore as dash is not supported as ENV variables
key = key.toUpperCase(Locale.ENGLISH).replace('-', '_');

// a service should have both the host and port defined
String port = System.getenv(key + PORT_PREFIX);

String port = IOHelper.lookupEnvironmentVariable(key + PORT_PREFIX);
if (port != null) {
return port;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Locale;

import org.apache.camel.spi.PropertiesFunction;
import org.apache.camel.util.IOHelper;
import org.apache.camel.util.StringHelper;

/**
Expand Down Expand Up @@ -57,8 +58,8 @@ public String apply(String remainder) {
key = key.toUpperCase(Locale.ENGLISH).replace('-', '_');

// a service should have both the host and port defined
String host = System.getenv(key + HOST_PREFIX);
String port = System.getenv(key + PORT_PREFIX);
String host = IOHelper.lookupEnvironmentVariable(key + HOST_PREFIX);
String port = IOHelper.lookupEnvironmentVariable(key + PORT_PREFIX);

if (host != null && port != null) {
return host + ":" + port;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public static String toEnvVar(String name) {
public static Optional<String> lookupPropertyFromSysOrEnv(String name) {
String answer = System.getProperty(name);
if (answer == null) {
answer = System.getenv(toEnvVar(name));
answer = IOHelper.lookupEnvironmentVariable(name);
}

return Optional.ofNullable(answer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.apache.camel.RuntimeCamelException;
import org.apache.camel.spi.ExchangeFormatter;
import org.apache.camel.support.processor.DefaultExchangeFormatter;
import org.apache.camel.util.IOHelper;
import org.apache.camel.util.TimeUtils;

public final class LanguageHelper {
Expand Down Expand Up @@ -169,18 +170,7 @@ public static ExchangeFormatter getOrCreateExchangeFormatter(
* @return the environment variable value
*/
public static String sysenv(String name) {
String answer = null;
if (name != null) {
// lookup OS env with upper case key
name = name.toUpperCase();
answer = System.getenv(name);
// some OS do not support dashes in keys, so replace with underscore
if (answer == null) {
String noDashKey = name.replace('-', '_');
answer = System.getenv(noDashKey);
}
}
return answer;
return IOHelper.lookupEnvironmentVariable(name);
}

public static String escapeQuotes(String text) {
Expand Down
1 change: 1 addition & 0 deletions core/camel-util/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@
<environmentVariables>
<FOO_SERVICE_HOST>myserver</FOO_SERVICE_HOST>
<FOO_SERVICE_PORT>8081</FOO_SERVICE_PORT>
<CAMEL_KAMELET_AWS_S3_SOURCE_BUCKET_NAME_OR_ARN>mys3arn</CAMEL_KAMELET_AWS_S3_SOURCE_BUCKET_NAME_OR_ARN>
</environmentVariables>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ public static String resolvePath(String path) throws IllegalArgumentException {
int pos = fun.indexOf("${env:");
if (pos != -1) {
String key = fun.substring(pos + 6);
String value = System.getenv(key);
String value = IOHelper.lookupEnvironmentVariable(key);
if (value != null) {
path = path.replace("${env:" + key + "}", value);
}
}
pos = fun.indexOf("${env.");
if (pos != -1) {
String key = fun.substring(pos + 6);
String value = System.getenv(key);
String value = IOHelper.lookupEnvironmentVariable(key);
if (value != null) {
path = path.replace("${env." + key + "}", value);
}
Expand Down
22 changes: 22 additions & 0 deletions core/camel-util/src/main/java/org/apache/camel/util/IOHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,15 @@ public static String normalizeCharset(String charset) {

/**
* Lookup the OS environment variable in a safe manner by using upper case keys and underscore instead of dash.
*
* At first lookup attempt is made without considering camelCase keys. The second lookup is converting camelCase to
* underscores.
*
* For example given an ENV variable in either format: - CAMEL_KAMELET_AWS_S3_SOURCE_BUCKETNAMEORARN=myArn -
* CAMEL_KAMELET_AWS_S3_SOURCE_BUCKET_NAME_OR_ARN=myArn
*
* Then the following keys can lookup both ENV formats above: - camel.kamelet.awsS3Source.bucketNameOrArn -
* camel.kamelet.aws-s3-source.bucketNameOrArn - camel.kamelet.aws-s3-source.bucket-name-or-arn
*/
public static String lookupEnvironmentVariable(String key) {
// lookup OS env with upper case key
Expand All @@ -683,6 +692,19 @@ public static String lookupEnvironmentVariable(String key) {

value = System.getenv(normalizedKey);
}
if (value == null) {
// camelCase keys should use underscore as separator
String caseKey = StringHelper.camelCaseToDash(key);
caseKey = caseKey.toUpperCase();
// some OS do not support dashes in keys, so replace with underscore
String normalizedKey = caseKey.replace('-', '_');

// and replace dots with underscores so keys like my.key are
// translated to MY_KEY
normalizedKey = normalizedKey.replace('.', '_');

value = System.getenv(normalizedKey);
}
return value;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public void testLookupEnvironmentVariable() {
assertEquals("8081", IOHelper.lookupEnvironmentVariable("foo-service.port"));
assertEquals("8081", IOHelper.lookupEnvironmentVariable("foo-service-port"));
assertEquals("8081", IOHelper.lookupEnvironmentVariable("foo.service.port"));

assertEquals("mys3arn", IOHelper.lookupEnvironmentVariable("camel.kamelet.aws-s3-source.bucketNameOrArn"));
assertEquals("mys3arn", IOHelper.lookupEnvironmentVariable("camel.kamelet.aws-s3-source.bucket-name-or-arn"));
assertEquals("mys3arn", IOHelper.lookupEnvironmentVariable("camel.kamelet.awsS3Source.bucketNameOrArn"));
}

@Test
Expand Down

0 comments on commit d550864

Please sign in to comment.