summaryrefslogtreecommitdiff
path: root/spec/support/shared/functional/securable_resource_with_reporting.rb
blob: 8a2ceed8379289ea1731070c3282133e3c5906bc (plain)
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
394
395
396
397
398

require 'functional/resource/base'

ALL_EXPANDED_PERMISSIONS = ["generic read",
                            "generic write",
                            "generic execute",
                            "generic all",
                            "delete",
                            "read permissions",
                            "change permissions",
                            "take ownership",
                            "synchronize",
                            "access system security",
                            "read data / list directory",
                            "write data / add file",
                            "append data / add subdirectory",
                            "read extended attributes",
                            "write extended attributes",
                            "execute / traverse",
                            "delete child",
                            "read attributes",
                            "write attributes"]


shared_examples_for "a securable resource with reporting" do

  include_context "diff disabled"

  let(:current_resource) do
    provider = resource.provider_for_action(resource.action)
    provider.load_current_resource
    provider.current_resource
  end

  # Default mode varies based on implementation. Providers that use a tempfile
  # will default to 0600. Providers that use File.open will default to 0666 -
  # umask
  # let(:default_mode) { ((0100666 - File.umask) & 07777).to_s(8) }

  describe "reading file security metadata for reporting on unix", :unix_only => true do
    # According to POSIX standard created files get either the
    # effective gid of the process or inherits the gid of the parent
    # directory based on file system. Since it's hard to guess what
    # would happen on each platform we create a dummy file and see
    # what the group name should be.
    before do
      FileUtils.touch(path)
      @expected_gid = File.stat(path).gid
      @expected_group_name = Etc.getgrgid(@expected_gid).name
      FileUtils.rm_rf(path)
    end

    context "when the target file doesn't exist" do
      before do
        resource.action(:create)
      end

      it "has empty values for file metadata in 'current_resource'" do
        expect(current_resource.owner).to be_nil
        expect(current_resource.group).to be_nil
        expect(current_resource.mode).to be_nil
      end

      context "and no security metadata is specified in new_resource" do
        it "sets the metadata values on the new_resource as strings after creating" do
          resource.run_action(:create)
          # TODO: most stable way to specify?
          expect(resource.owner).to eq(Etc.getpwuid(Process.uid).name)
          expect(resource.group).to eq(@expected_group_name)
          expect(resource.mode).to eq("0#{default_mode}")
        end
      end

      context "and owner is specified with a String (username) in new_resource", :requires_root => true do

        # TODO/bug: duplicated from the "securable resource" tests

        if ohai[:platform] == "aix"
          let(:expected_user_name) { 'guest' }
        else
          let(:expected_user_name) { 'nobody' }
        end

        before do
          resource.owner(expected_user_name)
          resource.run_action(:create)
        end

        it "sets the owner on new_resource to the username (String) of the desired owner" do
          expect(resource.owner).to eq(expected_user_name)
        end

      end

      context "and owner is specified with an Integer (uid) in new_resource", :requires_root => true do

        # TODO: duplicated from "securable resource"
        if ohai[:platform] == "aix"
          let(:expected_user_name) { 'guest' }
        else
          let(:expected_user_name) { 'nobody' }
        end
        let(:expected_uid) { Etc.getpwnam(expected_user_name).uid }
        let(:desired_gid) { 1337 }
        let(:expected_gid) { 1337 }

        before do
          resource.owner(expected_uid)
          resource.run_action(:create)
        end

        it "sets the owner on new_resource to the uid (Integer) of the desired owner" do
          expect(resource.owner).to eq(expected_uid)
        end
      end

      context "and group is specified with a String (group name)", :requires_root => true do

        let(:expected_group_name) { Etc.getgrent.name }

        before do
          resource.group(expected_group_name)
          resource.run_action(:create)
        end

        it "sets the group on new_resource to the group name (String) of the group" do
          expect(resource.group).to eq(expected_group_name)
        end

      end

      context "and group is specified with an Integer (gid)", :requires_root => true do
        let(:expected_gid) { Etc.getgrent.gid }

        before do
          resource.group(expected_gid)
          resource.run_action(:create)
        end

        it "sets the group on new_resource to the gid (Integer)" do
          expect(resource.group).to eq(expected_gid)
        end

      end

      context "and mode is specified as a String" do
        # Need full permission for owner here or else remote directory gets
        # into trouble trying to manage nested directories
        let(:set_mode) { "0740" }
        let(:expected_mode) { "0740" }

        before do
          resource.mode(set_mode)
          resource.run_action(:create)
        end

        it "sets mode on the new_resource as a String" do
          expect(resource.mode).to eq(expected_mode)
        end
      end

      context "and mode is specified as an Integer" do
        let(:set_mode) { 00740 }

        let(:expected_mode) { "0740" }
        before do
          resource.mode(set_mode)
          resource.run_action(:create)
        end

        it "sets mode on the new resource as a String" do
          expect(resource.mode).to eq(expected_mode)
        end
      end
    end

    context "when the target file exists" do
      before do
        FileUtils.touch(resource.path)
        resource.action(:create)
      end

      context "and no security metadata is specified in new_resource" do
        it "sets the current values on current resource as strings" do
          # TODO: most stable way to specify?
          expect(current_resource.owner).to eq(Etc.getpwuid(Process.uid).name)
          expect(current_resource.group).to eq(@expected_group_name)
          expect(current_resource.mode).to eq("0#{((0100666 - File.umask) & 07777).to_s(8)}")
        end
      end

      context "and owner is specified with a String (username) in new_resource" do

        let(:expected_user_name) { Etc.getpwuid(Process.uid).name }

        before do
          resource.owner(expected_user_name)
        end

        it "sets the owner on new_resource to the username (String) of the desired owner" do
          expect(current_resource.owner).to eq(expected_user_name)
        end

      end

      context "and owner is specified with an Integer (uid) in new_resource" do

        let(:expected_uid) { Process.uid }

        before do
          resource.owner(expected_uid)
        end

        it "sets the owner on new_resource to the uid (Integer) of the desired owner" do
          expect(current_resource.owner).to eq(expected_uid)
        end
      end

      context "and group is specified with a String (group name)" do
        before do
          resource.group(@expected_group_name)
        end

        it "sets the group on new_resource to the group name (String) of the group" do
          expect(current_resource.group).to eq(@expected_group_name)
        end

      end

      context "and group is specified with an Integer (gid)" do
        before do
          resource.group(@expected_gid)
        end

        it "sets the group on new_resource to the gid (Integer)" do
          expect(current_resource.group).to eq(@expected_gid)
        end

      end

      context "and mode is specified as a String" do
        let(:default_create_mode) { (0100666 - File.umask) }
        let(:expected_mode) { "0#{(default_create_mode & 07777).to_s(8)}" }

        before do
          resource.mode(expected_mode)
        end

        it "sets mode on the new_resource as a String" do
          expect(current_resource.mode).to eq(expected_mode)
        end
      end

      context "and mode is specified as an Integer" do
        let(:set_mode) { (0100666 - File.umask) & 07777 }
        let(:expected_mode) { "0#{set_mode.to_s(8)}" }

        before do
          resource.mode(set_mode)
        end

        it "sets mode on the new resource as a String" do
          expect(current_resource.mode).to eq(expected_mode)
        end
      end
    end
  end

  describe "reading file security metadata for reporting on windows", :windows_only do

    context "when the target file doesn't exist" do

      # Windows reporting data should look like this (+/- ish):
      # { "owner" => "bob", "checksum" => "ffff", "access control" => { "bob" => { "permissions" => ["perm1", "perm2", ...], "flags" => [] }}}


      before do
        resource.action(:create)
      end

      it "has empty values for file metadata in 'current_resource'" do
        pending "windows reporting not yet fully supported"
        expect(current_resource.owner).to be_nil
        expect(current_resource.expanded_rights).to be_nil
      end

      context "and no security metadata is specified in new_resource" do
        before do
          pending "windows reporting not yet fully supported"
        end

        it "sets the metadata values on the new_resource as strings after creating" do
          resource.run_action(:create)
          # TODO: most stable way to specify?
          expect(resource.owner).to eq(etc.getpwuid(process.uid).name)
          expect(resource.state[:expanded_rights]).to eq({ "CURRENTUSER" => { "permissions" => ALL_EXPANDED_PERMISSIONS, "flags" => [] }})
          expect(resource.state[:expanded_deny_rights]).to eq({})
          expect(resource.state[:inherits]).to be_truthy
        end
      end


      context "and owner is specified with a string (username) in new_resource"  do

        # TODO/bug: duplicated from the "securable resource" tests
        let(:expected_user_name) { 'Guest' }

        before do
          resource.owner(expected_user_name)
          resource.run_action(:create)
        end

        it "sets the owner on new_resource to the username (string) of the desired owner" do
          expect(resource.owner).to eq(expected_user_name)
        end

      end

      context "and owner is specified with a fully qualified domain user" do

        # TODO: duplicated from "securable resource"
        let(:expected_user_name) { 'domain\user' }

        before do
          pending "windows reporting not yet fully supported"
          resource.owner(expected_user_name)
          resource.run_action(:create)
        end

        it "sets the owner on new_resource to the fully qualified name of the desired owner" do
          expect(resource.owner).to eq(expected_user_name)
        end
      end

    end

    context "when the target file exists" do
      before do
        pending "windows reporting not yet fully supported"
        FileUtils.touch(resource.path)
        resource.action(:create)
      end

      context "and no security metadata is specified in new_resource" do
        it "sets the current values on current resource as strings" do
          # TODO: most stable way to specify?
          expect(current_resource.owner).to eq(etc.getpwuid(process.uid).name)
          expect(current_resource.expanded_rights).to eq({ "CURRENTUSER" => ALL_EXPANDED_PERMISSIONS })
        end
      end

      context "and owner is specified with a string (username) in new_resource" do

        let(:expected_user_name) { etc.getpwuid(process.uid).name }

        before do
          resource.owner(expected_user_name)
        end

        it "sets the owner on current_resource to the username (string) of the desired owner" do
          expect(current_resource.owner).to eq(expected_user_name)
        end

      end

      context "and owner is specified as a fully qualified 'domain\\user' in new_resource" do

        let(:expected_user_name) { 'domain\user' }

        before do
          resource.owner(expected_user_name)
        end

        it "sets the owner on current_resource to the fully qualified name of the desired owner" do
          expect(current_resource.owner).to eq(expected_uid)
        end
      end

      context "and access rights are specified on the new_resource" do
        # TODO: before do blah

        it "sets the expanded_rights on the current resource" do
          skip
        end
      end

      context "and no access rights are specified on the current resource" do
        # TODO: before do blah

        it "sets the expanded rights on the current resource" do
          skip
        end
      end


    end
  end
end