Class: Puppetserver::Ca::Action::List
- Inherits:
-
Object
- Object
- Puppetserver::Ca::Action::List
- Includes:
- Utils
- Defined in:
- lib/puppetserver/ca/action/list.rb
Constant Summary collapse
- SUMMARY =
'List certificates and CSRs'- BANNER =
<<-BANNER Usage: puppetserver ca list [--help] puppetserver ca list [--config] puppetserver ca list [--all] puppetserver ca list --certname NAME[,NAME] Description: List outstanding certificate requests. If --all is specified, signed and revoked certificates will be listed as well. Options: BANNER
- BODY =
JSON.dump({desired_state: 'signed'})
- VALID_FORMAT =
['text', 'json']
Class Method Summary collapse
Instance Method Summary collapse
- #format_alt_names(cert) ⇒ Object
- #format_authorization_extensions(cert) ⇒ Object
- #format_cert(cert, cert_column_width) ⇒ Object
- #format_cert_and_sha(cert, cert_column_width) ⇒ Object
- #get_cert_or_csr(settings, subject) ⇒ Object
- #get_certs_or_csrs(settings, queried_state = nil) ⇒ Object
-
#initialize(logger) ⇒ List
constructor
A new instance of List.
- #output_certs(certs) ⇒ Object
- #output_certs_by_state(all, output_format, requested, signed = [], revoked = [], missing = []) ⇒ Object
- #output_certs_json_format(all, requested, signed, revoked, missing) ⇒ Object
- #output_certs_text_format(requested, signed, revoked, missing) ⇒ Object
- #parse(args) ⇒ Object
- #run(input) ⇒ Object
- #separate_certs(all_certs) ⇒ Object
Constructor Details
#initialize(logger) ⇒ List
Returns a new instance of List.
35 36 37 |
# File 'lib/puppetserver/ca/action/list.rb', line 35 def initialize(logger) @logger = logger end |
Class Method Details
.parser(parsed = {}) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/puppetserver/ca/action/list.rb', line 39 def self.parser(parsed = {}) OptionParser.new do |opts| opts. = BANNER opts.on('--config CONF', 'Custom path to Puppet\'s config file') do |conf| parsed['config'] = conf end opts.on('--help', 'Display this command-specific help output') do |help| parsed['help'] = true end opts.on('--all', 'List all certificates') do |a| parsed['all'] = true end opts.on('--format FORMAT', "Valid formats are: 'text' (default), 'json'") do |f| parsed['format'] = f end opts.on('--certname NAME[,NAME]', Array, 'List the specified cert(s)') do |cert| parsed['certname'] = cert end end end |
Instance Method Details
#format_alt_names(cert) ⇒ Object
210 211 212 213 214 215 216 |
# File 'lib/puppetserver/ca/action/list.rb', line 210 def format_alt_names(cert) # In newer versions of the CA api we return subject_alt_names # in addition to dns_alt_names, this field includes DNS alt # names but also IP alt names. alt_names = cert['subject_alt_names'] || cert['dns_alt_names'] "alt names: #{alt_names}" unless alt_names.empty? end |
#format_authorization_extensions(cert) ⇒ Object
218 219 220 221 222 223 224 |
# File 'lib/puppetserver/ca/action/list.rb', line 218 def (cert) auth_exts = cert['authorization_extensions'] return nil if auth_exts.nil? || auth_exts.empty? values = auth_exts.map { |ext, value| "#{ext}: #{value}" }.join(', ') "authorization extensions: [#{values}]" end |
#format_cert(cert, cert_column_width) ⇒ Object
196 197 198 199 200 201 202 |
# File 'lib/puppetserver/ca/action/list.rb', line 196 def format_cert(cert, cert_column_width) [ format_cert_and_sha(cert, cert_column_width), format_alt_names(cert), (cert) ].compact.join("\t") end |
#format_cert_and_sha(cert, cert_column_width) ⇒ Object
204 205 206 207 208 |
# File 'lib/puppetserver/ca/action/list.rb', line 204 def format_cert_and_sha(cert, cert_column_width) justified_certname = cert['name'].ljust(cert_column_width + 6) sha = cert['fingerprints']['SHA256'] " #{justified_certname} (SHA256) #{sha}" end |
#get_cert_or_csr(settings, subject) ⇒ Object
245 246 247 248 249 250 251 252 253 |
# File 'lib/puppetserver/ca/action/list.rb', line 245 def get_cert_or_csr(settings, subject) result = Puppetserver::Ca::CertificateAuthority.new(@logger, settings).get_certificate_status(subject) if result return JSON.parse(result.body) else return nil end end |
#get_certs_or_csrs(settings, queried_state = nil) ⇒ Object
234 235 236 237 238 239 240 241 242 243 |
# File 'lib/puppetserver/ca/action/list.rb', line 234 def get_certs_or_csrs(settings, queried_state = nil) query = queried_state ? { :state => queried_state } : {} result = Puppetserver::Ca::CertificateAuthority.new(@logger, settings).get_certificate_statuses(query) if result return JSON.parse(result.body) else return nil end end |
#output_certs(certs) ⇒ Object
188 189 190 191 192 193 194 |
# File 'lib/puppetserver/ca/action/list.rb', line 188 def output_certs(certs) cert_column_width = certs.map { |c| c['name'].size }.max certs.each do |cert| @logger.inform(format_cert(cert, cert_column_width)) end end |
#output_certs_by_state(all, output_format, requested, signed = [], revoked = [], missing = []) ⇒ Object
125 126 127 128 129 130 131 |
# File 'lib/puppetserver/ca/action/list.rb', line 125 def output_certs_by_state(all, output_format, requested, signed = [], revoked = [], missing = []) if output_format == 'json' output_certs_json_format(all, requested, signed, revoked, missing) else output_certs_text_format(requested, signed, revoked, missing) end end |
#output_certs_json_format(all, requested, signed, revoked, missing) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/puppetserver/ca/action/list.rb', line 133 def output_certs_json_format(all, requested, signed, revoked, missing) grouped_cert = {} if all grouped_cert = { "requested" => requested, "signed" => signed, "revoked" => revoked }.to_json @logger.inform(grouped_cert) else grouped_cert["requested"] = requested unless requested.empty? grouped_cert["signed"] = signed unless signed.empty? grouped_cert["revoked"] = revoked unless revoked.empty? grouped_cert["missing"] = missing unless missing.empty? # If neither the '--all' flag or the '--certname' flag was passed in # and the requested cert array is empty, we output a JSON object # with an empty 'requested' key. Otherwise, we display # any of the classes that are currently in grouped_cert if grouped_cert.empty? @logger.inform({ "requested" => requested }.to_json) else @logger.inform(grouped_cert.to_json) end end end |
#output_certs_text_format(requested, signed, revoked, missing) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/puppetserver/ca/action/list.rb', line 159 def output_certs_text_format(requested, signed, revoked, missing) if revoked.empty? && signed.empty? && requested.empty? && missing.empty? @logger.inform "No certificates to list" return end unless requested.empty? @logger.inform "Requested Certificates:" output_certs(requested) end unless signed.empty? @logger.inform "Signed Certificates:" output_certs(signed) end unless revoked.empty? @logger.inform "Revoked Certificates:" output_certs(revoked) end unless missing.empty? @logger.inform "Missing Certificates:" missing.each do |name| @logger.inform " #{name}" end end end |
#parse(args) ⇒ Object
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/puppetserver/ca/action/list.rb', line 255 def parse(args) results = {} parser = self.class.parser(results) errors = CliParsing.parse_with_errors(parser, args) errors_were_handled = Errors.handle_with_usage(@logger, errors, parser.help) if errors_were_handled exit_code = 1 else exit_code = nil end return results, exit_code end |
#run(input) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/puppetserver/ca/action/list.rb', line 60 def run(input) config = input['config'] certnames = input['certname'] || [] all = input['all'] output_format = input['format'] || "text" missing = [] unless VALID_FORMAT.include?(output_format) Errors.handle_with_usage(@logger, ["Unknown format flag '#{output_format}'. Valid formats are '#{VALID_FORMAT.join("', '")}'."]) return 1 end if all && certnames.any? Errors.handle_with_usage(@logger, ['Cannot combine use of --all and --certname.']) return 1 end if config errors = FileSystem.validate_file_paths(config) return 1 if Errors.handle_with_usage(@logger, errors) end puppet = Config::Puppet.parse(config, @logger) return 1 if Errors.handle_with_usage(@logger, puppet.errors) if certnames.any? filter_names = lambda { |x| certnames.include?(x['name']) } else filter_names = lambda { |x| true } end if (all || certnames.any?) if certnames.size == 1 result = get_cert_or_csr(puppet.settings, certnames.first) if result.nil? found_certs = [{}] else found_certs = [result] end else found_certs = get_certs_or_csrs(puppet.settings) end if found_certs.nil? # nil is different from no certs found @logger.err('Error while getting certificates') return 1 end all_certs = found_certs.select { |cert| filter_names.call(cert) } requested, signed, revoked = separate_certs(all_certs) missing = certnames - all_certs.map { |cert| cert['name'] } output_certs_by_state(all, output_format, requested, signed, revoked, missing) else all_csrs = get_certs_or_csrs(puppet.settings, "requested") if all_csrs.nil? # nil is different from no certs found @logger.err('Error while getting certificate requests') return 1 end output_certs_by_state(all, output_format, all_csrs) end return missing.any? ? 1 : 0 end |
#separate_certs(all_certs) ⇒ Object
226 227 228 229 230 231 232 |
# File 'lib/puppetserver/ca/action/list.rb', line 226 def separate_certs(all_certs) certs = all_certs.group_by { |v| v["state"]} requested = certs.fetch("requested", []) signed = certs.fetch("signed", []) revoked = certs.fetch("revoked", []) return requested, signed, revoked end |