Skip to content

Commit 1c9942c

Browse files
authoredFeb 11, 2025
JAVA-5781 Add env var override (#1623)
JAVA-5781
1 parent e4e5e69 commit 1c9942c

File tree

4 files changed

+55
-26
lines changed

4 files changed

+55
-26
lines changed
 

‎driver-core/src/main/com/mongodb/internal/connection/ClientMetadataHelper.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ private enum Orchestrator {
224224
K8S("kubernetes") {
225225
@Override
226226
boolean isCurrentOrchestrator() {
227-
return System.getenv("KUBERNETES_SERVICE_HOST") != null;
227+
return FaasEnvironment.getEnv("KUBERNETES_SERVICE_HOST") != null;
228228
}
229229
},
230230
UNKNOWN(null);

‎driver-core/src/main/com/mongodb/internal/connection/FaasEnvironment.java

+21-9
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020

2121
import java.util.ArrayList;
2222
import java.util.Arrays;
23+
import java.util.HashMap;
2324
import java.util.List;
25+
import java.util.Map;
2426

2527
enum FaasEnvironment {
2628
AWS_LAMBDA("aws.lambda"),
@@ -29,21 +31,23 @@ enum FaasEnvironment {
2931
VERCEL("vercel"),
3032
UNKNOWN(null);
3133

34+
static final Map<String, String> ENV_OVERRIDES_FOR_TESTING = new HashMap<>();
35+
3236
static FaasEnvironment getFaasEnvironment() {
3337
List<FaasEnvironment> result = new ArrayList<>();
34-
String awsExecutionEnv = System.getenv("AWS_EXECUTION_ENV");
38+
String awsExecutionEnv = getEnv("AWS_EXECUTION_ENV");
3539

36-
if (System.getenv("VERCEL") != null) {
40+
if (getEnv("VERCEL") != null) {
3741
result.add(FaasEnvironment.VERCEL);
3842
}
3943
if ((awsExecutionEnv != null && awsExecutionEnv.startsWith("AWS_Lambda_"))
40-
|| System.getenv("AWS_LAMBDA_RUNTIME_API") != null) {
44+
|| getEnv("AWS_LAMBDA_RUNTIME_API") != null) {
4145
result.add(FaasEnvironment.AWS_LAMBDA);
4246
}
43-
if (System.getenv("FUNCTIONS_WORKER_RUNTIME") != null) {
47+
if (getEnv("FUNCTIONS_WORKER_RUNTIME") != null) {
4448
result.add(FaasEnvironment.AZURE_FUNC);
4549
}
46-
if (System.getenv("K_SERVICE") != null || System.getenv("FUNCTION_NAME") != null) {
50+
if (getEnv("K_SERVICE") != null || getEnv("FUNCTION_NAME") != null) {
4751
result.add(FaasEnvironment.GCP_FUNC);
4852
}
4953
// vercel takes precedence over aws.lambda
@@ -56,6 +60,14 @@ static FaasEnvironment getFaasEnvironment() {
5660
return result.get(0);
5761
}
5862

63+
@Nullable
64+
public static String getEnv(final String key) {
65+
if (ENV_OVERRIDES_FOR_TESTING.containsKey(key)) {
66+
return ENV_OVERRIDES_FOR_TESTING.get(key);
67+
}
68+
return System.getenv(key);
69+
}
70+
5971
@Nullable
6072
private final String name;
6173

@@ -95,11 +107,11 @@ public Integer getMemoryMb() {
95107
public String getRegion() {
96108
switch (this) {
97109
case AWS_LAMBDA:
98-
return System.getenv("AWS_REGION");
110+
return getEnv("AWS_REGION");
99111
case GCP_FUNC:
100-
return System.getenv("FUNCTION_REGION");
112+
return getEnv("FUNCTION_REGION");
101113
case VERCEL:
102-
return System.getenv("VERCEL_REGION");
114+
return getEnv("VERCEL_REGION");
103115
default:
104116
return null;
105117
}
@@ -108,7 +120,7 @@ public String getRegion() {
108120
@Nullable
109121
private static Integer getEnvInteger(final String name) {
110122
try {
111-
String value = System.getenv(name);
123+
String value = getEnv(name);
112124
return Integer.parseInt(value);
113125
} catch (NumberFormatException e) {
114126
return null;

‎driver-core/src/test/functional/com/mongodb/client/WithWrapper.java

+2-16
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,11 @@
1616

1717
package com.mongodb.client;
1818

19+
import com.mongodb.internal.connection.FaasEnvironmentAccessor;
1920
import com.mongodb.lang.Nullable;
2021

21-
import java.lang.reflect.Field;
2222
import java.util.Map;
2323

24-
import static java.lang.System.getenv;
25-
2624
@FunctionalInterface
2725
public interface WithWrapper {
2826

@@ -34,7 +32,7 @@ static WithWrapper withWrapper() {
3432

3533
default WithWrapper withEnvironmentVariable(final String name, @Nullable final String value) {
3634
return runnable -> {
37-
Map<String, String> innerMap = getEnvInnerMap();
35+
Map<String, String> innerMap = FaasEnvironmentAccessor.getFaasEnvMap();
3836
String original = innerMap.get(name);
3937
if (value == null) {
4038
innerMap.remove(name);
@@ -65,16 +63,4 @@ default WithWrapper withSystemProperty(final String name, final String value) {
6563
};
6664
}
6765

68-
static Map<String, String> getEnvInnerMap() {
69-
try {
70-
Map<String, String> env = getenv();
71-
Field field = env.getClass().getDeclaredField("m");
72-
field.setAccessible(true);
73-
@SuppressWarnings("unchecked")
74-
Map<String, String> result = (Map<String, String>) field.get(env);
75-
return result;
76-
} catch (IllegalAccessException | NoSuchFieldException e) {
77-
throw new RuntimeException(e);
78-
}
79-
}
8066
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.mongodb.internal.connection;
18+
19+
import java.util.Map;
20+
21+
/**
22+
* In the same package as FaasEnvironment, to access package-private
23+
*/
24+
public final class FaasEnvironmentAccessor {
25+
private FaasEnvironmentAccessor() {
26+
}
27+
28+
public static Map<String, String> getFaasEnvMap() {
29+
return FaasEnvironment.ENV_OVERRIDES_FOR_TESTING;
30+
}
31+
}

0 commit comments

Comments
 (0)
Failed to load comments.