SSH访问管理 - 用Vault OTP引擎控制SSH访问(第2部分)

517 阅读3分钟

SSH访问管理 - 用Vault OTP引擎控制SSH访问(第2部分)

这是SSH访问管理博客系列的第二部分。在这个系列的第一部分中,我们涵盖了SSH访问管理的问题,解决方案的设计,如何设置Vault服务器并在上面设置SSH OTP引擎。在这篇文章中,我们将介绍:

设置带有SSH助手的堡垒服务器

让我们首先设置一个EC2实例作为堡垒主机。我们将使用Hashicorp的Vault SSH Helper来验证用户与Vault服务器的身份,并在Vault确认后向用户提供访问权限。

Vault SSH Helper是一个用于与HashiCorp Vault服务器通信的代理。它允许机器使用由我们的Vault服务器创建的一次性密码(OTP),然后将其作为每个SSH连接请求的客户端认证凭据。这个辅助代理必须安装在你想要SSH进入的目标主机上,在这种情况下就是堡垒。

  1. 安装Vault SSH辅助工具
# Download the vault-ssh-helper
$ wget https://releases.hashicorp.com/vault-ssh-helper/0.1.4/vault-ssh-helper_0.1.4_linux_amd64.zip

# Unzip the vault-ssh-helper in /usr/local/bin
$ sudo unzip -q vault-ssh-helper_0.1.4_linux_amd64.zip -d /usr/local/bin

# Make sure that vault-ssh-helper is executable
$ sudo chmod 0755 /usr/local/bin/vault-ssh-helper

# Set the usr and group of vault-ssh-helper to root
$ sudo chown root:root /usr/local/bin/vault-ssh-helper

# Setup vault ssh-helper config /etc/vault-ssh-helper.d/config.hcl
sudo mkdir /etc/vault-ssh-helper.d
sudo touch /etc/vault-ssh-helper.d/config.hcl

#Contents of config.hcl has to be as below
vault_addr = "${VAULT_ADDRESS}"
ssh_mount_point = "ssh"
ca_cert = "-dev"
tls_skip_verify = true
allowed_roles = "*"
  • 用ip地址(http://ip.ip.ip.ip/)或域名(https://vault.example.com)等替换${VAULT_ADDRESS}。
  • 如果你想通过HTTP使用Vault,或者在生产前用它来测试设置*(建议在生产中通过HTTPS使用Vault*),那么'ca_cert'的值可以设置为'-dev','tls_skip_verify'为'true'。这个文件应该包含为Vault服务器部署的证书,并且tls_skip_verify应该被设置为false。你可以使用亚马逊证书管理器从AWS获取证书或获取Let's Encrypt证书。
  1. 设置pam.d配置
# Create a backup of pam.d file
$ sudo cp /etc/pam.d/sshd /etc/pam.d/sshd.orig

修改文件/etc/pam.d/sshd ,注释标准UNIX认证(@include common-auth)模块,并添加保险库ssh-helper模块,如下所示

#@include common-auth
auth requisite pam_exec.so quiet expose_authtok log=/tmp/vaultssh.log /usr/local/bin/vault-ssh-helper -dev -config=/etc/vault-ssh-helper.d/config.hcl
auth optional pam_unix.so not_set_pass use_first_pass nodelay
  1. 更新 sshd 服务,以选择最新的 pam.d 配置
# Make a backup of the original
$ sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.orig

添加或修改文件/etc/ssh/ssh_config ,以包括以下变化:

ChallengeResponseAuthentication yes
PasswordAuthentication no
UsePAM yes
  1. 重新启动sshd服务
sudo systemctl restart sshd
  1. 创建一个Unix用户,用来访问堡垒服务器,密码为OTP:
# Creates linux user unix-user-name
$ useradd -ms /bin/bash unix-user-name
$ usermod -aG sudo unix-user-name
  1. vault-ssh-helper的日志可以在下面找到,用于调试。
tail -f 100 /tmp/vaultssh.log

设置用户访问的Vault客户端

这个旅程真的很漫长,我们现在正走向最后一站。每一个需要通过Vault验证并希望SSH进入堡垒服务器的客户或用户都需要安装Vault客户端。

  1. 安装客户端
  • 链接中下载Vault客户端
  • 复制可执行文件到/usr/local/bin
  • 通过检查版本来检查Vault是否已经安装。
$ vault --version
Vault v1.3.1
  1. 连接到Vault服务器
# Setup your Vault server address
$ export VAULT_ADDR="http://vault.example.com"

# You can create users through Vault UI and the login to vault with username and password to get the token
$ export VAULT_TOKEN=`vault login -token-only -method=userpass username=johndoe password=unthinkable`

# Or to get a quick preview you can export the vault root token generated on vault initialisation (Not recommended for production)
$ export VAULT_TOKEN="s.######"

# Check if the values have been set in the env
$ env | grep 'VAULT'
VAULT_ADDR=http://vault.example.com
VAULT_TOKEN=s.yourvaulttokenfornow
  1. 从Vault服务器获取OTP
# username in the below result is the unix user we had created inside the bastion server. We are using the Vault OTP engine we created in part 1 of the blog series.
$ vault write ssh/creds/otp-engine ip=#{bastion-ip}
Key                Value
---                -----
lease_id           ssh/creds/otp-engine/DFjbgD5cN6dmwPuJ01SKzn9z
lease_duration     1h
lease_renewable    false
ip                 {bastion-ip}
key                99375f15-53d8-3388-7d62-0379a7e2034b
key_type           otp
port               22
username           unix-user-name

# Once you have obtained the OTP(key) you can now SSH into the bastion with unix-user and the OTP as password.
$ ssh unix-user-name@#{bastion-ip}
password:99375f15-53d8-3388-7d62-0379a7e2034b
  1. OTP的过期时间可以由管理员自定义,建议将过期时间设置为30分钟以下。另外,获得的密钥或OTP只能使用一次。必须提出新的请求以获得新的OTP,以便再次登录。

总结

在这两部分博客系列中,我们学习了如何使用Hashicorp的Vault和Bastion服务器设计的系统来处理SSH访问管理问题。

众所周知,Vault是一把具有多种功能的瑞士军刀,如果你有兴趣,你可以在Hashicorp的网站上阅读更多关于Vault的信息。