【问题标题】:Hashicorp Vault - Agent Injector - does it make sense?Hashicorp Vault - Agent Injector - 有意义吗?
【发布时间】:2021-07-05 19:18:26
【问题描述】:

我有关于 Hashicorp Vault 的基本问题。我想使用 Spring 应用程序将一些秘密(数据库密码)从 Vault 注入到容器中。

我已经准备了一个特定的注释,用于将 Vault 与 Kubernetes 一起使用,一切正常,将 PASSWORD 变量保存为允许我在 application.properties 文件中使用的环境。

template:
metadata:
  annotations:
    vault.hashicorp.com/agent-inject: "true"
    vault.hashicorp.com/role: "myapp-role"
    vault.hashicorp.com/agent-inject-secret-foo: "secret/creds"
    vault.hashicorp.com/agent-inject-template-foo: |
      {{`{{- with secret "secret/creds" -}}
      PASSWORD={{ .Data.passcode }}
      {{- end }}`}}
  labels:
    app.kubernetes.io/name: {{ $appName }}
    app.kubernetes.io/instance: {{ .Release.Name }}
spec:
  containers:
    - name: {{ $appName }}
      image: "{{ .Values.vvvv.image.repository }}:{{ .Values.vvvv.image.tag }}"
      command: ["/bin/bash", "-c","while read line; do export $line; done < /vault/secrets/foo; /usr/local/tomcat/bin/catalina.sh run"]
      volumeMounts:
        - name: application-properties
          mountPath: /usr/local/tomcat/lib/application.properties
          subPath: application.properties
      ports:
        - name: http
          containerPort: 8080
          protocol: TCP     

这个问题很简单,有意义吗?注入代理将带有纯文本密码的文件保存在 /vault 路径中,所以每个人都可以看到这个秘密......另一个问题,我怎样才能轮换应用程序的凭据?我应该在 spring 应用程序中使用特定的控制器吗?

【问题讨论】:

    标签: kubernetes kubernetes-helm hashicorp-vault


    【解决方案1】:

    我认为这绝对是有道理的,因为其目的是避免在规范中硬编码凭据。

    inject 代理将带有纯文本密码的文件保存在 /vault 中 路径,所以每个人都可以看到这个秘密。

    即使在裸机服务器或云实例中,凭据也以纯文本形式保存。对于 k8s,它位于容器内。在这两种情况下,您都可以控制谁可以访问您的实例或 k8s pod。只有授权人员才能对生产集群中的 pod 进行必要的访问。

    如何轮换应用程序的凭据

    Vault 代理注入器在同一个 pod 中运行一个 sidecar 容器以及您的应用容器。其目的是定期查找保管库机密中的任何更改。如果你执行kubectl describe po &lt;pod-name&gt;,你会发现一个边车容器vault-agent正在运行。

    kubectl get po app-example-deployment-7c4b45cf8-4fkr7
    NAME                                     READY   STATUS    RESTARTS   AGE
    app-example-deployment-7c4b45cf8-4fkr7   2/2     Running   0          166m
    

    kubectl describe pod app-example-deployment-7c4b45cf8-4fkr7

    ...
    vault-agent:
        Container ID:  docker://b6f9df32ed903d684c972401f41e15a8f6b1bec62aa111bfd9c693159af1ff09
        Image:         vault:1.7.0
        Image ID:      docker-pullable://vault@sha256:635cf1c3f9b10fe03aad375f94cc61f63d74a189662165285a8bf1c189ea04b8
        Port:          <none>
        Host Port:     <none>
        Command:
          /bin/sh
          -ec
        Args:
          echo ${VAULT_CONFIG?} | base64 -d > /home/vault/config.json && vault agent -config=/home/vault/config.json
        State:          Running
          Started:      Tue, 13 Apr 2021 15:40:10 +0100
        Ready:          True
        Restart Count:  0
        Limits:
          cpu:     500m
          memory:  128Mi
        Requests:
          cpu:     250m
          memory:  64Mi
        Environment:
          VAULT_LOG_LEVEL:   info
          VAULT_LOG_FORMAT:  standard
    ...
    

    在部署期间成功从保管库中获取机密后:

    kubectl exec -it app-example-deployment-7c4b45cf8-4fkr7 -c app -- cat /vault/secrets/db-creds
    mongodb+srv://testUser:testPass@test-5xxxx.mongodb.net/testDb
    

    如果我在密码设置为“testPass2”的情况下更改 Vault 中的 kv 密码,我无需执行任何操作,因为 vault-agent 边车容器会自动为我更新。

    kubectl exec -it app-example-deployment-7c4b45cf8-4fkr7 -c app -- cat /vault/secrets/db-creds
    mongodb+srv://testUser:testPass2@test-5xxxx.mongodb.net/testDb
    

    vault-agent sidecar 容器日志中,您会看到类似的内容。

    kubectl logs app-example-deployment-7c4b45cf8-4fkr7 -c vault-agent --follow
    2021-04-13T14:40:10.426Z [INFO]  sink.file: creating file sink
    2021-04-13T14:40:10.426Z [INFO]  sink.file: file sink configured: path=/home/vault/.vault-token mode=-rw-r-----
    ==> Vault agent started! Log data will stream in below:
    
    ==> Vault agent configuration:
    
                         Cgo: disabled
                   Log Level: info
                     Version: Vault v1.7.0
                 Version Sha: 4e222b85c40a810b74400ee3c54449479e32bb9f
    
    2021-04-13T14:40:10.426Z [INFO]  template.server: starting template server
    [INFO] (runner) creating new runner (dry: false, once: false)
    2021-04-13T14:40:10.427Z [INFO]  auth.handler: starting auth handler
    2021-04-13T14:40:10.427Z [INFO]  auth.handler: authenticating
    2021-04-13T14:40:10.427Z [INFO]  sink.server: starting sink server
    [INFO] (runner) creating watcher
    2021-04-13T14:40:10.437Z [INFO]  auth.handler: authentication successful, sending token to sinks
    2021-04-13T14:40:10.437Z [INFO]  auth.handler: starting renewal process
    2021-04-13T14:40:10.437Z [INFO]  template.server: template server received new token
    [INFO] (runner) stopping
    [INFO] (runner) creating new runner (dry: false, once: false)
    [INFO] (runner) creating watcher
    [INFO] (runner) starting
    2021-04-13T14:40:10.437Z [INFO]  sink.file: token written: path=/home/vault/.vault-token
    2021-04-13T14:40:10.439Z [INFO]  auth.handler: renewed auth token
    [INFO] (runner) rendered "(dynamic)" => "/vault/secrets/db-creds"
    2021-04-13T15:23:43.315Z [INFO]  auth.handler: renewed auth token
    [INFO] (runner) rendered "(dynamic)" => "/vault/secrets/db-creds"
    2021-04-13T16:07:16.191Z [INFO]  auth.handler: renewed auth token
    

    【讨论】:

    • 到目前为止一切都很好,但它并没有反映在使用机密的服务中。有什么解决方法吗? (可能会触发服务重启)
    • 是的,这取决于应用程序如何处理秘密刷新。一个例子是,如果你将秘密的值导出到一个 Env 变量中,如果你的应用程序引用它来完成特定的工作,它会注意到新值。不过,您的里程可能会有所不同。
    猜你喜欢
    • 1970-01-01
    • 2021-05-07
    • 2017-01-20
    • 2021-04-18
    • 1970-01-01
    • 2020-05-01
    • 2017-05-16
    • 2019-07-28
    • 2017-12-18
    相关资源
    最近更新 更多