# class Puppet::SSL::Inventory

Keep track of all of our known certificates.

### Attributes

path[R]

### Public Class Methods

new() click to toggle source

    # File lib/puppet/ssl/inventory.rb, line 26
    def initialize
      @path = Puppet[:cert_inventory]
    end

### Public Instance Methods

add(cert) click to toggle source

Add a certificate to our inventory.

    # File lib/puppet/ssl/inventory.rb, line 9
    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(cert) click to toggle source

Format our certificate for output.

    # File lib/puppet/ssl/inventory.rb, line 21
    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

rebuild() click to toggle source

Rebuild the inventory from scratch. This should happen if the file is entirely missing or if it’s somehow corrupted.

    # File lib/puppet/ssl/inventory.rb, line 32
    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

serial(name) click to toggle source

Find the serial number for a given certificate.

    # File lib/puppet/ssl/inventory.rb, line 43
    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