How to use docker secret/environment variable in golang dockertest.resource instead of hardcoded password

huangapple go评论80阅读模式
英文:

How to use docker secret/environment variable in golang dockertest.resource instead of hardcoded password

问题

我们使用以下代码来对与数据库系统交互的服务进行单元测试。

https://sergiocarracedo.es/integration-tests-in-golang-with-dockertest/

MySQL的root密码在特定行中是硬编码的,这会带来安全问题。有没有办法将其作为环境变量或Docker密钥传递?

resource, err := pool.Run("mysql", "5.7", []string{"MYSQL_ROOT_PASSWORD=secret"})

英文:

We use the below code for unit testing the services that talk to a database system.

https://sergiocarracedo.es/integration-tests-in-golang-with-dockertest/

MySQL root password is hardcoded in the particular line and creates security issues. Is there any way we can pass that as env variable or docker secret ?

resource, err := pool.Run("mysql", "5.7", []string{"MYSQL_ROOT_PASSWORD=secret"})

答案1

得分: 2

你可以使用环境变量来实现。

  1. 首先,在你的代码中使用 os.Getenv() 获取环境变量。
mysqlPwd := os.Getenv("MYSQL_ROOT_PASSWORD")
  1. 然后在运行 Docker 时使用 -e 选项来设置环境变量。
docker run -e MYSQL_ROOT_PASSWORD=secret
英文:

You can use the environment variable.

  1. First of all, get the env variable via os.Getenv() in your code
mysqlPwd := os.Getenv("MYSQL_ROOT_PASSWORD")
  1. Then run the docker with the -e option
docker run -e MYSQL_ROOT_PASSWORD=secret

huangapple
  • 本文由 发表于 2022年3月30日 19:46:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/71676635.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定