Skip to content

Commit cc2a29f

Browse files
committed
fix: stronger wording about Vault root token usage.
1 parent f7cabb4 commit cc2a29f

File tree

1 file changed

+38
-22
lines changed

1 file changed

+38
-22
lines changed

content/posts/2025-03-04-spring_vault.md

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -62,25 +62,13 @@ First, set up a Vault instance for local development using `docker compose`:
6262
+ timeout: 30s
6363
+ retries: 10
6464
+ start_period: 1s
65-
+ vault-init:
66-
+ image: docker.io/hashicorp/vault:latest
67-
+ depends_on:
68-
+ vault: {condition: service_healthy}
69-
+ mysql: {condition: service_healthy}
70-
+ environment:
71-
+ - MYSQL_ROOT_PASSWORD=my_password
72-
+ - VAULT_TOKEN=my-vault-root-token
73-
+ - VAULT_ADDR=http://vault:8200
74-
+ volumes:
75-
+ - ./scripts/vault-init.sh:/vault-init.sh
76-
+ command: sh /vault-init.sh
7765
app:
7866
build: .
7967
ports: [ "8080:8080" ]
8068
```
8169
</details>
8270

83-
Then add a script to [configure the MySQL secrets engine in Vault][vault-mysql] using Vault's built-in database credential management plugins:
71+
Once Vault is up and running, add a script to [configure the MySQL secrets engine in Vault][vault-mysql] using Vault's built-in database credential management plugins:
8472

8573
<details open><summary><code>scripts/vault-init.sh</code></summary>
8674

@@ -117,6 +105,38 @@ vault write database/roles/$ROLE_NAME \
117105

118106
Note that this Vault database role has no `max_ttl` so that `spring-cloud-vault` can refresh its dynamic database credentials indefinitely.
119107

108+
You can use docker-compose to ensure run this script runs before your server boots:
109+
<details open><summary><code>compose.yaml</code></summary>
110+
111+
```diff
112+
--- a/compose.yaml
113+
+++ b/compose.yaml
114+
@@ -11,6 +11,33 @@ vault:
115+
timeout: 30s
116+
retries: 10
117+
start_period: 1s
118+
+ vault-init:
119+
+ image: docker.io/hashicorp/vault:latest
120+
+ depends_on:
121+
+ vault: {condition: service_healthy}
122+
+ mysql: {condition: service_healthy}
123+
+ environment:
124+
+ - MYSQL_ROOT_PASSWORD=my_password
125+
+ - VAULT_TOKEN=my-vault-root-token # for demo only
126+
+ - VAULT_ADDR=http://vault:8200
127+
+ volumes:
128+
+ - ./scripts/vault-init.sh:/vault-init.sh
129+
+ command: sh /vault-init.sh
130+
app:
131+
build: .
132+
ports: [ "8080:8080" ]
133+
mysql: {condition: service_healthy}
134+
+ vault: {condition: service_healthy}
135+
+ vault-init: {condition: service_completed_successfully}
136+
volumes:
137+
```
138+
</details>
139+
120140
<!-- https://github.com/rptcloud/spring-cloud-vault-demo/commit/f8537658836bf0945be57c8cc68e8ec8433f814a -->
121141

122142
Next, add `spring-cloud-vault` to your dependencies:
@@ -200,10 +220,10 @@ Also, in this demo, the Spring Boot app needs a `$VAULT_TOKEN` to authenticate:
200220
ports: [ "8080:8080" ]
201221
depends_on:
202222
mysql: {condition: service_healthy}
203-
+ vault: {condition: service_healthy}
204-
+ vault-init: {condition: service_completed_successfully}
223+
vault: {condition: service_healthy}
224+
vault-init: {condition: service_completed_successfully}
205225
+ environment:
206-
+ - VAULT_TOKEN=my-vault-root-token
226+
+ - VAULT_TOKEN=my-vault-root-token # for demo only
207227
volumes:
208228
- ./config/application.yaml:/opt/app/config/application.yaml:ro
209229
# mount an external config file in a location that Spring Boot will check
@@ -212,12 +232,8 @@ Also, in this demo, the Spring Boot app needs a `$VAULT_TOKEN` to authenticate:
212232

213233
</details>
214234

215-
<aside>
216-
217-
(This example uses the Vault root token that we configured when setting up Vault.
218-
In anything more than a demo you should probably use one of Vault's many supported [authentication methods][vault-auth-methods] to use identity providers such as AWS, Azure, or Kubernetes to authenticate to Vault.)
219-
220-
</aside>
235+
Note that this example app uses Vault's root token to authenticate to Vault for demonstration purposes only.
236+
In production you should authenticate to Vault using a trusted identity from AWS, Azure, Kubernetes, or one of Vault's other [authentication methods][vault-auth-methods].
221237

222238

223239
Now if you start your app, it should successfully initialize:

0 commit comments

Comments
 (0)