我正在尝试在我的本地 circleci 上配置一个作业(使用 docker executor,image: google/cloud-sdk:latest
),并且该作业需要解密一个 sops gcp kms 加密文件。我已经为 gcp kms 解密服务设置了一个谷歌服务帐户(我可以运行脚本,通过 circleci 作业运行,通过服务帐户解密 sops 文件在本地成功,所以我知道服务帐户设置是有效的) . 这是我的工作方式。
1-我base64编码谷歌服务帐户json文件:base64 path/to/service_aacount_file.json
2- 我运行 circleci 作业,在 circleci 上设置 GCLOUD_SERVICE_KEY 环境变量,使用上一步中的 base64 编码内容:circleci local execute --env GCLOUD_SERVICE_KEY='<Base64EncodedServiceAccountJsonFileContent>' --job '<MyJob>'
3-这是我的circleci配置:
- run:
name: <MyJob>
command: |
apt-get install -y docker
apt-get install -y sudo
cd $(pwd)/path/to/jobcode
echo $GCLOUD_SERVICE_KEY | base64 -d > ${HOME}/<MyGoogleServiceAccountJsonFile.json>
export GOOGLE_APPLICATION_CREDENTIALS="${HOME}/<MyGoogleServiceAccountJsonFile.json>"
gcloud auth activate-service-account --key-file ${HOME}/<MyGoogleServiceAccountJsonFile.json>
echo $GOOGLE_APPLICATION_CREDENTIALS
ls -halt $GOOGLE_APPLICATION_CREDENTIALS
cat $GOOGLE_APPLICATION_CREDENTIALS
sudo ./<RunJob.sh>
4-执行作业时出现以下错误:
Failed to get the data key required to decrypt the SOPS file.
Group 0: FAILED
projects/<MyProject>/locations/<MyLocation>/keyRings/<MySopsKeyring>/cryptoKeys/<MyKey>: FAILED
- | Cannot create GCP KMS service: google: could not find
| default credentials. See
| https://developers.google.com/accounts/docs/application-default-credentials
| for more information.
Recovery failed because no master key was able to decrypt the file. In
order for SOPS to recover the file, at least one key has to be successful,
but none were.
5-此外,从控制台输出:
a- 我可以看到服务帐户已成功激活:Activated service account credentials for: [<MyServiceAccount>@<MyProject>.iam.gserviceaccount.com]
b- GOOGLE_APPLICATION_CREDENTIALS 环境变量设置为服务帐户 json 的路径:/path/to/service_account.json
c- 上述文件已正确进行 base64 解码并包含有效的 json:
{
"client_x509_cert_url": "<MyUrl>",
"auth_uri": "<MyAuthUri>",
"private_key": "<MyPrivateKey>",
"client_email": "<ClientEmail>",
"private_key_id": "<PrivateKeyId>",
"client_id": "<ClientId>",
"token_uri": "<TokenUri>",
"project_id": "<ProjectId>",
"type": "<ServiceAccount>",
"auth_provider_x509_cert_url": "<AuthProviderCertUrl>"
}
6-我尝试过的其他一些事情:
a- 尝试在环境变量中设置谷歌项目名称,但仍然是同样的错误。
b-尝试将 GOOGLE_APPLICATION_CREDENTIALS 设置为文件的内容,而不是文件路径,但结果相同。
c- 尝试通过提供不带引号或单引号的文件路径来设置 GOOGLE_APPLICATION_CREDENTIALS,但仍然没有区别。
d- 尝试通过做设置 $BASH_ENV echo 'export GOOGLE_APPLICATION_CREDENTIALS=path/to/service_account.json' >> $BASH_ENV
,但同样的错误
请帮忙。