diff --git a/docs/reference/README.md b/docs/reference/README.md index 7d3223a2..63d74858 100644 --- a/docs/reference/README.md +++ b/docs/reference/README.md @@ -21,6 +21,7 @@ - [gcp/gcr](./gcp/gcr.md) - Google Container Registry - [gcp/gcs](./gcp/gcs.md) - Google Cloud Storage - [gcp/gke](./gcp/gke.md) - Google Kubernetes Engine +- [gcp/secretmanager](./gcp/secretmanager.md) - Google Cloud Secret Manager - [git](./git.md) - Git operations - [go](./go.md) - Go build operations - [io](./io.md) - IO operations diff --git a/stdlib/.dagger/env/google-secretmanager/.gitignore b/stdlib/.dagger/env/google-secretmanager/.gitignore new file mode 100644 index 00000000..01ec19b0 --- /dev/null +++ b/stdlib/.dagger/env/google-secretmanager/.gitignore @@ -0,0 +1,2 @@ +# dagger state +state/** diff --git a/stdlib/.dagger/env/google-secretmanager/values.yaml b/stdlib/.dagger/env/google-secretmanager/values.yaml new file mode 100644 index 00000000..687d4e4b --- /dev/null +++ b/stdlib/.dagger/env/google-secretmanager/values.yaml @@ -0,0 +1,32 @@ +plan: + package: ./gcp/secretmanager/tests +name: google-secretmanager +inputs: + TestConfig.gcpConfig.project: + text: dagger-ci-324421 + TestConfig.gcpConfig.region: + text: us-west2 + TestConfig.gcpConfig.serviceKey: + secret: ENC[AES256_GCM,data:/8H0KaL2vHzKcinRtBpwnheo49WiFjN0kTnQ3ur82LF/8/GLOyW91tDEketPBhUJ7boeLWZoh0uashHeiySJ6uBBq7fi6ChJUrIgpE16VAFPlTTcyTSRGF/nynoyBe0loksAO2j32m4sBqZwjXvYc01a+A1IM5z5VRVmDqSfIHSIrsE/1zq67enoCOin6uLTmggrRFIu60WhNqD98JB2LC+M6GbchoaZh+3UI7IbUXYPepCYXwDLq8Qpcr5YxRbNfDsQaQx+Om8oc+KgJ2W7acz5uok8/sJG1w4GXvDFrLOz0WXMNxujru2ZvUOuMApaxeOVl/ahX3gtevOVmgQKMKAKf/Mcn1d4YNQbKIan7eQ/w/Hgt3W28DYLh6oI3k0A5a8q1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w503YIkOmPMM8SJ8DiBl2MGO16KTI2zvs75FpdAyVanAJmIvV5nLpUpyxEdNnStc+bhSURMpP4Mee7PsZOmAiZ9tw+7LkwN0PnHJ3CbxKQesw6zmkQ4sAyGQwGSJbo3ej87W/8f2EurGuHyn+3UhsNhzYNT/8HQz1UdwROBd46hmG8TiQYsFzSydFaL9fN4P3yoJC1VXnofmgxYaM5cFodP9k6iX1zzOe5ZWTZP/lsTDyl6UnaqdSSzfSkQA28XVj9TtRp62GnRwNgl4UkdNVItjjaAzb160x1WDJfbrVjEu58unlG/DPqvIzrbnstLBe5iVvCNqhTZUQUOPeOWGC0AIO083yoRT58fsTW3Lc2FUeNISnmvAUCcN4YNu+z3GcexmUH7ne9mYbfxLRwiIVpo0zXLYYRSQ/42n38TTqSDBMsbfujylO8zaaEd9/Vyzl2PMT1u8H1QGL0KgRcjC+OkNtRNoZ64qJVS7GVmg8iHyTsKWuqZhJC69OqS5L1QY3hqd38GVRzgOuy7JILWieFMLkX7AIzX616SEsU=,iv:m+Qoz57df2gJCv7sNbTLnaRrtv0d8ykzoJqeiCBP3Vw=,tag:clDEYDs9bq4G/2yw5bqKRg==,type:str] + TestSecrets.secret.secrets.databasePassword: + secret: ENC[AES256_GCM,data:hQcppp1Pj5k=,iv:91IMRRCRytEQ8WRSGRQIcKVnPJ9rIyikhAe8sxNNSKo=,tag:qnzIAoeHe0cDAm7xJtVOuQ==,type:str] +sops: + kms: [] + gcp_kms: [] + azure_kv: [] + hc_vault: [] + age: + - recipient: age1rjf5lcyh8g06d4l0ujty40jjqm075pd2hrz24wm3hgdw6zkawgdqep02wa + enc: | + -----BEGIN AGE ENCRYPTED FILE----- + YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSBldUNYQTlPeG1tK2IwZ1RR + L2lQZlRoNDM4MXRaRWVwUFRZVUloaUJPM25RClczUkZYT3VYbHhFck1FK0Y4bnAr + Zi9oUWZEcVgzdGdtS1RWU3J1RytTbEUKLS0tIDQwd2dxSXJGMWtjdVJkY3VOOGgx + cE9kYkVxZU1IbDlMOVVjVFhRT0RtN0UK/xqmdQCPXmUFIzm1rW2EGNOjGR0fYjIR + GsbeEnNhIso1eW48/LeNVlmypyRm05qV6uI5pTn4ecVTeYl9jWzCnA== + -----END AGE ENCRYPTED FILE----- + lastmodified: "2021-09-05T14:40:55Z" + mac: ENC[AES256_GCM,data:ukt19jzNMVS7FlKqagUbPb5d82yRWKXzU6Nwrm/bj4Yr4Fix8YujPmEPna32+EyPNCTwY9Wgoxvy7/QF3EootTcTl6FFH2l6v4aVJDIh4iGQo6HpFH1lYyBtDiDNVvLMOxxfqpahoRBpY0fNFOloEh/+Fr77WFwO+E2ksciYEXE=,iv:3yKQAXtTcmjyWfS5JCIOjhxj4JcdhMZnyMBc/6Tvc2A=,tag:aJPd868CiZ6+hcwIkCLK4w==,type:str] + pgp: [] + encrypted_suffix: secret + version: 3.7.1 diff --git a/stdlib/gcp/secretmanager/secrets.cue b/stdlib/gcp/secretmanager/secrets.cue new file mode 100644 index 00000000..5c5b83e3 --- /dev/null +++ b/stdlib/gcp/secretmanager/secrets.cue @@ -0,0 +1,76 @@ +// Google Cloud Secret Manager +package secretmanager + +import ( + "alpha.dagger.io/dagger" + "alpha.dagger.io/dagger/op" + "alpha.dagger.io/gcp" + "alpha.dagger.io/os" +) + +#Secrets: { + // GCP Config + config: gcp.#Config + + // Map of secrets + secrets: [name=string]: dagger.#Secret + + // Deploy encrypted secrets + deployment: os.#Container & { + image: gcp.#GCloud & {"config": config} + shell: path: "/bin/bash" + always: true + + for name, s in secrets { + secret: "/tmp/secrets/\(name)": s + } + + command: #""" + # Loop on all files, including hidden files + shopt -s dotglob + echo "{}" > /tmp/output.json + for FILE in /tmp/secrets/*; do + BOOL=0 # Boolean + gcloud secrets describe "${FILE##*/}" 2>/dev/null > /dev/null + status=$? + + # If secret not found + if [[ ! "${status}" -eq 0 ]]; then + (\ + RES=$(gcloud secrets create "${FILE##*/}" --replication-policy automatic --data-file "${FILE}" --format='value(name)' | sed -n '1!p') \ + && cat <<< $(cat /tmp/output.json | jq ".|.\"${FILE##*/}\"=\"$RES\"") > /tmp/output.json \ + ) || (echo "Error while creating secret ${FILE##*/}" >&2 && exit 1) + BOOL=1 + else + (\ + RES=$(gcloud secrets versions add "${FILE##*/}" --data-file "${FILE}" --format='value(name)' | sed -n '1!p') \ + && cat <<< $(cat /tmp/output.json | jq ".|.\"${FILE##*/}\"=\"$RES\"") > /tmp/output.json \ + ) || (echo "Error while updating secret ${FILE##*/}" >&2 && exit 1) + BOOL=1 + fi + if [ $BOOL -eq 0 ]; then + (\ + RES=$(gcloud secrets describe "${FILE##*/}" --format='value(name)') \ + && cat <<< $(cat /tmp/output.json | jq ".|.\"${FILE##*/}\"=\"$RES\"") > /tmp/output.json \ + ) || (echo "Error while retrieving secret ${FILE##*/}" >&2 && exit 1) + fi + done + """# + } + + // dynamic references + references: { + [string]: string + } + + references: #up: [ + op.#Load & { + from: deployment + }, + + op.#Export & { + source: "/tmp/output.json" + format: "json" + }, + ] +} diff --git a/stdlib/gcp/secretmanager/tests/secrets.cue b/stdlib/gcp/secretmanager/tests/secrets.cue new file mode 100644 index 00000000..c2e7ed12 --- /dev/null +++ b/stdlib/gcp/secretmanager/tests/secrets.cue @@ -0,0 +1,33 @@ +package secretmanager + +import ( + "alpha.dagger.io/dagger" + "alpha.dagger.io/gcp" + "alpha.dagger.io/gcp/secretmanager" + "alpha.dagger.io/os" +) + +TestConfig: gcpConfig: gcp.#Config + +TestSecrets: { + secret: secretmanager.#Secrets & { + config: TestConfig.gcpConfig + secrets: { + databasePassword: dagger.#Secret @dagger(input) + } + } + + if len(secret.references) > 0 { + cleanup: os.#Container & { + image: gcp.#GCloud & { + config: TestConfig.gcpConfig + } + shell: path: "/bin/bash" + always: true + + command: #""" + gcloud -q secrets delete databasePassword + """# + } + } +} diff --git a/stdlib/universe.bats b/stdlib/universe.bats index b0470e94..0bc69ce6 100644 --- a/stdlib/universe.bats +++ b/stdlib/universe.bats @@ -172,6 +172,16 @@ setup() { dagger -e google-gke up } +@test "google cloud: secretmanager" { + run dagger -e google-secretmanager up + assert_success + + # ensure the secret has been created + run dagger query -e google-secretmanager TestSecrets.secret.references.databasePassword -f text + assert_success + assert_output --regexp '^projects\/[0-9]+\/secrets\/databasePassword' +} + @test "google cloud: cloudrun" { dagger -e google-cloudrun up }