-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Created classes for parsing certificates, keys and secrets identifiers. #15851
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9ea9ea6
Created model classes for certificates, keys and secrets identifiers.
vcolin7 de1a8c6
Fixed spotbugs issues.
vcolin7 7005cbf
Added unit tests.
vcolin7 0064f04
Fixed checkstyle and build issues.
vcolin7 e400d29
Added support for deleted collections ("deletedcertificates", "delete…
vcolin7 afbc426
Added more validity checks and modified unit tests.
vcolin7 6eb4b28
Fixed checkstyle issues.
vcolin7 9c6a2ac
Applied PR feedback.
vcolin7 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
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
98 changes: 98 additions & 0 deletions
98
...n/java/com/azure/security/keyvault/certificates/models/KeyVaultCertificateIdentifier.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,98 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package com.azure.security.keyvault.certificates.models; | ||
|
|
||
| import com.azure.security.keyvault.certificates.CertificateAsyncClient; | ||
| import com.azure.security.keyvault.certificates.CertificateClient; | ||
|
|
||
| import java.net.MalformedURLException; | ||
| import java.net.URL; | ||
|
|
||
| /** | ||
| * Information about a {@link KeyVaultCertificate} parsed from the certificate URL. You can use this information when | ||
| * calling methods of {@link CertificateClient} or {@link CertificateAsyncClient}. | ||
| */ | ||
| public final class KeyVaultCertificateIdentifier { | ||
| private final String certificateId, vaultUrl, name, version; | ||
|
|
||
| private KeyVaultCertificateIdentifier(String certificateId, String vaultUrl, String name, String version) { | ||
| this.certificateId = certificateId; | ||
| this.vaultUrl = vaultUrl; | ||
| this.name = name; | ||
| this.version = version; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the key identifier used to create this object | ||
| * | ||
| * @return The certificate identifier. | ||
| */ | ||
| public String getCertificateId() { | ||
| return certificateId; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the URL of the Key Vault. | ||
| * | ||
| * @return The Key Vault URL. | ||
| */ | ||
| public String getVaultUrl() { | ||
| return vaultUrl; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the name of the certificate. | ||
| * | ||
| * @return The certificate name. | ||
| */ | ||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the optional version of the certificate. | ||
| * | ||
| * @return The certificate version. | ||
| */ | ||
| public String getVersion() { | ||
| return version; | ||
| } | ||
|
|
||
| /** | ||
| * Create a new {@link KeyVaultCertificateIdentifier} from a given certificate identifier. | ||
| * | ||
| * <p>Valid examples are: | ||
| * | ||
| * <ul> | ||
| * <li>https://{key-vault-name}.vault.azure.net/certificates/{certificate-name}</li> | ||
| * <li>https://{key-vault-name}.vault.azure.net/certificates/{certificate-name}/pending</li> | ||
| * <li>https://{key-vault-name}.vault.azure.net/certificates/{certificate-name}/{unique-version-id}</li> | ||
| * <li>https://{key-vault-name}.vault.azure.net/deletedcertificates/{deleted-certificate-name}</li> | ||
| * </ul> | ||
| * | ||
| * @param certificateId The certificate identifier to extract information from. | ||
| * @return a new instance of {@link KeyVaultCertificateIdentifier}. | ||
| * @throws IllegalArgumentException if the given identifier is {@code null}. | ||
| * @throws MalformedURLException if the given identifier is not a valid Key Vault Certificate identifier | ||
| */ | ||
| public static KeyVaultCertificateIdentifier parse(String certificateId) throws IllegalArgumentException, MalformedURLException { | ||
| if (certificateId == null) { | ||
| throw new IllegalArgumentException("certificateId cannot be null"); | ||
| } | ||
|
|
||
| URL url = new URL(certificateId); | ||
| // We expect an identifier with either 2 or 3 path segments: collection + name [+ version] | ||
| String[] pathSegments = url.getPath().split("/"); | ||
|
|
||
| if ((pathSegments.length != 3 && pathSegments.length != 4) // More or less segments in the URI than expected. | ||
| || !"https".equals(url.getProtocol()) // Invalid protocol. | ||
| || (!"certificates".equals(pathSegments[1]) && !"deletedcertificates".equals(pathSegments[1])) // Invalid collection. | ||
| || ("deletedcertificates".equals(pathSegments[1]) && pathSegments.length == 4)) { // Deleted items do not include a version. | ||
| throw new IllegalArgumentException("certificateId is not a valid Key Vault Certificate identifier"); | ||
| } | ||
|
|
||
| return new KeyVaultCertificateIdentifier(certificateId, url.getProtocol() + "://" + url.getHost(), | ||
|
vcolin7 marked this conversation as resolved.
Outdated
|
||
| pathSegments[2], pathSegments.length == 4 ? pathSegments[3] : null); | ||
| } | ||
| } | ||
83 changes: 83 additions & 0 deletions
83
...va/com/azure/security/keyvault/certificates/models/KeyVaultCertificateIdentifierTest.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,83 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package com.azure.security.keyvault.certificates.models; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.net.MalformedURLException; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
|
||
| class KeyVaultCertificateIdentifierTest { | ||
| @Test | ||
| void parseWithoutVersion() throws MalformedURLException { | ||
| String certificateId = "https://test-key-vault.vault.azure.net/certificates/test-certificate"; | ||
| KeyVaultCertificateIdentifier keyVaultCertificateIdentifier = | ||
| KeyVaultCertificateIdentifier.parse(certificateId); | ||
|
|
||
| assertEquals(certificateId, keyVaultCertificateIdentifier.getCertificateId()); | ||
| assertEquals("https://test-key-vault.vault.azure.net", keyVaultCertificateIdentifier.getVaultUrl()); | ||
| assertEquals("test-certificate", keyVaultCertificateIdentifier.getName()); | ||
| assertNull(keyVaultCertificateIdentifier.getVersion()); | ||
| } | ||
|
|
||
| @Test | ||
| void parseWithVersion() throws MalformedURLException { | ||
| String certificateId = "https://test-key-vault.vault.azure.net/certificates/test-certificate/version"; | ||
| KeyVaultCertificateIdentifier keyVaultCertificateIdentifier = | ||
| KeyVaultCertificateIdentifier.parse(certificateId); | ||
|
|
||
| assertEquals(certificateId, keyVaultCertificateIdentifier.getCertificateId()); | ||
| assertEquals("https://test-key-vault.vault.azure.net", keyVaultCertificateIdentifier.getVaultUrl()); | ||
| assertEquals("test-certificate", keyVaultCertificateIdentifier.getName()); | ||
| assertEquals("version", keyVaultCertificateIdentifier.getVersion()); | ||
| } | ||
|
|
||
| @Test | ||
| void parseForDeletedCertificate() throws MalformedURLException { | ||
| String certificateId = "https://test-key-vault.vault.azure.net/deletedcertificates/test-certificate"; | ||
| KeyVaultCertificateIdentifier keyVaultCertificateIdentifier = KeyVaultCertificateIdentifier.parse(certificateId); | ||
|
|
||
| assertEquals(certificateId, keyVaultCertificateIdentifier.getCertificateId()); | ||
| assertEquals("https://test-key-vault.vault.azure.net", keyVaultCertificateIdentifier.getVaultUrl()); | ||
| assertEquals("test-certificate", keyVaultCertificateIdentifier.getName()); | ||
| } | ||
|
|
||
| @Test | ||
| void parseInvalidIdentifierForDeletedCertificate() { | ||
| String certificateId = "https://test-key-vault.vault.azure.net/deletedcertificates/test-certificate/version"; | ||
| Exception exception = assertThrows(IllegalArgumentException.class, | ||
| () -> KeyVaultCertificateIdentifier.parse(certificateId)); | ||
|
|
||
| assertEquals("certificateId is not a valid Key Vault Certificate identifier", exception.getMessage()); | ||
| } | ||
|
|
||
| @Test | ||
| void parseNullIdentifier() { | ||
| Exception exception = assertThrows(IllegalArgumentException.class, | ||
| () -> KeyVaultCertificateIdentifier.parse(null)); | ||
|
|
||
| assertEquals("certificateId cannot be null", exception.getMessage()); | ||
| } | ||
|
|
||
| @Test | ||
| void parseInvalidIdentifierWithWrongCollection() { | ||
| String certificateId = "https://test-key-vault.vault.azure.net/keys/test-certificate"; | ||
| Exception exception = assertThrows(IllegalArgumentException.class, | ||
| () -> KeyVaultCertificateIdentifier.parse(certificateId)); | ||
|
|
||
| assertEquals("certificateId is not a valid Key Vault Certificate identifier", exception.getMessage()); | ||
| } | ||
|
|
||
| @Test | ||
| void parseInvalidIdentifierWithExtraSegment() { | ||
| String certificateId = "https://test-key-vault.vault.azure.net/keys/test-certificate/version/extra-segment"; | ||
| Exception exception = assertThrows(IllegalArgumentException.class, | ||
| () -> KeyVaultCertificateIdentifier.parse(certificateId)); | ||
|
|
||
| assertEquals("certificateId is not a valid Key Vault Certificate identifier", exception.getMessage()); | ||
| } | ||
| } |
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
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
98 changes: 98 additions & 0 deletions
98
...ult-keys/src/main/java/com/azure/security/keyvault/keys/models/KeyVaultKeyIdentifier.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,98 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package com.azure.security.keyvault.keys.models; | ||
|
|
||
| import com.azure.security.keyvault.keys.KeyAsyncClient; | ||
| import com.azure.security.keyvault.keys.KeyClient; | ||
|
|
||
| import java.net.MalformedURLException; | ||
| import java.net.URL; | ||
|
|
||
| /** | ||
| * Information about a {@link KeyVaultKey} parsed from the key URL. You can use this information when calling methods | ||
| * of {@link KeyClient} or {@link KeyAsyncClient}. | ||
| */ | ||
| public final class KeyVaultKeyIdentifier { | ||
| private final String keyId, vaultUrl, name, version; | ||
|
|
||
| private KeyVaultKeyIdentifier(String keyId, String vaultUrl, String name, String version) { | ||
| this.keyId = keyId; | ||
| this.vaultUrl = vaultUrl; | ||
| this.name = name; | ||
| this.version = version; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the key identifier used to create this object. | ||
| * | ||
| * @return The key identifier. | ||
| */ | ||
| public String getKeyId() { | ||
| return keyId; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the URL of the Key Vault. | ||
| * | ||
| * @return The Key Vault URL. | ||
| */ | ||
| public String getVaultUrl() { | ||
| return vaultUrl; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the name of the key. | ||
| * | ||
| * @return The key name. | ||
| */ | ||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the optional version of the key. | ||
| * | ||
| * @return The key version. | ||
| */ | ||
| public String getVersion() { | ||
| return version; | ||
| } | ||
|
|
||
| /** | ||
| * Create a new {@link KeyVaultKeyIdentifier} from a given key identifier. | ||
| * | ||
| * <p>Valid examples are: | ||
| * | ||
| * <ul> | ||
| * <li>https://{key-vault-name}.vault.azure.net/keys/{key-name}</li> | ||
| * <li>https://{key-vault-name}.vault.azure.net/keys/{key-name}/pending</li> | ||
| * <li>https://{key-vault-name}.vault.azure.net/keys/{key-name}/{unique-version-id}</li> | ||
| * <li>https://{key-vault-name}.vault.azure.net/deletedkeys/{deleted-key-name}</li> | ||
| * </ul> | ||
| * | ||
| * @param keyId The key identifier to extract information from. | ||
| * @return a new instance of {@link KeyVaultKeyIdentifier}. | ||
| * @throws IllegalArgumentException if the given identifier is {@code null}. | ||
| * @throws MalformedURLException if the given identifier is not a valid Key Vault Key identifier | ||
| */ | ||
| public static KeyVaultKeyIdentifier parse(String keyId) throws IllegalArgumentException, MalformedURLException { | ||
| if (keyId == null) { | ||
| throw new IllegalArgumentException("keyId cannot be null"); | ||
| } | ||
|
|
||
| URL url = new URL(keyId); | ||
|
vcolin7 marked this conversation as resolved.
Outdated
|
||
| // We expect an identifier with either 2 or 3 path segments: collection + name [+ version] | ||
| String[] pathSegments = url.getPath().split("/"); | ||
|
|
||
| if ((pathSegments.length != 3 && pathSegments.length != 4) // More or less segments in the URI than expected. | ||
| || !"https".equals(url.getProtocol()) // Invalid protocol. | ||
| || (!"keys".equals(pathSegments[1]) && !"deletedkeys".equals(pathSegments[1])) // Invalid collection. | ||
| || ("deletedkeys".equals(pathSegments[1]) && pathSegments.length == 4)) { // Deleted items do not include a version. | ||
| throw new IllegalArgumentException("keyId is not a valid Key Vault Key identifier"); | ||
| } | ||
|
|
||
| return new KeyVaultKeyIdentifier(keyId, url.getProtocol() + "://" + url.getHost(), pathSegments[2], | ||
|
vcolin7 marked this conversation as resolved.
Outdated
|
||
| pathSegments.length == 4 ? pathSegments[3] : null); | ||
| } | ||
| } | ||
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.