【问题标题】:Token authentication not working when Hashicorp vault is sealed当 Hashicorp 保险库被密封时,令牌身份验证不起作用
【发布时间】:2020-08-28 05:44:27
【问题描述】:

我正在开发一个示例应用程序,我想在其中连接到 Hashicorp 保险库以获取数据库凭据。下面是我的应用程序的 bootstrap.yml。

spring:
  application:
    name: phonebook

  cloud:
    config:
      uri: http://localhost:8888/
    vault:
      uri: http://localhost:8200
      authentication: token
      token: s.5bXvCP90f4GlQMKrupuQwH7C

  profiles:
    active:
    - local,test

当保管库服务器解封时,应用程序会正确构建。 Maven 正确地从 Vault 中获取数据库用户名。当我在密封保管库后运行构建时,由于以下错误,构建失败。

org.springframework.vault.VaultException: Status 503 Service Unavailable [secret/application]: error performing token check: Vault is sealed; nested exception is org.springframework.web.client.HttpServerErrorException$ServiceUnavailable: 503 Service Unavailable: [{"errors":["error performing token check: Vault is sealed"]}

我该如何解决这个问题?我希望 maven 在构建过程中获取数据库用户名和密码,即使保险库是密封的,也不会出现任何问题。

【问题讨论】:

  • 密封 Vault 的全部目的是禁止所有 api 调用。让电话通过的唯一方法是打开它。调查它被密封的原因,并采取措施尽可能保持它不密封(多节点、自动解封、调查崩溃等...)
  • 我手动密封的。那么,我是否应该在运行应用程序时始终保持保险库未密封?
  • 根据文档Prior to unsealing, almost no operations are possible with Vault。参考vaultproject.io/docs/concepts/seal

标签: spring-boot spring-cloud-config hashicorp-vault spring-cloud-vault-config


【解决方案1】:

Vault的好处在于它不是简单的静态存储,在环境发生任何变化时,你都需要做一些动作,才能拥有一个稳定可用的系统。

建议:为流程自动化创建一个脚本。

示例。我有一个多服务系统,我的一些服务使用 Vault 来获取配置。

init.sh:

#!/bin/bash

export VAULT_ADDR="http://localhost:8200"

vault operator unseal <token1>
vault operator unseal <token2>
vault operator unseal <token3>
vault login <main token>

vault secrets enable -path=<path>/ -description="secrets for My projects" kv
vault auth enable approle

vault policy write application-policy-dev ./application-policy-DEV.hcl

application.sh:

#!/bin/bash

export VAULT_ADDR="http://localhost:8200"
vault login <main token>

vault delete <secret>/<app_path>
vault delete sys/policy/<app>-policy
vault delete auth/approle/role/<app>-role
vault kv put <secret>/<app_path> - < <(yq m ./application.yaml)
vault policy write <app>-policy ./<app>-policy.hcl

vault write auth/approle/role/<app>-role token_policies="application-policy"
role_id=$(vault read auth/approle/role/<app>-role/role-id -format="json" | jq -r '.data.role_id')
secret_id=$(vault write auth/approle/role/<app>-role/secret-id -format="json" | jq -r '.data.secret_id')
token=$(vault write auth/approle/login role_id="${role_id}" secret_id=${secret_id} -format="json" | jq -r '.auth.client_token')

echo 'Token:' ${token}

其中&lt;app&gt; - 应用程序的名称,application.yaml - 带有配置的文件,&lt;app&gt;-policy.hcl - 带有策略的文件

当然,所有这些文件都不应该公开,仅用于保管库管理。

在环境发生任何变化或保管库期间终止时,只需运行 init.sh。要获取应用程序的令牌,请运行 application.sh。另外,如果您需要更改配置参数,请在application.yaml 中更改,运行application.sh 并使用结果令牌。

脚本结果(用于我的一项服务):

Key                  Value
---                  -----
token                *****
token_accessor       *****
token_duration       ∞
token_renewable      false
token_policies       ["root"]
identity_policies    []
policies             ["root"]
Success! Data deleted (if it existed) at: <secret>/<app>
Success! Data deleted (if it existed) at: sys/policy/<app>-policy
Success! Data deleted (if it existed) at: auth/approle/role/<app>-role
Success! Data written to: <secret>/<app>
Success! Uploaded policy: <app>-policy
Success! Data written to: auth/approle/role/<app>-role
Token: s.dn2o5b7tvxHLMWint1DvxPRJ

Process finished with exit code 0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-01
    • 2021-09-19
    • 2015-12-15
    • 2020-02-25
    • 2018-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-18
    相关资源
    最近更新 更多