我正在努力寻找将 secret 配置数据传递给我的 Node.Js 应用程序的最佳方式。
问题是我需要它在我的本地机器上运行,在 CI 环境中进行测试和登台,以及在生产 (AWS) 上运行。
我正在考虑使用此处描述的 docker secret :
https://medium.com/better-programming/how-to-handle-docker-secrets-in-node-js-3aa04d5bf46e
问题在于,它仅在您将 Docker 作为服务(通过 Swarm)运行时才有效,我可以在本地执行此操作,但不能在 AWS ECS 和 CI 上执行。 (或者我在那里遗漏了什么?)
然后我也可以使用 Amazon secret ,但我如何在我的 CI 环境和本地环境中获取它们?或者如果我没有互联网?
有没有办法制作一个单独的文件或我可以在每个环境中使用的东西,无论它是通过docker run
运行的本地文件吗?或 CI
一个或 AWS ECS
一?
最佳答案
Isn't there a way to make like a separate file or something that I could use for every environment no matter whether it's my local one running via docker run or the CI one or the AWS ECS one? Or if I don't have the internet?
定位 N-环境条件如 没有互联网 是一种很难在 AWS 服务上中继的东西,用于保密,如存储参数等。
我可以建议使用 dot env这将是环境独立的,您只需要处理来自不同来源的不同环境,例如
因此,处理这个每个环境都会消耗适当的 Dot ENV 文件,您可以在 中添加逻辑Docker entrypoint .
#!/bin/sh
if [ "${NODE_ENV}" == "production" ];then
# in production we are in aws and we can pull dot env file from s3
aws s3 cp s3://mybucket/production/.env /app/.env
elif [ "${NODE_ENV}" == "staging" ];then
# in staging we also assume that we are in aws and we can pull dot env file from s3
aws s3 cp s3://mybucket/staging/.env /app/.env
elif [ "${NODE_ENV}" == "ci" ];then
# generate dynamic ENV or pull from s3
aws s3 cp s3://mybucket/ci/.env
else
echo "running against local env, please dot env file like docker run -it $PWD/.env:/app/.env ..."
fi
echo "Startin node application"
exec node "$@
在 s3 上启用加密,加上只有生产环境应该能够提取生产环境文件,更强大的策略将导致更安全的机制。对于本地设置,您可以尝试
docker run -it -e NODE_ENV="local" --rm $PWD/.env:/app/.env myapp
关于amazon-web-services - 我可以在 AWS ECS 基础设施中使用 docker key 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62793405/