Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions .evergreen/.evg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1740,6 +1740,12 @@ axes:
display_name: "8.0"
variables:
VERSION: "8.0"
# 8.2 is used solely for Windows testing. MongoDB 8.0 binaries are affected by SERVER-116018 on Windows,
# and the fix is only available starting from 8.2.
- id: "8.2"
display_name: "8.2"
variables:
VERSION: "8.2"
Comment thread
vbabanin marked this conversation as resolved.
- id: "7.0"
display_name: "7.0"
variables:
Expand Down Expand Up @@ -1770,6 +1776,9 @@ axes:
- id: "ubuntu"
display_name: "Ubuntu"
run_on: "ubuntu2004-small"
- id: "windows"
display_name: "Windows"
run_on: "windows-2022-latest-small"

- id: "topology"
display_name: "Topology"
Expand Down Expand Up @@ -2332,7 +2341,7 @@ buildvariants:
#

- matrix_name: "tests-zlib-compression"
matrix_spec: { compressor: "zlib", auth: "noauth", ssl: "nossl", jdk: "jdk8", version: "*", topology: "standalone", os: "linux" }
matrix_spec: { compressor: "zlib", auth: "noauth", ssl: "nossl", jdk: "jdk8", version: [ "4.2", "4.4", "5.0", "6.0", "7.0", "8.0", "latest" ], topology: "standalone", os: "linux" }
display_name: "${version} ${compressor} ${topology} ${auth} ${ssl} ${jdk} ${os} "
tags: [ "tests-variant" ]
tasks:
Expand All @@ -2342,7 +2351,7 @@ buildvariants:
- name: "test-legacy-task"

- matrix_name: "tests-snappy-compression"
matrix_spec: { compressor: "snappy", auth: "noauth", ssl: "nossl", jdk: "jdk8", version: "*", topology: "standalone", os: "linux" }
matrix_spec: { compressor: "snappy", auth: "noauth", ssl: "nossl", jdk: "jdk8", version: [ "4.2", "4.4", "5.0", "6.0", "7.0", "8.0", "latest" ], topology: "standalone", os: "linux" }
display_name: "${version} ${compressor} ${topology} ${auth} ${ssl} ${jdk} ${os} "
tags: [ "tests-variant" ]
tasks:
Expand Down Expand Up @@ -2403,6 +2412,22 @@ buildvariants:
- name: "test-core-task"
- name: "test-legacy-task"

- matrix_name: "tests-jdk-secure-windows"
matrix_spec: { auth: "auth", ssl: "ssl", jdk: "jdk17",
version: [ "8.2" ],
topology: "*", os: "windows" }
display_name: "${os}: ${version} ${topology} ${auth} ${ssl} ${jdk}"
tags: [ "tests-variant" ]
tasks:
- name: "test-sync-task"
exec_timeout_secs: 7200
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests run longer on Windows ~1h 30min - 1h 40 min, so it is bumped to 2 hours.

- name: "test-reactive-task"
exec_timeout_secs: 7200
- name: "test-core-task"
exec_timeout_secs: 7200
- name: "test-legacy-task"
exec_timeout_secs: 7200

- matrix_name: "tests-require-api-version"
matrix_spec: { api-version: "required", auth: "auth", ssl: "nossl", jdk: [ "jdk21" ], version: [ "5.0", "6.0", "7.0", "8.0", "latest" ],
topology: "standalone", os: "linux" }
Expand Down Expand Up @@ -2540,6 +2565,15 @@ buildvariants:
tasks:
- name: "csfle-tests-with-mongocryptd-task"

- matrix_name: "csfle-tests-with-mongocryptd-windows"
matrix_spec: { os: "windows",
version: [ "8.0", "latest" ],
topology: [ "replicaset" ] }
Comment thread
vbabanin marked this conversation as resolved.
display_name: "${os} CSFLE with mongocryptd: ${version}"
tasks:
- name: "csfle-tests-with-mongocryptd-task"
exec_timeout_secs: 7200

- matrix_name: "socks5-tests"
matrix_spec: { os: "linux", ssl: [ "nossl", "ssl" ], version: [ "latest" ], topology: [ "replicaset" ], socks-auth: [ "auth", "noauth" ] }
display_name: "SOCKS5 proxy ${socks-auth} : ${version} ${topology} ${ssl} ${jdk} ${os}"
Expand Down
10 changes: 9 additions & 1 deletion .evergreen/run-csfle-tests-with-mongocryptd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,16 @@ provision_ssl () {
cp ${JAVA_HOME}/lib/security/cacerts mongo-truststore
${JAVA_HOME}/bin/keytool -importcert -trustcacerts -file ${DRIVERS_TOOLS}/.evergreen/x509gen/ca.pem -keystore mongo-truststore -storepass changeit -storetype JKS -noprompt

# Use native paths on Windows (cygwin paths like /cygdrive/c/... are not understood by the JDK)
local CURRENT_DIR
if [ "Windows_NT" == "$OS" ]; then
CURRENT_DIR=$(cygpath -m "$(pwd)")
else
CURRENT_DIR=$(pwd)
fi

# We add extra gradle arguments for SSL
export GRADLE_EXTRA_VARS="-Pssl.enabled=true -Pssl.keyStoreType=pkcs12 -Pssl.keyStore=`pwd`/client.pkc -Pssl.keyStorePassword=bithere -Pssl.trustStoreType=jks -Pssl.trustStore=`pwd`/mongo-truststore -Pssl.trustStorePassword=changeit"
export GRADLE_EXTRA_VARS="-Pssl.enabled=true -Pssl.keyStoreType=pkcs12 -Pssl.keyStore=${CURRENT_DIR}/client.pkc -Pssl.keyStorePassword=bithere -Pssl.trustStoreType=jks -Pssl.trustStore=${CURRENT_DIR}/mongo-truststore -Pssl.trustStorePassword=changeit"
}

############################################
Expand Down
11 changes: 10 additions & 1 deletion .evergreen/run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,16 @@ provision_ssl () {
cp ${JAVA_HOME}/lib/security/cacerts mongo-truststore
${JAVA_HOME}/bin/keytool -importcert -trustcacerts -file ${DRIVERS_TOOLS}/.evergreen/x509gen/ca.pem -keystore mongo-truststore -storepass changeit -storetype JKS -noprompt

# Use native paths on Windows (cygwin paths like /cygdrive/c/... are not understood by the JDK)
local CURRENT_DIR
if [ "Windows_NT" == "$OS" ]; then
CURRENT_DIR=$(cygpath -m "$(pwd)")
else
CURRENT_DIR=$(pwd)
fi

# We add extra gradle arguments for SSL
export GRADLE_EXTRA_VARS="-Pssl.enabled=true -Pssl.keyStoreType=pkcs12 -Pssl.keyStore=`pwd`/client.pkc -Pssl.keyStorePassword=bithere -Pssl.trustStoreType=jks -Pssl.trustStore=`pwd`/mongo-truststore -Pssl.trustStorePassword=changeit"
export GRADLE_EXTRA_VARS="-Pssl.enabled=true -Pssl.keyStoreType=pkcs12 -Pssl.keyStore=${CURRENT_DIR}/client.pkc -Pssl.keyStorePassword=bithere -Pssl.trustStoreType=jks -Pssl.trustStore=${CURRENT_DIR}/mongo-truststore -Pssl.trustStorePassword=changeit"
}

provision_multi_mongos_uri_for_ssl () {
Expand Down Expand Up @@ -124,6 +132,7 @@ if [ ! -z "$REQUIRE_API_VERSION" ]; then
export API_VERSION="-Dorg.mongodb.test.api.version=1"
fi


echo "Running $AUTH tests over $SSL for $TOPOLOGY and connecting to $MONGODB_URI"

echo "Running tests with Java ${JAVA_VERSION}"
Expand Down
15 changes: 11 additions & 4 deletions .evergreen/setup-env.bash
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
# Java configurations for evergreen

export JDK8="/opt/java/jdk8"
export JDK11="/opt/java/jdk11"
export JDK17="/opt/java/jdk17"
export JDK21="/opt/java/jdk21"
if [ "Windows_NT" == "$OS" ]; then
export JDK8="/cygdrive/c/java/jdk8"
export JDK11="/cygdrive/c/java/jdk11"
export JDK17="/cygdrive/c/java/jdk17"
export JDK21="/cygdrive/c/java/jdk21"
else
export JDK8="/opt/java/jdk8"
export JDK11="/opt/java/jdk11"
export JDK17="/opt/java/jdk17"
export JDK21="/opt/java/jdk21"
fi
# note that `JDK21_GRAALVM` is used in `run-graalvm-native-image-app.sh`
# by dynamically constructing the variable name
export JDK21_GRAALVM="/opt/java/jdk21-graalce"
Comment thread
vbabanin marked this conversation as resolved.
Expand Down
3 changes: 2 additions & 1 deletion bson/src/test/unit/util/JsonPoweredTestHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ public static List<BsonDocument> getTestDocuments(final String resourcePath) {
public FileVisitResult visitFile(final Path filePath, final BasicFileAttributes attrs) throws IOException {
if (filePath.toString().endsWith(".json")) {
if (fileSystem == null) {
files.add(getTestDocumentWithMetaData(filePath.toString().substring(filePath.toString().lastIndexOf(resourcePath))));
String filePathStr = filePath.toString().replace('\\', '/');
files.add(getTestDocumentWithMetaData(filePathStr.substring(filePathStr.lastIndexOf(resourcePath))));
} else {
files.add(getTestDocumentWithMetaData(filePath.toString()));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright 2008-present MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.mongodb.internal;

import com.mongodb.lang.Nullable;

import java.util.HashMap;
import java.util.Map;
import java.util.function.UnaryOperator;

/**
* Centralized access to environment variables. All production code should use
* this class instead of calling {@link System#getenv(String)} directly.
Comment thread
vbabanin marked this conversation as resolved.
*
* <p>Tests can override values via {@link #envOverride()}.</p>
*/
public final class EnvironmentProvider {
Copy link
Copy Markdown
Member Author

@vbabanin vbabanin May 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this PR introduces EnvironmentProvider (and consolidates env handling)

We previously used a reflection-based test helper in AbstractMucromiterProseTests to override environment variables. That approach broke on Windows due to differences in the JDK’s ProcessEnvironment implementation.

What worked on Linux/macOS

ProcessEnvironment effectively exposes a single backing map. The reflection helper mutates that map, and System.getenv() / System.getenv("KEY") read from the same data, thus overrides are visible.

What breaks on Windows

The JDK maintains two maps:

  • theEnvironment (HashMap): backs System.getenv() (returns the full map)
  • theCaseInsensitiveEnvironment (TreeMap, without case-insensitive comparator): backs System.getenv(String key) (single-key lookup)

The reflection helper only updated theEnvironment. Our production code uses System.getenv("OBSERVABILITY_MONGODB_ENABLED"), which reads from theCaseInsensitiveEnvironment, so the override was invisible on Windows, thus Micrometer tests failed.

What this PR changes

Introduces EnvironmentProvider to avoid reflection/JDK internals entirely. Production code calls EnvironmentProvider.getEnv() instead of System.getenv().

Tests override the provider via envOverride(...) (swapping the underlying supplier/lambda), which behaves consistently across platforms.

EnviromentOverride is AutoCloseable, so tests can use try-with-resources and avoid boilerplate while keeping overrides scoped and explicit.

private static UnaryOperator<String> envLookup = System::getenv;

private EnvironmentProvider() {
}

@Nullable
public static String getEnv(final String key) {
return envLookup.apply(key);
}

/** Exists only for testing **/
public static EnvironmentOverride envOverride() {
return new EnvironmentOverride();
}

public static final class EnvironmentOverride implements AutoCloseable {
private final Map<String, String> overrides = new HashMap<>();
private final UnaryOperator<String> original;

private EnvironmentOverride() {
original = envLookup;
envLookup = key -> overrides.containsKey(key)
? overrides.get(key)
: original.apply(key);
}

public EnvironmentOverride set(final String key, @Nullable final String value) {
overrides.put(key, value);
return this;
}

@Override
public void close() {
envLookup = original;
}
Comment thread
vbabanin marked this conversation as resolved.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.mongodb.internal.authentication;

import com.mongodb.AwsCredential;
import com.mongodb.internal.EnvironmentProvider;
import org.bson.BsonDocument;

import java.util.HashMap;
Expand All @@ -29,7 +30,7 @@ class BuiltInAwsCredentialSupplier implements Supplier<AwsCredential> {

@Override
public AwsCredential get() {
if (System.getenv("AWS_ACCESS_KEY_ID") != null) {
if (EnvironmentProvider.getEnv("AWS_ACCESS_KEY_ID") != null) {
return obtainFromEnvironmentVariables();
} else {
return obtainFromEc2OrEcsResponse();
Expand All @@ -38,13 +39,13 @@ public AwsCredential get() {

private static AwsCredential obtainFromEnvironmentVariables() {
return new AwsCredential(
System.getenv("AWS_ACCESS_KEY_ID"),
System.getenv("AWS_SECRET_ACCESS_KEY"),
System.getenv("AWS_SESSION_TOKEN"));
EnvironmentProvider.getEnv("AWS_ACCESS_KEY_ID"),
EnvironmentProvider.getEnv("AWS_SECRET_ACCESS_KEY"),
EnvironmentProvider.getEnv("AWS_SESSION_TOKEN"));
}

private static AwsCredential obtainFromEc2OrEcsResponse() {
String path = System.getenv("AWS_CONTAINER_CREDENTIALS_RELATIVE_URI");
String path = EnvironmentProvider.getEnv("AWS_CONTAINER_CREDENTIALS_RELATIVE_URI");
BsonDocument ec2OrEcsResponse = path == null ? BsonDocument.parse(getEc2Response()) : BsonDocument.parse(getEcsResponse(path));

return new AwsCredential(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@

package com.mongodb.internal.connection;

import com.mongodb.internal.EnvironmentProvider;
import com.mongodb.lang.Nullable;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

enum FaasEnvironment {
AWS_LAMBDA("aws.lambda"),
Expand All @@ -31,8 +30,6 @@ enum FaasEnvironment {
VERCEL("vercel"),
UNKNOWN(null);

static final Map<String, String> ENV_OVERRIDES_FOR_TESTING = new HashMap<>();

static FaasEnvironment getFaasEnvironment() {
List<FaasEnvironment> result = new ArrayList<>();
String awsExecutionEnv = getEnv("AWS_EXECUTION_ENV");
Expand Down Expand Up @@ -62,10 +59,7 @@ static FaasEnvironment getFaasEnvironment() {

@Nullable
public static String getEnv(final String key) {
if (ENV_OVERRIDES_FOR_TESTING.containsKey(key)) {
return ENV_OVERRIDES_FOR_TESTING.get(key);
}
return System.getenv(key);
return EnvironmentProvider.getEnv(key);
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.mongodb.internal.connection;

import com.mongodb.AuthenticationMechanism;
import com.mongodb.internal.EnvironmentProvider;
import com.mongodb.MongoClientException;
import com.mongodb.MongoCommandException;
import com.mongodb.MongoConfigurationException;
Expand Down Expand Up @@ -235,8 +236,8 @@ private static OidcCallback getTestCallback() {
@VisibleForTesting(otherwise = VisibleForTesting.AccessModifier.PRIVATE)
static OidcCallback getK8sCallback() {
return (context) -> {
String azure = System.getenv(K8S_AZURE_FILE);
String aws = System.getenv(K8S_AWS_FILE);
String azure = EnvironmentProvider.getEnv(K8S_AZURE_FILE);
String aws = EnvironmentProvider.getEnv(K8S_AWS_FILE);
String path;
if (azure != null) {
path = azure;
Expand Down Expand Up @@ -542,7 +543,7 @@ public boolean isComplete() {
}

private static String readTokenFromFile() {
String path = System.getenv(OIDC_TOKEN_FILE);
String path = EnvironmentProvider.getEnv(OIDC_TOKEN_FILE);
if (path == null) {
throw new MongoClientException(
format("Environment variable must be specified: %s", OIDC_TOKEN_FILE));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import java.io.StringWriter;

import static com.mongodb.internal.observability.micrometer.TracingManager.ENV_OBSERVABILITY_QUERY_TEXT_MAX_LENGTH;
import static java.lang.System.getenv;
import static com.mongodb.internal.EnvironmentProvider.getEnv;
import static java.util.Optional.ofNullable;


Expand Down Expand Up @@ -64,7 +64,7 @@ public MicrometerTracer(final ObservationRegistry observationRegistry, final boo
final int textMaxLength, @Nullable final ObservationConvention<MongodbObservationContext> customConvention) {
this.allowCommandPayload = allowCommandPayload;
this.observationRegistry = observationRegistry;
this.textMaxLength = ofNullable(getenv(ENV_OBSERVABILITY_QUERY_TEXT_MAX_LENGTH))
this.textMaxLength = ofNullable(getEnv(ENV_OBSERVABILITY_QUERY_TEXT_MAX_LENGTH))
.map(Integer::parseInt)
.orElse(textMaxLength);
this.convention = customConvention != null ? customConvention : new DefaultMongodbObservationConvention();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import static com.mongodb.observability.micrometer.MongodbObservation.MONGODB_COMMAND;
import static com.mongodb.observability.micrometer.MongodbObservation.MONGODB_OPERATION;
import static com.mongodb.observability.micrometer.MongodbObservation.MONGODB_TRANSACTION;
import static java.lang.System.getenv;
import static com.mongodb.internal.EnvironmentProvider.getEnv;

/**
* Manages tracing spans for MongoDB driver activities.
Expand Down Expand Up @@ -82,7 +82,7 @@ public TracingManager(@Nullable final ObservabilitySettings observabilitySetting
throw new IllegalArgumentException("Only Micrometer based observability is currently supported");
}

String envOtelInstrumentationEnabled = getenv(ENV_OBSERVABILITY_ENABLED);
String envOtelInstrumentationEnabled = getEnv(ENV_OBSERVABILITY_ENABLED);
boolean enableTracing = true;
if (envOtelInstrumentationEnabled != null) {
enableTracing = Boolean.parseBoolean(envOtelInstrumentationEnabled);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@

package com.mongodb.client;

import com.mongodb.internal.connection.FaasEnvironmentAccessor;
import com.mongodb.internal.EnvironmentProvider;
import com.mongodb.lang.Nullable;

import java.util.Map;

@FunctionalInterface
public interface WithWrapper {

Expand All @@ -32,22 +30,12 @@ static WithWrapper withWrapper() {

default WithWrapper withEnvironmentVariable(final String name, @Nullable final String value) {
return runnable -> {
Map<String, String> innerMap = FaasEnvironmentAccessor.getFaasEnvMap();
String original = innerMap.get(name);
if (value == null) {
innerMap.remove(name);
} else {
innerMap.put(name, value);
}
try {
this.run(runnable);
} finally {
if (original == null) {
innerMap.remove(name);
} else {
innerMap.put(name, original);
this.run(() -> {
try (EnvironmentProvider.EnvironmentOverride env = EnvironmentProvider.envOverride()) {
env.set(name, value);
runnable.run();
}
}
});
};
}

Expand Down
Loading