Skip to content

Commit bf62fe8

Browse files
committed
do not bake kms in image
to account for workspaces
1 parent 13ca545 commit bf62fe8

4 files changed

Lines changed: 45 additions & 25 deletions

File tree

Dockerfile

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,11 @@ RUN apt-get update && apt-get install -y wget unzip
66
RUN wget -q https://releases.hashicorp.com/vault/${VAULT_VERSION}/vault_${VAULT_VERSION}_linux_amd64.zip
77
RUN unzip vault_${VAULT_VERSION}_linux_amd64.zip
88

9-
FROM alpine:3.23@sha256:5b10f432ef3da1b8d4c7eb6c487f2f5a8f096bc91145e68878dd4a5019afde11 as config
10-
ARG KMS_KEY_RING=vault-server
11-
ARG KMS_CRYPTO_KEY=vault
12-
COPY vault-server.hcl.tmpl /tmp/vault-server.hcl.tmpl
13-
RUN sed \
14-
-e "s/__KMS_KEY_RING__/${KMS_KEY_RING}/g" \
15-
-e "s/__KMS_CRYPTO_KEY__/${KMS_CRYPTO_KEY}/g" \
16-
/tmp/vault-server.hcl.tmpl > /tmp/config.hcl
17-
18-
FROM alpine:3.23@sha256:5b10f432ef3da1b8d4c7eb6c487f2f5a8f096bc91145e68878dd4a5019afde11 as certs
9+
FROM alpine:3.23@sha256:5b10f432ef3da1b8d4c7eb6c487f2f5a8f096bc91145e68878dd4a5019afde11
1910
RUN apk --update add ca-certificates
20-
21-
FROM scratch
22-
COPY --from=certs /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
11+
RUN mkdir -p /etc/vault
2312
COPY --from=builder /vault .
24-
COPY --from=config /tmp/config.hcl /etc/vault/config.hcl
25-
ENTRYPOINT ["/vault", "server", "-config", "/etc/vault/config.hcl"]
13+
COPY vault-server.hcl.tmpl /etc/vault/config.hcl.tmpl
14+
COPY docker-entrypoint.sh /docker-entrypoint.sh
15+
RUN chmod +x /docker-entrypoint.sh
16+
ENTRYPOINT ["/docker-entrypoint.sh"]

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ The GCP project needs the following non-standard APIs enabled:
2020

2121
After this module has been ran, the Vault server is up and running and has been initialized. The root token is encrypted in a GCS bucket.
2222

23+
The Vault image now renders its seal config at container startup from the
24+
runtime `KMS_KEY_RING` and `KMS_CRYPTO_KEY` environment variables. That keeps
25+
the KMS binding out of the built image so multiple environments can safely
26+
share the same module code and repository without image-content drift.
27+
The module also forces the Vault image build to `linux/amd64` so Cloud Run
28+
always receives a compatible image even when Terraform runs from Apple Silicon
29+
or another non-amd64 host.
30+
2331
If you list the GCS storage bucket you will see a new set of directories created by Vault:
2432

2533
```
@@ -116,7 +124,7 @@ HA Enabled false
116124

117125
| Name | Source | Version |
118126
|------|--------|---------|
119-
| <a name="module_vault"></a> [vault](#module\_vault) | git::https://github.com/libops/terraform-cloudrun-v2 | 0.5.1 |
127+
| <a name="module_vault"></a> [vault](#module\_vault) | git::https://github.com/libops/terraform-cloudrun-v2 | 0.5.2 |
120128

121129
## Resources
122130

@@ -165,4 +173,4 @@ HA Enabled false
165173
| <a name="output_key_bucket"></a> [key\_bucket](#output\_key\_bucket) | n/a |
166174
| <a name="output_repo"></a> [repo](#output\_repo) | n/a |
167175
| <a name="output_vault-url"></a> [vault-url](#output\_vault-url) | The URL to the Vault instance. |
168-
<!-- END_TF_DOCS -->
176+
<!-- END_TF_DOCS -->

docker-entrypoint.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/sh
2+
3+
set -eu
4+
5+
: "${KMS_KEY_RING:?KMS_KEY_RING is required}"
6+
: "${KMS_CRYPTO_KEY:?KMS_CRYPTO_KEY is required}"
7+
8+
escape_sed_replacement() {
9+
printf '%s' "$1" | sed 's/[&|]/\\&/g'
10+
}
11+
12+
config_path="${VAULT_CONFIG_PATH:-/tmp/vault-config.hcl}"
13+
14+
sed \
15+
-e "s|__KMS_KEY_RING__|$(escape_sed_replacement "$KMS_KEY_RING")|g" \
16+
-e "s|__KMS_CRYPTO_KEY__|$(escape_sed_replacement "$KMS_CRYPTO_KEY")|g" \
17+
/etc/vault/config.hcl.tmpl > "$config_path"
18+
19+
exec /vault server -config "$config_path"

main.tf

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ locals {
2525
gsa = "${local.account_id}@${var.project}.iam.gserviceaccount.com"
2626
vault_image_context_sha = sha1(join("", [
2727
filesha1("${path.module}/Dockerfile"),
28+
filesha1("${path.module}/docker-entrypoint.sh"),
2829
filesha1("${path.module}/vault-server.hcl.tmpl"),
2930
]))
3031
data_bucket_name = trimspace(var.data_bucket_name) != "" ? trimspace(var.data_bucket_name) : lower(
@@ -95,22 +96,17 @@ resource "google_artifact_registry_repository" "private" {
9596
# docker build vault server image
9697
resource "docker_image" "vault" {
9798
name = local.image_name
99+
platform = "linux/amd64"
98100

99101
build {
100102
context = path.module
101103
dockerfile = "Dockerfile"
102-
build_args = {
103-
KMS_KEY_RING = var.kms_key_ring_name
104-
KMS_CRYPTO_KEY = var.kms_key_name
105-
}
106104
}
107105

108106
keep_locally = false
109107

110108
triggers = {
111109
dir_sha = local.vault_image_context_sha
112-
ring = var.kms_key_ring_name
113-
key = var.kms_key_name
114110
}
115111
}
116112

@@ -122,8 +118,6 @@ resource "docker_registry_image" "vault" {
122118

123119
triggers = {
124120
dir_sha = local.vault_image_context_sha
125-
ring = var.kms_key_ring_name
126-
key = var.kms_key_name
127121
}
128122
}
129123

@@ -200,6 +194,14 @@ module "vault" {
200194
name = "GOOGLE_PROJECT"
201195
value = var.project
202196
},
197+
{
198+
name = "KMS_KEY_RING"
199+
value = var.kms_key_ring_name
200+
},
201+
{
202+
name = "KMS_CRYPTO_KEY"
203+
value = var.kms_key_name
204+
},
203205
{
204206
name = "GOOGLE_STORAGE_BUCKET"
205207
value = google_storage_bucket.vault["data"].name

0 commit comments

Comments
 (0)