[英]How to retrieve secret from cross region AWS Secret Manager in terraform
[英]How to retrive secret from AWS Secret Manager during GitLab CI/CD
在 GitLab 的 CI/CD 管道中,我想從 AWS 機密管理器中檢索機密並將其分配給變量DATABASE_CONNECTION
。 跑步者可以訪問 AWS SM,我想使用 AWS CLI 來做到這一點
deploy-dev: # This job runs in the deploy stage.
image: dockerxxxx/awscli
stage: deploy
environment:
name: development
script:
- aws secretsmanager get-secret-value --secret-id MyTestSecret
這將返回 JSON 類似於
{
"ARN": "arn:aws:secretsmanager:us-west-2:123456789012:secret:MyTestSecret-a1b2c3",
"Name": "MyTestSecret",
"VersionId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111",
"SecretString": "{\"DB_URL\":\"connection string\"}",
"VersionStages": [
"AWSCURRENT"
],
"CreatedDate": 1523477145.713
}
如何在腳本中將 DB_URL 的值分配給 DATABASE_CONNECTION?
我知道 GitLab 有可變選項來存儲帶有掩碼的秘密。 但出於安全原因,我不允許使用該選項。 GitLab 已與保險庫集成,但我也沒有該選項
您可以使用get-secret-value
命令的--output
和--query
參數來獲取原始密碼文本作為命令的結果。 在這種情況下,由於您的密鑰是 JSON 格式的字符串,因此您可以使用jq
解析密鑰中的 JSON。
script: # be sure to install `jq` if needed
- jq --version # make sure jq is installed!
- secret_value="$(aws secretsmanager get-secret-value --secret-id MyTestSecret --output text --query SecretString)"
- db_url="$(jq -r .DB_URL <<< $secret_value)"
# db_url will contain the value of the DB_URL key in your secret JSON string
- export MY_APP_CONNECTION_STRING="$db_url" # or however you need to use it
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.