使用 SASL 认证和加密进行远程管理

Simple Authentication and Security Layer (SASL) 提供了安全认证和数据加密,但是允许与传统或外部认证和授权服务集成。

在其最简单的形式下,SASL 可用来定义用于进行授权的凭证的数据库。在较复杂的场景中,它可与外部认证服务(例如 Kerberos 或轻量级目录访问协议 (LDAP))一起工作以对用户进行认证。在这两种场景中,如果远程管理请求并非在受保护的传输层安全性 (TLS) 连接上面运行,那么 libvirtd 守护程序通过需要“类属安全性服务”应用程序编程接口 (GSSAPI) 作为 SASL 方法来提供机密性。libvirtd 守护程序可将 DIGEST-MD5 而不是 GSSAPI 用作 SASL 方法。但是,MD5 散列被视为不安全,不应该使用。SASL 的这些变体支持对推送的数据进行加密。为简便起见,此示例将 DIGEST-MD5 用作 SASL 方法。

要在最简单的场景(没有外部认证或 TLS 安全性)中使用 SASL 配置远程管理,请完成下列步骤:

  1. 登录到 KVM 主机。
  2. 保存 /etc/libvirt/libvirtd.conf 文件和 /etc/sysconfig/libvirtd 文件的副本。
  3. 编辑 /etc/libvirt/libvirtd.conf 文件,并进行下列更改:
    1. 通过将 listen_tls 配置伪指令设置为 0 来禁用该伪指令(因为没有配置任何 TLS 证书)。否则,libvirtd 守护程序启动会失败。
    2. 确保通过将配置伪指令 listen_tcp 设置为 1 来启用该伪指令。
    3. auth_tcp 配置伪指令设置为 sasl,以启用通过 TCP 进行 SASL 认证。

    以下示例显示了这些参数(与股票 libvirtd.conf 文件对比),突出显示了要删除 (-) 和增加 (+) 的更改:

    --- libvirtd.conf.orig  2012-01-04 11:28:32.000000000 -0600
    +++ libvirtd.conf       2012-01-04 11:34:02.000000000 -0600
    @@ -19,7 +19,7 @@
     # using this capability.
     #
     # This is enabled by default, uncomment this to disable it
    -#listen_tls = 0
    +listen_tls = 0
     # Listen for unencrypted TCP connections on the public TCP/IP port.
     # NB, must pass the --listen flag to the libvirtd daemon process for this to
    @@ -30,7 +30,7 @@
     # DIGEST_MD5 and GSSAPI (Kerberos5)
     #
     # This is disabled by default, uncomment this to enable it.
    -#listen_tcp = 1
    +listen_tcp = 1
     
     
    @@ -143,7 +143,7 @@
     # Don't do this outside of a dev/test scenario. For real world
     # use, always enable SASL and use the GSSAPI or DIGEST-MD5
     # mechanism in /etc/sasl2/libvirt.conf
    -#auth_tcp = "sasl"
    +auth_tcp = "sasl"
     # Change the authentication scheme for TLS sockets.
     #
  4. 编辑 /etc/sysconfig/libvirtd 文件并启用 --listen 参数,以便 libvirtd 守护程序侦听 TCP/IP 连接:
    --- libvirtd.orig       2012-01-04 11:41:37.000000000 -0600
    +++ libvirtd    2012-01-04 11:31:33.000000000 -0600
    @@ -3,7 +3,7 @@
     
     # Listen for TCP/IP connections
     # NB. must setup TLS/SSL keys prior to using this
    -#LIBVIRTD_ARGS="--listen"
    +LIBVIRTD_ARGS="--listen"
     
     # Override Kerberos service keytab for SASL/GSSAPI
     #KRB5_KTNAME=/etc/libvirt/krb5.tab
  5. 重新启动 libvirtd 守护程序,以使更改生效:
    # /etc/init.d/libvirtd restart
    Stopping libvirtd daemon:                                  [  OK  ]
    Starting libvirtd daemon:                                  [  OK  ]
  6. 既然 libvirtd 守护程序接受 TCP 连接,请将一些用户添加至 SASL 数据库。以下示例使用 saslpasswd2 命令将 admin 用户添加至 libvirt 凭证数据库。
    # saslpasswd2 -a libvirt admin
    Password:
    Again (for verification):
    注:
    • libvirt 是 libvirtd 守护程序认证域的 SASL 数据库的预期名称。请勿将任何其他名称用于此数据库。
    • 请使这些凭证保持安全状态,因为此数据库中的每个用户都有权登录并运行远程虚拟机管理。
  7. 如果 KVM 主机正在运行防火墙,请确保该防火墙允许传入的流量通过 libvirtd 守护程序 TCP 侦听端口。在缺省情况下,该侦听端口为 16509。
  8. 通过使用 SASL 认证(以指示启用了 libvirt 的应用程序与 TCP 传输进行连接)来验证设置是否已成功。以下示例从远程管理站运行 virsh 命令,并以新创建的用户 admin 的身份登录,来在 kvmhost.company.org 系统中启动 guest02 实例:
    # virsh -c qemu+tcp://kvmhost.company.org/system start guest02
    Please enter your authentication name:admin
    Please enter your password:
    Domain guest02 started