Skip to content

Commit f4a0ced

Browse files
update readme and changelog for jca (Azure#22283)
1 parent faf0204 commit f4a0ced

File tree

20 files changed

+191
-181
lines changed

20 files changed

+191
-181
lines changed

sdk/keyvault/azure-security-keyvault-jca/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## 1.0.0-beta.8 (Unreleased)
44
### New Features
55
- Load JRE key store certificates to AzureKeyVault key store. ([#21845](https://github.com/Azure/azure-sdk-for-java/pull/21845))
6+
- Support properties of azure.cert-path.well-known and azure.cert-path.custom to support load cert from file system. ([#21947](https://github.com/Azure/azure-sdk-for-java/pull/21947))
67

78
## 1.0.0-beta.7 (2021-05-24)
89
### New Features

sdk/keyvault/azure-security-keyvault-jca/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,16 @@ try (CloseableHttpClient client = HttpClients.custom().setConnectionManager(mana
112112

113113
Note if you want to use Azure managed identity, you should set the value of `azure.keyvault.uri`, and the rest of the parameters would be `null`.
114114

115+
### File-System certificates
116+
You can load the certificate in the file system as a trusted certificate by configure the following properties.
117+
118+
```yaml
119+
azure:
120+
cert-path:
121+
well-known: # The file location where you store the well-known certificate
122+
custom: # The file location where you store the custom certificate
123+
```
124+
115125
## Troubleshooting
116126
### General
117127
Azure Key Vault JCA clients raise exceptions. For example, if you try to check a client's identity with a certificate chain that does not include a trusted certificate, a `CertificateException` will be thrown. In the following snippet, the error is handled gracefully by catching the exception and displaying additional information about the error.

sdk/keyvault/azure-security-keyvault-jca/src/main/java/com/azure/security/keyvault/jca/KeyVaultKeyStore.java

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.HashMap;
2323
import java.util.logging.Logger;
2424

25+
import static java.util.logging.Level.FINE;
2526
import static java.util.logging.Level.WARNING;
2627

2728
/**
@@ -50,14 +51,14 @@ public final class KeyVaultKeyStore extends KeyStoreSpi {
5051
private final JreCertificates jreCertificates;
5152

5253
/**
53-
* Store well Know certificates loaded from file system.
54+
* Store well Know certificates loaded from specific path.
5455
*/
55-
private final FileSystemCertificates wellKnowCertificates;
56+
private final SpecificPathCertificates wellKnowCertificates;
5657

5758
/**
58-
* Store custom certificates loaded from file system.
59+
* Store custom certificates loaded from specific path.
5960
*/
60-
private final FileSystemCertificates customCertificates;
61+
private final SpecificPathCertificates customCertificates;
6162

6263
/**
6364
* Store certificates loaded from KeyVault.
@@ -131,8 +132,8 @@ public KeyVaultKeyStore() {
131132
.map(Boolean::parseBoolean)
132133
.orElse(false);
133134
jreCertificates = JreCertificates.getInstance();
134-
wellKnowCertificates = FileSystemCertificates.FileSystemCertificatesFactory.getWellKnownFileSystemCertificates(wellKnowPath);
135-
customCertificates = FileSystemCertificates.FileSystemCertificatesFactory.getCustomFileSystemCertificates(customPath);
135+
wellKnowCertificates = SpecificPathCertificates.getSpecificPathCertificates(wellKnowPath);
136+
customCertificates = SpecificPathCertificates.getSpecificPathCertificates(customPath);
136137
keyVaultCertificates = new KeyVaultCertificates(refreshInterval, keyVaultClient);
137138
classpathCertificates = new ClasspathCertificates();
138139
allCertificates = Arrays.asList(jreCertificates, wellKnowCertificates, customCertificates, keyVaultCertificates, classpathCertificates);
@@ -255,17 +256,11 @@ public void engineLoad(KeyStore.LoadStoreParameter param) {
255256
}
256257
keyVaultCertificates.setKeyVaultClient(keyVaultClient);
257258
}
258-
loadCertificates();
259+
classpathCertificates.loadCertificatesFromClasspath();
259260
}
260261

261262
@Override
262263
public void engineLoad(InputStream stream, char[] password) {
263-
loadCertificates();
264-
}
265-
266-
private void loadCertificates() {
267-
wellKnowCertificates.loadCertificatesFromFileSystem();
268-
customCertificates.loadCertificatesFromFileSystem();
269264
classpathCertificates.loadCertificatesFromClasspath();
270265
}
271266

@@ -281,7 +276,7 @@ private List<String> getAllAliases() {
281276
aliasLists.forEach((key, value) -> {
282277
value.forEach(a -> {
283278
if (allAliases.contains(a)) {
284-
LOGGER.log(WARNING, String.format("The certificate with alias %s under %s already exists", a, key));
279+
LOGGER.log(FINE, String.format("The certificate with alias %s under %s already exists", a, key));
285280
} else {
286281
allAliases.add(a);
287282
}

sdk/keyvault/azure-security-keyvault-jca/src/main/java/com/azure/security/keyvault/jca/FileSystemCertificates.java renamed to sdk/keyvault/azure-security-keyvault-jca/src/main/java/com/azure/security/keyvault/jca/SpecificPathCertificates.java

Lines changed: 24 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,27 @@
2929
/**
3030
* Store certificates loaded from file system.
3131
*/
32-
public final class FileSystemCertificates implements AzureCertificates {
32+
public final class SpecificPathCertificates implements AzureCertificates {
33+
34+
private static final Map<String, SpecificPathCertificates> CACHE = new HashMap<>();
3335

3436
/**
3537
* Stores the logger.
3638
*/
37-
private static final Logger LOGGER = Logger.getLogger(FileSystemCertificates.class.getName());
39+
private static final Logger LOGGER = Logger.getLogger(SpecificPathCertificates.class.getName());
3840

3941
/**
40-
* Stores the jre key store aliases.
42+
* Stores the specific path aliases.
4143
*/
4244
private final List<String> aliases = new ArrayList<>();
4345

4446
/**
45-
* Stores the file system certificates by alias.
47+
* Stores the specific path certificates by alias.
4648
*/
4749
private final Map<String, Certificate> certificates = new HashMap<>();
4850

4951
/**
50-
* Stores the file system certificate keys by alias.
52+
* Stores the specific path certificate keys by alias.
5153
*/
5254
private final Map<String, Key> certificateKeys = new HashMap<>();
5355

@@ -80,8 +82,9 @@ public void deleteEntry(String alias) {
8082
*
8183
* @param certificatePath Store the file path where certificates are placed
8284
*/
83-
private FileSystemCertificates(String certificatePath) {
85+
private SpecificPathCertificates(String certificatePath) {
8486
this.certificatePath = certificatePath;
87+
loadCertificatesFromSpecificPath();
8588
}
8689

8790
/**
@@ -93,7 +96,7 @@ private FileSystemCertificates(String certificatePath) {
9396
public void setCertificateEntry(String alias, Certificate certificate) {
9497
//Add verification to avoid certificate files with the same file name but different suffixes
9598
if (aliases.contains(alias)) {
96-
LOGGER.log(WARNING, "Cannot load certificates with the same alias in file system", alias);
99+
LOGGER.log(WARNING, "Cannot load certificates with the same alias in specific path", alias);
97100
return;
98101
}
99102
aliases.add(alias);
@@ -119,26 +122,27 @@ private void setCertificateByFile(File file) throws IOException {
119122
new Object[]{alias, file.getName()});
120123
}
121124
} catch (CertificateException e) {
122-
LOGGER.log(WARNING, "Unable to load file system certificate from: " + file.getName(), e);
125+
LOGGER.log(WARNING, "Unable to load specific path certificate from: " + file.getName(), e);
123126
}
124127
}
125128

126129
/**
127130
* Load certificates in the file directory
128131
*/
129-
void loadCertificatesFromFileSystem() {
132+
private void loadCertificatesFromSpecificPath() {
130133
try {
131134
List<File> files = getFiles();
132135
for (File file : files) {
133136
setCertificateByFile(file);
134137
}
135138
} catch (IOException ioe) {
136-
LOGGER.log(WARNING, "Unable to determine certificates to file system", ioe);
139+
LOGGER.log(WARNING, "Unable to determine certificates to specific path", ioe);
137140
}
138141
}
139142

140143
/**
141144
* Get alias from file
145+
*
142146
* @param file File containing certificate information
143147
* @return certificate alias
144148
*/
@@ -170,45 +174,17 @@ private List<File> getFiles() {
170174
}
171175

172176
/**
173-
* Factory of FileSystemCertificate, to avoid loading files multiple times
177+
* Get File System certificates by path
178+
*
179+
* @param path certificate path, which works only in first time
180+
* @return file certificate
174181
*/
175-
public static class FileSystemCertificatesFactory {
176-
177-
private static volatile FileSystemCertificates customFileSystemCertificates;
178-
179-
/**
180-
* Get Singleton custom file system certificates
181-
* @param path custom certificate path, which works only in first time
182-
* @return custom file certificate
183-
*/
184-
public static FileSystemCertificates getCustomFileSystemCertificates(String path) {
185-
if (customFileSystemCertificates == null) {
186-
synchronized (FileSystemCertificatesFactory.class) {
187-
if (customFileSystemCertificates == null) {
188-
customFileSystemCertificates = new FileSystemCertificates(path);
189-
}
190-
}
191-
}
192-
return customFileSystemCertificates;
193-
}
194-
195-
private static volatile FileSystemCertificates wellKnownFileSystemCertificates;
196-
197-
/**
198-
* Get Singleton well known file system certificates
199-
* @param path well known certificate path, which works only in first time
200-
* @return well known file certificate
201-
*/
202-
public static FileSystemCertificates getWellKnownFileSystemCertificates(String path) {
203-
if (wellKnownFileSystemCertificates == null) {
204-
synchronized (FileSystemCertificatesFactory.class) {
205-
if (wellKnownFileSystemCertificates == null) {
206-
wellKnownFileSystemCertificates = new FileSystemCertificates(path);
207-
}
208-
}
209-
}
210-
return wellKnownFileSystemCertificates;
182+
public static synchronized SpecificPathCertificates getSpecificPathCertificates(String path) {
183+
SpecificPathCertificates result = CACHE.getOrDefault(path, null);
184+
if (result == null) {
185+
result = new SpecificPathCertificates(path);
186+
CACHE.put(path, result);
211187
}
188+
return result;
212189
}
213-
214190
}

sdk/keyvault/azure-security-keyvault-jca/src/test/java/com/azure/security/keyvault/jca/FileSystemCertificatesTest.java

Lines changed: 0 additions & 64 deletions
This file was deleted.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.azure.security.keyvault.jca;
5+
6+
import org.junit.jupiter.api.Assertions;
7+
import org.junit.jupiter.api.Test;
8+
9+
public class SpecificPathCertificatesTest {
10+
11+
SpecificPathCertificates specificPathCertificates;
12+
13+
public static String getFilePath(String packageName) {
14+
String filepath = "\\src\\test\\resources\\" + packageName;
15+
return System.getProperty("user.dir") + filepath.replace("\\", System.getProperty("file.separator"));
16+
}
17+
18+
@Test
19+
public void testSetCertificateEntry() {
20+
specificPathCertificates = SpecificPathCertificates.getSpecificPathCertificates(getFilePath("custom\\"));
21+
Assertions.assertTrue(specificPathCertificates.getAliases().contains("sideload"));
22+
}
23+
}

sdk/keyvault/azure-security-test-keyvault-jca/src/test/java/com/azure/security/keyvault/jca/test/FileSystemCertificatesTest.java

Lines changed: 0 additions & 38 deletions
This file was deleted.

sdk/keyvault/azure-security-test-keyvault-jca/src/test/java/com/azure/security/keyvault/jca/test/JreKeyStoreTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static void init() {
3737
/*
3838
* Set system properties.
3939
*/
40-
PropertyConvertorUtils.putEnvironmentPropertyToSystemPropertyForKeyVaultJca(PropertyConvertorUtils.SYSTEM_PROPERTIES);
40+
PropertyConvertorUtils.putEnvironmentPropertyToSystemPropertyForKeyVaultJca();
4141
}
4242

4343
@Test

sdk/keyvault/azure-security-test-keyvault-jca/src/test/java/com/azure/security/keyvault/jca/test/KeyVaultCertificatesTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public class KeyVaultCertificatesTest {
5454

5555
@BeforeAll
5656
public static void setEnvironmentProperty() {
57-
PropertyConvertorUtils.putEnvironmentPropertyToSystemPropertyForKeyVaultJca(PropertyConvertorUtils.SYSTEM_PROPERTIES);
57+
PropertyConvertorUtils.putEnvironmentPropertyToSystemPropertyForKeyVaultJca();
5858
PropertyConvertorUtils.addKeyVaultJcaProvider();
5959
certificateName = System.getenv("AZURE_KEYVAULT_CERTIFICATE_NAME");
6060
}

sdk/keyvault/azure-security-test-keyvault-jca/src/test/java/com/azure/security/keyvault/jca/test/KeyVaultJcaProviderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class KeyVaultJcaProviderTest {
2424
*/
2525
@Test
2626
public void testGetCertificate() throws Exception {
27-
PropertyConvertorUtils.putEnvironmentPropertyToSystemPropertyForKeyVaultJca(PropertyConvertorUtils.SYSTEM_PROPERTIES);
27+
PropertyConvertorUtils.putEnvironmentPropertyToSystemPropertyForKeyVaultJca();
2828
PropertyConvertorUtils.addKeyVaultJcaProvider();
2929
KeyStore keystore = PropertyConvertorUtils.getKeyVaultKeyStore();
3030
assertNotNull(keystore.getCertificate(System.getenv("AZURE_KEYVAULT_CERTIFICATE_NAME")));

0 commit comments

Comments
 (0)