vault +kes+minio 实现minio文件加密

Linux CentOS 部署vault+kes+minio

Hashicorp Vault 密钥库部署

一、安装下载(官方网址:https://developer.hashicorp.com/vault/install)

sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
sudo yum -y install vault

二、验证安装

[root@localhost vault]# vault

三、开发环境启动

注意:只能运用在开发环境,因为每次启动的Root Token值都会发生变化,之前上传的文件会访问不了

1、启动开发服务器

1.使用-help标志列出可用的命令选项vault server。
vault server -help

2…开发模式启动 Vault 服务器:

vault server -dev
显示了Unseal Key和 Root Token值。

3.设置环境变量

启动一个新的终端会话。
export VAULT_ADDR …从终端输出复制并运行命令。这将配置 Vault 客户端与开发服务器通信。
export VAULT_ADDR='http://127.0.0.1:8200'
export VAULT_TOKEN="hvs.6j4cuewowBGit65rheNoceI7"
Vault CLI 使用环境变量确定向哪些 Vault 服务器发送请求 VAULT_ADDR。
将解封密钥保存在某处。不必担心如何安全地保存它。现在,只需将其保存在任何地方即可。
将环境变量值设置为终端输出中显示的VAULT_TOKEN生成的 Root Token值。
4.验证服务器正在运行
通过运行命令来验证服务器是否正在运行vault status。

vault status

四、测试环境部署 Vault

1.取消设置VAULT_TOKEN环境变量。

unset VAULT_TOKEN

2.配置 Vault

新建config.hcl文件
vim config.hcl

文件内容

storage "raft" {
  path    = "/opt/vault/data"
  node_id = "node1"
}

listener "tcp" {
  address     = "127.0.0.1:8200"
  tls_disable = "true"
}

api_addr = "http://127.0.0.1:8200"
cluster_addr = "https://127.0.0.1:8201"
ui = true
3.启动服务器

设置-config标志以指向保存上述配置的正确路径。
vault server -config=config.hcl -log-level=trace
后台启动:

nohup vault server -config=config.hcl -log-level=trace > vault-20240715-001.log 2>&1 &

4.初始化Vault

初始化是配置 Vault 的过程。当针对从未与 Vault 一起使用过的新后端启动服务器时,此过程仅发生一次。在 HA 模式下运行时,此过程每个集群发生一次,而不是每个服务器发生一次。在初始化期间,将生成加密密钥、创建解封密钥并创建初始根令牌。
启动一个新的终端会话,并设置VAULT_ADDR环境变量。
export VAULT_ADDR='http://127.0.0.1:8200'
要初始化 Vault,请使用vault operator init。这是一个未经身份验证的 请求,但它仅适用于没有现有数据的全新 Vault:
vault operator init
初始化输出两个非常重要的信息:解封密钥和初始根令牌。这是 Vault 有史以来唯一一次知道所有这些数据,也是解封密钥如此接近的唯一一次。

每次重启服务器,解密都需要使用Unseal Key,

Unseal Key 1: zHuNXNBP0te+WfyP9Ixxxxxxxxxxxxxxxxxx
Unseal Key 2: 1SwaZo/Ucii0Tlyh6Gpxxxxxxxxxxxxxxxxxx
Unseal Key 3: HqvaQcSnQjFdEbJnC/xxxxxxxxxxxxxxxxxx
Unseal Key 4: QwaqHdQt0JKw21jK/xxxxxxxxxxxxxxxxxx
Unseal Key 5: hmMButB5GKT2HWd/xxxxxxxxxxxxxxxxxx

Initial Root Token: hvs.CYRd5xcE2Zyxxxxxxxxx

Vault initialized with 5 key shares and a key threshold of 3. Please securely
distribute the key shares printed above. When the Vault is re-sealed,
restarted, or stopped, you must supply at least 3 of these keys to unseal it
before it can start servicing requests.

Vault does not store the generated root key. Without at least 3 keys to
reconstruct the root key, Vault will remain permanently sealed!

It is possible to generate new unseal keys, provided you have a quorum of
existing unseal keys shares. See "vault operator rekey" for more information.
5.密封/启封

每个初始化的 Vault 服务器都以密封状态启动。从配置来看,Vault 可以访问物理存储,但无法读取任何内容,因为它不知道如何解密。教 Vault 如何解密数据的过程称为解封Vault。
每次启动 Vault 时都必须解封。可以通过 API 和命令行进行解封。要解封 Vault,您必须拥有阈值数量的解封密钥。在上面的输出中,请注意“密钥阈值”为 3。这意味着要解封 Vault,您需要生成的 5 个密钥中的 3 个。

vault operator unseal

vault operator unseal zHuNXNBP0te+WfyP9Ixxxxxxxxxxxxxxxxxx
vault operator unseal 1SwaZo/Ucii0Tlyh6Gpxxxxxxxxxxxxxxxxxx
vault operator unseal HqvaQcSnQjFdEbJnC/xxxxxxxxxxxxxxxxxx

粘贴有效密钥并确认后,您会看到 Vault 仍处于密封状态,但已取得进展。Vault 知道它有 3 把密钥中的 1 把。由于算法的性质,Vault 在达到阈值之前不知道它是否拥有正确的密钥。
还要注意,解封过程是有状态的。你可以转到另一台计算机,使用vault operator unseal,只要它指向同一台服务器,另一台计算机就可以继续解封过程。这对于解封过程的设计非常重要:
需要多个人使用多个密钥来解封 Vault。Vault 可以从多台计算机解封,密钥绝不能放在一起。单个恶意操作员没有足够的密钥来实施恶意行为。
继续vault operator unseal完成解锁保险库。要解锁保险库,您必须使用三个不同的解锁钥匙,重复使用相同的钥匙将不起作用。
6.为初始根令牌进行身份验证(它与解封密钥一起包含在输出中)。
vault login hvs.CYRd5xcE2Zyxxxxxxxxx

五、设置 KES 对 Vault 的访问权限

1.创建保管库策略

Vault 策略定义 KES 服务器可以访问的 API 路径。创建一个名为 的文本文件kes-policy.hcl。

vim kes-policy.hcl

策略的内容根据所使用的K/V引擎而有所不同。
K/V/V1
kes-policy.hcl对K/V后端使用以下策略v1:

path "kv/*" {
   capabilities = [ "create", "read", "delete", "list" ]
}
2.将策略写入 Vault

以下命令在 Vault 中创建策略:
vault policy write kes-policy kes-policy.hcl

返回信息:Success! Uploaded policy: kes-policy

3.启用身份验证

此步骤允许 KES 服务器向 Vault 进行身份验证。在本教程中,我们使用AppRole身份验证方法。
vault auth enable approle
返回信息:Success! Enabled approle auth method at: approle/

4.创建 KES 角色

以下命令添加一个名为kes-serverat Vault 的新角色:
vault write auth/approle/role/kes-server token_num_uses=0 secret_id_num_uses=0 period=5m
返回信息:Success! Data written to: auth/approle/role/kes-server

5.将策略绑定到角色

以下命令将kes-server角色绑定到kes-policy:
vault write auth/approle/role/kes-server policies=kes-policy
返回信息:Success! Data written to: auth/approle/role/kes-server

6.生成 AppRole ID

为 KES 服务器请求 AppRole ID:
vault read auth/approle/role/kes-server/role-id

返回信息

Key        Value

---        -----

role_id    b4227228-f908-769b-59f0-xxxxxxxx
7.生成 AppRole 机密

向 KES 服务器请求 AppRole 机密:
vault write -f auth/approle/role/kes-server/secret-id

返回信息

Key                   Value

---                   -----

secret_id             0f09fb12-a77e-30db-7bdb-xxxxxxx
secret_id_accessor    883a4224-8a25-d506-e088-xxxxxxx
secret_id_num_uses    0
secret_id_ttl         0s 

AppRole 机密打印为secret_id。您可以忽略secret_id_accessor。

kes部署

一、下载安装kes(https://min.io/docs/kes/tutorials/getting-started/)
1.赋予文件可执行权限
chmod +x kes
2.通过从提示符或终端运行以下命令来确认命令的可用性(在可执行文件的对应目录下):
./kes --help
返回信息:

Usage:
    kes [options] <command>
Commands:
    server                   Start a KES server.

    ls                       List keys, policies and identites.
    key                      Manage cryptographic keys.
    policy                   Manage KES policies.
    identity                 Manage KES identities.
    
    log                      Print error and audit log events.
    status                   Print server status.
    metric                   Print server metrics.
    
    migrate                  Migrate KMS data.
    update                   Update KES binary.

Options:
    -v, --version            Print version information.
        --auto-completion    Install auto-completion for this shell.
    -h, --help               Print command line options.

3.生成 KES 服务器私钥和证书
为 KES 服务器生成 TLS 私钥和证书。此密钥用于域名验证。
KES 服务器默认是安全的,只能使用 TLS 运行。在本指南中,为简单起见,我们使用自签名证书。
以下命令生成一个新的 TLS 私钥( )以及为 IP和 DNS 名称颁发的private.key自签名 X.509 证书( ) :public.crt127.0.0.1localhost

./kes identity new --key private.key --cert public.crt --ip "127.0.0.1" --dns localhost

返回信息:

Your API key:
   kes:v1:ANu879Xmo+c0kcwnWhEf9lyxxxxxxxx
This is the only time it is shown. Keep it secret and secure!
Your Identity:
   b294e6672b688456160d02fcee100c54xxxxxxxxxxx
The identity is not a secret. It can be shared. Any peer
needs this identity in order to verify your API key.
The generated TLS private key is stored at: server.key
The generated TLS certificate is stored at: server.cert
The identity can be computed again via:
    kes identity of kes:v1:ANu879Xmo+c0kcwnxxxxxxxxx
    kes identity of server.cert

4.生成客户端凭证
客户端应用程序需要凭证才能访问 KES 服务器。根据客户端密钥和证书生成 API 密钥,以向 KES 服务器验证客户端应用程序。
以下命令为 生成新的 TLS 私钥/公钥对MyApp:
./kes identity new --key=client.key --cert=client.crt MyApp
返回信息:

Your API key:
   kes:v1:AHIF3WOBjl1N0yO0/Dxxxxx
This is the only time it is shown. Keep it secret and secure!
Your Identity:
   94ba32053f70cdc37ee8b36f4c246645xxxxxx
The identity is not a secret. It can be shared. Any peer
needs this identity in order to verify your API key.
The generated TLS private key is stored at: client.key
The generated TLS certificate is stored at: client.crt
The identity can be computed again via:
    kes identity of kes:v1:AHIF3WOBjl1N0yxxxxxxx
    kes identity of client.crt

注意:可以随时重新计算指纹Identity:kes identity of client.crt
5.配置 KES 服务器
创建 KES 服务器配置文件:vim config.yml

确保部分中的身份policy与您的client.crt身份相匹配。

address: 0.0.0.0:7373 # 监听端口7373上的所有网络接口
admin:
  identity: 94ba32053f70cdc37ee8b36f4c246645c54exxxxxxxx  # 使用客户端的identity
tls:
  key: /opt/kes/private.key    # 使用KES 服务器私钥
  cert: /opt/kes/public.crt    # 使用KES 服务器证书
keystore:
   vault:
     endpoint: http://127.0.0.1:8200
     version:  v1 # The K/V engine version - either "v1" or "v2".
     engine:   kv # The engine path of the K/V engine. The default is "kv".
     approle:
       id:     "b4227228-f908-769b-59f0-xxxxxxx" # 你的 AppRole ID
       secret: "0f09fb12-a77e-30db-7bdb-xxxxxxx" # 你的 AppRole Secret

6.启动kes服务
./kes server --config config.yml
返回信息

Version                           2024-06-17T15-47-05Z    commit=12195cc387d860517221548b6xxxx
Runtime                           go1.22.4 linux/amd64    compiler=gc
License                           AGPLv3                  https://www.gnu.org/licenses/agpl-3.0.html
Copyright                         MinIO, Inc.  2015-2024  https://min.io

KMS                               Hashicorp Vault: http://127.0.0.1:8200
API                               · https://127.0.0.1:7373
            · https://192.168.10.128:7373
            · https://192.168.122.1:7373
            · https://172.17.0.1:7373

Docs                              https://min.io/docs/kes

Admin                             b294e6672b688456160d02fcee100c54b2aeb61xxxxxxxx
Logs                              error=stderr level=INFO
            audit=stdout level=INFO
MLock                             enabled

=> Server is up and running...

minio部署

1.下载安装(https://min.io/download?license=agpl&platform=linux)
wget https://dl.min.io/server/minio/release/linux-amd64/minio

返回信息

--2024-07-15 17:32:50--  https://dl.min.io/server/minio/release/linux-amd64/minio
正在解析主机 dl.min.io (dl.min.io)... 178.128.69.202, 138.68.11.125
正在连接 dl.min.io (dl.min.io)|178.128.69.202|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:103399576 (99M) [application/octet-stream]
正在保存至: “minio”

赋予可执行权限: chmod +x minio

2.MinIO 服务器设置

  1. MINIO_KMS_KES_ENDPOINT

    此环境变量告诉 MinIO 访问哪个 KES 服务器:

    export MINIO_KMS_KES_ENDPOINT=https://127.0.0.1:7373
    
  2. 设置 MinIO 客户端凭证

    这些环境变量设置 MinIO 用于访问 KES 服务器的访问凭据:

    export MINIO_KMS_KES_CERT_FILE=client.crt
    
    export MINIO_KMS_KES_KEY_FILE=client.key
    
  3. 设置 MinIO 默认密钥

    如果 MinIO 的 S3 客户端未指定加密密钥,则此环境变量设置 MinIO 使用的默认密钥。

    Copy

    export MINIO_KMS_KES_KEY_NAME=minio-default-key
    

    注:如果不存在该密钥,MinIO 会自动创建该密钥。

  4. 信任 KES 服务器证书

    如果 KES 服务器使用由受信任的证书颁发机构颁发的证书,则此步骤是可选的。

    当使用自签名证书时,MinIO 无法验证 KES 服务器证书。此环境变量手动建立信任关系。

    export MINIO_KMS_KES_CAPATH=public.crt
    

    在这种情况下,public.crt是 KES 服务器的公共证书。

  5. 设置 MinIO 根凭据:

    export MINIO_ROOT_USER=minio
    export MINIO_ROOT_PASSWORD=minio123
    

​ 注:设置了服务启动时候的登录用户名和密码

运行文件,启动minio服务器

MINIO_ROOT_USER=minio MINIO_ROOT_PASSWORD=minio123 ./minio server --address '0.0.0.0:9000' --console-address '0.0.0.0:9001' /opt/minio/data

  • 21
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值