~ubuntu-branches/ubuntu/oneiric/puppet/oneiric-security

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
#!/usr/bin/env ruby

require File.dirname(__FILE__) + '/../../../../spec_helper'
require 'puppet/network/http'

describe "Puppet::Network::HTTP::MongrelREST", "when initializing" do
    confine "Mongrel is not available" => Puppet.features.mongrel?

    before do
        require 'puppet/network/http/mongrel/rest'

        @mock_mongrel = mock('Mongrel server')
        @mock_mongrel.stubs(:register)
        @mock_model = mock('indirected model')
        Puppet::Indirector::Indirection.stubs(:model).with(:foo).returns(@mock_model)
        @params = { :server => @mock_mongrel, :handler => :foo }
    end

    it "should require access to a Mongrel server" do
        Proc.new { Puppet::Network::HTTP::MongrelREST.new(@params.delete_if {|k,v| :server == k })}.should raise_error(ArgumentError)
    end

    it "should require an indirection name" do
        Proc.new { Puppet::Network::HTTP::MongrelREST.new(@params.delete_if {|k,v| :handler == k })}.should raise_error(ArgumentError)        
    end

    it "should look up the indirection model from the indirection name" do
        Puppet::Indirector::Indirection.expects(:model).with(:foo).returns(@mock_model)
        Puppet::Network::HTTP::MongrelREST.new(@params)
    end

    it "should fail if the indirection is not known" do
        Puppet::Indirector::Indirection.expects(:model).with(:foo).returns(nil)
        Proc.new { Puppet::Network::HTTP::MongrelREST.new(@params) }.should raise_error(ArgumentError)
    end
end

describe "Puppet::Network::HTTP::MongrelREST", "when receiving a request" do
    confine "Mongrel is not available" => Puppet.features.mongrel?

    before do
        @mock_request = stub('mongrel http request')
        @mock_head = stub('response head')
        @mock_body = stub('response body', :write => true)
        @mock_response = stub('mongrel http response')
        @mock_response.stubs(:start).yields(@mock_head, @mock_body)
        @mock_model_class = stub('indirected model class')
        @mock_mongrel = stub('mongrel http server', :register => true)
        Puppet::Indirector::Indirection.stubs(:model).with(:foo).returns(@mock_model_class)
        @handler = Puppet::Network::HTTP::MongrelREST.new(:server => @mock_mongrel, :handler => :foo)
    end

    def setup_find_request(params = {})
        @mock_request.stubs(:params).returns({  Mongrel::Const::REQUEST_METHOD => 'GET', 
                                                Mongrel::Const::REQUEST_PATH => '/foo/key',
                                                'QUERY_STRING' => ''}.merge(params))
        @mock_model_class.stubs(:find)
    end

    def setup_search_request(params = {})
        @mock_request.stubs(:params).returns({  Mongrel::Const::REQUEST_METHOD => 'GET', 
                                                Mongrel::Const::REQUEST_PATH => '/foos',
                                                'QUERY_STRING' => '' }.merge(params))
        @mock_model_class.stubs(:search).returns([])        
    end

    def setup_destroy_request(params = {})
        @mock_request.stubs(:params).returns({  Mongrel::Const::REQUEST_METHOD => 'DELETE', 
                                                Mongrel::Const::REQUEST_PATH => '/foo/key',
                                                'QUERY_STRING' => '' }.merge(params))
        @mock_model_class.stubs(:destroy)
    end

    def setup_save_request(params = {})
        @mock_request.stubs(:params).returns({  Mongrel::Const::REQUEST_METHOD => 'PUT', 
                                                Mongrel::Const::REQUEST_PATH => '/foo',
                                                'QUERY_STRING' => '' }.merge(params))
        @mock_request.stubs(:body).returns('this is a fake request body')
        @mock_model_instance = stub('indirected model instance', :save => true)
        @mock_model_class.stubs(:from_yaml).returns(@mock_model_instance)
    end

    def setup_bad_request
        @mock_request.stubs(:params).returns({ Mongrel::Const::REQUEST_METHOD => 'POST', Mongrel::Const::REQUEST_PATH => '/foos'})        
    end

    it "should call the model find method if the request represents a singular HTTP GET" do
        setup_find_request
        @mock_model_class.expects(:find).with { |key, args| key == 'key' }
        @handler.process(@mock_request, @mock_response)
    end

    it "should call the model search method if the request represents a plural HTTP GET" do
        setup_search_request
        @mock_model_class.expects(:search).returns([])
        @handler.process(@mock_request, @mock_response)
    end

    it "should call the model destroy method if the request represents an HTTP DELETE" do
        setup_destroy_request
        @mock_model_class.expects(:destroy).with { |key, args| key == 'key' }
        @handler.process(@mock_request, @mock_response)
    end

    it "should call the model save method if the request represents an HTTP PUT" do
        setup_save_request
        @mock_model_instance.expects(:save)
        @handler.process(@mock_request, @mock_response)
    end

    it "should fail if the HTTP method isn't supported" do
        @mock_request.stubs(:params).returns({ Mongrel::Const::REQUEST_METHOD => 'POST', Mongrel::Const::REQUEST_PATH => '/foo'})
        @mock_response.expects(:start).with(404)
        @handler.process(@mock_request, @mock_response)
    end

    it "should fail if the request's pluralization is wrong" do
        @mock_request.stubs(:params).returns({ Mongrel::Const::REQUEST_METHOD => 'DELETE', Mongrel::Const::REQUEST_PATH => '/foos/key'})
        @mock_response.expects(:start).with(404)
        @handler.process(@mock_request, @mock_response)

        @mock_request.stubs(:params).returns({ Mongrel::Const::REQUEST_METHOD => 'PUT', Mongrel::Const::REQUEST_PATH => '/foos/key'})
        @mock_response.expects(:start).with(404)
        @handler.process(@mock_request, @mock_response)
    end

    it "should fail if the request is for an unknown path" do
        @mock_request.stubs(:params).returns({  Mongrel::Const::REQUEST_METHOD => 'GET', 
                                                Mongrel::Const::REQUEST_PATH => '/bar/key',
                                                'QUERY_STRING' => '' })
        @mock_response.expects(:start).with(404)
        @handler.process(@mock_request, @mock_response)
    end

    describe "and determining the request parameters", :shared => true do
        confine "Mongrel is not available" => Puppet.features.mongrel?

        before do
            @mock_request.stubs(:params).returns({})
        end

        it "should include the HTTP request parameters" do
            @mock_request.expects(:params).returns('QUERY_STRING' => 'foo=baz&bar=xyzzy')
            result = @handler.params(@mock_request)
            result["foo"].should == "baz"
            result["bar"].should == "xyzzy"
        end

        it "should pass the client's ip address to model find" do
            @mock_request.stubs(:params).returns("REMOTE_ADDR" => "ipaddress")
            @handler.params(@mock_request)[:ip].should == "ipaddress"
        end

        it "should use the :ssl_client_header to determine the parameter when looking for the certificate" do
            Puppet.settings.stubs(:value).returns "eh"
            Puppet.settings.expects(:value).with(:ssl_client_header).returns "myheader"
            @mock_request.stubs(:params).returns("myheader" => "/CN=host.domain.com")
            @handler.params(@mock_request)
        end

        it "should retrieve the hostname by matching the certificate parameter" do
            Puppet.settings.stubs(:value).returns "eh"
            Puppet.settings.expects(:value).with(:ssl_client_header).returns "myheader"
            @mock_request.stubs(:params).returns("myheader" => "/CN=host.domain.com")
            @handler.params(@mock_request)[:node].should == "host.domain.com"
        end

        it "should use the :ssl_client_header to determine the parameter for checking whether the host certificate is valid" do
            Puppet.settings.stubs(:value).with(:ssl_client_header).returns "certheader"
            Puppet.settings.expects(:value).with(:ssl_client_verify_header).returns "myheader"
            @mock_request.stubs(:params).returns("myheader" => "SUCCESS", "certheader" => "/CN=host.domain.com")
            @handler.params(@mock_request)
        end

        it "should consider the host authenticated if the validity parameter contains 'SUCCESS'" do
            Puppet.settings.stubs(:value).with(:ssl_client_header).returns "certheader"
            Puppet.settings.stubs(:value).with(:ssl_client_verify_header).returns "myheader"
            @mock_request.stubs(:params).returns("myheader" => "SUCCESS", "certheader" => "/CN=host.domain.com")
            @handler.params(@mock_request)[:authenticated].should be_true
        end

        it "should consider the host unauthenticated if the validity parameter does not contain 'SUCCESS'" do
            Puppet.settings.stubs(:value).with(:ssl_client_header).returns "certheader"
            Puppet.settings.stubs(:value).with(:ssl_client_verify_header).returns "myheader"
            @mock_request.stubs(:params).returns("myheader" => "whatever", "certheader" => "/CN=host.domain.com")
            @handler.params(@mock_request)[:authenticated].should be_false
        end

        it "should consider the host unauthenticated if no certificate information is present" do
            Puppet.settings.stubs(:value).with(:ssl_client_header).returns "certheader"
            Puppet.settings.stubs(:value).with(:ssl_client_verify_header).returns "myheader"
            @mock_request.stubs(:params).returns("myheader" => nil, "certheader" => "SUCCESS")
            @handler.params(@mock_request)[:authenticated].should be_false
        end

        it "should not pass a node name to model method if no certificate information is present" do
            Puppet.settings.stubs(:value).returns "eh"
            Puppet.settings.expects(:value).with(:ssl_client_header).returns "myheader"
            @mock_request.stubs(:params).returns("myheader" => nil)
            @handler.params(@mock_request).should_not be_include(:node)
        end
    end

    describe "when finding a model instance" do |variable|
        confine "Mongrel is not available" => Puppet.features.mongrel?

        it "should fail to find model if key is not specified" do
            @mock_request.stubs(:params).returns({ Mongrel::Const::REQUEST_METHOD => 'GET', Mongrel::Const::REQUEST_PATH => '/foo'})
            @mock_response.expects(:start).with(404)
            @handler.process(@mock_request, @mock_response)
        end

        it "should use a common method for determining the request parameters" do
            setup_find_request('QUERY_STRING' => 'foo=baz&bar=xyzzy')
            @handler.expects(:params).returns(:foo => :baz, :bar => :xyzzy)
            @mock_model_class.expects(:find).with do |key, args|
                args[:foo] == :baz and args[:bar] == :xyzzy
            end
            @handler.process(@mock_request, @mock_response)
        end

        it "should generate a 200 response when a model find call succeeds" do
            setup_find_request
            @mock_response.expects(:start).with(200)
            @handler.process(@mock_request, @mock_response)
        end

        it "should return a serialized object when a model find call succeeds" do
            setup_find_request
            @mock_model_instance = stub('model instance')
            @mock_model_instance.expects(:to_yaml)
            @mock_model_class.stubs(:find).returns(@mock_model_instance)
            @handler.process(@mock_request, @mock_response)                  
        end

        it "should serialize a controller exception when an exception is thrown by find" do
           setup_find_request
           @mock_model_class.expects(:find).raises(ArgumentError) 
           @mock_response.expects(:start).with(404)
           @handler.process(@mock_request, @mock_response)        
        end
    end

    describe "when destroying a model instance" do |variable|
        confine "Mongrel is not available" => Puppet.features.mongrel?

        it "should fail to destroy model if key is not specified" do
            @mock_request.stubs(:params).returns({ Mongrel::Const::REQUEST_METHOD => 'DELETE', Mongrel::Const::REQUEST_PATH => '/foo'})
            @mock_response.expects(:start).with(404)
            @handler.process(@mock_request, @mock_response)
        end

        it "should use a common method for determining the request parameters" do
            setup_destroy_request('QUERY_STRING' => 'foo=baz&bar=xyzzy')
            @handler.expects(:params).returns(:foo => :baz, :bar => :xyzzy)
            @mock_model_class.expects(:destroy).with do |key, args|
                args[:foo] == :baz and args[:bar] == :xyzzy
            end
            @handler.process(@mock_request, @mock_response)
        end

        it "should pass HTTP request parameters to model destroy" do
            setup_destroy_request('QUERY_STRING' => 'foo=baz&bar=xyzzy')
            @mock_model_class.expects(:destroy).with do |key, args|
                key == 'key' and args['foo'] == 'baz' and args['bar'] == 'xyzzy'
            end
            @handler.process(@mock_request, @mock_response)
        end

        it "should generate a 200 response when a model destroy call succeeds" do
            setup_destroy_request
            @mock_response.expects(:start).with(200)
            @handler.process(@mock_request, @mock_response)
        end

        it "should return a serialized success result when a model destroy call succeeds" do
            setup_destroy_request
            @mock_model_class.stubs(:destroy).returns(true)
            @mock_body.expects(:write).with("--- true\n")
            @handler.process(@mock_request, @mock_response)
        end

        it "should serialize a controller exception when an exception is thrown by destroy" do
            setup_destroy_request
            @mock_model_class.expects(:destroy).raises(ArgumentError) 
            @mock_response.expects(:start).with(404)
            @handler.process(@mock_request, @mock_response)                 
        end
    end

    describe "when saving a model instance" do |variable|    
        confine "Mongrel is not available" => Puppet.features.mongrel?

        it "should fail to save model if data is not specified" do
            @mock_request.stubs(:params).returns({ Mongrel::Const::REQUEST_METHOD => 'PUT', Mongrel::Const::REQUEST_PATH => '/foo'})
            @mock_request.stubs(:body).returns('')
            @mock_response.expects(:start).with(404)
            @handler.process(@mock_request, @mock_response)
        end

        it "should use a common method for determining the request parameters" do
            setup_save_request('QUERY_STRING' => 'foo=baz&bar=xyzzy')
            @handler.expects(:params).returns(:foo => :baz, :bar => :xyzzy)
            @mock_model_instance.expects(:save).with do |args|
                args[:foo] == :baz and args[:bar] == :xyzzy
            end
            @handler.process(@mock_request, @mock_response)
        end

        it "should generate a 200 response when a model save call succeeds" do
            setup_save_request
            @mock_response.expects(:start).with(200)
            @handler.process(@mock_request, @mock_response)
        end

        it "should return a serialized object when a model save call succeeds" do
            setup_save_request
            @mock_model_instance.stubs(:save).returns(@mock_model_instance)
            @mock_model_instance.expects(:to_yaml).returns('foo')
            @handler.process(@mock_request, @mock_response)        
        end

        it "should serialize a controller exception when an exception is thrown by save" do
            setup_save_request
            @mock_model_instance.expects(:save).raises(ArgumentError) 
            @mock_response.expects(:start).with(404)
            @handler.process(@mock_request, @mock_response)                         
        end
    end

    describe "when searching for model instances" do |variable|
        confine "Mongrel is not available" => Puppet.features.mongrel?

        it "should use a common method for determining the request parameters" do
            setup_search_request('QUERY_STRING' => 'foo=baz&bar=xyzzy')
            @handler.expects(:params).returns(:foo => :baz, :bar => :xyzzy)
            @mock_model_class.expects(:search).with do |args|
                args[:foo] == :baz and args[:bar] == :xyzzy
            end
            @handler.process(@mock_request, @mock_response)
        end

        it "should pass HTTP request parameters to model search" do
            setup_search_request('QUERY_STRING' => 'foo=baz&bar=xyzzy')
            @mock_model_class.expects(:search).with do |args|
                args['foo'] == 'baz' and args['bar'] == 'xyzzy'
            end.returns([])
            @handler.process(@mock_request, @mock_response)
        end      

        it "should generate a 200 response when a model search call succeeds" do
            setup_search_request
            @mock_response.expects(:start).with(200)
            @handler.process(@mock_request, @mock_response)
        end

        it "should return a list of serialized objects when a model search call succeeds" do
            setup_search_request
            mock_matches = [1..5].collect {|i| mock("model instance #{i}", :to_yaml => "model instance #{i}") }
            @mock_model_class.stubs(:search).returns(mock_matches)
            @handler.process(@mock_request, @mock_response)                          
        end

        it "should serialize a controller exception when an exception is thrown by search" do
            setup_search_request
            @mock_model_class.expects(:search).raises(ArgumentError) 
            @mock_response.expects(:start).with(404)
            @handler.process(@mock_request, @mock_response)                
        end
    end    

    it "should serialize a controller exception if the request fails" do
        setup_bad_request     
        @mock_response.expects(:start).with(404)
        @handler.process(@mock_request, @mock_response)        
    end
end