summaryrefslogtreecommitdiff
path: root/lib/chef_zero/data_store/default_facade.rb
blob: fd5e7b7324a74bcdcb58eea1357893c33263f626 (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
require 'chef_zero/data_store/interface_v2'

module ChefZero
  module DataStore
    class DefaultFacade < ChefZero::DataStore::InterfaceV2
      def initialize(real_store, osc_compat)
        @real_store = real_store
        @osc_compat = osc_compat
        clear
      end

      attr_reader :real_store
      attr_reader :osc_compat

      def default(path, name=nil)
        value = @defaults
        for part in path
          break if !value
          value = value[part]
        end
        value = value[name] if value && name
        if value.is_a?(Proc)
          return value.call(self, path)
        else
          if value.nil?
            # ACLs are a special case: defaults for them exist as long as the
            # underlying object does
            if (path[0] == 'acls' || (path[0] == 'organizations' && path[2] == 'acls')) &&
               target_object_exists?(path)
              return '{}'
            end
          end
          return value
        end
      end

      def target_object_exists?(acl_path)
        if acl_path[0] == 'organizations'
          org_path = acl_path[0..1]
          object_part = acl_path[3..-1]
          if object_part == [ 'organization' ]
            exists_dir?(org_path)
          else
            path = org_path + object_part
            if object_part.size == 2 && %w(cookbooks data).include?(object_part[0])
              exists_dir?(path)
            else
              exists?(path)
            end
          end
        elsif acl_path[0] == 'acls'
          exists?(acl_path[1..-1])
        end
      end

      def delete_default(path)
        value = @defaults
        for part in path[0..-2]
          break if !value
          value = value[part]
        end
        if value
          !!value.delete(path[-1])
        else
          false
        end
      end

      def clear
        real_store.clear if real_store.respond_to?(:clear)
        @defaults = {
          'organizations' => {},
          'acls' => {}
        }
        unless osc_compat
          @defaults['users'] = {
            'pivotal' => '{}'
          }
        end
      end

      def create_dir(path, name, *options)
        if default(path, name) && !options.include?(:recursive)
          raise DataAlreadyExistsError.new(path + [name])
        end
        begin
          real_store.create_dir(path, name, *options)
        rescue DataNotFoundError
          if default(path)
            real_store.create_dir(path, name, :recursive, *options)
          else
            raise
          end
        end

        # If the org hasn't been created, create its defaults
        if path.size > 0 && path[0] == 'organizations'
          options_hash = options.last
          requestor = options_hash.is_a?(Hash) ? options_hash[:requestor] : nil
          if path.size == 1
            @defaults['organizations'][name] ||= org_defaults(name, requestor)
          else
            @defaults['organizations'][path[1]] ||= org_default(path[1], requestor)
          end
        end
      end

      def create(path, name, data, *options)
        if default(path, name) && !options.include?(:create_dir)
          raise DataAlreadyExistsError.new(path + [name])
        end
        begin
          real_store.create(path, name, data, *options)
        rescue DataNotFoundError
          if default(path)
            real_store.create(path, name, data, :create_dir, *options)
          else
            raise
          end
        end
        # If the org hasn't been created, create its defaults
        if path.size > 0 && path[0] == 'organizations'
          options_hash = options.last
          requestor = options_hash.is_a?(Hash) ? options_hash[:requestor] : nil
          if path.size == 1
            @defaults['organizations'][name] ||= org_defaults(name, options[:requestor])
          else
            @defaults['organizations'][path[1]] ||= org_defaults(path[1], options[:requestor])
          end
        end
      end

      def get(path, request=nil)
        begin
          real_store.get(path, request)
        rescue DataNotFoundError
          result = default(path)
          if result
            result
          else
            raise
          end
        end
      end

      def set(path, data, *options)
        begin
          real_store.set(path, data, *options)
        rescue DataNotFoundError
          if default(path)
            real_store.set(path, data, :create, :create_dir, *options)
          else
            raise
          end
        end
      end

      def delete(path)
        deleted = delete_default(path)
        begin
          real_store.delete(path)
        rescue DataNotFoundError
          if deleted
            return
          else
            raise
          end
        end
      end

      def delete_dir(path, *options)
        deleted = delete_default(path)
        begin
          real_store.delete_dir(path, *options)
        rescue DataNotFoundError
          if !deleted
            raise
          end
        end
      end

      def list(path)
        default_results = default(path)
        default_results = default_results.keys if default_results
        begin
          real_results = real_store.list(path)
          if default_results
            (real_results + default_results).uniq
          else
            real_results
          end
        rescue DataNotFoundError
          if default_results
            default_results
          else
            raise
          end
        end
      end

      def exists?(path)
        real_store.exists?(path) || default(path)
      end

      def exists_dir?(path)
        real_store.exists_dir?(path) || default(path)
      end

      def org_defaults(name, requestor)
        result = {
          'clients' => {
            "#{name}-validator" => '{ "validator": true }'
          },
          'cookbooks' => {},
          'data' => {},
          'environments' => {
            '_default' => '{ "description": "The default Chef environment" }'
          },
          'file_store' => {
            'checksums' => {}
          },
          'nodes' => {},
          'roles' => {},
          'sandboxes' => {},
          'users' => {},

          'org' => '{}',
          'containers' => {
            'clients' => '{}',
            'containers' => '{}',
            'cookbooks' => '{}',
            'data' => '{}',
            'environments' => '{}',
            'groups' => '{}',
            'nodes' => '{}',
            'roles' => '{}',
            'sandboxes' => '{}'
          },
          'groups' => {
            'admins' => admins_group(requestor),
            'billing-admins' => '{}',
            'clients' => clients_group,
            'users' => users_group(requestor),
          },
          'acls' => {
            'clients' => {},
            'containers' => {
              'cookbooks' => '{
                "create": { "groups": [ "admins", "users" ] },
                "read":   { "groups": [ "admins", "users", "clients" ] },
                "update": { "groups": [ "admins", "users" ] },
                "delete": { "groups": [ "admins", "users" ] }
              }',
              'environments' => '{
                "create": { "groups": [ "admins", "users" ] },
                "read":   { "groups": [ "admins", "users", "clients" ] },
                "update": { "groups": [ "admins", "users" ] },
                "delete": { "groups": [ "admins", "users" ] }
              }',
              'roles' => '{
                "create": { "groups": [ "admins", "users" ] },
                "read":   { "groups": [ "admins", "users", "clients" ] },
                "update": { "groups": [ "admins", "users" ] },
                "delete": { "groups": [ "admins", "users" ] }
              }',
              'data' => '{
                "create": { "groups": [ "admins", "users", "clients" ] },
                "read":   { "groups": [ "admins", "users", "clients" ] },
                "update": { "groups": [ "admins", "users", "clients" ] },
                "delete": { "groups": [ "admins", "users", "clients" ] }
              }',
              'nodes' => '{
                "create": { "groups": [ "admins", "users", "clients" ] },
                "read":   { "groups": [ "admins", "users", "clients" ] },
                "update": { "groups": [ "admins", "users" ] },
                "delete": { "groups": [ "admins", "users" ] }
              }',
              'clients' => '{
                "read": { "groups": [ "admins", "users" ] },
                "delete": { "groups": [ "admins", "users" ] }
              }',
              'groups' => '{}',
              'containers' => %'{
                "create": { "actors": [ "#{requestor}" ] },
                "read":   { "actors": [ "#{requestor}" ], "groups": [ "admins", "users" ] },
                "update": { "actors": [ "#{requestor}" ] },
                "delete": { "actors": [ "#{requestor}" ] },
                "grant":  { "actors": [ "#{requestor}" ] }
              }',
              'sandboxes' => '{
                "create":   { "groups": [ "admins", "users" ] }
              }'
            },
            'cookbooks' => {},
            'data' => {},
            'environments' => {},
            'groups' => {
              # It's a little weird that the default acls for groups
              # allows users to read, but these groups don't.
              'admins' => '{ "read": { "groups": [ "admins" ] } }',
              'clients' => '{ "read": { "groups": [ "admins" ] } }',
              'users' => '{ "read": { "groups": [ "admins" ] } }',
              'billing-admins' => '{
                "create": { "groups": [ ] },
                "read":   { "groups": [ "billing-admins" ] },
                "update": { "groups": [ "billing-admins" ] },
                "delete": { "groups": [ ] },
                "grant": { "groups": [ ] }
              }',
            },
            'nodes' => {},
            'roles' => {},
            'organizations' => '{
              "read": { "groups": [ "admins", "users" ] }
            }',
            'sandboxes' => {}
          },
          'association_requests' => {},
        }

        if osc_compat
          result['users']['admin'] = '{ "admin": "true" }'
          result['clients']["#{name}-webui"] = '{ "admin": true }'
        else
          result['users'][requestor] = '{}'
        end

        result
      end

      def admins_group(requestor)
        proc do |data, path|
          admins = data.list(path[0..1] + [ 'users' ]).select do |name|
            user = JSON.parse(data.get(path[0..1] + [ 'users', name ]), :create_additions => false)
            user['admin']
          end
          admins += data.list(path[0..1] + [ 'clients' ]).select do |name|
            client = JSON.parse(data.get(path[0..1] + [ 'clients', name ]), :create_additions => false)
            client['admin']
          end
          JSON.pretty_generate({ 'actors' => ([ requestor ] + admins).uniq })
        end
      end

      def clients_group
        proc do |data, path|
          clients = data.list(path[0..1] + [ 'clients' ])
          JSON.pretty_generate({ 'clients' => clients })
        end
      end

      def users_group(requestor)
        proc do |data, path|
          users = data.list(path[0..1] + [ 'users' ])
          JSON.pretty_generate({ 'users' => ([ requestor ] + users).uniq })
        end
      end
    end
  end
end