~ubuntu-branches/ubuntu/quantal/puppet/quantal-security

« back to all changes in this revision

Viewing changes to .pc/CVE-2011-3872.patch/spec/unit/ssl/certificate_authority/interface_spec.rb

  • Committer: Bazaar Package Importer
  • Author(s): Marc Deslauriers
  • Date: 2011-10-24 15:05:12 UTC
  • Revision ID: james.westby@ubuntu.com-20111024150512-yxqwfdp6hcs6of5l
Tags: 2.7.1-1ubuntu3.2
* SECURITY UPDATE: puppet master impersonation via incorrect certificates
  - debian/patches/CVE-2011-3872.patch: refactor certificate handling.
  - Thanks to upstream for providing the patch.
  - CVE-2011-3872

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env rspec
 
2
require 'spec_helper'
 
3
 
 
4
require 'puppet/ssl/certificate_authority'
 
5
 
 
6
shared_examples_for "a normal interface method" do
 
7
  it "should call the method on the CA for each host specified if an array was provided" do
 
8
    @ca.expects(@method).with("host1")
 
9
    @ca.expects(@method).with("host2")
 
10
 
 
11
    @applier = Puppet::SSL::CertificateAuthority::Interface.new(@method, :to => %w{host1 host2})
 
12
 
 
13
    @applier.apply(@ca)
 
14
  end
 
15
 
 
16
  it "should call the method on the CA for all existing certificates if :all was provided" do
 
17
    @ca.expects(:list).returns %w{host1 host2}
 
18
 
 
19
    @ca.expects(@method).with("host1")
 
20
    @ca.expects(@method).with("host2")
 
21
 
 
22
    @applier = Puppet::SSL::CertificateAuthority::Interface.new(@method, :to => :all)
 
23
 
 
24
    @applier.apply(@ca)
 
25
  end
 
26
end
 
27
 
 
28
describe Puppet::SSL::CertificateAuthority::Interface do
 
29
  before do
 
30
    @class = Puppet::SSL::CertificateAuthority::Interface
 
31
  end
 
32
  describe "when initializing" do
 
33
    it "should set its method using its settor" do
 
34
      @class.any_instance.expects(:method=).with(:generate)
 
35
      @class.new(:generate, :to => :all)
 
36
    end
 
37
 
 
38
    it "should set its subjects using the settor" do
 
39
      @class.any_instance.expects(:subjects=).with(:all)
 
40
      @class.new(:generate, :to => :all)
 
41
    end
 
42
 
 
43
    it "should set the digest if given" do
 
44
      interface = @class.new(:generate, :to => :all, :digest => :digest)
 
45
      interface.digest.should == :digest
 
46
    end
 
47
 
 
48
    it "should set the digest to md5 if none given" do
 
49
      interface = @class.new(:generate, :to => :all)
 
50
      interface.digest.should == :MD5
 
51
    end
 
52
  end
 
53
 
 
54
  describe "when setting the method" do
 
55
    it "should set the method" do
 
56
      @class.new(:generate, :to => :all).method.should == :generate
 
57
    end
 
58
 
 
59
    it "should fail if the method isn't a member of the INTERFACE_METHODS array" do
 
60
      Puppet::SSL::CertificateAuthority::Interface::INTERFACE_METHODS.expects(:include?).with(:thing).returns false
 
61
 
 
62
      lambda { @class.new(:thing, :to => :all) }.should raise_error(ArgumentError)
 
63
    end
 
64
  end
 
65
 
 
66
  describe "when setting the subjects" do
 
67
    it "should set the subjects" do
 
68
      @class.new(:generate, :to => :all).subjects.should == :all
 
69
    end
 
70
 
 
71
    it "should fail if the subjects setting isn't :all or an array", :'fails_on_ruby_1.9.2' => true do
 
72
      lambda { @class.new(:generate, "other") }.should raise_error(ArgumentError)
 
73
    end
 
74
  end
 
75
 
 
76
  it "should have a method for triggering the application" do
 
77
    @class.new(:generate, :to => :all).should respond_to(:apply)
 
78
  end
 
79
 
 
80
  describe "when applying" do
 
81
    before do
 
82
      # We use a real object here, because :verify can't be stubbed, apparently.
 
83
      @ca = Object.new
 
84
    end
 
85
 
 
86
    it "should raise InterfaceErrors" do
 
87
      @applier = @class.new(:revoke, :to => :all)
 
88
 
 
89
      @ca.expects(:list).raises Puppet::SSL::CertificateAuthority::Interface::InterfaceError
 
90
 
 
91
      lambda { @applier.apply(@ca) }.should raise_error(Puppet::SSL::CertificateAuthority::Interface::InterfaceError)
 
92
    end
 
93
 
 
94
    it "should log non-Interface failures rather than failing" do
 
95
      @applier = @class.new(:revoke, :to => :all)
 
96
 
 
97
      @ca.expects(:list).raises ArgumentError
 
98
 
 
99
      Puppet.expects(:err)
 
100
 
 
101
      lambda { @applier.apply(@ca) }.should_not raise_error
 
102
    end
 
103
 
 
104
    describe "with an empty array specified and the method is not list" do
 
105
      it "should fail" do
 
106
        @applier = @class.new(:sign, :to => [])
 
107
        lambda { @applier.apply(@ca) }.should raise_error(ArgumentError)
 
108
      end
 
109
    end
 
110
 
 
111
    describe ":generate" do
 
112
      it "should fail if :all was specified" do
 
113
        @applier = @class.new(:generate, :to => :all)
 
114
        lambda { @applier.apply(@ca) }.should raise_error(ArgumentError)
 
115
      end
 
116
 
 
117
      it "should call :generate on the CA for each host specified" do
 
118
        @applier = @class.new(:generate, :to => %w{host1 host2})
 
119
 
 
120
        @ca.expects(:generate).with("host1")
 
121
        @ca.expects(:generate).with("host2")
 
122
 
 
123
        @applier.apply(@ca)
 
124
      end
 
125
    end
 
126
 
 
127
    describe ":verify" do
 
128
      before { @method = :verify }
 
129
      #it_should_behave_like "a normal interface method"
 
130
 
 
131
      it "should call the method on the CA for each host specified if an array was provided" do
 
132
        # LAK:NOTE Mocha apparently doesn't allow you to mock :verify, but I'm confident this works in real life.
 
133
      end
 
134
 
 
135
      it "should call the method on the CA for all existing certificates if :all was provided" do
 
136
        # LAK:NOTE Mocha apparently doesn't allow you to mock :verify, but I'm confident this works in real life.
 
137
      end
 
138
    end
 
139
 
 
140
    describe ":destroy" do
 
141
      before { @method = :destroy }
 
142
      it_should_behave_like "a normal interface method"
 
143
    end
 
144
 
 
145
    describe ":revoke" do
 
146
      before { @method = :revoke }
 
147
      it_should_behave_like "a normal interface method"
 
148
    end
 
149
 
 
150
    describe ":sign" do
 
151
      describe "and an array of names was provided" do
 
152
        before do
 
153
          @applier = @class.new(:sign, :to => %w{host1 host2})
 
154
        end
 
155
 
 
156
        it "should sign the specified waiting certificate requests" do
 
157
          @ca.expects(:sign).with("host1")
 
158
          @ca.expects(:sign).with("host2")
 
159
 
 
160
          @applier.apply(@ca)
 
161
        end
 
162
      end
 
163
 
 
164
      describe "and :all was provided" do
 
165
        it "should sign all waiting certificate requests" do
 
166
          @ca.stubs(:waiting?).returns(%w{cert1 cert2})
 
167
 
 
168
          @ca.expects(:sign).with("cert1")
 
169
          @ca.expects(:sign).with("cert2")
 
170
 
 
171
          @applier = @class.new(:sign, :to => :all)
 
172
          @applier.apply(@ca)
 
173
        end
 
174
 
 
175
        it "should fail if there are no waiting certificate requests" do
 
176
          @ca.stubs(:waiting?).returns([])
 
177
 
 
178
          @applier = @class.new(:sign, :to => :all)
 
179
          lambda { @applier.apply(@ca) }.should raise_error(Puppet::SSL::CertificateAuthority::Interface::InterfaceError)
 
180
        end
 
181
      end
 
182
    end
 
183
 
 
184
    describe ":list" do
 
185
      describe "and an empty array was provided" do
 
186
        it "should print a string containing all certificate requests" do
 
187
          @ca.expects(:waiting?).returns %w{host1 host2}
 
188
          @ca.stubs(:verify)
 
189
 
 
190
          @applier = @class.new(:list, :to => [])
 
191
 
 
192
          @applier.expects(:puts).with "host1\nhost2"
 
193
 
 
194
          @applier.apply(@ca)
 
195
        end
 
196
      end
 
197
 
 
198
      describe "and :all was provided" do
 
199
        it "should print a string containing all certificate requests and certificates" do
 
200
          @ca.expects(:waiting?).returns %w{host1 host2}
 
201
          @ca.expects(:list).returns %w{host3 host4}
 
202
          @ca.stubs(:verify)
 
203
          @ca.stubs(:fingerprint).returns "fingerprint"
 
204
          @ca.expects(:verify).with("host3").raises(Puppet::SSL::CertificateAuthority::CertificateVerificationError.new(23), "certificate revoked")
 
205
 
 
206
          @applier = @class.new(:list, :to => :all)
 
207
 
 
208
          @applier.expects(:puts).with "host1 (fingerprint)"
 
209
          @applier.expects(:puts).with "host2 (fingerprint)"
 
210
          @applier.expects(:puts).with "- host3 (fingerprint) (certificate revoked)"
 
211
          @applier.expects(:puts).with "+ host4 (fingerprint)"
 
212
 
 
213
          @applier.apply(@ca)
 
214
        end
 
215
      end
 
216
 
 
217
      describe "and :signed was provided" do
 
218
        it "should print a string containing all signed certificate requests and certificates" do
 
219
          @ca.expects(:list).returns %w{host1 host2}
 
220
 
 
221
          @applier = @class.new(:list, :to => :signed)
 
222
 
 
223
          @applier.apply(@ca)
 
224
        end
 
225
      end
 
226
 
 
227
      describe "and an array of names was provided" do
 
228
        it "should print a string of all named hosts that have a waiting request" do
 
229
          @ca.expects(:waiting?).returns %w{host1 host2}
 
230
          @ca.expects(:list).returns %w{host3 host4}
 
231
          @ca.stubs(:fingerprint).returns "fingerprint"
 
232
          @ca.stubs(:verify)
 
233
 
 
234
          @applier = @class.new(:list, :to => %w{host1 host2 host3 host4})
 
235
 
 
236
          @applier.expects(:puts).with "host1 (fingerprint)"
 
237
          @applier.expects(:puts).with "host2 (fingerprint)"
 
238
          @applier.expects(:puts).with "+ host3 (fingerprint)"
 
239
          @applier.expects(:puts).with "+ host4 (fingerprint)"
 
240
 
 
241
          @applier.apply(@ca)
 
242
        end
 
243
      end
 
244
    end
 
245
 
 
246
    describe ":print" do
 
247
      describe "and :all was provided" do
 
248
        it "should print all certificates" do
 
249
          @ca.expects(:list).returns %w{host1 host2}
 
250
 
 
251
          @applier = @class.new(:print, :to => :all)
 
252
 
 
253
          @ca.expects(:print).with("host1").returns "h1"
 
254
          @applier.expects(:puts).with "h1"
 
255
 
 
256
          @ca.expects(:print).with("host2").returns "h2"
 
257
          @applier.expects(:puts).with "h2"
 
258
 
 
259
          @applier.apply(@ca)
 
260
        end
 
261
      end
 
262
 
 
263
      describe "and an array of names was provided" do
 
264
        it "should print each named certificate if found" do
 
265
          @applier = @class.new(:print, :to => %w{host1 host2})
 
266
 
 
267
          @ca.expects(:print).with("host1").returns "h1"
 
268
          @applier.expects(:puts).with "h1"
 
269
 
 
270
          @ca.expects(:print).with("host2").returns "h2"
 
271
          @applier.expects(:puts).with "h2"
 
272
 
 
273
          @applier.apply(@ca)
 
274
        end
 
275
 
 
276
        it "should log any named but not found certificates" do
 
277
          @applier = @class.new(:print, :to => %w{host1 host2})
 
278
 
 
279
          @ca.expects(:print).with("host1").returns "h1"
 
280
          @applier.expects(:puts).with "h1"
 
281
 
 
282
          @ca.expects(:print).with("host2").returns nil
 
283
          Puppet.expects(:err).with { |msg| msg.include?("host2") }
 
284
 
 
285
          @applier.apply(@ca)
 
286
        end
 
287
      end
 
288
    end
 
289
 
 
290
    describe ":fingerprint" do
 
291
      it "should fingerprint with the set digest algorithm" do
 
292
        @applier = @class.new(:fingerprint, :to => %w{host1}, :digest => :digest)
 
293
 
 
294
        @ca.expects(:fingerprint).with("host1", :digest).returns "fingerprint1"
 
295
        @applier.expects(:puts).with "host1 fingerprint1"
 
296
 
 
297
        @applier.apply(@ca)
 
298
      end
 
299
 
 
300
      describe "and :all was provided" do
 
301
        it "should fingerprint all certificates (including waiting ones)" do
 
302
          @ca.expects(:list).returns %w{host1}
 
303
          @ca.expects(:waiting?).returns %w{host2}
 
304
 
 
305
          @applier = @class.new(:fingerprint, :to => :all)
 
306
 
 
307
          @ca.expects(:fingerprint).with("host1", :MD5).returns "fingerprint1"
 
308
          @applier.expects(:puts).with "host1 fingerprint1"
 
309
 
 
310
          @ca.expects(:fingerprint).with("host2", :MD5).returns "fingerprint2"
 
311
          @applier.expects(:puts).with "host2 fingerprint2"
 
312
 
 
313
          @applier.apply(@ca)
 
314
        end
 
315
      end
 
316
 
 
317
      describe "and an array of names was provided" do
 
318
        it "should print each named certificate if found" do
 
319
          @applier = @class.new(:fingerprint, :to => %w{host1 host2})
 
320
 
 
321
          @ca.expects(:fingerprint).with("host1", :MD5).returns "fingerprint1"
 
322
          @applier.expects(:puts).with "host1 fingerprint1"
 
323
 
 
324
          @ca.expects(:fingerprint).with("host2", :MD5).returns "fingerprint2"
 
325
          @applier.expects(:puts).with "host2 fingerprint2"
 
326
 
 
327
          @applier.apply(@ca)
 
328
        end
 
329
      end
 
330
    end
 
331
  end
 
332
end