-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add key vault dynamic refresh parameter #20765
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
Closed
zhichengliu12581
wants to merge
64
commits into
Azure:master
from
zhichengliu12581:add-key-vault-dynamic-refresh-parameter
Closed
Changes from all commits
Commits
Show all changes
64 commits
Select commit
Hold shift + click to select a range
814d82f
add key vault dynamic refresh
zhichengliu12581 b264a59
add static function and update parameter to static
zhichengliu12581 7f9e831
merge master
zhichengliu12581 8cf927e
add parameter and fix error
zhichengliu12581 fa9c2bd
update sample and add tests
zhichengliu12581 294de13
merge master
zhichengliu12581 78af221
implement the delete method in order to add test
zhichengliu12581 3eedf43
update README.md
zhichengliu12581 fd2df3b
Extraction method and change test
zhichengliu12581 7cfa907
try to fix pipeline error
zhichengliu12581 6d6f6be
remove error context
zhichengliu12581 b799c08
merge master
zhichengliu12581 e108259
update with comment
zhichengliu12581 7dbd980
revert changes cause change function to refresh certificates
zhichengliu12581 ebb3e70
revert KeyVaultKeyStore
zhichengliu12581 d47a635
Store side load certificates and portal certificates separately
zhichengliu12581 5a018f3
create class to store parameter and provide function to make paramete…
zhichengliu12581 6e93adb
enable over trust manager in server side for refresh certificates
zhichengliu12581 ce6aa3a
merge master
zhichengliu12581 bad43a8
add Annotation
zhichengliu12581 3fea835
revert unimportant changes for easy review
zhichengliu12581 1fd57e5
nothing changed, just format code
zhichengliu12581 f8ab509
add properties to enable refresh when have untrusted certificates
zhichengliu12581 ac9fcce
nothing changed, just format code
zhichengliu12581 ee9a5a4
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-java …
zhichengliu12581 c379cc9
add test and Override delete function
zhichengliu12581 7be8c8a
Add configuration to cancel the test
zhichengliu12581 b71aca4
add tests for certificateInfo
zhichengliu12581 54463fa
update names and optimization test with comments
zhichengliu12581 0498b85
create class to store side load certificatees
zhichengliu12581 1ed62c7
nothing changed, just add Annotation to fix error
zhichengliu12581 afd72db
nothing changed, remove unused import to fix error
zhichengliu12581 31c42e5
add test for ClasspathCertificate
zhichengliu12581 9aa5a69
nothing changed, just format code
zhichengliu12581 12196cf
wrap duplicated code into methods
zhichengliu12581 5f38dd9
add time unit in docs and README.md
zhichengliu12581 2128289
update certificate class and change names
zhichengliu12581 0472463
update tests
zhichengliu12581 661b29e
change "" to null
zhichengliu12581 97e746b
add it test for Property:refresh-certificates-when-have-un-trust-cert…
zhichengliu12581 c40b74f
change by comments
zhichengliu12581 d937123
Optimize the code according to comments
zhichengliu12581 f73401e
update README.md
zhichengliu12581 2e4241e
rename AzureCertificates
zhichengliu12581 fbae05c
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-java …
zhichengliu12581 a6e01b9
try to fix pipeline error and rename function
zhichengliu12581 4b44995
update readme
zhichengliu12581 c382f25
Simplify the code
zhichengliu12581 665791b
rename system properties value
zhichengliu12581 7b25713
Modify the verification when inserting a certificate
zhichengliu12581 ea9ffe4
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-java …
zhichengliu12581 3bff173
try to find pipeline error
zhichengliu12581 ea3967f
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-java …
zhichengliu12581 80dc6fa
add temp test class just for find out why error in pipeline
zhichengliu12581 e584999
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-java …
zhichengliu12581 e8f189c
Thoroughly modify the test to see if it is an error caused by the code
zhichengliu12581 a84d627
try to find way to fix error in pipeline
zhichengliu12581 e25aa8b
another try to fix pipeline error by change the way to create SSLContext
zhichengliu12581 1a4e0ab
Update ServerSocketTempTest.java
zhichengliu12581 6f9cbd5
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-java …
zhichengliu12581 cbe3c23
revert change cause master have change the getAlias and add log
zhichengliu12581 7d8e5b3
final test to find error
zhichengliu12581 d9ffd21
revert change to let pipeline success
zhichengliu12581 18f364b
fix readme error
zhichengliu12581 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
39 changes: 39 additions & 0 deletions
39
...ecurity-keyvault-jca/src/main/java/com/azure/security/keyvault/jca/AzureCertificates.java
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 |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package com.azure.security.keyvault.jca; | ||
|
|
||
| import java.security.Key; | ||
| import java.security.cert.Certificate; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Store Azure Certificates | ||
| */ | ||
| public interface AzureCertificates { | ||
|
|
||
| /** | ||
| * Get certificate aliases. | ||
| * @return certificate aliases | ||
| */ | ||
| List<String> getAliases(); | ||
|
|
||
| /** | ||
| * Get certificates. | ||
| * @return certificates | ||
| */ | ||
| Map<String, Certificate> getCertificates(); | ||
|
|
||
| /** | ||
| * Get certificate keys. | ||
| * @return certificate keys | ||
| */ | ||
| Map<String, Key> getCertificateKeys(); | ||
|
|
||
| /** | ||
| * Delete certificate info by alias if exits | ||
| * @param alias certificate alias | ||
| */ | ||
| void deleteEntry(String alias); | ||
| } |
99 changes: 99 additions & 0 deletions
99
...ity-keyvault-jca/src/main/java/com/azure/security/keyvault/jca/ClasspathCertificates.java
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 |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package com.azure.security.keyvault.jca; | ||
|
|
||
| import java.security.Key; | ||
| import java.security.cert.Certificate; | ||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Store certificates loaded from classpath. | ||
| */ | ||
| public class ClasspathCertificates implements AzureCertificates { | ||
|
|
||
| /** | ||
| * Store certificates' alias. | ||
| */ | ||
| private final List<String> aliases = new ArrayList<>(); | ||
|
|
||
| /** | ||
| * Stores the certificates by alias. | ||
| */ | ||
| private final Map<String, Certificate> certificates = new HashMap<>(); | ||
|
|
||
| /** | ||
| * Stores the certificate keys by alias. | ||
| */ | ||
| private final Map<String, Key> certificateKeys = new HashMap<>(); | ||
|
|
||
| /** | ||
| * Get certificate aliases. | ||
| * @return certificate aliases | ||
| */ | ||
| @Override | ||
| public List<String> getAliases() { | ||
| return aliases; | ||
| } | ||
|
|
||
| /** | ||
| * Get certificates. | ||
| * @return certificates | ||
| */ | ||
| @Override | ||
| public Map<String, Certificate> getCertificates() { | ||
| return certificates; | ||
| } | ||
|
|
||
| /** | ||
| * Get certificate keys. | ||
| * @return certificate keys | ||
| */ | ||
| @Override | ||
| public Map<String, Key> getCertificateKeys() { | ||
| return certificateKeys; | ||
| } | ||
|
|
||
| /** | ||
| * Remove alias if exist. | ||
| * @param alias certificate alias | ||
| */ | ||
| public void removeAlias(String alias) { | ||
| aliases.remove(alias); | ||
| } | ||
|
|
||
| /** | ||
| * Remove certificate if exist. | ||
| * @param alias certificate alias | ||
| */ | ||
| public void removeCertificate(String alias) { | ||
| certificates.remove(alias); | ||
| } | ||
|
|
||
| /** | ||
| * Add certificate. | ||
| * @param alias certificate alias | ||
| * @param certificate certificate | ||
| */ | ||
| public void setCertificateEntry(String alias, Certificate certificate) { | ||
| if (!aliases.contains(alias)) { | ||
| aliases.add(alias); | ||
| certificates.put(alias, certificate); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Delete certificate info by alias if exits | ||
| * @param alias certificate alias | ||
| */ | ||
| @Override | ||
| public void deleteEntry(String alias) { | ||
| aliases.remove(alias); | ||
| certificates.remove(alias); | ||
| certificateKeys.remove(alias); | ||
| } | ||
|
|
||
| } | ||
161 changes: 161 additions & 0 deletions
161
...rity-keyvault-jca/src/main/java/com/azure/security/keyvault/jca/KeyVaultCertificates.java
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 |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package com.azure.security.keyvault.jca; | ||
|
|
||
| import java.security.Key; | ||
| import java.security.cert.Certificate; | ||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
| import java.util.Date; | ||
| import java.util.Collections; | ||
| import java.util.Objects; | ||
|
|
||
| /** | ||
| * Store certificates loaded from KeyVault. | ||
| */ | ||
| public class KeyVaultCertificates implements AzureCertificates { | ||
|
|
||
| /** | ||
| * Stores the list of aliases. | ||
| */ | ||
| private List<String> aliases = new ArrayList<>(); | ||
|
|
||
| /** | ||
| * Stores the certificates by alias. | ||
| */ | ||
| private final Map<String, Certificate> certificates = new HashMap<>(); | ||
|
|
||
| /** | ||
| * Stores the certificate keys by alias. | ||
| */ | ||
| private final Map<String, Key> certificateKeys = new HashMap<>(); | ||
|
|
||
| /** | ||
| * Stores the last time refresh certificates and alias | ||
| */ | ||
| private Date lastRefreshTime; | ||
|
|
||
| /** | ||
| * Stores the last force refresh time. | ||
| */ | ||
| private static volatile Date lastForceRefreshTime = new Date(); | ||
|
|
||
| private KeyVaultClient keyVaultClient; | ||
|
|
||
| private final long refreshInterval; | ||
|
|
||
| void setKeyVaultClient(KeyVaultClient keyVaultClient) { | ||
| this.keyVaultClient = keyVaultClient; | ||
| } | ||
|
|
||
| KeyVaultCertificates(long refreshInterval, KeyVaultClient keyVaultClient) { | ||
| this.refreshInterval = refreshInterval; | ||
| this.keyVaultClient = keyVaultClient; | ||
| } | ||
|
|
||
| boolean certificatesNeedRefresh() { | ||
| if (lastRefreshTime == null || lastForceRefreshTime.after(lastRefreshTime)) { | ||
| return true; | ||
| } | ||
| if (refreshInterval > 0) { | ||
| return lastRefreshTime.getTime() + refreshInterval < new Date().getTime(); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * Get certificate aliases. | ||
| * @return certificate aliases | ||
| */ | ||
| @Override | ||
| public List<String> getAliases() { | ||
| refreshCertificatesIfNeeded(); | ||
| return aliases; | ||
| } | ||
|
|
||
| /** | ||
| * Get certificates. | ||
| * @return certificates | ||
| */ | ||
| @Override | ||
| public Map<String, Certificate> getCertificates() { | ||
| refreshCertificatesIfNeeded(); | ||
| return certificates; | ||
| } | ||
|
|
||
| /** | ||
| * Get certificates. | ||
| * @return certificate keys | ||
| */ | ||
| @Override | ||
| public Map<String, Key> getCertificateKeys() { | ||
| refreshCertificatesIfNeeded(); | ||
| return certificateKeys; | ||
| } | ||
|
|
||
| private void refreshCertificatesIfNeeded() { | ||
| if (certificatesNeedRefresh()) { | ||
| refreshCertificates(); | ||
| } | ||
| } | ||
|
|
||
| private void refreshCertificates() { | ||
| aliases = keyVaultClient.getAliases(); | ||
| certificateKeys.clear(); | ||
| certificates.clear(); | ||
| Optional.ofNullable(aliases) | ||
| .orElse(Collections.emptyList()) | ||
| .forEach(alias -> { | ||
| Key key = keyVaultClient.getKey(alias, null); | ||
| if (!Objects.isNull(key)) { | ||
| certificateKeys.put(alias, key); | ||
| } | ||
| Certificate certificate = keyVaultClient.getCertificate(alias); | ||
| if (!Objects.isNull(certificate)) { | ||
| certificates.put(alias, certificate); | ||
| } | ||
| }); | ||
| lastRefreshTime = new Date(); | ||
| } | ||
|
|
||
| /** | ||
| * Get latest alias by certificate which in portal | ||
| * @param certificate certificate got | ||
| * @return certificate' alias if exist. | ||
| */ | ||
| String refreshAndGetAliasByCertificate(Certificate certificate) { | ||
| updateLastForceRefreshTime(); | ||
| return getCertificates().entrySet() | ||
chenrujun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| .stream() | ||
| .filter(entry -> certificate.equals(entry.getValue())) | ||
| .findFirst() | ||
| .map(Map.Entry::getKey) | ||
| .orElse(null); | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * Delete certificate info by alias if exits | ||
| * @param alias deleted certificate | ||
| */ | ||
| @Override | ||
| public void deleteEntry(String alias) { | ||
| if (aliases != null) { | ||
| aliases.remove(alias); | ||
| } | ||
| certificates.remove(alias); | ||
| certificateKeys.remove(alias); | ||
| } | ||
|
|
||
| /** | ||
| * Overall refresh certificates' info | ||
| */ | ||
| public static void updateLastForceRefreshTime() { | ||
| lastForceRefreshTime = new Date(); | ||
| } | ||
|
|
||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.