Skip to content
Open
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
43 changes: 40 additions & 3 deletions api/src/main/java/io/minio/Http.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -513,7 +514,8 @@ private static X509TrustManager getDefaultTrustManager()
return null;
}

private static X509TrustManager getCompositeTrustManager(String filePath, String dirPath)
private static X509TrustManager getCompositeTrustManager(
List<String> filePaths, String dirPath, Collection<? extends Certificate> certificates)
throws CertificateException, IOException, KeyStoreException, NoSuchAlgorithmException {
List<X509TrustManager> trustManagers = new ArrayList<>();

Expand All @@ -525,11 +527,22 @@ private static X509TrustManager getCompositeTrustManager(String filePath, String
if (dirTm != null) trustManagers.add(dirTm);
}

if (filePath != null && !filePath.isEmpty()) {
for (String filePath : filePaths) {
if (filePath == null || filePath.isEmpty()) continue;
X509TrustManager fileTm = getTrustManagerFromFile(filePath);
if (fileTm != null) trustManagers.add(fileTm);
}

if (!certificates.isEmpty()) {
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
ks.load(null);
int index = 0;
for (Certificate certificate : certificates) {
ks.setCertificateEntry("cert-given-" + (index++), certificate);
}
trustManagers.add(buildTrustManagerFromKeyStore(ks));
}

if (trustManagers.isEmpty()) return null;

return createCompositeTrustManager(trustManagers);
Expand Down Expand Up @@ -616,8 +629,32 @@ public static OkHttpClient enablePKCS12Certificates(
/** Enable external TLS certificates from given file path and all valid files from dir path. */
public static OkHttpClient enableExternalCertificates(
OkHttpClient client, String filePath, String dirPath) throws MinioException {
return enableExternalCertificatesFromFiles(client, Arrays.asList(filePath), dirPath);
}

/**
* Enable external TLS certificates from given file paths and all valid files from dir path. The
* client returned is the given one only when no trust manager is found at all, including none
* from the JVM trust store.
*/
public static OkHttpClient enableExternalCertificatesFromFiles(
OkHttpClient client, List<String> filePaths, String dirPath) throws MinioException {
return enableExternalCertificates(client, filePaths, dirPath, Collections.emptyList());
}

/**
* Enable external TLS certificates from given file paths, all valid files from dir path and given
* certificates in addition to the JVM trust store. The client returned is the given one only when
* no trust manager is found at all, including none from the JVM trust store.
*/
public static OkHttpClient enableExternalCertificates(
OkHttpClient client,
List<String> filePaths,
String dirPath,
Collection<? extends Certificate> certificates)
throws MinioException {
try {
X509TrustManager tm = getCompositeTrustManager(filePath, dirPath);
X509TrustManager tm = getCompositeTrustManager(filePaths, dirPath, certificates);
if (tm == null) return client;

SSLContext sslContext = SSLContext.getInstance("TLS");
Expand Down
81 changes: 75 additions & 6 deletions api/src/main/java/io/minio/credentials/IamAwsProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,21 @@
import com.fasterxml.jackson.databind.json.JsonMapper;
import io.minio.Http;
import io.minio.Time;
import io.minio.errors.MinioException;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Paths;
import java.security.ProviderException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Objects;
Expand Down Expand Up @@ -60,6 +67,7 @@ public class IamAwsProvider extends EnvironmentProvider {
// Custom endpoint to fetch IAM role credentials.
private final HttpUrl customEndpoint;
private final OkHttpClient httpClient;
private final boolean customHttpClient;
private final ObjectMapper mapper;
private Credentials credentials;

Expand All @@ -68,6 +76,7 @@ public IamAwsProvider(@Nullable String customEndpoint, @Nullable OkHttpClient cu
(customEndpoint != null)
? Objects.requireNonNull(HttpUrl.parse(customEndpoint), "Invalid custom endpoint")
: null;
this.customHttpClient = customHttpClient != null;
// HTTP/1.1 is only supported in default client because of HTTP/2 in OkHttpClient cause 5
// minutes timeout on program exit.
this.httpClient =
Expand Down Expand Up @@ -142,15 +151,75 @@ private String containerAuthorizationToken() {
}
}

/**
* Get STS endpoint from AWS_ENDPOINT_URL_STS, which is the opt-in to this behavior. The global
* AWS_ENDPOINT_URL is not read. AWS_IGNORE_CONFIGURED_ENDPOINT_URLS=true skips the variable. See
* <a href="https://docs.aws.amazon.com/sdkref/latest/guide/feature-ss-endpoints.html">AWS
* documentation</a>.
*/
private HttpUrl stsEndpointFromEnv() {
if ("true".equalsIgnoreCase(getProperty("AWS_IGNORE_CONFIGURED_ENDPOINT_URLS"))) return null;

String value = getProperty("AWS_ENDPOINT_URL_STS");
if (value == null || value.isEmpty()) return null;

HttpUrl url = HttpUrl.parse(value);
// The value is left out of the message as it may contain credentials.
if (url == null) throw new ProviderException("Invalid AWS_ENDPOINT_URL_STS value");
return url;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}

/**
* Get HTTP client trusting certificates of AWS_CA_BUNDLE in addition to the default trust store.
* It is used only when the STS endpoint comes from AWS_ENDPOINT_URL_STS. A caller supplied client
* is returned as is. See <a
* href="https://docs.aws.amazon.com/sdkref/latest/guide/feature-gen-config.html">AWS
* documentation</a>.
*/
OkHttpClient stsHttpClient() {
String caBundle = getProperty("AWS_CA_BUNDLE");
if (customHttpClient || caBundle == null || caBundle.isEmpty()) return httpClient;

// Parse the bundle once and hand the certificates over, so a bundle replaced meanwhile cannot
// be read twice. The default trust store would hide an empty bundle.
Collection<? extends Certificate> certificates;
try (InputStream in = Files.newInputStream(Paths.get(caBundle))) {
certificates = CertificateFactory.getInstance("X.509").generateCertificates(in);
} catch (InvalidPathException | IOException | CertificateException e) {
throw new ProviderException("Unable to read AWS_CA_BUNDLE " + caBundle, e);
}
if (certificates.isEmpty()) {
throw new ProviderException("AWS_CA_BUNDLE " + caBundle + " contains no certificates");
}

try {
// Keep SSL_CERT_FILE and SSL_CERT_DIR in effect as the default trust store is extended.
return Http.enableExternalCertificates(
httpClient,
Arrays.asList(getProperty("SSL_CERT_FILE")),
getProperty("SSL_CERT_DIR"),
certificates);
} catch (MinioException e) {
throw new ProviderException("Unable to load certificates for STS request", e);
}
}

private Credentials fetchCredentials(String tokenFile) {
HttpUrl url = this.customEndpoint;
// AWS_CA_BUNDLE is read only for an endpoint from AWS_ENDPOINT_URL_STS.
boolean fromEnv = false;
if (url == null) {
url = stsEndpointFromEnv();
fromEnv = url != null;
}
if (url == null) {
String region = getProperty("AWS_REGION");
url =
HttpUrl.parse(
(region == null)
? "https://sts.amazonaws.com"
: "https://sts." + region + ".amazonaws.com");
if (region == null || region.isEmpty()) {
url = HttpUrl.parse("https://sts.amazonaws.com");
} else {
String suffix = region.startsWith("cn-") ? ".amazonaws.com.cn" : ".amazonaws.com";
url = HttpUrl.parse("https://sts." + region + suffix);
}
}

Provider provider =
Expand All @@ -168,7 +237,7 @@ private Credentials fetchCredentials(String tokenFile) {
null,
getProperty("AWS_ROLE_ARN"),
getProperty("AWS_ROLE_SESSION_NAME"),
httpClient);
fromEnv ? stsHttpClient() : httpClient);
credentials = provider.fetch();
return credentials;
}
Expand Down
31 changes: 31 additions & 0 deletions api/src/test/java/io/minio/HttpExternalCertificatesNullTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* MinIO Java SDK for Amazon S3 Compatible Cloud Storage, (C) 2026 MinIO, 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 io.minio;

import io.minio.errors.MinioException;
import okhttp3.OkHttpClient;
import org.junit.Assert;
import org.junit.Test;

public class HttpExternalCertificatesNullTest {
/** A literal null file path must keep binding to the String overload, as before. */
@Test
public void literalNullFilePathCompiles() throws MinioException {
OkHttpClient client = Http.enableExternalCertificates(new OkHttpClient(), null, null);
Assert.assertNotNull(client);
}
}
Loading