在 Vault 中存储默认环境变量,而不是在 docker-compose 中存储标准服务的 env 文件

发布于 2025-01-18 01:36:03 字数 950 浏览 2 评论 0原文

我有一个docker-compose使用标准软件容器,例如:

  • infuxdb
  • mariadb
  • -node-red

node 工业单板计算机(可能无法连接到Internet)

进行初始设置(堆叠堆栈),我通过其环境变量文件(例如incod> infuxdb.env )传递了一些标准凭据(例如管理员凭据)代码> Mariadb.env

services:
  influxdb:
    image: influxdb:2.0
    env_file:
      - influxdb.env
  nodered:
    image: nodered/node-red:2.2.2
    env_file:
      - node-red.env

INFLUXDB_ADMIN_USER=admin
INFLUXDB_ADMIN_PASSWORD=password!#$2
# other env vars that might be crucial for initial stack boot up

在磁盘上,仍然很脆弱。我想了解Hashicorp保管库是否可以提供合理的解决方案,在该解决方案中,可以将这些凭据(秘密)存储为键值对,并在运行时提供给Docker-Compose服务。

我了解一个瓶颈,因为我使用了标准容器(现成的),并且它们可能没有保险库集成。但是,我仍然可以使用Vault存储ENV VAR并让服务在运行时访问它们吗?还是我必须为这些容器编写 side-cars ,然后让他们接受这些env var值?

I have a docker-compose stack which uses standard software containers like:

  • InfluxDB
  • MariaDB
  • Node-Red

running on a Industrial Single Board Computer (which may not be connected to the internet)

for initial setup (bringing the stack up), I pass some standard credentials like admin credentials via their environment variable files e.g. influxdb.env, mariadb.env etc.

A typical example of a docker-compose.yml here is:

services:
  influxdb:
    image: influxdb:2.0
    env_file:
      - influxdb.env
  nodered:
    image: nodered/node-red:2.2.2
    env_file:
      - node-red.env

An example of influxdb.env could be:

INFLUXDB_ADMIN_USER=admin
INFLUXDB_ADMIN_PASSWORD=password!#$2
# other env vars that might be crucial for initial stack boot up

These files are on the disk and can still be vulnerable. I wish to understand if Hashicorp Vault can provide a plausible solution where such credentials (secrets) can be stored as key-value pairs and be made available to the docker-compose services upon runtime.

I understand one bottleneck that since I am using standard containers (ready-to-use) and they may not have vault integration. However, can I still use vault to store the env vars and let the services access them on runtime? Or do I have to write side-cars for these containers and then let them accept these env var values?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

顾铮苏瑾 2025-01-25 01:36:03

这里有一些限制需要处理:

  • 不将机密永久存储在存储
  • docker-compose 命令行
  • Vault 的输出格式

Docker Composer 可以从文件中读取其环境变量。我建议您创建该文件并使用 --env-file 参数将其提供给 docker-compose

我可以想到两种写入该文件的方法:

  1. 将多个 vault kv get 的输出以 NAME=VALUE 格式写入文件
  2. 使用Vault代理的模板引擎

第一个选项非常简单。调用输出机密并将其发送到文件的函数:

#!/bin/bash

function write_vault_secret_to_env_file() {
    local ENVIRONMENT_VARIABLE_NAME=$1
    local SECRET_PATH=$2
    local SECRET_NAME=$3

    echo "$ENVIRONMENT_VARIABLE_NAME=$(vault kv get --field $SECRET_NAME $SECRET_PATH)"
}

echo "$(write_vault_secret_to_env_file FIRST_ENVIROMENT_VAR secret/my-path/things first-secret)" >> my-env-file.sh 
echo "$(write_vault_secret_to_env_file SECOND_ENVIROMENT_VAR secret/my-path/stuff second-secret)" >> my-env-file.sh 

Vault 代理的模板引擎 功能更强大,但设置更复杂。

另一个建议是使用 Vault 的动态机密数据库(支持 InfluxDB) 。但您需要为 Vault 提供数据库中的 DBA 权限。如果您每次都从头开始创建数据库,则可以设置 DBA 密码 dba-root,为 Vault 提供该密码,然后 指示它为您轮换

You have a few constraints to work with here:

  • Not storing secrets permanently in storage
  • docker-compose command line
  • Vault's output format

Docker composer can read it's environment variables from a file. I suggest that you create that file and provide it to docker-compose with the --env-file parameter.

I can think of two approach to write that file:

  1. Write the output of multiple vault kv get to a file, in NAME=VALUE format
  2. Use vault agent's template engine

The first option is quite straighforward. Call a function that outputs the secrets and send it to a file:

#!/bin/bash

function write_vault_secret_to_env_file() {
    local ENVIRONMENT_VARIABLE_NAME=$1
    local SECRET_PATH=$2
    local SECRET_NAME=$3

    echo "$ENVIRONMENT_VARIABLE_NAME=$(vault kv get --field $SECRET_NAME $SECRET_PATH)"
}

echo "$(write_vault_secret_to_env_file FIRST_ENVIROMENT_VAR secret/my-path/things first-secret)" >> my-env-file.sh 
echo "$(write_vault_secret_to_env_file SECOND_ENVIROMENT_VAR secret/my-path/stuff second-secret)" >> my-env-file.sh 

Vault agent 's template engine is much more powerfull, but is more complex to set up.

Another suggestion would be to use Vault's dynamic secrets for databases (InfluxDB is supported). But you need to provide Vault with DBA privileges in your database. If you create the database from scratch everytime, you could make the DBA password dba-root, give Vault that password and instruct it to rotate it for you.

病毒体 2025-01-25 01:36:03

类似于

  1. 解决
  2. 方案- 价值
  3. docker-compose.yml从Shell的环境变量中填充值。参见

详细信息:

  • 存储在金库中的KVS如下所示:

“

  • 定义export_secrets()函数>
export_secrets() {
  echo "Input data:"
  echo "$1"
  
  while read -r line; do
    key=$(echo $line | cut -d: -f1 | xargs)
    value=$(echo $line | cut -d: -f2 | xargs)

    echo "Parsed key-value pair: $key=$value"
    
    export $key="$value"
    
    echo "Exported environment variable: $(printenv "$key")"
  done
}

  • 函数函数设置vault
vault kv get -format=yaml -field=data -mount=secret appsecrets_demo | export_secrets
  • 通过通过验证它们echo $ database_password


  • ,从shell

    代替

services:
  influxdb:
    image: influxdb:2.0
    environment:
      DATABASE_PASSWORD: '${DATABASE_PASSWORD}'
  • 我用第二个Vault服务器验证了该解决方案。

A working solution similar to ixe013's without saving files:

  1. Fetch key-value pairs from vault in yaml format
  2. Set environment variables using the key-value pairs
  3. Populate values inside docker-compose.yml from Shell's environment variables. see docker documentation

Details:

  • KVs stored in Vault shown below:

KVs in Vault

  • Define an export_secrets() function
export_secrets() {
  echo "Input data:"
  echo "$1"
  
  while read -r line; do
    key=$(echo $line | cut -d: -f1 | xargs)
    value=$(echo $line | cut -d: -f2 | xargs)

    echo "Parsed key-value pair: $key=$value"
    
    export $key="$value"
    
    echo "Exported environment variable: $(printenv "$key")"
  done
}

  • Set environment values from Vault
vault kv get -format=yaml -field=data -mount=secret appsecrets_demo | export_secrets
  • Verify them by echo $DATABASE_PASSWORD

  • In docker-compose.yml, substitute from the shell

services:
  influxdb:
    image: influxdb:2.0
    environment:
      DATABASE_PASSWORD: '${DATABASE_PASSWORD}'
  • I verified this solution with a second vault server.

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文