Parse Azure PublishSettings to get PEM Certificate

A typical Azure PublishSettings file will look like below:

<?xml version=”1.0″ encoding=”utf-8″?>
<PublishData>
<PublishProfile
PublishMethod=”AzureServiceManagementAPI”
Url=”https://management.core.windows.net/”    ManagementCertificate=”Cert-Data”>
<Subscription
Id=”Subscription-Id”
Name=”Subscription Name” />
</PublishProfile>
</PublishData>

The code snippet below will generate PEM file from your Azure PublishSettings file.

Save the code as generate.rb. Run it as:
ruby generate.rb filename.publishsettings

def generate(settings_file_name)
require ‘nokogiri’
require ‘base64’
require ‘openssl’
require ‘uri’

doc = Nokogiri::XML(File.open(settings_file_name))
profile = doc.at_css(“PublishProfile”)
@cert_data = OpenSSL::PKCS12.new(Base64.decode64(profile.attribute(“ManagementCertificate”).value))
@host_name = URI(profile.attribute(“Url”).value).host
@subscription_id =  doc.at_css(“Subscription”).attribute(“Id”).value

puts @cert_data
puts @cert_data.key
puts @cert_data.certificate
puts @host_name
puts @subscription_id

pem_data= @cert_data.key.to_s + @cert_data.certificate.to_s

File.write(“your_certificate.pem”, pem_data)
end

generate ARGV[0]

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s