cheffish 0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +201 -0
  3. data/README.md +4 -0
  4. data/Rakefile +23 -0
  5. data/lib/chef/provider/chef_client.rb +44 -0
  6. data/lib/chef/provider/chef_data_bag.rb +50 -0
  7. data/lib/chef/provider/chef_data_bag_item.rb +273 -0
  8. data/lib/chef/provider/chef_environment.rb +78 -0
  9. data/lib/chef/provider/chef_node.rb +82 -0
  10. data/lib/chef/provider/chef_role.rb +79 -0
  11. data/lib/chef/provider/chef_user.rb +48 -0
  12. data/lib/chef/provider/private_key.rb +160 -0
  13. data/lib/chef/provider/public_key.rb +83 -0
  14. data/lib/chef/resource/chef_client.rb +44 -0
  15. data/lib/chef/resource/chef_data_bag.rb +18 -0
  16. data/lib/chef/resource/chef_data_bag_item.rb +114 -0
  17. data/lib/chef/resource/chef_environment.rb +71 -0
  18. data/lib/chef/resource/chef_node.rb +18 -0
  19. data/lib/chef/resource/chef_role.rb +104 -0
  20. data/lib/chef/resource/chef_user.rb +51 -0
  21. data/lib/chef/resource/in_parallel.rb +6 -0
  22. data/lib/chef/resource/private_key.rb +39 -0
  23. data/lib/chef/resource/public_key.rb +16 -0
  24. data/lib/cheffish.rb +245 -0
  25. data/lib/cheffish/actor_provider_base.rb +120 -0
  26. data/lib/cheffish/chef_provider_base.rb +222 -0
  27. data/lib/cheffish/cheffish_server_api.rb +21 -0
  28. data/lib/cheffish/inline_resource.rb +88 -0
  29. data/lib/cheffish/key_formatter.rb +93 -0
  30. data/lib/cheffish/recipe_dsl.rb +98 -0
  31. data/lib/cheffish/version.rb +4 -0
  32. data/spec/integration/chef_client_spec.rb +48 -0
  33. data/spec/integration/chef_node_spec.rb +75 -0
  34. data/spec/integration/chef_user_spec.rb +48 -0
  35. data/spec/integration/private_key_spec.rb +356 -0
  36. data/spec/integration/recipe_dsl_spec.rb +29 -0
  37. data/spec/support/key_support.rb +29 -0
  38. data/spec/support/spec_support.rb +148 -0
  39. metadata +124 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0512c4f800b394e72ccee8479c30444f491433c6
4
+ data.tar.gz: 85a52358428a51a00c4d4f70952cc0d57e76ec82
5
+ SHA512:
6
+ metadata.gz: ed5ae7503d424bd77886cd56d5e282d113964d7ab79e73def85583aea4071e9cf1ad4556d2c9af0e4d382f873d542f5950d1b7fc0634920589b40ffce76606a4
7
+ data.tar.gz: ca0aeb3416e9847ca03f9258c5e6f66411bf1bb69ad3e0f83255e601ed13781ceff23cb66e14a6aebe5ee211ccab1cd74186180230c97e8e9dacdf989b04adbd
data/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,4 @@
1
+ Cheffish
2
+ ========
3
+
4
+ This library lets you manipulate Chef in Chef.
@@ -0,0 +1,23 @@
1
+ require 'bundler'
2
+ require 'rubygems'
3
+ require 'rubygems/package_task'
4
+ require 'rdoc/task'
5
+ require 'rspec/core/rake_task'
6
+
7
+ Bundler::GemHelper.install_tasks
8
+
9
+ task :default => :spec
10
+
11
+ desc "Run specs"
12
+ RSpec::Core::RakeTask.new(:spec) do |spec|
13
+ spec.pattern = 'spec/**/*_spec.rb'
14
+ end
15
+
16
+ gem_spec = eval(File.read("cheffish.gemspec"))
17
+
18
+ RDoc::Task.new do |rdoc|
19
+ rdoc.rdoc_dir = 'rdoc'
20
+ rdoc.title = "cheffish #{gem_spec.version}"
21
+ rdoc.rdoc_files.include('README*')
22
+ rdoc.rdoc_files.include('lib/**/*.rb')
23
+ end
@@ -0,0 +1,44 @@
1
+ require 'cheffish/actor_provider_base'
2
+ require 'chef/resource/chef_client'
3
+ require 'chef/chef_fs/data_handler/client_data_handler'
4
+
5
+ class Chef::Provider::ChefClient < Cheffish::ActorProviderBase
6
+
7
+ def whyrun_supported?
8
+ true
9
+ end
10
+
11
+ def actor_type
12
+ 'client'
13
+ end
14
+
15
+ action :create do
16
+ create_actor
17
+ end
18
+
19
+ action :delete do
20
+ delete_actor
21
+ end
22
+
23
+ #
24
+ # Helpers
25
+ #
26
+
27
+ def resource_class
28
+ Chef::Resource::ChefClient
29
+ end
30
+
31
+ def data_handler
32
+ Chef::ChefFS::DataHandler::ClientDataHandler.new
33
+ end
34
+
35
+ def keys
36
+ {
37
+ 'name' => :name,
38
+ 'admin' => :admin,
39
+ 'validator' => :validator,
40
+ 'public_key' => :source_key
41
+ }
42
+ end
43
+
44
+ end
@@ -0,0 +1,50 @@
1
+ require 'cheffish/chef_provider_base'
2
+ require 'chef/resource/chef_data_bag'
3
+
4
+ class Chef::Provider::ChefDataBag < Cheffish::ChefProviderBase
5
+
6
+ def whyrun_supported?
7
+ true
8
+ end
9
+
10
+ action :create do
11
+ if !current_resource_exists?
12
+ converge_by "create data bag #{new_resource.name} at #{rest.url}" do
13
+ rest.post("data", { 'name' => new_resource.name })
14
+ end
15
+ end
16
+ end
17
+
18
+ action :delete do
19
+ if current_resource_exists?
20
+ converge_by "delete data bag #{new_resource.name} at #{rest.url}" do
21
+ rest.delete("data/#{new_resource.name}")
22
+ end
23
+ end
24
+ end
25
+
26
+ def load_current_resource
27
+ begin
28
+ @current_resource = json_to_resource(rest.get("data/#{new_resource.name}"))
29
+ rescue Net::HTTPServerException => e
30
+ if e.response.code == "404"
31
+ @current_resource = not_found_resource
32
+ else
33
+ raise
34
+ end
35
+ end
36
+ end
37
+
38
+ #
39
+ # Helpers
40
+ #
41
+ # Gives us new_json, current_json, not_found_json, etc.
42
+
43
+ def resource_class
44
+ Chef::Resource::ChefDataBag
45
+ end
46
+
47
+ def json_to_resource(json)
48
+ Chef::Resource::ChefDataBag.new(json['name'])
49
+ end
50
+ end
@@ -0,0 +1,273 @@
1
+ require 'cheffish/chef_provider_base'
2
+ require 'chef/resource/chef_data_bag_item'
3
+ require 'chef/chef_fs/data_handler/data_bag_item_data_handler'
4
+ require 'chef/encrypted_data_bag_item'
5
+
6
+ class Chef::Provider::ChefDataBagItem < Cheffish::ChefProviderBase
7
+
8
+ def whyrun_supported?
9
+ true
10
+ end
11
+
12
+ action :create do
13
+ differences = calculate_differences
14
+
15
+ if current_resource_exists?
16
+ if differences.size > 0
17
+ description = [ "update data bag item #{new_resource.id} at #{rest.url}" ] + differences
18
+ converge_by description do
19
+ rest.put("data/#{new_resource.data_bag}/#{new_resource.id}", normalize_for_put(new_json))
20
+ end
21
+ end
22
+ else
23
+ description = [ "create data bag item #{new_resource.id} at #{rest.url}" ] + differences
24
+ converge_by description do
25
+ rest.post("data/#{new_resource.data_bag}", normalize_for_post(new_json))
26
+ end
27
+ end
28
+ end
29
+
30
+ action :delete do
31
+ if current_resource_exists?
32
+ converge_by "delete data bag item #{new_resource.id} at #{rest.url}" do
33
+ rest.delete("data/#{new_resource.data_bag}/#{new_resource.id}")
34
+ end
35
+ end
36
+ end
37
+
38
+ def load_current_resource
39
+ begin
40
+ json = rest.get("data/#{new_resource.data_bag}/#{new_resource.id}")
41
+ resource = Chef::Resource::ChefDataBagItem.new(new_resource.name)
42
+ resource.raw_data json
43
+ @current_resource = resource
44
+ rescue Net::HTTPServerException => e
45
+ if e.response.code == "404"
46
+ @current_resource = not_found_resource
47
+ else
48
+ raise
49
+ end
50
+ end
51
+
52
+ # Determine if data bag is encrypted and if so, what its version is
53
+ first_real_key, first_real_value = (current_resource.raw_data || {}).select { |key, value| key != 'id' && !value.nil? }.first
54
+ if first_real_value
55
+ if first_real_value.is_a?(Hash) &&
56
+ first_real_value['version'].is_a?(Integer) &&
57
+ first_real_value['version'] > 0 &&
58
+ first_real_value.has_key?('encrypted_data')
59
+
60
+ current_resource.encrypt true
61
+ current_resource.encryption_version first_real_value['version']
62
+
63
+ decrypt_error = nil
64
+
65
+ # Check if the desired secret is the one (which it generally should be)
66
+
67
+ if new_resource.secret || new_resource.secret_path
68
+ begin
69
+ Chef::EncryptedDataBagItem::Decryptor.for(first_real_value, new_secret).for_decrypted_item
70
+ current_resource.secret new_secret
71
+ rescue Chef::EncryptedDataBagItem::DecryptionFailure
72
+ decrypt_error = $!
73
+ end
74
+ end
75
+
76
+ # If the current secret doesn't work, look through the specified old secrets
77
+
78
+ if !current_resource.secret
79
+ old_secrets = []
80
+ if new_resource.old_secret
81
+ old_secrets += Array(new_resource.old_secret)
82
+ end
83
+ if new_resource.old_secret_path
84
+ old_secrets += Array(new_resource.old_secret_path).map do |secret_path|
85
+ Chef::EncryptedDataBagItem.load_secret(new_resource.old_secret_file)
86
+ end
87
+ end
88
+ old_secrets.each do |secret|
89
+ begin
90
+ Chef::EncryptedDataBagItem::Decryptor.for(first_real_value, secret).for_decrypted_item
91
+ current_resource.secret secret
92
+ rescue Chef::EncryptedDataBagItem::DecryptionFailure
93
+ decrypt_error = $!
94
+ end
95
+ end
96
+
97
+ # If we couldn't figure out the secret, emit a warning (this isn't a fatal flaw unless we
98
+ # need to reuse one of the values from the data bag)
99
+ if !current_resource.secret
100
+ if decrypt_error
101
+ Chef::Log.warn "Existing data bag is encrypted, but could not decrypt: #{decrypt_error.message}."
102
+ else
103
+ Chef::Log.warn "Existing data bag is encrypted, but no secret was specified."
104
+ end
105
+ end
106
+ end
107
+ end
108
+ else
109
+
110
+ # There are no encryptable values, so pretend encryption is the same as desired
111
+
112
+ current_resource.encrypt new_resource.encrypt
113
+ current_resource.encryption_version new_resource.encryption_version
114
+ if new_resource.secret || new_resource.secret_path
115
+ current_resource.secret new_secret
116
+ end
117
+ end
118
+ end
119
+
120
+ def new_json
121
+ @new_json ||= begin
122
+ if new_encrypt
123
+ # Encrypt new stuff
124
+ result = encrypt(new_decrypted, new_secret, new_resource.encryption_version)
125
+ else
126
+ result = new_decrypted
127
+ end
128
+ result
129
+ end
130
+ end
131
+
132
+ def new_encrypt
133
+ new_resource.encrypt.nil? ? current_resource.encrypt : new_resource.encrypt
134
+ end
135
+
136
+ def new_secret
137
+ @new_secret ||= begin
138
+ if new_resource.secret
139
+ new_resource.secret
140
+ elsif new_resource.secret_path
141
+ Chef::EncryptedDataBagItem.load_secret(new_resource.secret_path)
142
+ elsif new_resource.encrypt.nil?
143
+ current_resource.secret
144
+ else
145
+ raise "Data bag item #{new_resource.name} has encryption on but no secret or secret_path is specified"
146
+ end
147
+ end
148
+ end
149
+
150
+ def decrypt(json, secret)
151
+ Chef::EncryptedDataBagItem.new(json, secret).to_hash
152
+ end
153
+
154
+ def encrypt(json, secret, version)
155
+ old_version = Chef::Config[:data_bag_encrypt_version]
156
+ Chef::Config[:data_bag_encrypt_version] = version
157
+ begin
158
+ Chef::EncryptedDataBagItem.encrypt_data_bag_item(json, secret)
159
+ ensure
160
+ Chef::Config[:data_bag_encrypt_version] = old_version
161
+ end
162
+ end
163
+
164
+ # Get the desired (new) json pre-encryption, for comparison purposes
165
+ def new_decrypted
166
+ @new_decrypted ||= begin
167
+ if new_resource.complete
168
+ result = new_resource.raw_data || {}
169
+ else
170
+ result = current_decrypted.merge(new_resource.raw_data || {})
171
+ end
172
+ result['id'] = new_resource.id
173
+ apply_modifiers(new_resource.raw_data_modifiers, result)
174
+ end
175
+ end
176
+
177
+ # Get the current json decrypted, for comparison purposes
178
+ def current_decrypted
179
+ @current_decrypted ||= begin
180
+ if current_resource.secret
181
+ decrypt(current_resource.raw_data || { 'id' => new_resource.id }, current_resource.secret)
182
+ elsif current_resource.encrypt
183
+ raise "Could not decrypt current data bag item #{current_resource.name}"
184
+ else
185
+ current_resource.raw_data || { 'id' => new_resource.id }
186
+ end
187
+ end
188
+ end
189
+
190
+ # Figure out the differences between new and current
191
+ def calculate_differences
192
+ if new_encrypt
193
+ if current_resource.encrypt
194
+ # Both are encrypted, check if the encryption type is the same
195
+ description = ''
196
+ if new_secret != current_resource.secret
197
+ description << ' with new secret'
198
+ end
199
+ if new_resource.encryption_version != current_resource.encryption_version
200
+ description << " from v#{current_resource.encryption_version} to v#{new_resource.encryption_version} encryption"
201
+ end
202
+
203
+ if description != ''
204
+ # Encryption is different, we're reencrypting
205
+ differences = [ "re-encrypt#{description}"]
206
+ else
207
+ # Encryption is the same, we're just updating
208
+ differences = []
209
+ end
210
+ else
211
+ # New stuff should be encrypted, old is not. Encrypting.
212
+ differences = [ "encrypt with v#{new_resource.encryption_version} encryption" ]
213
+ end
214
+
215
+ # Get differences in the actual json
216
+ if current_resource.secret
217
+ json_differences(current_decrypted, new_decrypted, false, '', differences)
218
+ elsif current_resource.encrypt
219
+ # Encryption is different and we can't read the old values. Only allow the change
220
+ # if we're overwriting the data bag item
221
+ if !new_resource.complete
222
+ raise "Cannot encrypt #{new_resource.name} due to failure to decrypt existing resource. Set 'complete true' to overwrite or add the old secret as old_secret / old_secret_path."
223
+ end
224
+ differences = [ "overwrite data bag item (cannot decrypt old data bag item)"]
225
+ differences = (new_resource.raw_data.keys & current_resource.raw_data.keys).map { |key| "overwrite #{key}"}
226
+ differences += (new_resource.raw_data.keys - current_resource.raw_data.keys).map { |key| "add #{key}"}
227
+ differences += (current_resource.raw_data.keys - new_resource.raw_data.keys).map { |key| "remove #{key}" }
228
+ else
229
+ json_differences(current_decrypted, new_decrypted, false, '', differences)
230
+ end
231
+ else
232
+ if current_resource.encrypt
233
+ # New stuff should not be encrypted, old is. Decrypting.
234
+ differences = [ "decrypt data bag item to plaintext" ]
235
+ else
236
+ differences = []
237
+ end
238
+ json_differences(current_decrypted, new_decrypted, true, '', differences)
239
+ end
240
+ differences
241
+ end
242
+
243
+ #
244
+ # Helpers
245
+ #
246
+
247
+ def resource_class
248
+ Chef::Resource::ChefDataBagItem
249
+ end
250
+
251
+ def data_handler
252
+ Chef::ChefFS::DataHandler::DataBagItemDataHandler.new
253
+ end
254
+
255
+ def keys
256
+ {
257
+ 'id' => :id,
258
+ 'data_bag' => :data_bag,
259
+ 'raw_data' => :raw_data
260
+ }
261
+ end
262
+
263
+ def not_found_resource
264
+ resource = super
265
+ resource.data_bag new_resource.data_bag
266
+ resource
267
+ end
268
+
269
+ def fake_entry
270
+ FakeEntry.new("#{new_resource.id}.json", FakeEntry.new(new_resource.data_bag))
271
+ end
272
+
273
+ end