Sha256: 85b50c000f6c83bf89862e874e73c080e60937a6031da463c4ae36d6d7fa4df8

Contents?: true

Size: 1.4 KB

Versions: 76

Compression:

Stored size: 1.4 KB

Contents

require 'puppet/ssl'
require 'puppet/ssl/certificate'

# Keep track of all of our known certificates.
class Puppet::SSL::Inventory
  attr_reader :path

  # Add a certificate to our inventory.
  def add(cert)
    cert = cert.content if cert.is_a?(Puppet::SSL::Certificate)

    # Create our file, if one does not already exist.
    rebuild unless FileTest.exist?(@path)

    Puppet.settings.write(:cert_inventory, "a") do |f|
      f.print format(cert)
    end
  end

  # Format our certificate for output.
  def format(cert)
    iso = '%Y-%m-%dT%H:%M:%S%Z'
    "0x%04x %s %s %s\n" % [cert.serial,  cert.not_before.strftime(iso), cert.not_after.strftime(iso), cert.subject]
  end

  def initialize
    @path = Puppet[:cert_inventory]
  end

  # Rebuild the inventory from scratch.  This should happen if
  # the file is entirely missing or if it's somehow corrupted.
  def rebuild
    Puppet.notice "Rebuilding inventory file"

    Puppet.settings.write(:cert_inventory) do |f|
      f.print "# Inventory of signed certificates\n# SERIAL NOT_BEFORE NOT_AFTER SUBJECT\n"
    end

    Puppet::SSL::Certificate.indirection.search("*").each { |cert| add(cert) }
  end

  # Find the serial number for a given certificate.
  def serial(name)
    return nil unless FileTest.exist?(@path)

    File.readlines(@path).each do |line|
      next unless line =~ /^(\S+).+\/CN=#{name}$/

      return Integer($1)
    end

    return nil
  end
end

Version data entries

76 entries across 76 versions & 4 rubygems

Version Path
puppet-parse-0.1.4 lib/vendor/puppet/ssl/inventory.rb
puppet-parse-0.1.3 lib/vendor/puppet/ssl/inventory.rb
puppet-parse-0.1.2 lib/vendor/puppet/ssl/inventory.rb
puppet-parse-0.1.1 lib/vendor/puppet/ssl/inventory.rb
puppet-2.7.26 lib/puppet/ssl/inventory.rb
puppet-2.7.25 lib/puppet/ssl/inventory.rb
puppet-2.7.24 lib/puppet/ssl/inventory.rb
puppet-3.3.2 lib/puppet/ssl/inventory.rb
puppet-3.3.1 lib/puppet/ssl/inventory.rb
puppet-3.3.1.rc3 lib/puppet/ssl/inventory.rb
puppet-3.3.1.rc2 lib/puppet/ssl/inventory.rb
puppet-3.3.1.rc1 lib/puppet/ssl/inventory.rb
puppet-3.3.0 lib/puppet/ssl/inventory.rb
puppet-3.3.0.rc3 lib/puppet/ssl/inventory.rb
puppet-3.3.0.rc2 lib/puppet/ssl/inventory.rb
puppet-3.2.4 lib/puppet/ssl/inventory.rb
puppet-2.7.23 lib/puppet/ssl/inventory.rb
puppet-3.2.3 lib/puppet/ssl/inventory.rb
puppet-3.2.3.rc1 lib/puppet/ssl/inventory.rb
puppet-3.2.2 lib/puppet/ssl/inventory.rb