-
Notifications
You must be signed in to change notification settings - Fork 25.1k
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
Add SSL Configuration Library #37287
Merged
Merged
Changes from 2 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e417040
Add SSL Configuration Library
tvernum ca341f6
Fix copyright on DerParser
tvernum 2361017
Remove irrelevant assert from test
tvernum 9a33a7a
Merge branch 'master' into reindex-ssl-lib/1
tvernum 82d0b84
Address feedback
tvernum cde210f
Fix tests to run on BC-FIPS JVM
tvernum 32ba20e
Remove debugging output
tvernum c64e31b
Merge branch 'master' into reindex-ssl-lib/1
tvernum a5d0144
Address feedback on Gradle setup for ssl-config
tvernum 17bf8bd
Merge branch 'master' into reindex-ssl-lib/1
tvernum a052d35
Remove archivesBaseName from build.gradle
tvernum 26b3f09
Merge branch 'master' into reindex-ssl-lib/1
tvernum 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 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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you 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. | ||
*/ | ||
|
||
archivesBaseName = 'elasticsearch-ssl-config' | ||
|
||
dependencies { | ||
compile "org.elasticsearch:elasticsearch-core:${version}" | ||
|
||
if (isEclipse == false || project.path == ":libs:dissect-tests") { | ||
tvernum marked this conversation as resolved.
Show resolved
Hide resolved
|
||
testCompile("org.elasticsearch.test:framework:${version}") { | ||
exclude group: 'org.elasticsearch', module: 'elasticsearch-ssl-config' | ||
} | ||
} | ||
|
||
testCompile "com.carrotsearch.randomizedtesting:randomizedtesting-runner:${versions.randomizedrunner}" | ||
testCompile "junit:junit:${versions.junit}" | ||
testCompile "org.hamcrest:hamcrest-all:${versions.hamcrest}" | ||
} | ||
|
||
jarHell.enabled = false | ||
tvernum marked this conversation as resolved.
Show resolved
Hide resolved
|
||
forbiddenApisMain { | ||
replaceSignatureFiles 'jdk-signatures' | ||
} | ||
forbiddenPatterns { | ||
exclude '**/*.key' | ||
exclude '**/*.pem' | ||
exclude '**/*.p12' | ||
exclude '**/*.jks' | ||
} |
128 changes: 128 additions & 0 deletions
128
libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/DefaultJdkTrustConfig.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you 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 org.elasticsearch.common.ssl; | ||
|
||
import org.elasticsearch.common.Nullable; | ||
|
||
import javax.net.ssl.TrustManagerFactory; | ||
import javax.net.ssl.X509ExtendedTrustManager; | ||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.security.GeneralSecurityException; | ||
import java.security.KeyStore; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.function.BiFunction; | ||
|
||
/** | ||
* This class represents a trust configuration that corresponds to the default trusted CAs of the JDK | ||
*/ | ||
final class DefaultJdkTrustConfig implements SslTrustConfig { | ||
|
||
private final BiFunction<String, String, String> systemProperties; | ||
private final char[] trustStorePassword; | ||
|
||
/** | ||
* Create a trust config that uses System properties to determine the TrustStore type, and the relevant password. | ||
*/ | ||
DefaultJdkTrustConfig() { | ||
this(System::getProperty); | ||
} | ||
|
||
/** | ||
* Create a trust config that uses supplied {@link BiFunction} to determine the TrustStore type, and the relevant password. | ||
*/ | ||
DefaultJdkTrustConfig(BiFunction<String, String, String> systemProperties) { | ||
this(systemProperties, isPkcs11Truststore(systemProperties) ? getSystemTrustStorePassword(systemProperties) : null); | ||
} | ||
|
||
/** | ||
* @param trustStorePassword the password for the truststore. It applies only when PKCS#11 tokens are used, is null otherwise | ||
*/ | ||
DefaultJdkTrustConfig(BiFunction<String, String, String> systemProperties, @Nullable char[] trustStorePassword) { | ||
this.systemProperties = systemProperties; | ||
this.trustStorePassword = trustStorePassword; | ||
} | ||
|
||
@Override | ||
public X509ExtendedTrustManager createTrustManager() { | ||
try { | ||
return KeyStoreUtil.createTrustManager(getSystemTrustStore(), TrustManagerFactory.getDefaultAlgorithm()); | ||
} catch (GeneralSecurityException e) { | ||
throw new SslConfigException("failed to initialize a TrustManager for the system keystore", e); | ||
} | ||
} | ||
|
||
/** | ||
* When a PKCS#11 token is used as the system default keystore/truststore, we need to pass the keystore | ||
* password when loading, even for reading certificates only ( as opposed to i.e. JKS keystores where | ||
* we only need to pass the password for reading Private Key entries ). | ||
* | ||
* @return the KeyStore used as truststore for PKCS#11 initialized with the password, null otherwise | ||
*/ | ||
private KeyStore getSystemTrustStore() { | ||
if (isPkcs11Truststore(systemProperties) && trustStorePassword != null) { | ||
try { | ||
KeyStore keyStore = KeyStore.getInstance("PKCS11"); | ||
keyStore.load(null, trustStorePassword); | ||
return keyStore; | ||
} catch (GeneralSecurityException | IOException e) { | ||
throw new SslConfigException("failed to load the system PKCS#11 truststore", e); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
private static boolean isPkcs11Truststore(BiFunction<String, String, String> systemProperties) { | ||
return systemProperties.apply("javax.net.ssl.trustStoreType", "").equalsIgnoreCase("PKCS11"); | ||
} | ||
|
||
private static char[] getSystemTrustStorePassword(BiFunction<String, String, String> systemProperties) { | ||
return systemProperties.apply("javax.net.ssl.trustStorePassword", "").toCharArray(); | ||
} | ||
|
||
@Override | ||
public Collection<Path> getDependentFiles() { | ||
return Collections.emptyList(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "JDK-trusted-certs"; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
final DefaultJdkTrustConfig that = (DefaultJdkTrustConfig) o; | ||
return Arrays.equals(this.trustStorePassword, that.trustStorePassword); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Arrays.hashCode(trustStorePassword); | ||
} | ||
} |
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.
It would be preferable to alter
project.name
insettings.gradle
as that would have effects across the board, including what you are looking for here. Also means that if we were to publish this the correct pom would be generated without additional configuration and better visibility in the IDE.There are some additional changes needed in
settings.gradle
to keep Eclipse happy, see the discussion in #36477 for a similar change.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.
@atorok I pushed a5d0144 which I think covers what you've recommended.
Can you let me know if there's more we need to do?
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.
Thanks @tvernum , looks good! You can now also remove this line
archiveBaseName
since with the changed project name that's the default now.