-
Notifications
You must be signed in to change notification settings - Fork 95
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 method for creating an SSLContext that merges both the trust store and custom certificates #2677
Open
Eric-Alvarez
wants to merge
7
commits into
palantir:develop
Choose a base branch
from
Eric-Alvarez:ealvarez/mergedssl
base: develop
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
Add method for creating an SSLContext that merges both the trust store and custom certificates #2677
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e30bd71
Add method for creating an SSLContext that merges both the trust stor…
7ca0c78
Update doc
383385c
Revert top level SSLSocketFactories methods
33f6302
Make public
dc0c416
Revert "Make public"
87ce879
Revert "Revert top level SSLSocketFactories methods"
cb7961e
Update docs
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
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 |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
|
||
import com.google.common.base.Throwables; | ||
import com.palantir.conjure.java.api.config.ssl.SslConfiguration; | ||
import com.palantir.conjure.java.api.config.ssl.SslConfiguration.StoreType; | ||
import com.palantir.logsafe.SafeArg; | ||
import com.palantir.logsafe.exceptions.SafeRuntimeException; | ||
import java.nio.file.Path; | ||
|
@@ -91,6 +92,20 @@ public static SSLSocketFactory createSslSocketFactory( | |
return sslContext.getSocketFactory(); | ||
} | ||
|
||
/** | ||
* Create a {@link SSLSocketFactory} by merging the provided certificates and configuration. | ||
* | ||
* @param trustCertificatesByAlias a map of X.509 certificate in PEM or DER format by the alias to load the | ||
* certificate as. | ||
* @param config an {@link SslConfiguration} describing the trust store configuration | ||
* @param provider The preferred security {@link Provider} | ||
*/ | ||
public static SSLSocketFactory createSslSocketFactory( | ||
Map<String, PemX509Certificate> trustCertificatesByAlias, SslConfiguration config, Provider provider) { | ||
SSLContext sslContext = createSslContext(config, trustCertificatesByAlias, provider); | ||
return sslContext.getSocketFactory(); | ||
} | ||
|
||
/** | ||
* Create an {@link SSLContext} initialized from the provided configuration. | ||
* | ||
|
@@ -142,6 +157,20 @@ public static SSLContext createSslContext( | |
return createSslContext(trustManagers, new KeyManager[] {}, provider); | ||
} | ||
|
||
/** | ||
* Create an {@link SSLContext} by merging the provided configuration and certificates. | ||
* | ||
* @param trustCertificatesByAlias a map of X.509 certificate in PEM or DER format by the alias to load the | ||
* certificate as. | ||
* @param config an {@link SslConfiguration} describing the trust store configuration | ||
* @param provider The preferred security {@link Provider} | ||
*/ | ||
public static SSLContext createSslContext( | ||
SslConfiguration config, Map<String, PemX509Certificate> trustCertificatesByAlias, Provider provider) { | ||
TrustManager[] trustManagers = createMergedTrustManagers(config, trustCertificatesByAlias); | ||
return createSslContext(trustManagers, new KeyManager[] {}, provider); | ||
} | ||
|
||
/** | ||
* Create an {@link SSLContext} initialized from the provided certificates. | ||
* @see SSLContext#init(KeyManager[], TrustManager[], SecureRandom) | ||
|
@@ -200,6 +229,20 @@ public static TrustManager[] createTrustManagers(Map<String, PemX509Certificate> | |
} | ||
} | ||
|
||
/** | ||
* Create SSL socket factory and trust manager by merging the given certificates and configuration, | ||
* see {@link #createX509TrustManager} and {@link #createSslSocketFactory}. | ||
*/ | ||
public static TrustManager[] createMergedTrustManagers( | ||
SslConfiguration config, Map<String, PemX509Certificate> trustCertificatesByAlias) { | ||
KeyStore keystore = getCombinedTrustStoreAndDefaultCas(config.trustStorePath(), config.trustStoreType()); | ||
|
||
KeyStores.addCertificatesToKeystore(trustCertificatesByAlias, keystore); | ||
|
||
return ConscryptCompatTrustManagers.wrap( | ||
getTrustManagerFactory(keystore).getTrustManagers()); | ||
} | ||
|
||
/** | ||
* Create SSL socket factory and trust manager from the given configuration, see {@link #createX509TrustManager} and | ||
* {@link #createSslSocketFactory}. | ||
|
@@ -269,6 +312,23 @@ public static KeyManager[] createKeyManagers(SslConfiguration config) { | |
|
||
private static TrustManagerFactory createTrustManagerFactory( | ||
Path trustStorePath, SslConfiguration.StoreType trustStoreType) { | ||
KeyStore keyStore = getCombinedTrustStoreAndDefaultCas(trustStorePath, trustStoreType); | ||
|
||
return getTrustManagerFactory(keyStore); | ||
} | ||
|
||
private static TrustManagerFactory getTrustManagerFactory(KeyStore keyStore) { | ||
try { | ||
TrustManagerFactory trustManagerFactory = | ||
TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); | ||
trustManagerFactory.init(keyStore); | ||
return trustManagerFactory; | ||
} catch (GeneralSecurityException e) { | ||
throw Throwables.propagate(e); | ||
} | ||
} | ||
|
||
private static KeyStore getCombinedTrustStoreAndDefaultCas(Path trustStorePath, StoreType trustStoreType) { | ||
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. This would be convenient if we made it public, this PR would not be necessary if this method was available. |
||
KeyStore keyStore; | ||
switch (trustStoreType) { | ||
case JKS: | ||
|
@@ -299,15 +359,7 @@ private static TrustManagerFactory createTrustManagerFactory( | |
"Unable to add certificate to store", e, SafeArg.of("certificateAlias", certAlias)); | ||
} | ||
}); | ||
|
||
try { | ||
TrustManagerFactory trustManagerFactory = | ||
TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); | ||
trustManagerFactory.init(keyStore); | ||
return trustManagerFactory; | ||
} catch (GeneralSecurityException e) { | ||
throw Throwables.propagate(e); | ||
} | ||
return keyStore; | ||
} | ||
|
||
private static KeyManagerFactory createKeyManagerFactory( | ||
|
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.
I noticed most of these methods aren't explicitly tested, do I need to add tests for my additions?