-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
107 additions
and
38 deletions.
There are no files selected for viewing
This file contains 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
74 changes: 68 additions & 6 deletions
74
azure-keyvault/src/main/java/keyvault/KeyVaultResource.java
This file contains 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 |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains 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,4 @@ | ||
package keyvault; | ||
|
||
public class SecretAttributes { | ||
} |
This file contains 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 |
---|---|---|
|
@@ -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; | ||
} | ||
|
||
/** | ||
|
@@ -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; | ||
} | ||
|
||
/** | ||
|
This file contains 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