Pages

Thursday, November 8, 2012

Automate Adding ESXi Hosts to vCenter With SSL Thumbprint

Recently, I was asked how to automate adding ESXi hosts to vCenter with a custom Perl script. For security purposes, vSphere vCenter requires SSL verification of the ESXi SSL certificate thumbprint. The automation challenge is generating the SHA1 digest for the ESXi host certificate, so it can then be passed as a parameter to AddHost_Task() method.

There are two solutions for getting the SSL thumbprint before adding an ESXi host to vCenter's inventory.
The first option is to call AddHost_Task() without the SSL thumbprint. The Task entity object will contain the SSL thumbprint as part of the task fault details.

The second option is to retrieve the ESXi host's SSL certificate from its web based configuration file listing. With the SSL certificate, the SHA1 digest can be generated in code.

Traditionally, the need to do full certificate chain validation before adding an ESXi host to a production vCenter Inventory would not be important as the hypervisor is typically built by a trusted source. However, the growing adoption of IaaS architecture models and automated, unattended deployments of Virtual Infrastructure increases the possibility of injecting an unsupported or even compromised ESXi hypervisor build into the production environment. The use of a Certificate Authority can become a real solution to verifying the validity of an ESXi host.

I generally recommend the second option. With the certificate, it's possible to do detailed verification and custom processing before adding the ESXi host to vCenter.

So how can you retrieve the ESXi SSL certificate? Each ESXi host provides a web share containing many of its configuration files, including the SSL certificate in PEM format. The URI to access the configuration file web share is https://<FQDN|IP>:443/host. You'll need valid credentials to access the web share, in this example we'll use the root password configured during the ESXi installation. Once successfully connected, the SSL certificate is listed as ssl_cert.

Note: If you're curious, there is another web share that points to the ESXi host's datastores. It's possible to use HTTP GET and POST operations to download and upload files to the datastore folders. The URI for the folder web share is https://<FQDN|IP>:443/folder.

Of course, the intention here is to automate the download of the ESXi SSL certificate. Using Perl's LWP::UserAgent, fetching the certificate is straightforward. The one detail to note is the realm name parameter to the UserAgent credentials() method. On ESXi systems, the realm name is VMware HTTP Server. As we'll be using the ESXi root login when adding the host to vCenter, we'll use those same credentials here to connect to the configuration file web share.

The certificate's content is in PEM format, so it will have to be converted to DER. The MIME::Base64 module provides decode_base64() to perform the conversion. Once the certificate data is available in DER format, the sha1_hex() method of Digest::SHA1 is used to generate a SHA1 digest of the certificate. Finally, some string manipulation is required to format the SHA1 digest with colon separators to get the final version of the SSL thumbprint.

Note: The MIME::Base64 and Digest::SHA1 modules are standard in most Perl installations and were selected since they will not require additional module installation before use.