Skip to content

Commit

Permalink
Fixes #302 - Add Set Secret
Browse files Browse the repository at this point in the history
  • Loading branch information
mnriem committed Dec 31, 2023
1 parent 9fd33f3 commit c6e1bde
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 38 deletions.
15 changes: 15 additions & 0 deletions azure-keyvault/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@
<artifactId>azure-identity</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-json</artifactId>
<version>1.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-xml</artifactId>
<version>1.0.0-beta.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-security-keyvault-secrets</artifactId>
Expand Down Expand Up @@ -93,6 +105,9 @@
</execution>
</executions>
<configuration>
<environmentVariables>
<AZURE_LOG_LEVEL>verbose</AZURE_LOG_LEVEL>
</environmentVariables>
<systemPropertyVariables>
<javax.net.ssl.trustStore>${basedir}/src/test/certs/keystore</javax.net.ssl.trustStore>
<javax.net.ssl.trustStorePassword>password</javax.net.ssl.trustStorePassword>
Expand Down
74 changes: 68 additions & 6 deletions azure-keyvault/src/main/java/keyvault/KeyVaultResource.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,95 @@
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.
*
* @author Manfred Riem ([email protected])
*/
@Path("keyvault")
@Singleton
public class KeyVaultResource {

/**
* Stores the secrets.
*/
private Map<String, Map<String, SecretBundle>> secrets = new HashMap<>();

/**
* Get the secret.
*
*
* <p>
* 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
* </p>
*
*
* @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<String, SecretBundle> secretsMap = secrets.get(keyVault);
if (secretsMap != null) {
secret = secretsMap.get(secretName);
}
return secret;
}

/**
* Set the secret.
*
* <p>
* For more information, see
* https://learn.microsoft.com/en-us/rest/api/keyvault/secrets/set-secret/set-secret?tabs=HTTP
* </p>
*
* @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<String, SecretBundle> secretsMap = secrets.get(keyVault);
if (secretsMap == null) {
secretsMap = new HashMap<>();
secrets.put(keyVault, secretsMap);
}

secretsMap.put(secretName, secret);
return secret;
}
}
4 changes: 4 additions & 0 deletions azure-keyvault/src/main/java/keyvault/SecretAttributes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package keyvault;

public class SecretAttributes {
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,25 @@
*
* @author Manfred Riem ([email protected])
*/
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;
}

/**
Expand All @@ -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;
}

/**
Expand Down
9 changes: 6 additions & 3 deletions azure-keyvault/src/test/java/keyvault/KeyVaultIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}

0 comments on commit c6e1bde

Please sign in to comment.