You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: examples/src/main/java/io/dapr/examples/secrets/README.md
+61-145
Original file line number
Diff line number
Diff line change
@@ -1,31 +1,28 @@
1
1
# Dapr's Secret Store Sample
2
2
3
-
In this sample, we'll see how to retrieve a secret using Dapr's Java SDK.
3
+
In this sample, we'll see how to retrieve a secret using Dapr's Java SDK.
4
4
This sample includes two files:
5
5
6
-
* docker-compose-vault.yml (Starts Hashicorp's Vault as a container)
7
6
* 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)
7
+
* Existing Dapr component file in `< repo dir >/examples/components/local_file.yaml`
10
8
11
9
Visit [this](https://docs.dapr.io/developing-applications/building-blocks/secrets/secrets-overview/) link for more information about secret stores in Dapr.
12
-
10
+
13
11
## Secret store sample using the Java-SDK
14
12
15
-
In this example, the component used is Hashicorp Vault, but others are also available.
13
+
In this example, the component used is local file(not recommended for production use), but others are also available.
16
14
17
-
Visit [this](https://github.com/dapr/components-contrib/tree/master/secretstores) link for more information about secret stores implementations.
15
+
Visit [this](https://github.com/dapr/components-contrib/tree/master/secretstores) link for more information about secret store implementations.
18
16
19
17
20
18
## Pre-requisites
21
19
22
20
*[Dapr and Dapr Cli](https://docs.dapr.io/getting-started/install-dapr/).
@@ -49,167 +46,89 @@ Then get into the examples directory:
49
46
cd examples
50
47
```
51
48
52
-
### Setting Vault locally
53
-
54
-
Before getting into the application code, follow these steps in order to set up a local instance of Vault. This is needed for the local instances. Steps are:
55
-
56
-
1. To run the vault container locally run:
57
-
<!-- Docker is writing output to stderr ... -->
58
-
59
-
<!-- STEP
60
-
name: Start vault
61
-
expected_stderr_lines:
62
-
- 'Creating network "secrets_default" with the default driver'
63
-
sleep: 10
64
-
-->
65
-
66
-
```bash
67
-
docker-compose -f ./src/main/java/io/dapr/examples/secrets/docker-compose-vault.yml up -d
68
-
```
69
-
70
-
<!-- END_STEP -->
71
-
72
-
2. Run `docker ps` to see the container running locally:
73
-
74
-
```bash
75
-
342d3522ca14 vault "docker-entrypoint.s…" 34 seconds ago Up About
76
-
a minute 0.0.0.0:8200->8200/tcp secrets_hashicorp_vault_1
77
-
```
78
-
Click [here](https://hub.docker.com/_/vault/) for more information about the container image for Hashicorp's Vault.
79
-
80
-
### Create a secret in Vault
81
-
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:
82
-
83
-
Export the `VAULT_ADDR` for vault CLI:
84
-
```bash
85
-
export VAULT_ADDR=http://127.0.0.1:8200/
86
-
```
87
-
88
-
Login to Hashicorp's Vault:
89
-
90
-
<!-- STEP
91
-
name: Vault login
92
-
expected_stdout_lines:
93
-
- "Success! You are now authenticated. The token information displayed below"
94
-
- "token myroot"
95
-
env:
96
-
VAULT_ADDR: "http://127.0.0.1:8200/"
97
-
-->
98
-
99
-
```bash
100
-
vault login myroot
101
-
```
102
-
103
-
<!-- END_STEP -->
104
-
105
-
Create secret (replace `$MY_FAVORITE_MOVIE` with a title of our choice):
106
-
107
-
<!-- STEP
108
-
name: Create movie vault secret
109
-
expected_stdout_lines:
110
-
- "version 1"
111
-
env:
112
-
VAULT_ADDR: "http://127.0.0.1:8200/"
113
-
MY_FAVORITE_MOVIE: "Star Wars"
114
-
-->
115
-
116
-
```bash
117
-
vault kv put secret/dapr/movie title="$MY_FAVORITE_MOVIE"
118
-
```
119
-
120
-
<!-- END_STEP -->
49
+
### Creating a JSON secret file locally
121
50
122
-
Create random secret:
51
+
Dapr's API for secret store only support read operations. For this sample to run, we will first create a secret file with a JSON string that contains two keys: `redisPassword` and `randomKey`.
123
52
124
53
<!-- STEP
125
-
name: Create random vault secret
126
-
expected_stdout_lines:
127
-
- "version 1"
128
-
env:
129
-
VAULT_ADDR: "http://127.0.0.1:8200/"
54
+
name: create local file
130
55
-->
131
56
132
57
```bash
133
-
vault kv put secret/dapr/randomKey testVal="value"
The program receives one and only one argument: the secret's key to be fetched.
184
-
After identifying the key to be fetched, it will retrieve it from the pre-defined secret store: `vault`.
185
-
The secret store's name **must** match the component's name defined in `< repo dir >/examples/components/hashicorp_vault.yaml`.
105
+
The program receives two arguments at least: one's the secret store name and the others are secret's keys to be fetched.
106
+
After identifying the secret store name that created and the keys to be fetched, it will retrieve them from the pre-defined secret store: `< repo dir >/examples/components/secrets/secret.json`.
107
+
The secret store's name **must** match the component's name defined in `< repo dir >/examples/components/secrets/local_file.yaml`.
186
108
The Dapr client is also within a try-with-resource block to properly close the client at the end.
187
109
188
-
Execute the following script in order to run the example:
110
+
Execute the following script in order to run the example:
189
111
190
112
<!-- STEP
191
113
name: Validate normal run
192
114
expected_stdout_lines:
193
-
- '== APP == {"title":"Star Wars"}'
194
-
- '== APP == {"testVal":"value"}'
195
-
env:
196
-
VAULT_ADDR: "http://127.0.0.1:8200/"
115
+
- '== APP == {"redisPassword":"root123"}'
116
+
- '== APP == {"randomKey":"value"}'
197
117
background: true
198
118
sleep: 5
199
119
-->
200
120
201
121
```bash
202
-
dapr run --components-path ./components/secrets --app-id secrets1 -- java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.secrets.SecretClient movie
0 commit comments