之前已经介绍过了libvirt api的上手使用方式:
这里再补充一些细节问题。
TLS安全认证访问:
之前我们给出的例子都是直接用tcp访问的,这就需要被访问的服务器开放tcp访问的端口,也就是说,任何机器只要知道了服务器的ip,都是可以访问上面的libvirt接口的。这是十分危险的,在生产环境中是不可取的。
所以,libvirt支持基于tls认证的安全访问协议。连接格式如下:
qemu+tls://10.4.54.10
或者
qemu://10.4.54.10(因为默认就是走tls的)
要想正常访问,必须在客户端和服务端配置证书,下面就介绍一个这个配置过程。
1、首先自然是证书生成工具的安装。libvirt官方推荐的工具是:GnuTLS。不过可惜,官网给出的地然居然不正确。GnuTLS项目,官网地址如下:
下载页面为:
这里介绍的windows环境下配置,所以进入页面:
2、解压下载的工具。通过命令行进入\bin目录(或者将该bin目录配置到环境变量的path即可在任意路径访问):3、
首先为你的证书生成私钥(Create a private key for your CA):
certtool --generate-privkey > cakey.pem
然后,为你证书进行自签名(and self-sign it by creating a file with the signature details called ca.info containing):
cn = Name of your organization
ca
cert_signing_key
certtool --generate-self-signed --load-privkey cakey.pem \
--template ca.info --outfile cacert.pem
先在\bin下创建一个叫ca.info的文件,里面写上如上第一段内容,
然后执行第二段引用所示命令:
报错。这下头疼了。。笔者翻箱倒柜,翻江倒海找了一通。。实在没有找到答案。。。考虑到证书的制作过程中没有与机器相关的信息,所以,决定求助笔者非常不熟悉的linux。掏出putty,登录,重新执行上面第一个命令。生成私钥:
然后同样按照上面的方式进行签名,果然轻松成功!
此时已经可以删除ca.info文件了。继续生成证书,接下来开始生成服务端私钥:
certtool --generate-privkey > serverkey.pem
然后签名:
certtool --generate-certificate --load-privkey serverkey.pem
--load-ca-certificate cacert.pem --load-ca-privkey cakey.pem
--template server.info --outfile servercert.pem
同样是依靠一个叫做server.info的文件。先创建改文件,写入如下格式内容:
organization = Name of your organization
cn = oirase
tls_www_server
encryption_key
signing_key
这里只有cn这个属性需要注意,他应该是当前主机的主机名:(only the CN field matters, which as explained above must be the server's hostname)- (The CN must match the hostname which clients will be using to connect to the server. In the example below, clients will be connecting to the server using a URI of xen://oirase/, so the CN must be "oirase".)
如果不知道主机名,用ip当然也是可以的。那么你的访问就是通过IP访问了。:)
成功。将服务端证书拷贝服务器的相应位置:
serverkey.pem is the server's private key which should be copied to the server only as /etc/pki/libvirt/private/serverkey.pem.
servercert.pem is the server's certificate which can be installed on the server as /etc/pki/libvirt/servercert.pem.
最后是客户端的证书,过程类似,这里只列出主要过程和命令:
1、创建私钥:
certtool --generate-privkey > clientkey.pem
2、创建client.info模版文件:
country = GB
state = London
locality = London
organization = Red Hat
cn = client1
tls_www_client
encryption_key
signing_key
然后签名:
certtool --generate-certificate --load-privkey clientkey.pem
--load-ca-certificate cacert.pem --load-ca-privkey cakey.pem
--template client.info --outfile clientcert.pem
3、 拷贝证书到客户端的指定位置(linux):
cp clientkey.pem /etc/pki/libvirt/private/clientkey.pem
cp clientcert.pem /etc/pki/libvirt/clientcert.pem
其中client.info中国家等信息可根据你的情况指定。
至此,需要的证书已经都准备好了。证书有了,服务端也部署了。但是windows的客户端不知道该部署到什么地方?因为网上的说明给出的都是linux文件路径,没说windows下的情况。不过笔者相信libvirt的错误日志:)于是:
怎么样,位置有了吧。赶紧拷贝过去试试。
有的同学可能发现,这里只给出了一个位置,我们有三个证书呢。别急,你看这个路径,比照网上给出的linux的路径格式:Location
Machine
Description
Required fields
/etc/pki/CA/cacert.pem
Installed on all clients and servers
CA's certificate (more info)
n/a
/etc/pki/libvirt/ private/serverkey.pem
Installed on the server
Server's private key (more info)
n/a
/etc/pki/libvirt/ servercert.pem
Installed on the server
Server's certificate signed by the CA. (more info)
CommonName (CN) must be the hostname of the server as it is seen by clients.
/etc/pki/libvirt/ private/clientkey.pem
Installed on the client
Client's private key. (more info)
n/a
/etc/pki/libvirt/ clientcert.pem
Installed on the client
Client's certificate signed by the CA (more info)
Distinguished Name (DN) can be checked against an access control list (tls_allowed_dn_list).
其实基本已经可以猜出windows下的目录组织了。linux在的/etc目录,就想当于我们这里的..\libvirt目录,子目录的组织相同即可。
没猜出来也不用担心,最多我们复制一个证书尝试一次,错误信息会依次告诉你所有的路径的:)
再访问一下,成功~
补充说明一下关于动态链接文件的问题:
之前介绍的时候,笔者只提到说调用libvirt Java api需要一个virt.dl文件,这个文件笔者是拷贝的libvirt-0.dll文件改名而来,然后访问成功。
笔者后来发现,原来libvirt 安装目录的bin文件夹下的所有文件,其实都是改dll文件的依赖文件,当笔者将其他文件删除或转义的时候,依然会报找不到virt.dll文件的错误。所以,如果调用libvirt Java API开发,可以将所有的dll拷贝到一个指定位置,然后指定jna.library.path到该位置即可。
关于文中提到的尚未解决的问题,如果您有解决的方案或是任何想法,欢迎留言讨论,笔者不堪忍受这种困扰。如果笔者找到了方案,一定会补充进来。以上,欢迎讨论赐教:)