-
Notifications
You must be signed in to change notification settings - Fork 523
Handle multiple directories in SSL_CERT_DIR env variable #1697
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
iiljkic
wants to merge
3
commits into
minio:master
Choose a base branch
from
iiljkic:handle-multiple-cert-dirs
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ | |
| import com.google.common.collect.Multimap; | ||
| import io.minio.credentials.Credentials; | ||
| import io.minio.errors.MinioException; | ||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.io.RandomAccessFile; | ||
|
|
@@ -423,15 +424,15 @@ private static X509TrustManager buildTrustManagerFromKeyStore(KeyStore ks) | |
| } | ||
|
|
||
| private static int setCertificateEntry( | ||
| CertificateFactory cf, KeyStore ks, Path file, String namePrefix) | ||
| CertificateFactory cf, KeyStore ks, Path certPath, String namePrefix) | ||
| throws CertificateException, IOException, KeyStoreException { | ||
| try (InputStream in = Files.newInputStream(file)) { | ||
| int index = 0; | ||
| try (InputStream in = Files.newInputStream(certPath)) { | ||
| int certsInFile = 0; | ||
| while (in.available() > 0) { | ||
| X509Certificate cert = (X509Certificate) cf.generateCertificate(in); | ||
| ks.setCertificateEntry(namePrefix + (index++), cert); | ||
| ks.setCertificateEntry(namePrefix + (certsInFile++), cert); | ||
| } | ||
| return index; | ||
| return certsInFile; | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -444,30 +445,42 @@ private static X509TrustManager getTrustManagerFromFile(String filePath) | |
| return buildTrustManagerFromKeyStore(ks); | ||
| } | ||
|
|
||
| private static X509TrustManager getTrustManagerFromDir(String dirPath) | ||
| private static X509TrustManager getTrustManagerFromDirs(String dirPaths) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we need to say |
||
| throws CertificateException, IOException, KeyStoreException, NoSuchAlgorithmException { | ||
| CertificateFactory cf = CertificateFactory.getInstance("X.509"); | ||
| KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); | ||
| final CertificateFactory cf = CertificateFactory.getInstance("X.509"); | ||
| final KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); | ||
| ks.load(null); | ||
|
|
||
| int index = 0; | ||
| try (Stream<Path> paths = Files.walk(Paths.get(dirPath))) { | ||
| int number = 1; | ||
| for (Path file : (Iterable<Path>) paths.filter(Files::isRegularFile)::iterator) { | ||
| try { | ||
| index += setCertificateEntry(cf, ks, file, "cert-dir-file-" + number + "-"); | ||
| number++; | ||
| } catch (CertificateException | IOException | KeyStoreException e) { | ||
| // Ignore these errors. | ||
| int totalCertificates = 0; | ||
| int fileNumber = 1; | ||
| for (Path directory : getDirectories(dirPaths)) { | ||
| try (Stream<Path> paths = Files.walk(directory)) { | ||
| for (Path certPath : (Iterable<Path>) paths.filter(Files::isRegularFile)::iterator) { | ||
| try { | ||
| totalCertificates += | ||
| setCertificateEntry(cf, ks, certPath, "cert-dir-file-" + fileNumber + "-"); | ||
| } catch (CertificateException | IOException | KeyStoreException e) { | ||
| // Ignore these errors. | ||
| } | ||
| fileNumber++; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (index == 0) return null; | ||
| if (totalCertificates == 0) return null; | ||
|
|
||
| return buildTrustManagerFromKeyStore(ks); | ||
| } | ||
|
|
||
| private static List<Path> getDirectories(String dirPaths) { | ||
| return Stream.of(dirPaths.split(File.pathSeparator)) | ||
| .map(String::trim) | ||
| .filter(s -> !s.isEmpty()) | ||
| .map(Paths::get) | ||
| .filter(Files::isDirectory) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| private static X509TrustManager getDefaultTrustManager() | ||
| throws CertificateException, IOException, KeyStoreException, NoSuchAlgorithmException { | ||
| TrustManagerFactory factory = | ||
|
|
@@ -479,15 +492,15 @@ private static X509TrustManager getDefaultTrustManager() | |
| return null; | ||
| } | ||
|
|
||
| private static X509TrustManager getCompositeTrustManager(String filePath, String dirPath) | ||
| private static X509TrustManager getCompositeTrustManager(String filePath, String dirPaths) | ||
| throws CertificateException, IOException, KeyStoreException, NoSuchAlgorithmException { | ||
| List<X509TrustManager> trustManagers = new ArrayList<>(); | ||
|
|
||
| X509TrustManager defaultTm = getDefaultTrustManager(); | ||
| if (defaultTm != null) trustManagers.add(defaultTm); | ||
|
|
||
| if (dirPath != null && !dirPath.isEmpty()) { | ||
| X509TrustManager dirTm = getTrustManagerFromDir(dirPath); | ||
| if (dirPaths != null && !dirPaths.isEmpty()) { | ||
| X509TrustManager dirTm = getTrustManagerFromDirs(dirPaths); | ||
| if (dirTm != null) trustManagers.add(dirTm); | ||
| } | ||
|
|
||
|
|
@@ -579,11 +592,11 @@ public static OkHttpClient enablePKCS12Certificates( | |
| httpClient, trustStorePath, trustStorePassword, keyStorePath, keyStorePassword, "PKCS12"); | ||
| } | ||
|
|
||
| /** Enable external TLS certificates from given file path and all valid files from dir path. */ | ||
| /** Enable external TLS certificates from given file path and all valid files from dir paths. */ | ||
| public static OkHttpClient enableExternalCertificates( | ||
| OkHttpClient client, String filePath, String dirPath) throws MinioException { | ||
| OkHttpClient client, String filePath, String dirPaths) throws MinioException { | ||
| try { | ||
| X509TrustManager tm = getCompositeTrustManager(filePath, dirPath); | ||
| X509TrustManager tm = getCompositeTrustManager(filePath, dirPaths); | ||
| if (tm == null) return client; | ||
|
|
||
| SSLContext sslContext = SSLContext.getInstance("TLS"); | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are just renaming variables here. I don't think it is needed.