|
| 1 | +# Dapr's Secret Store Sample |
| 2 | + |
| 3 | +In this sample, we'll see how to retrieve a secret using Dapr's Java SDK. |
| 4 | +This sample includes two files: |
| 5 | + |
| 6 | +* docker-compose-vault.yml (Starts Hashicorp's Vault as a container) |
| 7 | +* SecretClient.java (Reads a secret from Dapr's Secret Store) |
| 8 | +* Existing Dapr component file in `< repo dir >/examples/components/hashicorp_vault.yaml` |
| 9 | +* Existing token file in `< repo dir >/examples/.hashicorp_vault_token` (Consumed by `daprd`'s vault component above) |
| 10 | + |
| 11 | +Visit [this](https://github.com/dapr/docs/tree/master/concepts/secrets) link for more information about secret stores in Dapr. |
| 12 | + |
| 13 | +## Secret store sample using the Java-SDK |
| 14 | + |
| 15 | +In this example, the component used is Hashicorp Vault, but others are also available. |
| 16 | + |
| 17 | +Visit [this](https://github.com/dapr/components-contrib/tree/master/secretstores) link for more information about secret stores implementations. |
| 18 | + |
| 19 | + |
| 20 | +## Pre-requisites |
| 21 | + |
| 22 | +* [Dapr and Dapr Cli](https://github.com/dapr/docs/blob/master/getting-started/environment-setup.md#environment-setup). |
| 23 | +* Java JDK 11 (or greater): [Oracle JDK](https://www.oracle.com/technetwork/java/javase/downloads/index.html#JDK11) or [OpenJDK](https://jdk.java.net/13/). |
| 24 | +* [Apache Maven](https://maven.apache.org/install.html) version 3.x. |
| 25 | +* Hashicorp's vault client [installed](https://www.vaultproject.io/docs/install/). |
| 26 | + |
| 27 | +### Checking out the code |
| 28 | + |
| 29 | +Clone this repository: |
| 30 | + |
| 31 | +```sh |
| 32 | +git clone https://github.com/dapr/java-sdk.git |
| 33 | +cd java-sdk |
| 34 | +``` |
| 35 | + |
| 36 | +Then build the Maven project: |
| 37 | + |
| 38 | +```sh |
| 39 | +# make sure you are in the `java-sdk` directory. |
| 40 | +mvn install |
| 41 | +``` |
| 42 | +### Setting Vault locally |
| 43 | + |
| 44 | +Before getting into the application code, follow these steps in order to setup a local instance of Vault. This is needed for the local instances. Steps are: |
| 45 | + |
| 46 | +1. navigate to the [repo-root] with `cd java-sdk` |
| 47 | +2. Run `docker-compose -f ./examples/src/main/java/io/dapr/examples/secrets/docker-compose-vault.yml up -d` to run the container locally |
| 48 | +3. Run `docker ps` to see the container running locally: |
| 49 | + |
| 50 | +```bash |
| 51 | +342d3522ca14 vault "docker-entrypoint.s…" 34 seconds ago Up About |
| 52 | +a minute 0.0.0.0:8200->8200/tcp secrets_hashicorp_vault_1 |
| 53 | +``` |
| 54 | +Click [here](https://hub.docker.com/_/vault/) for more information about the container image for Hashicorp's Vault. |
| 55 | + |
| 56 | +### Create a secret in Vault |
| 57 | +Dapr's API for secret store only support read operations. For this sample to run, we will first create a secret via the Vault's cli commands: |
| 58 | + |
| 59 | +1. Login: |
| 60 | +```bash |
| 61 | +vault login myroot |
| 62 | +``` |
| 63 | + |
| 64 | +2. Create secret (replace `[my favorite movie]` with a title of our choice): |
| 65 | +```bash |
| 66 | +vault kv put secret/dapr/movie title="[my favorite movie]" |
| 67 | +``` |
| 68 | + |
| 69 | +In the command above, `secret` means the secret engine in Hashicorp's Vault. |
| 70 | +Then, `dapr` is the prefix as defined in `< repo dir >/examples/components/hashicorp_vault.yaml`. |
| 71 | +Finally, `movie` is the secret name and then a `key=value` pair. |
| 72 | + |
| 73 | +A secret in dapr is a dictionary. In this sample, only one key-value pair is used but more can be added as an exercise for the reader. |
| 74 | + |
| 75 | +### Running the secret store sample |
| 76 | + |
| 77 | +The example's main function is in `SecretClient.java`. |
| 78 | + |
| 79 | +```java |
| 80 | +public class SecretClient { |
| 81 | + |
| 82 | + private static final String SECRET_STORE_NAME = "vault"; |
| 83 | + |
| 84 | + ///... |
| 85 | + |
| 86 | + public static void main(String[] args) throws Exception { |
| 87 | + ///... |
| 88 | + String secretKey = args[0]; |
| 89 | + DaprClient client = (new DaprClientBuilder()).build(); |
| 90 | + Map<String, String> secret = client.getSecret(SECRET_STORE_NAME, secretKey).block(); |
| 91 | + System.out.println(JSON_SERIALIZER.writeValueAsString(secret)); |
| 92 | + } |
| 93 | +///... |
| 94 | +} |
| 95 | +``` |
| 96 | +The program receives one and only one argument: the secret's key to be fetched. |
| 97 | +After identifying the key to be fetched, it will retrieve it from the pre-defined secret store: `vault`. |
| 98 | +The secret store's name **must** match the component's name defined in `< repo dir >/examples/components/hashicorp_vault.yaml`. |
| 99 | + |
| 100 | + Execute the follow script in order to run the example: |
| 101 | +```sh |
| 102 | +cd to [repo-root]/examples |
| 103 | +dapr run -- java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.secrets.SecretClient movie |
| 104 | +``` |
| 105 | + |
| 106 | +Once running, the program should print the output as follows: |
| 107 | + |
| 108 | +``` |
| 109 | +== APP == {"title":"[my favorite movie]"} |
| 110 | +``` |
| 111 | + |
| 112 | +Thanks for playing. |
0 commit comments