File: puppet_spec.rb

package info (click to toggle)
ruby-puppetserver-ca-cli 2.7.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 696 kB
  • sloc: ruby: 6,970; sh: 4; makefile: 3
file content (393 lines) | stat: -rw-r--r-- 11,355 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
124
125
126
127
128
129
130
131
132
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
158
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
require 'spec_helper'
require 'utils/ssl'
require 'puppetserver/ca/config/puppet'
require 'puppetserver/ca/logger'
require 'stringio'
require 'fileutils'

RSpec.describe 'Puppetserver::Ca::Config::Puppet' do
  subject { Puppetserver::Ca::Config::Puppet.new }
  let(:stdout) { StringIO.new }
  let(:stderr) { StringIO.new }
  let(:logger) { Puppetserver::Ca::Logger.new(:info, stdout, stderr) }

  it 'parses basic inifile' do
    parsed = subject.parse_text(<<-INI)
    certname = foo.example.org

    [agent]
      server = certname

    [master]
      dns_alt_names=puppet,foo
      cadir        = /var/www/super-secure

    [main]
    environment = prod_1_env
    INI

    expect(parsed.keys).to include(:agent, :master, :main)
    expect(parsed[:main]).to include({
      certname: 'foo.example.org',
      environment: 'prod_1_env'
    })
    expect(parsed[:master]).to include({
      dns_alt_names: 'puppet,foo',
      cadir: '/var/www/super-secure'
    })
    expect(parsed[:agent]).to include({
      server: 'certname'
    })
  end

  it 'discards weird file metadata info' do
    parsed = subject.parse_text(<<-INI)
    [ca]
      cadir = /var/www/ca {user = service}

    [master]
      coming_at = Innsmouth
      mantra = Pu̴t t̢h͞é ̢ćlas̸s̡e̸s̢ ̴in̡ arra̴y͡s̸ ̸o͟r̨ ̸hashe͜s̀ or w̨h̕át̀ev̵èr

    Now ̸to m̡et̴apr̷og҉ram a si͠mpl͞e ḑsl
    INI

    expect(parsed).to include({ca: {cadir: '/var/www/ca'}})
  end

  it 'correctly parses values with spaces and comments' do
    parsed = subject.parse_text(<<-INI)
    [master]
      ca_name = Some Other CA
      root_ca_name = Another Root CA # comment
      cadir = /some/where discard/this # here
    INI

    expect(parsed[:master]).to include({
      ca_name: 'Some Other CA',
      root_ca_name: 'Another Root CA',
      cadir: '/some/where'
    })
  end

  it 'resolves dependent settings properly' do
    Dir.mktmpdir do |tmpdir|
      puppet_conf = File.join(tmpdir, 'puppet.conf')
      File.open puppet_conf, 'w' do |f|
        f.puts(<<-INI)
          [agent]
            publickeydir = /agent/pubkeys

          [main]
            certname = fooberry

          [master]
            ssldir = /foo/bar
            cacrl = /fizz/buzz/crl.pem
        INI
      end

      conf = Puppetserver::Ca::Config::Puppet.new(puppet_conf)
      conf.load(logger: logger)

      expect(conf.errors).to be_empty
      expect(conf.settings[:localcacert]).to eq('/foo/bar/certs/ca.pem')
      expect(conf.settings[:cacrl]).to eq('/fizz/buzz/crl.pem')
      expect(conf.settings[:hostpubkey]).to eq('/agent/pubkeys/fooberry.pem')
    end
  end

  it 'correctly reads server_list' do
    p1 = subject.parse_text(<<-INI)
    [main]
      server_list = foo:80,bar,baz:99
    INI
    expect(p1[:main]).to include({
      server_list: 'foo:80,bar,baz:99'
    })

    s1 = subject.resolve_settings(p1[:main], logger)
    expect(s1[:server_list]).to eq([
      ["foo", "80"], ["bar"], ["baz", "99"]
    ])

    p2 = subject.parse_text(<<-INI)
      [main]
        server = foo
    INI
    s2 = subject.resolve_settings(p2[:main], logger)
    expect(s2[:server_list]).to eq([])
  end

  it 'uses server_list for default ca_server and ca_port' do
    parsed = subject.parse_text(<<-INI)
      [main]
        server_list = ca.example.com:8080

      [master]
        server = foo-master
    INI

    settings = subject.resolve_settings(parsed[:main].merge(parsed[:master]), logger)

    expect(settings[:server_list]).to eq([["ca.example.com", "8080"]])
    expect(settings[:ca_server]).to eq("ca.example.com")
    expect(settings[:ca_port]).to eq('8080')
  end

  it 'combines agent, main, and master sections properly' do
    Dir.mktmpdir do |tmpdir|
      puppet_conf = File.join(tmpdir, 'puppet.conf')
      File.open puppet_conf, 'w' do |f|
        f.puts(<<-INI)
          [agent]
            server = agent-server
            cacrl = agent-cacrl

          [main]
            server = main-server
            cacrl = main-cacrl
            certname = main-certname

          [master]
            server = master-server
            certname = master-certname
        INI
      end

      conf = Puppetserver::Ca::Config::Puppet.new(puppet_conf)
      conf.load(logger: logger)

      expect(conf.errors).to be_empty
      expect(conf.settings[:certname]).to eq('master-certname')
      expect(conf.settings[:server]).to eq('master-server')
      expect(conf.settings[:cacrl]).to eq('main-cacrl')
    end
  end

  it 'converts ca_ttl setting correctly into seconds' do
    Dir.mktmpdir do |tmpdir|
      puppet_conf = File.join(tmpdir, 'puppet.conf')
      File.open puppet_conf, 'w' do |f|
        f.puts(<<-INI)
          [master]
            ca_ttl = 5y
        INI
      end

      conf = Puppetserver::Ca::Config::Puppet.new(puppet_conf)
      conf.load(logger: logger)

      expect(conf.errors).to be_empty
      expect(conf.settings[:ca_ttl]).to eq(157680000)
    end
  end

  context "when dns_alt_names are provided" do
    it 'prepends "DNS" to unprefixed alt names and includes default certname' do
      allow_any_instance_of(Puppetserver::Ca::Config::Puppet).
        to receive(:default_certname).and_return("chihuahua-333")

      Dir.mktmpdir do |tmpdir|
        puppet_conf = File.join(tmpdir, 'puppet.conf')
        File.open puppet_conf, 'w' do |f|
          f.puts(<<-INI)
            [master]
              dns_alt_names = foo.com,IP:123.456.789
          INI
        end

        conf = Puppetserver::Ca::Config::Puppet.new(puppet_conf)
        conf.load(logger: logger)

        expect(conf.errors).to be_empty
        expect(conf.settings[:subject_alt_names]).
          to eq('DNS:chihuahua-333, DNS:foo.com, IP:123.456.789')
      end
    end
  end

  context "when dns_alt_names are NOT provided" do
    it 'it returns an empty string for subject_alt_names' do
      Dir.mktmpdir do |tmpdir|
        puppet_conf = File.join(tmpdir, 'puppet.conf')
        File.open puppet_conf, 'w' do |f|
          f.puts(<<-INI)
            [master]
              certname = foo.com
          INI
        end

        conf = Puppetserver::Ca::Config::Puppet.new(puppet_conf)
        conf.load(logger: logger)

        expect(conf.errors).to be_empty
        expect(conf.settings[:subject_alt_names]).
          to eq('')
      end
    end
  end

  it 'errs if it cannot resolve dependent settings properly' do
    Dir.mktmpdir do |tmpdir|
      puppet_conf = File.join(tmpdir, 'puppet.conf')
      File.open puppet_conf, 'w' do |f|
        f.puts(<<-INI)
          [master]
            ssldir = $vardir/ssl
        INI
      end

      conf = Puppetserver::Ca::Config::Puppet.new(puppet_conf)
      conf.load(logger: logger)

      expect(conf.errors.first).to include('$vardir in $vardir/ssl')
      expect(conf.settings[:localcacert]).to eq('$vardir/ssl/certs/ca.pem')
    end
  end

  it 'warns if the cadir is set to a custom location within the ssldir' do
    Dir.mktmpdir do |tmpdir|
      puppet_conf = File.join(tmpdir, 'puppet.conf')
      ssldir = File.join(tmpdir, 'ssl')
      cadir = File.join(ssldir, 'ca')
      File.open puppet_conf, 'w' do |f|
        f.puts(<<-INI)
          [main]
            ssldir = #{ssldir}

          [master]
            cadir = #{cadir}
        INI
      end

      conf = Puppetserver::Ca::Config::Puppet.new(puppet_conf)
      settings = conf.load(logger: logger)

      expect(settings[:cadir]).to eq(cadir)
      expect(stderr.string.each_line.first).
        to match(/migrate out from the puppet confdir to the \/etc\/puppet\/puppetserver\/ca/)
    end
  end

  it 'warns if the cadir already exists within the ssldir' do
    Dir.mktmpdir do |tmpdir|
      puppet_conf = File.join(tmpdir, 'puppet.conf')
      ssldir = File.join(tmpdir, 'ssl')
      cadir = File.join(ssldir, 'ca')
      FileUtils.mkdir_p cadir
      File.open puppet_conf, 'w' do |f|
        f.puts(<<-INI)
          [main]
            confdir = #{tmpdir}
            ssldir = #{ssldir}
        INI
      end

      conf = Puppetserver::Ca::Config::Puppet.new(puppet_conf)
      settings = conf.load(logger: logger)

      expect(settings[:cadir]).to eq(cadir)
      expect(stderr.string.each_line.first).
        to match(/migrate out from the puppet confdir to the \/etc\/puppet\/puppetserver\/ca/)
    end
  end

  it 'does not warn if configured for a cadir outside of the ssldir' do
    Dir.mktmpdir do |tmpdir|
      puppet_conf = File.join(tmpdir, 'puppet.conf')
      ssldir = File.join(tmpdir, 'ssl')
      cadir = File.join(tmpdir, 'ca')
      File.open puppet_conf, 'w' do |f|
        f.puts(<<-INI)
          [main]
            ssldir = #{ssldir}

          [master]
            cadir = #{cadir}
        INI
      end

      conf = Puppetserver::Ca::Config::Puppet.new(puppet_conf)
      settings = conf.load(logger: logger)

      expect(settings[:cadir]).to eq(cadir)
      expect(stderr.string).to be_empty
    end
  end

  it 'does not warn and returns the correct directory by default w/o existing directories or config' do
    Dir.mktmpdir do |tmpdir|
      puppet_conf = File.join(tmpdir, 'puppet.conf')
      confdir = File.join(tmpdir, 'puppet_confdir')
      File.open puppet_conf, 'w' do |f|
        f.puts(<<-INI)
          [main]
            confdir = #{confdir}
        INI
      end

      conf = Puppetserver::Ca::Config::Puppet.new(puppet_conf)
      settings = conf.load(logger: logger)

      expect(settings[:ssldir]).to eq('/var/lib/puppet/ssl')
      expect(settings[:cadir]).to eq(File.join(confdir, 'puppetserver', 'ca'))
      expect(stderr.string).to be_empty
    end
  end

  context 'building the default certname' do
    it 'when there is a domain, concatenates the hostname and domain' do
      expect(Facter).to receive(:value).with(:hostname).and_return("foo")
      expect(Facter).to receive(:value).with(:domain).and_return("example.com")

      conf = Puppetserver::Ca::Config::Puppet.new
      expect(conf.default_certname).to eq("foo.example.com")
    end

    it 'when domain is nil, returns just the hostname' do
      expect(Facter).to receive(:value).with(:hostname).and_return("foo")
      expect(Facter).to receive(:value).with(:domain).and_return(nil)

      conf = Puppetserver::Ca::Config::Puppet.new
      expect(conf.default_certname).to eq("foo")
    end
  end

  it 'loads masterport correctly' do
    masterport = 1234
    Dir.mktmpdir do |tmpdir|
      puppet_conf = File.join(tmpdir, 'puppet.conf')
      File.open puppet_conf, 'w' do |f|
        f.puts(<<-INI)
          [server]
            masterport = #{masterport}
        INI
      end

      conf = Puppetserver::Ca::Config::Puppet.new(puppet_conf)
      settings = conf.load(logger: logger)

      expect(settings[:ca_port]).to eq("#{masterport}")
      expect(stderr.string).to be_empty
    end
  end
  it 'loads serverport correctly' do
    serverport = 1234
    Dir.mktmpdir do |tmpdir|
      puppet_conf = File.join(tmpdir, 'puppet.conf')
      File.open puppet_conf, 'w' do |f|
        f.puts(<<-INI)
          [server]
            serverport = #{serverport}
        INI
      end

      conf = Puppetserver::Ca::Config::Puppet.new(puppet_conf)
      settings = conf.load(logger: logger)

      expect(settings[:ca_port]).to eq("#{serverport}")
      expect(stderr.string).to be_empty
    end
  end
end