forked from Azure/azure-sdk-for-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Build the SSL context with jre key store to back up the KeyVaultKeySt…
…ore's own functions. (Azure#23923) * Build the SSL context with jre key store * Add exemption of spotbugs * Added test for cutomized https client Co-authored-by: michaelqi793
- Loading branch information
1 parent
2028a28
commit a468e67
Showing
6 changed files
with
168 additions
and
73 deletions.
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
93 changes: 93 additions & 0 deletions
93
...-jca/src/main/java/com/azure/security/keyvault/jca/implementation/JREKeyStoreFactory.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,93 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
package com.azure.security.keyvault.jca.implementation; | ||
|
||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.security.AccessController; | ||
import java.security.KeyStore; | ||
import java.security.KeyStoreException; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.security.PrivilegedAction; | ||
import java.security.cert.CertificateException; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.logging.Logger; | ||
import java.util.stream.Stream; | ||
|
||
import static java.util.logging.Level.WARNING; | ||
|
||
/** | ||
* This class provides a JRE key store. | ||
*/ | ||
public final class JREKeyStoreFactory { | ||
private static final String JAVA_HOME = privilegedGetProperty("java.home", ""); | ||
private static final Path STORE_PATH = Paths.get(JAVA_HOME).resolve("lib").resolve("security"); | ||
private static final Path DEFAULT_STORE = STORE_PATH.resolve("cacerts"); | ||
private static final Path JSSE_DEFAULT_STORE = STORE_PATH.resolve("jssecacerts"); | ||
private static final String KEY_STORE_PASSWORD = privilegedGetProperty("javax.net.ssl.keyStorePassword", "changeit"); | ||
private static final Logger LOGGER = Logger.getLogger(JREKeyStoreFactory.class.getName()); | ||
private static final KeyStore JRE_KEY_STORE = getJreKeyStore(); | ||
|
||
|
||
private JREKeyStoreFactory() { | ||
|
||
} | ||
|
||
/** | ||
* This method returns the instance of JRE key store | ||
* @return the JRE key store. | ||
*/ | ||
public static KeyStore getDefaultKeyStore() { | ||
return JRE_KEY_STORE; | ||
} | ||
|
||
|
||
private static KeyStore getJreKeyStore() { | ||
KeyStore defaultKeyStore = null; | ||
try { | ||
defaultKeyStore = KeyStore.getInstance(KeyStore.getDefaultType()); | ||
loadKeyStore(defaultKeyStore); | ||
} catch (KeyStoreException e) { | ||
LOGGER.log(WARNING, "Unable to get the jre key store.", e); | ||
} | ||
return defaultKeyStore; | ||
} | ||
|
||
private static void loadKeyStore(KeyStore ks) { | ||
try (InputStream inputStream = Files.newInputStream(getKeyStoreFile())) { | ||
ks.load(inputStream, KEY_STORE_PASSWORD.toCharArray()); | ||
} catch (IOException | NoSuchAlgorithmException | CertificateException e) { | ||
LOGGER.log(WARNING, "unable to load the jre key store", e); | ||
} | ||
} | ||
|
||
private static Path getKeyStoreFile() { | ||
return Stream.of(getConfiguredKeyStorePath(), JSSE_DEFAULT_STORE, DEFAULT_STORE) | ||
.filter(Objects::nonNull) | ||
.filter(Files::exists) | ||
.filter(Files::isReadable) | ||
.findFirst() | ||
.orElse(null); | ||
} | ||
|
||
private static Path getConfiguredKeyStorePath() { | ||
String configuredKeyStorePath = privilegedGetProperty("javax.net.ssl.keyStore", ""); | ||
return Optional.of(configuredKeyStorePath) | ||
.filter(path -> !path.isEmpty()) | ||
.map(Paths::get) | ||
.orElse(null); | ||
} | ||
|
||
private static String privilegedGetProperty(String theProp, String defaultVal) { | ||
return AccessController.doPrivileged( | ||
(PrivilegedAction<String>) () -> { | ||
String value = System.getProperty(theProp, ""); | ||
return (value.isEmpty()) ? defaultVal : value; | ||
}); | ||
} | ||
} |
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
18 changes: 18 additions & 0 deletions
18
.../src/test/java/com/azure/security/keyvault/jca/implementation/JREKeyStoreFactoryTest.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,18 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
package com.azure.security.keyvault.jca.implementation; | ||
|
||
import com.azure.security.keyvault.jca.KeyVaultKeyStore; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.security.KeyStore; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
|
||
public class JREKeyStoreFactoryTest { | ||
@Test | ||
public void test() { | ||
KeyStore jreKeyStore = JREKeyStoreFactory.getDefaultKeyStore(); | ||
assertFalse(jreKeyStore.getType().equals(KeyVaultKeyStore.KEY_STORE_TYPE)); | ||
} | ||
} |
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