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
34 changes: 34 additions & 0 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"
- 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 @@ -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
- 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 on lines +2568 to +2571
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

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

The comment above says MongoDB 8.0 binaries are affected on Windows and the fix is only available starting from 8.2, but this Windows CSFLE matrix still schedules version: [ "8.0", "latest" ]. This looks inconsistent and may cause avoidable Windows CI failures; consider using 8.2 here as well (or clarify why CSFLE is exempt).

Copilot uses AI. Check for mistakes.
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
16 changes: 15 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,12 @@ if [ ! -z "$REQUIRE_API_VERSION" ]; then
export API_VERSION="-Dorg.mongodb.test.api.version=1"
fi

# Log Windows version info for diagnostics
if [ "Windows_NT" == "$OS" ]; then
echo "Windows build info:"
cmd /c ver
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 on lines +8 to 16
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

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

On Windows, the JDK paths are switched to /cygdrive/c/..., but JDK21_GRAALVM remains hard-coded to a Linux path later in this script. If any Windows variants ever run the GraalVM native-image task, this will break. Consider applying the same OS switch for JDK21_GRAALVM (or documenting/enforcing that GraalVM tasks never run on Windows).

Suggested change
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"
# note that `JDK21_GRAALVM` is used in `run-graalvm-native-image-app.sh`
# by dynamically constructing the variable name
export JDK21_GRAALVM="/cygdrive/c/java/jdk21-graalce"
else
export JDK8="/opt/java/jdk8"
export JDK11="/opt/java/jdk11"
export JDK17="/opt/java/jdk17"
export JDK21="/opt/java/jdk21"
# 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"
fi

Copilot uses AI. Check for mistakes.
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 on lines +26 to +27
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

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

The Javadoc claims "All production code should use this class instead of calling System.getenv directly", but there are still non-test sources in the repo using System.getenv (e.g., driver-lambda/src/main/com/mongodb/lambdatest/LambdaTestApp.java). Either narrow the statement (e.g., "driver-core production code") or migrate those remaining call sites to keep the documentation accurate.

Suggested change
* Centralized access to environment variables. All production code should use
* this class instead of calling {@link System#getenv(String)} directly.
* Centralized access to environment variables for driver-core production code.
* Production code in this module should use this class instead of calling
* {@link System#getenv(String)} directly.

Copilot uses AI. Check for mistakes.
*
* <p>Tests can override values via {@link #envOverride()}.</p>
*/
public final class EnvironmentProvider {
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 on lines +31 to +66
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

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

EnvironmentProvider uses a mutable static envLookup without any synchronization/volatile semantics. If envOverride() is used while other threads call getEnv, reads can observe stale values and overrides can be lost/reset unexpectedly (especially if overrides are opened/closed out of order). Consider making the override mechanism thread-safe (e.g., use a synchronized stack/AtomicReference/volatile, or a ThreadLocal override for tests) and/or guard against non-LIFO close ordering.

Copilot uses AI. Check for mistakes.
}
}
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 @@ -38,7 +38,7 @@
import static com.mongodb.internal.observability.micrometer.MongodbObservation.LowCardinalityKeyNames.EXCEPTION_TYPE;
import static com.mongodb.internal.observability.micrometer.MongodbObservation.MONGODB_OBSERVATION;
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 @@ -75,7 +75,7 @@ public MicrometerTracer(final ObservationRegistry observationRegistry) {
public MicrometerTracer(final ObservationRegistry observationRegistry, final boolean allowCommandPayload, final int textMaxLength) {
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
import static com.mongodb.internal.observability.micrometer.MongodbObservation.LowCardinalityKeyNames.SYSTEM;
import static com.mongodb.internal.observability.micrometer.MongodbObservation.LowCardinalityKeyNames.TRANSACTION_NUMBER;
import static com.mongodb.internal.observability.micrometer.MongodbObservation.LowCardinalityKeyNames.CURSOR_ID;
import static java.lang.System.getenv;
import static com.mongodb.internal.EnvironmentProvider.getEnv;

/**
* Manages tracing spans for MongoDB driver activities.
Expand Down Expand Up @@ -93,7 +93,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
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ void testDockerMetadataIncluded() {
@Test
void testDockerAndKubernetesMetadataIncluded() {
try (MockedStatic<Files> pathsMockedStatic = Mockito.mockStatic(Files.class)) {
Path path = Paths.get(File.separator + "/.dockerenv");
Path path = Paths.get(File.separator + ".dockerenv");
pathsMockedStatic.when(() -> Files.exists(path)).thenReturn(true);

withWrapper()
Expand Down
Loading