puppetserver-ca 2.1.0 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 497bfffece8f53a956a7b1e668e9e822cd96e1e14ee9491c8e7cc88d9344a373
4
- data.tar.gz: a77a2b34ef63e9952daf7b67f90ba895427584e02e859739a93092246775a5a6
3
+ metadata.gz: 92fe6ca44899e2b5aeec75304212ff99b5e4530cbed2e0b451ad2dd77e2bf09a
4
+ data.tar.gz: c46bc437a85f0a52fde2d3b2b51bda909ad179c1997056810b397d4b27ae4867
5
5
  SHA512:
6
- metadata.gz: 175e6787a090312bca7d48f042fba739e35b0eb41bde6059408ad1490788049906b5ccfa1c4408c1c391491c7ea73a98ba31b34062ec61eeb101aad20aaf749e
7
- data.tar.gz: 1fd6b2216952dca8053b03d5f2b09db65e6b4b3529a6c025ebb86331901f56aa3f1a25bb2c7cda3bbd7da3592ecd691a1e3b169b6e2166b57802b48adb0c6101
6
+ metadata.gz: 5c7ff17130d558382fa05f5f5bcb537ce9f2bc63a13078d1014783f636fcf0b0c02e604a0d4bf5694a486f67a56dec653430cea0c31eb911b7c15c9ea4e7f59d
7
+ data.tar.gz: 3ad80f74f02aac6a0dab1601cf54aad14756e0b1c0e2e0a30075d1eeafedff03b4546bc7c0cb7bc6dc6197b71abdb9f74816cd4596050b39e6c0f47ca47baf45
data/README.md CHANGED
@@ -55,6 +55,11 @@ To create a new keypair and certificate for a certname:
55
55
  puppetserver ca generate --certname foo.example.com
56
56
  ```
57
57
 
58
+ To enable verbose mode:
59
+ ```
60
+ puppetserver ca --verbose <action>
61
+ ```
62
+
58
63
  For more details, see the help output:
59
64
  ```
60
65
  puppetserver ca --help
@@ -68,7 +73,7 @@ for more details.
68
73
  ## Development
69
74
 
70
75
  After checking out the repo, run `bin/setup` to install dependencies. Then,
71
- run `rake spec` to run the tests. You can also run `bin/console` for an
76
+ run `bundle exec rake spec` to run the tests. You can also run `bin/console` for an
72
77
  interactive prompt that will allow you to experiment.
73
78
 
74
79
  To install this gem onto your local machine, run `bundle exec rake install`.
@@ -92,8 +97,7 @@ To test your changes on a VM:
92
97
  1. To confirm that installation was successful, run `puppetserver ca --help`
93
98
 
94
99
  ### Releasing
95
- To release a new version, run the [release pipeline](https://jenkins-master-prod-1.delivery.puppetlabs.net/job/platform_puppetserver-ca_init-multijob_main/), which will bump the version, tag, build, and release the gem.
96
-
100
+ To release a new version, run the [release pipeline](https://jenkins-platform.delivery.puppetlabs.net/job/platform_puppetserver-ca_init-multijob_main/), which will bump the version, tag, build, and release the gem.
97
101
 
98
102
  ## Contributing & Support
99
103
 
@@ -30,6 +30,7 @@ Options:
30
30
  BANNER
31
31
 
32
32
  BODY = JSON.dump({desired_state: 'signed'})
33
+ VALID_FORMAT = ['text', 'json']
33
34
 
34
35
  def initialize(logger)
35
36
  @logger = logger
@@ -47,6 +48,9 @@ Options:
47
48
  opts.on('--all', 'List all certificates') do |a|
48
49
  parsed['all'] = true
49
50
  end
51
+ opts.on('--format FORMAT', "Valid formats are: 'text' (default), 'json'") do |f|
52
+ parsed['format'] = f
53
+ end
50
54
  opts.on('--certname NAME[,NAME]', Array, 'List the specified cert(s)') do |cert|
51
55
  parsed['certname'] = cert
52
56
  end
@@ -57,9 +61,15 @@ Options:
57
61
  config = input['config']
58
62
  certnames = input['certname'] || []
59
63
  all = input['all']
64
+ output_format = input['format'] || "text"
65
+
66
+ unless VALID_FORMAT.include?(output_format)
67
+ Errors.handle_with_usage(@logger, ["Unknown format flag '#{output_format}'. Valid formats are '#{VALID_FORMAT.join("', '")}'."])
68
+ return 1
69
+ end
60
70
 
61
71
  if all && certnames.any?
62
- Errors.handle_with_usage(@logger, ['Cannot combine use of --all and --certname'])
72
+ Errors.handle_with_usage(@logger, ['Cannot combine use of --all and --certname.'])
63
73
  return 1
64
74
  end
65
75
 
@@ -71,24 +81,60 @@ Options:
71
81
  puppet = Config::Puppet.parse(config, @logger)
72
82
  return 1 if Errors.handle_with_usage(@logger, puppet.errors)
73
83
 
74
- filter_names = certnames.any? \
75
- ? lambda { |x| certnames.include?(x['name']) }
76
- : lambda { |x| true }
84
+ if certnames.any?
85
+ filter_names = lambda { |x| certnames.include?(x['name']) }
86
+ else
87
+ filter_names = lambda { |x| true }
88
+ end
77
89
 
78
90
  all_certs = get_all_certs(puppet.settings).select { |cert| filter_names.call(cert) }
79
91
  requested, signed, revoked = separate_certs(all_certs)
80
92
  missing = certnames - all_certs.map { |cert| cert['name'] }
81
93
 
82
- (all || certnames.any?) \
83
- ? output_certs_by_state(requested, signed, revoked, missing)
84
- : output_certs_by_state(requested)
94
+ if (all || certnames.any?)
95
+ output_certs_by_state(all, output_format, requested, signed, revoked, missing)
96
+ else
97
+ output_certs_by_state(all, output_format, requested)
98
+ end
99
+
100
+ return missing.any? ? 1 : 0
101
+ end
85
102
 
86
- return missing.any? \
87
- ? 1
88
- : 0
103
+ def output_certs_by_state(all, output_format, requested, signed = [], revoked = [], missing = [])
104
+ if output_format == 'json'
105
+ output_certs_json_format(all, requested, signed, revoked, missing)
106
+ else
107
+ output_certs_text_format(requested, signed, revoked, missing)
108
+ end
89
109
  end
90
110
 
91
- def output_certs_by_state(requested, signed = [], revoked = [], missing = [])
111
+ def output_certs_json_format(all, requested, signed, revoked, missing)
112
+ grouped_cert = {}
113
+
114
+ if all
115
+ grouped_cert = { "requested" => requested,
116
+ "signed" => signed,
117
+ "revoked" => revoked }.to_json
118
+ @logger.inform(grouped_cert)
119
+ else
120
+ grouped_cert["requested"] = requested unless requested.empty?
121
+ grouped_cert["signed"] = signed unless signed.empty?
122
+ grouped_cert["revoked"] = revoked unless revoked.empty?
123
+ grouped_cert["missing"] = missing unless missing.empty?
124
+
125
+ # If neither the '--all' flag or the '--certname' flag was passed in
126
+ # and the requested cert array is empty, we output a JSON object
127
+ # with an empty 'requested' key. Otherwise, we display
128
+ # any of the classes that are currently in grouped_cert
129
+ if grouped_cert.empty?
130
+ @logger.inform({ "requested" => requested }.to_json)
131
+ else
132
+ @logger.inform(grouped_cert.to_json)
133
+ end
134
+ end
135
+ end
136
+
137
+ def output_certs_text_format(requested, signed, revoked, missing)
92
138
  if revoked.empty? && signed.empty? && requested.empty? && missing.empty?
93
139
  @logger.inform "No certificates to list"
94
140
  return
@@ -165,7 +211,12 @@ Options:
165
211
 
166
212
  def get_all_certs(settings)
167
213
  result = Puppetserver::Ca::CertificateAuthority.new(@logger, settings).get_certificate_statuses
168
- result ? JSON.parse(result.body) : []
214
+
215
+ if result
216
+ return JSON.parse(result.body)
217
+ else
218
+ return []
219
+ end
169
220
  end
170
221
 
171
222
  def parse(args)
@@ -176,8 +227,11 @@ Options:
176
227
 
177
228
  errors_were_handled = Errors.handle_with_usage(@logger, errors, parser.help)
178
229
 
179
- exit_code = errors_were_handled ? 1 : nil
180
-
230
+ if errors_were_handled
231
+ exit_code = 1
232
+ else
233
+ exit_code = nil
234
+ end
181
235
  return results, exit_code
182
236
  end
183
237
  end
@@ -23,7 +23,7 @@ module Puppetserver
23
23
 
24
24
  def initialize(logger, settings)
25
25
  @logger = logger
26
- @client = HttpClient.new(settings)
26
+ @client = HttpClient.new(@logger, settings)
27
27
  @ca_server = settings[:ca_server]
28
28
  @ca_port = settings[:ca_port]
29
29
  end
@@ -64,8 +64,10 @@ BANNER
64
64
 
65
65
 
66
66
  def self.run(cli_args = ARGV, out = STDOUT, err = STDERR)
67
- logger = Puppetserver::Ca::Logger.new(:info, out, err)
68
67
  parser, general_options, unparsed = parse_general_inputs(cli_args)
68
+ level = general_options.delete('verbose') ? :debug : :info
69
+
70
+ logger = Puppetserver::Ca::Logger.new(level, out, err)
69
71
 
70
72
  if general_options['version']
71
73
  logger.inform Puppetserver::Ca::VERSION
@@ -121,6 +123,9 @@ BANNER
121
123
  opts.on('--version', 'Display the version') do |v|
122
124
  parsed['version'] = true
123
125
  end
126
+ opts.on('--verbose', 'Display low-level information') do |verbose|
127
+ parsed['verbose'] = true
128
+ end
124
129
 
125
130
  opts.separator ACTION_OPTIONS
126
131
  opts.separator "\nSee `puppetserver ca <action> --help` for detailed info"
@@ -13,6 +13,10 @@ module Puppetserver
13
13
  @err = err
14
14
  end
15
15
 
16
+ def level
17
+ @level
18
+ end
19
+
16
20
  def debug(text)
17
21
  if @level >= LEVELS[:debug]
18
22
  @out.puts(text)
@@ -19,7 +19,8 @@ module Puppetserver
19
19
 
20
20
  # Not all connections require a client cert to be present.
21
21
  # For example, when querying the status endpoint.
22
- def initialize(settings, with_client_cert: true)
22
+ def initialize(logger, settings, with_client_cert: true)
23
+ @logger = logger
23
24
  @store = make_store(settings[:localcacert],
24
25
  settings[:certificate_revocation],
25
26
  settings[:hostcrl])
@@ -50,7 +51,7 @@ module Puppetserver
50
51
  # The Connection object should have HTTP verbs defined on it that take
51
52
  # a body (and optional overrides). Returns whatever the block given returned.
52
53
  def with_connection(url, &block)
53
- request = ->(conn) { block.call(Connection.new(conn, url)) }
54
+ request = ->(conn) { block.call(Connection.new(conn, url, @logger)) }
54
55
 
55
56
  begin
56
57
  Net::HTTP.start(url.host, url.port,
@@ -85,29 +86,35 @@ module Puppetserver
85
86
  # and defines methods named after HTTP verbs that are called on the
86
87
  # saved connection, returning a Result.
87
88
  class Connection
88
- def initialize(net_http_connection, url_struct)
89
+ def initialize(net_http_connection, url_struct, logger)
89
90
  @conn = net_http_connection
90
91
  @url = url_struct
92
+ @logger = logger
91
93
  end
92
94
 
93
95
  def get(url_overide = nil, headers = {})
94
96
  url = url_overide || @url
95
97
  headers = DEFAULT_HEADERS.merge(headers)
96
98
 
99
+ @logger.debug("Making a GET request at #{url.full_url}")
100
+
97
101
  request = Net::HTTP::Get.new(url.to_uri, headers)
98
102
  result = @conn.request(request)
99
-
100
103
  Result.new(result.code, result.body)
104
+
101
105
  end
102
106
 
103
107
  def put(body, url_override = nil, headers = {})
104
108
  url = url_override || @url
105
109
  headers = DEFAULT_HEADERS.merge(headers)
106
110
 
111
+ @logger.debug("Making a PUT request at #{url.full_url}")
112
+
107
113
  request = Net::HTTP::Put.new(url.to_uri, headers)
108
114
  request.body = body
109
115
  result = @conn.request(request)
110
116
 
117
+
111
118
  Result.new(result.code, result.body)
112
119
  end
113
120
 
@@ -115,6 +122,8 @@ module Puppetserver
115
122
  url = url_override || @url
116
123
  headers = DEFAULT_HEADERS.merge(headers)
117
124
 
125
+ @logger.debug("Making a DELETE request at #{url.full_url}")
126
+
118
127
  result = @conn.request(Net::HTTP::Delete.new(url.to_uri, headers))
119
128
 
120
129
  Result.new(result.code, result.body)
@@ -171,7 +180,7 @@ module Puppetserver
171
180
  # we commonly won't have one, don't require one for creating the connection.
172
181
  # Additionally, we want to ensure the server is stopped before migrating the CA dir to
173
182
  # avoid issues with writing to the CA dir and moving it.
174
- self.new(settings, with_client_cert: false).with_connection(status_url) do |conn|
183
+ self.new(logger, settings, with_client_cert: false).with_connection(status_url) do |conn|
175
184
  result = conn.get
176
185
  if result.body == "running"
177
186
  logger.err "Puppetserver service is running. Please stop it before attempting to run this command."
@@ -1,5 +1,5 @@
1
1
  module Puppetserver
2
2
  module Ca
3
- VERSION = "2.1.0"
3
+ VERSION = "2.2.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: puppetserver-ca
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Puppet, Inc.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-04-21 00:00:00.000000000 Z
11
+ date: 2021-07-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: facter