From c6e1bde8711367505c4ac5ea5072cbbf5072ec0e Mon Sep 17 00:00:00 2001 From: Manfred Riem Date: Sun, 31 Dec 2023 08:45:37 -0600 Subject: [PATCH] Fixes #302 - Add Set Secret --- azure-keyvault/pom.xml | 15 ++++ .../main/java/keyvault/KeyVaultResource.java | 74 +++++++++++++++++-- .../main/java/keyvault/SecretAttributes.java | 4 + ...{KeyVaultSecret.java => SecretBundle.java} | 43 ++++------- .../src/test/java/keyvault/KeyVaultIT.java | 9 ++- 5 files changed, 107 insertions(+), 38 deletions(-) create mode 100644 azure-keyvault/src/main/java/keyvault/SecretAttributes.java rename azure-keyvault/src/main/java/keyvault/{KeyVaultSecret.java => SecretBundle.java} (52%) diff --git a/azure-keyvault/pom.xml b/azure-keyvault/pom.xml index d0a91d9..6e4981c 100644 --- a/azure-keyvault/pom.xml +++ b/azure-keyvault/pom.xml @@ -39,6 +39,18 @@ azure-identity test + + com.azure + azure-json + 1.1.0 + test + + + com.azure + azure-xml + 1.0.0-beta.2 + test + com.azure azure-security-keyvault-secrets @@ -93,6 +105,9 @@ + + verbose + ${basedir}/src/test/certs/keystore password diff --git a/azure-keyvault/src/main/java/keyvault/KeyVaultResource.java b/azure-keyvault/src/main/java/keyvault/KeyVaultResource.java index 5b09652..b253bf8 100644 --- a/azure-keyvault/src/main/java/keyvault/KeyVaultResource.java +++ b/azure-keyvault/src/main/java/keyvault/KeyVaultResource.java @@ -1,8 +1,18 @@ package keyvault; +import jakarta.inject.Singleton; +import jakarta.json.bind.Jsonb; +import jakarta.json.bind.JsonbBuilder; import jakarta.ws.rs.GET; +import jakarta.ws.rs.HeaderParam; +import jakarta.ws.rs.PUT; import jakarta.ws.rs.Path; import jakarta.ws.rs.PathParam; +import jakarta.ws.rs.WebApplicationException; +import static jakarta.ws.rs.core.Response.Status.BAD_REQUEST; +import java.io.InputStream; +import java.util.HashMap; +import java.util.Map; /** * REST API for Azure KeyVault. @@ -10,24 +20,76 @@ * @author Manfred Riem (mriem@manorrock.com) */ @Path("keyvault") +@Singleton public class KeyVaultResource { + /** + * Stores the secrets. + */ + private Map> secrets = new HashMap<>(); + /** * Get the secret. - * + * *

- * For more information, see https://learn.microsoft.com/en-us/rest/api/keyvault/secrets/get-secret/get-secret?tabs=HTTP + * For more information, see + * https://learn.microsoft.com/en-us/rest/api/keyvault/secrets/get-secret/get-secret?tabs=HTTP *

- * + * * @param keyVault the key vault. * @param secretName the secret name. * @return the secret value. */ @Path("{name}/secrets/{secretName}") @GET - public KeyVaultSecret get( - @PathParam("name") String keyVault, + public SecretBundle getSecret( + @PathParam("name") String keyVault, @PathParam("secretName") String secretName) { - return new KeyVaultSecret("secretValue"); + SecretBundle secret = null; + Map secretsMap = secrets.get(keyVault); + if (secretsMap != null) { + secret = secretsMap.get(secretName); + } + return secret; + } + + /** + * Set the secret. + * + *

+ * For more information, see + * https://learn.microsoft.com/en-us/rest/api/keyvault/secrets/set-secret/set-secret?tabs=HTTP + *

+ * + * @param contentLength the content length. + * @param keyVault the key vault. + * @param secretName the secret name. + * @param inputStream the input stream. + * @return the response. + */ + @Path("{name}/secrets/{secretName}") + @PUT + public SecretBundle setSecret( + @HeaderParam("Content-Length") Integer contentLength, + @PathParam("name") String keyVault, + @PathParam("secretName") String secretName, InputStream inputStream) { + + SecretBundle secret; + + if (contentLength != null && contentLength > 0) { + Jsonb jsonb = JsonbBuilder.create(); + secret = jsonb.fromJson(inputStream, SecretBundle.class); + } else { + throw new WebApplicationException(500); + } + + Map secretsMap = secrets.get(keyVault); + if (secretsMap == null) { + secretsMap = new HashMap<>(); + secrets.put(keyVault, secretsMap); + } + + secretsMap.put(secretName, secret); + return secret; } } diff --git a/azure-keyvault/src/main/java/keyvault/SecretAttributes.java b/azure-keyvault/src/main/java/keyvault/SecretAttributes.java new file mode 100644 index 0000000..03a99b2 --- /dev/null +++ b/azure-keyvault/src/main/java/keyvault/SecretAttributes.java @@ -0,0 +1,4 @@ +package keyvault; + +public class SecretAttributes { +} diff --git a/azure-keyvault/src/main/java/keyvault/KeyVaultSecret.java b/azure-keyvault/src/main/java/keyvault/SecretBundle.java similarity index 52% rename from azure-keyvault/src/main/java/keyvault/KeyVaultSecret.java rename to azure-keyvault/src/main/java/keyvault/SecretBundle.java index 148263e..5618ba6 100644 --- a/azure-keyvault/src/main/java/keyvault/KeyVaultSecret.java +++ b/azure-keyvault/src/main/java/keyvault/SecretBundle.java @@ -5,40 +5,25 @@ * * @author Manfred Riem (mriem@manorrock.com) */ -public class KeyVaultSecret { - +public class SecretBundle { + /** - * Stores the id. + * Stores the attributes. */ - private String id; - + private SecretAttributes attributes; + /** * Stores the value. */ private String value; - - /** - * Constructor. - */ - public KeyVaultSecret() { - } /** - * Constructor. + * Get the attributes. * - * @param value the secret value. + * @return the attributes. */ - public KeyVaultSecret(String value) { - this.value = value; - } - - /** - * Get the id. - * - * @return the id. - */ - public String getId() { - return id; + public SecretAttributes getAttributes() { + return attributes; } /** @@ -49,14 +34,14 @@ public String getId() { public String getValue() { return value; } - + /** - * Set the id. + * Set the attributes. * - * @param id the id. + * @param attributes the attributes. */ - public void setId(String id) { - this.id = id; + public void setAttributes(SecretAttributes attributes) { + this.attributes = attributes; } /** diff --git a/azure-keyvault/src/test/java/keyvault/KeyVaultIT.java b/azure-keyvault/src/test/java/keyvault/KeyVaultIT.java index 1e6fa9d..1b95ec1 100644 --- a/azure-keyvault/src/test/java/keyvault/KeyVaultIT.java +++ b/azure-keyvault/src/test/java/keyvault/KeyVaultIT.java @@ -2,7 +2,8 @@ import com.azure.core.credential.BasicAuthenticationCredential; import com.azure.core.credential.TokenCredential; -import com.azure.identity.DefaultAzureCredentialBuilder; +import static com.azure.core.http.policy.HttpLogDetailLevel.BODY_AND_HEADERS; +import com.azure.core.http.policy.HttpLogOptions; import com.azure.security.keyvault.secrets.SecretClient; import com.azure.security.keyvault.secrets.SecretClientBuilder; import org.junit.jupiter.api.AfterEach; @@ -33,13 +34,15 @@ public void tearDown() { @Test public void testGetSecret() { - String keyVaultUri = "https://localhost:8200/api/keyvault/mykeyvault"; + String keyVaultUri = "https://localhost:8200/api/keyvault/myKeyvault"; SecretClient keyClient = new SecretClientBuilder() .vaultUrl(keyVaultUri) .credential(credential) + .httpLogOptions(new HttpLogOptions().setLogLevel(BODY_AND_HEADERS)) .buildClient(); - assertEquals("secretValue", keyClient.getSecret("secretKey").getValue()); + keyClient.setSecret("mySecret", "mySecretValue"); + assertEquals("mySecretValue", keyClient.getSecret("mySecret").getValue()); } }