knife-solo_data_bag 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/CHANGELOG.md ADDED
@@ -0,0 +1,2 @@
1
+ ## head
2
+
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in knife-solo_data_bag.gemspec
4
+ gemspec
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 2012 Thomas Bishop
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.
data/README.md ADDED
@@ -0,0 +1,69 @@
1
+ # Knife Solo Data Bag
2
+ A knife plugin to make working with data bags easier in a chef solo environment
3
+
4
+ ## Installation
5
+
6
+ gem install knife-solo_data_bag
7
+
8
+ ## Usage
9
+
10
+ ### Create
11
+ Create a plain text data bag
12
+
13
+ $ knife solo data bag create apps app_1
14
+
15
+ Create an encrypted data bag with the provided string as the secret
16
+
17
+ $ knife solo data bag create apps app_1 -s secret_key
18
+
19
+ Create an encrypted data bag with the provided file content as the secret
20
+
21
+ $ knife solo data bag create apps app_1 --secret-file 'SECRET_FILE'
22
+
23
+ ### Edit
24
+ Edit a plain text data bag
25
+
26
+ $ knife solo data bag edit apps app_1
27
+
28
+ Edit an encrypted data bag with the provided string as the secret
29
+
30
+ $ knife solo data bag edit apps app_1 -s secret_key
31
+
32
+ Edit an encrypted data bag with the provided file content as the secret
33
+
34
+ $ knife solo data bag edit apps app_1 --secret-file 'SECRET_FILE'
35
+
36
+ ### List
37
+ List all of the data bag
38
+
39
+ $ knife solo data bag list
40
+
41
+ ### Show
42
+ Show the plain text content of a data bag (if this is an encrypted data bag, it will return the encrypted data)
43
+
44
+ $ knife solo data bag show apps app_1
45
+
46
+ Show the unencrypted content of an encrypted data bag with the provided string as the secret
47
+
48
+ $ knife solo data bag show apps app_1 -s secret_key
49
+
50
+ Show the unencrypted content of an encrypted data bag with the provided file content as the secret
51
+
52
+ $ knife solo data bag show apps app_1 --secret-file 'SECRET_FILE'
53
+
54
+ ## Notes
55
+ This plugin will rely on the configured data_bag_path for placement of the data
56
+ bags. This defaults to '/var/chef/data_bags', but can be overriden in your chef
57
+ client config.
58
+
59
+ ### Chef Support
60
+ This plugin has only been tested with version 0.10.10 of chef.
61
+
62
+ ## Contribute
63
+ * Fork the project
64
+ * Make your feature addition or bug fix (with tests and docs) in a topic branch
65
+ * Bonus points for not mucking with the gemspec or version
66
+ * Send a pull request
67
+
68
+ ## License
69
+ See LICENSE for details
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ require 'rspec/core/rake_task'
5
+
6
+ task :default => [:spec]
7
+
8
+ desc 'Run specs'
9
+ RSpec::Core::RakeTask.new do |t|
10
+ t.rspec_opts = %w(-fs --color)
11
+ end
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/knife-solo_data_bag/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Tommy Bishop"]
6
+ gem.email = ["bishop.thomas@gmail.com"]
7
+ gem.description = %q{A knife plugin for working with data bags and chef solo}
8
+ gem.summary = %q{A knife plugin for working with data bags and chef solo}
9
+ gem.homepage = 'https://github.com/thbishop/knife-solo_data_bag'
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.name = "knife-solo_data_bag"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Knife::SoloDataBag::VERSION
17
+ gem.add_development_dependency 'chef', '~> 0.10.10'
18
+ gem.add_development_dependency 'rspec', '~> 2.10.0'
19
+ gem.add_development_dependency 'fakefs', '~> 0.4.0'
20
+ end
@@ -0,0 +1,49 @@
1
+ module KnifeSoloDataBag
2
+ module Helpers
3
+
4
+ def bag_item_path
5
+ File.expand_path File.join(bag_path, "#{item_name}.json")
6
+ end
7
+
8
+ def bag_path
9
+ File.expand_path File.join(bags_path, bag_name)
10
+ end
11
+
12
+ def bags_path
13
+ Chef::Config[:data_bag_path]
14
+ end
15
+
16
+ def secret_key
17
+ return config[:secret] if config[:secret]
18
+ Chef::EncryptedDataBagItem.load_secret config[:secret_file]
19
+ end
20
+
21
+ def should_be_encrypted?
22
+ config[:secret] || config[:secret_file]
23
+ end
24
+
25
+ def validate_bag_name_provided
26
+ unless bag_name
27
+ show_usage
28
+ ui.fatal 'You must supply a name for the data bag'
29
+ exit 1
30
+ end
31
+ end
32
+
33
+ def validate_bags_path_exists
34
+ unless File.directory? bags_path
35
+ raise Chef::Exceptions::InvalidDataBagPath,
36
+ "Configured data bag path '#{bags_path}' is invalid"
37
+ end
38
+ end
39
+
40
+ def validate_multiple_secrets_were_not_provided
41
+ if config[:secret] && config[:secret_file]
42
+ show_usage
43
+ ui.fatal 'Please specify either --secret or --secret-file only'
44
+ exit 1
45
+ end
46
+ end
47
+
48
+ end
49
+ end
@@ -0,0 +1,63 @@
1
+ module KnifeSoloDataBag
2
+
3
+ class SoloDataBagCreate < Chef::Knife
4
+
5
+ require 'chef/knife/helpers'
6
+ require 'fileutils'
7
+
8
+ include KnifeSoloDataBag::Helpers
9
+
10
+ banner 'knife solo data bag create BAG [ITEM] (options)'
11
+ category 'solo data bag'
12
+
13
+ attr_reader :bag_name, :item_name
14
+
15
+ option :secret,
16
+ :short => '-s SECRET',
17
+ :long => '--secret SECRET',
18
+ :description => 'The secret key to use to encrypt data bag item values'
19
+
20
+ option :secret_file,
21
+ :long => '--secret-file SECRET_FILE',
22
+ :description => 'A file containing the secret key to use to encrypt data bag item values'
23
+
24
+ def run
25
+ @bag_name, @item_name = @name_args
26
+ ensure_valid_arguments
27
+ create_bag_directory
28
+ create_bag_item if item_name
29
+ end
30
+
31
+ private
32
+ def bag_item_content(content)
33
+ return content unless should_be_encrypted?
34
+ Chef::EncryptedDataBagItem.encrypt_data_bag_item content, secret_key
35
+ end
36
+
37
+ def create_bag_directory
38
+ FileUtils.mkdir_p bag_path unless File.exists? bag_path
39
+ end
40
+
41
+ def create_bag_item
42
+ create_object({'id' => item_name}, "data_bag_item[#{item_name}]") do |output|
43
+ item = Chef::DataBagItem.from_hash bag_item_content(output)
44
+ item.data_bag bag_name
45
+ persist_bag_item item
46
+ end
47
+ end
48
+
49
+ def ensure_valid_arguments
50
+ validate_bag_name_provided
51
+ validate_bags_path_exists
52
+ validate_multiple_secrets_were_not_provided
53
+ end
54
+
55
+ def persist_bag_item(item)
56
+ File.open bag_item_path, 'w' do |f|
57
+ f.write item.to_json
58
+ end
59
+ end
60
+
61
+ end
62
+
63
+ end
@@ -0,0 +1,73 @@
1
+ module KnifeSoloDataBag
2
+
3
+ class SoloDataBagEdit < Chef::Knife
4
+
5
+ require 'chef/knife/helpers'
6
+
7
+ include KnifeSoloDataBag::Helpers
8
+
9
+ banner 'knife solo data bag edit BAG ITEM (options)'
10
+ category 'solo data bag'
11
+
12
+ attr_reader :bag_name, :item_name
13
+
14
+ option :secret,
15
+ :short => '-s SECRET',
16
+ :long => '--secret SECRET',
17
+ :description => 'The secret key to use to encrypt data bag item values'
18
+
19
+ option :secret_file,
20
+ :long => '--secret-file SECRET_FILE',
21
+ :description => 'A file containing the secret key to use to encrypt data bag item values'
22
+
23
+ def run
24
+ Chef::Config[:solo] = true
25
+ @bag_name, @item_name = @name_args
26
+ ensure_valid_arguments
27
+ edit_content
28
+ end
29
+
30
+ private
31
+ def edit_content
32
+ updated_content = edit_data existing_bag_item_content
33
+ item = Chef::DataBagItem.from_hash format_editted_content(updated_content)
34
+ item.data_bag bag_name
35
+ persist_bag_item item
36
+ end
37
+
38
+ def existing_bag_item_content
39
+ content = Chef::DataBagItem.load(bag_name, item_name).raw_data
40
+
41
+ return content unless should_be_encrypted?
42
+ Chef::EncryptedDataBagItem.new(content, secret_key).to_hash
43
+ end
44
+
45
+ def format_editted_content(content)
46
+ return content unless should_be_encrypted?
47
+ Chef::EncryptedDataBagItem.encrypt_data_bag_item content, secret_key
48
+ end
49
+
50
+ def ensure_valid_arguments
51
+ validate_bag_name_provided
52
+ validate_item_name_provided
53
+ validate_bags_path_exists
54
+ validate_multiple_secrets_were_not_provided
55
+ end
56
+
57
+ def persist_bag_item(item)
58
+ File.open bag_item_path, 'w' do |f|
59
+ f.write item.to_json
60
+ end
61
+ end
62
+
63
+ def validate_item_name_provided
64
+ unless item_name
65
+ show_usage
66
+ ui.fatal 'You must supply a name for the item'
67
+ exit 1
68
+ end
69
+ end
70
+
71
+ end
72
+
73
+ end
@@ -0,0 +1,32 @@
1
+ module KnifeSoloDataBag
2
+
3
+ class SoloDataBagList < Chef::Knife
4
+
5
+ require 'chef/knife/helpers'
6
+
7
+ include KnifeSoloDataBag::Helpers
8
+
9
+ banner 'knife solo data bag list (options)'
10
+ category 'solo data bag'
11
+
12
+ attr_reader :bag_name
13
+
14
+ def run
15
+ ensure_valid_arguments
16
+ output format_for_display(bags)
17
+ end
18
+
19
+ private
20
+ def bags
21
+ Dir.entries(bags_path).select do |i|
22
+ File.directory?(File.expand_path(File.join(bags_path, i))) && i != '.' && i != '..'
23
+ end
24
+ end
25
+
26
+ def ensure_valid_arguments
27
+ validate_bags_path_exists
28
+ end
29
+
30
+ end
31
+
32
+ end
@@ -0,0 +1,56 @@
1
+ module KnifeSoloDataBag
2
+
3
+ class SoloDataBagShow < Chef::Knife
4
+
5
+ require 'chef/knife/helpers'
6
+
7
+ include KnifeSoloDataBag::Helpers
8
+
9
+ banner 'knife solo data bag show BAG [ITEM] (options)'
10
+ category 'solo data bag'
11
+
12
+ attr_reader :bag_name, :item_name
13
+
14
+ option :secret,
15
+ :short => '-s SECRET',
16
+ :long => '--secret SECRET',
17
+ :description => 'The secret key to use to encrypt data bag item values'
18
+
19
+ option :secret_file,
20
+ :long => '--secret-file SECRET_FILE',
21
+ :description => 'A file containing the secret key to use to encrypt data bag item values'
22
+
23
+ def run
24
+ Chef::Config[:solo] = true
25
+ @bag_name, @item_name = @name_args
26
+ ensure_valid_arguments
27
+ display_content
28
+ end
29
+
30
+ private
31
+ def bag_content
32
+ Chef::DataBag.load bag_name
33
+ end
34
+
35
+ def bag_item_content
36
+ if should_be_encrypted?
37
+ Chef::EncryptedDataBagItem.load bag_name, item_name, secret_key
38
+ else
39
+ Chef::DataBagItem.load(bag_name, item_name).raw_data
40
+ end
41
+ end
42
+
43
+ def display_content
44
+ content = item_name ? bag_item_content : bag_content
45
+ output format_for_display(content)
46
+ end
47
+
48
+ def ensure_valid_arguments
49
+ validate_bag_name_provided
50
+ validate_bags_path_exists
51
+ validate_multiple_secrets_were_not_provided
52
+ end
53
+
54
+ end
55
+
56
+ end
@@ -0,0 +1,11 @@
1
+ require 'chef/exceptions'
2
+ require 'chef/knife'
3
+ require 'chef/knife/solo_data_bag_create'
4
+ require 'chef/knife/solo_data_bag_edit'
5
+ require 'chef/knife/solo_data_bag_list'
6
+ require 'chef/knife/solo_data_bag_show'
7
+ require 'knife-solo_data_bag/version'
8
+
9
+ module Knife
10
+ module SoloDataBag; end
11
+ end
@@ -0,0 +1,5 @@
1
+ module Knife
2
+ module SoloDataBag
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,11 @@
1
+ shared_context 'bag_name_not_provided' do
2
+ context 'when a name is not supplied' do
3
+ it 'should exit with an error message' do
4
+ lambda {
5
+ @knife.run
6
+ }.should raise_error SystemExit
7
+ @stdout.string.should match /usage/i
8
+ @stderr.string.should match /name for the data bag/
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,15 @@
1
+ shared_context 'bag_path_is_not_valid' do |args|
2
+ context 'when the data bag path is not valid' do
3
+ before do
4
+ File.stub(:directory?).and_return(false)
5
+ @knife.name_args = ['foo']
6
+ @knife.name_args.concat Array(args)
7
+ end
8
+
9
+ it 'should raise an invalid data bag path exception' do
10
+ lambda {
11
+ @knife.run
12
+ }.should raise_error Chef::Exceptions::InvalidDataBagPath
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,20 @@
1
+ shared_context 'secret_string_and_secret_file_are_both_provided' do |args|
2
+ context 'when specifying -s and --secret-file' do
3
+ before do
4
+ @knife.name_args = ['foo']
5
+ @knife.name_args.concat Array(args)
6
+ @knife.config[:secret] = 'foobar'
7
+ @knife.config[:secret_file] = 'secret.txt'
8
+ File.stub(:directory?).and_return(true)
9
+ end
10
+
11
+ it 'should exit with an error message' do
12
+ lambda {
13
+ @knife.run
14
+ }.should raise_error SystemExit
15
+ @stdout.string.should match /usage/i
16
+ @stderr.string.should match /either --secret or --secret-file/
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,8 @@
1
+ shared_context 'stubbed_out_stdout_and_stderr' do
2
+ before do
3
+ @stdout = StringIO.new
4
+ @stderr = StringIO.new
5
+ @knife.ui.stub!(:stdout).and_return(@stdout)
6
+ @knife.ui.stub!(:stderr).and_return(@stderr)
7
+ end
8
+ end
@@ -0,0 +1,6 @@
1
+ require 'fakefs/safe'
2
+ require 'knife-solo_data_bag'
3
+
4
+ ['contexts', 'helpers', 'matchers'].each do |dir|
5
+ Dir[File.expand_path(File.join(File.dirname(__FILE__),dir,'*.rb'))].each {|f| require f}
6
+ end
@@ -0,0 +1,94 @@
1
+ require 'spec_helper'
2
+
3
+ describe KnifeSoloDataBag::SoloDataBagCreate do
4
+ before do
5
+ @knife = subject
6
+ end
7
+
8
+ include_context 'stubbed_out_stdout_and_stderr'
9
+
10
+ describe 'run' do
11
+ include_context 'bag_name_not_provided'
12
+ include_context 'bag_path_is_not_valid'
13
+ include_context 'secret_string_and_secret_file_are_both_provided'
14
+
15
+ context 'with valid arguments' do
16
+ before do
17
+ @bags_path = '/var/chef/data_bags'
18
+ @bag_path = "#{@bags_path}/bag_1"
19
+ @knife.name_args = ['bag_1']
20
+ Chef::Config[:data_bag_path] = @bags_path
21
+
22
+ FakeFS.activate!
23
+ FileUtils.mkdir_p @bags_path
24
+ end
25
+
26
+ after do
27
+ FakeFS.deactivate!
28
+ FakeFS::FileSystem.clear
29
+ end
30
+
31
+ context 'if an item is not specified' do
32
+ it 'should create the data bag' do
33
+ @knife.run
34
+ File.directory?(@bags_path).should be_true
35
+ File.directory?(@bag_path).should be_true
36
+ end
37
+ end
38
+
39
+ context 'when also specifying an item' do
40
+ before do
41
+ @knife.name_args << 'bar'
42
+ @input_data = {'id' => 'foo', 'key_1' => 'value_1', 'key_2' => 'value_2'}
43
+ @item_path = "#{@bag_path}/bar.json"
44
+ @knife.stub(:create_object).and_yield(@input_data)
45
+ end
46
+
47
+ it 'should create the data bag item' do
48
+ @knife.run
49
+ JSON.parse(File.read(@item_path)).raw_data.should == @input_data
50
+ end
51
+
52
+ context 'when encrypting with -s or --secret' do
53
+ before do
54
+ @knife.name_args << 'bar'
55
+ @knife.config[:secret] = 'secret_key'
56
+ end
57
+
58
+ it 'should create the encrypted data bag item' do
59
+ @knife.run
60
+ content = JSON.parse(File.read(@item_path)).raw_data
61
+ @input_data.keys.reject{|i| i == 'id'}.each do |k|
62
+ content.should have_key k
63
+ content[k].should_not == @input_data[k]
64
+ end
65
+ end
66
+ end
67
+
68
+ context 'when encrypting with --secret-file' do
69
+ before do
70
+ @knife.name_args << 'bar'
71
+ @secret_path = '/var/chef/secret.txt'
72
+ @knife.config[:secret_file] = @secret_path
73
+ Chef::EncryptedDataBagItem.should_receive(:load_secret).
74
+ with(@secret_path).
75
+ and_return('psst')
76
+ end
77
+
78
+ it 'should create the encrypted data bag item' do
79
+ @knife.run
80
+ content = JSON.parse(File.read(@item_path)).raw_data
81
+ @input_data.keys.reject{|i| i == 'id'}.each do |k|
82
+ content.should have_key k
83
+ content[k].should_not == @input_data[k]
84
+ end
85
+ end
86
+ end
87
+
88
+ end
89
+
90
+ end
91
+
92
+ end
93
+
94
+ end
@@ -0,0 +1,101 @@
1
+ require 'spec_helper'
2
+
3
+ describe KnifeSoloDataBag::SoloDataBagEdit do
4
+ before do
5
+ @knife = subject
6
+ end
7
+
8
+ include_context 'stubbed_out_stdout_and_stderr'
9
+
10
+ describe 'run' do
11
+ include_context 'bag_name_not_provided'
12
+ include_context 'bag_path_is_not_valid', ['foo']
13
+ include_context 'secret_string_and_secret_file_are_both_provided', ['bar']
14
+
15
+ context 'when an item name is not provided' do
16
+ before do
17
+ @knife.name_args = ['bag_1']
18
+ end
19
+
20
+ it 'should exit with an error message' do
21
+ lambda {
22
+ @knife.run
23
+ }.should raise_error SystemExit
24
+ @stdout.string.should match /usage/i
25
+ @stderr.string.should match /name for the item/
26
+ end
27
+ end
28
+
29
+ context 'with valid arguments' do
30
+ before do
31
+ @bags_path = '/var/chef/data_bags'
32
+ @bag_path = "#{@bags_path}/bag_1"
33
+ @item_path = "#{@bag_path}/foo.json"
34
+ @knife.name_args = ['bag_1', 'foo']
35
+ @orig_data = {'id' => 'foo', 'who' => 'bob'}
36
+ @updated_data = {'id' => 'foo', 'who' => 'sue'}
37
+ @bag_item_foo = Chef::DataBagItem.from_hash @orig_data
38
+ @bag_item_foo.data_bag 'bag_1'
39
+ @updated_bag_item = Chef::DataBagItem.from_hash @updated_data
40
+ @updated_bag_item.data_bag 'bag_1'
41
+
42
+ FakeFS.activate!
43
+ FileUtils.mkdir_p @bag_path
44
+
45
+ Chef::DataBagItem.should_receive(:load).with('bag_1', 'foo').
46
+ and_return(@bag_item_foo)
47
+ @knife.stub(:edit_data).and_return(@updated_data)
48
+ Chef::Config[:data_bag_path] = @bags_path
49
+ end
50
+
51
+ after do
52
+ FakeFS.deactivate!
53
+ FakeFS::FileSystem.clear
54
+ end
55
+
56
+ it 'should edit the data bag item' do
57
+ @knife.run
58
+ JSON.parse(File.read(@item_path)).raw_data.should == @updated_data
59
+ end
60
+
61
+ context 'when encrypting with -s or --secret' do
62
+ before do
63
+ @knife.config[:secret] = 'secret_key'
64
+ Chef::EncryptedDataBagItem.should_receive(:new).
65
+ with(@bag_item_foo.raw_data, 'secret_key').
66
+ and_return(@updated_data)
67
+ end
68
+
69
+ it 'should edit the encrypted data bag item' do
70
+ @knife.run
71
+ content = JSON.parse(File.read(@item_path)).raw_data
72
+ content['who'].should_not == @orig_data['who']
73
+ content['who'].should_not be_nil
74
+ end
75
+ end
76
+
77
+ context 'when encrypting with --secret-file' do
78
+ before do
79
+ @secret_path = '/var/chef/secret.txt'
80
+ @knife.config[:secret_file] = @secret_path
81
+ Chef::EncryptedDataBagItem.stub(:load_secret).
82
+ with(@secret_path).
83
+ and_return('psst')
84
+ Chef::EncryptedDataBagItem.should_receive(:new).
85
+ with(@bag_item_foo.raw_data, 'psst').
86
+ and_return(@updated_data)
87
+ end
88
+
89
+ it 'should edit the encrypted data bag item' do
90
+ @knife.run
91
+ content = JSON.parse(File.read(@item_path)).raw_data
92
+ content['who'].should_not == @orig_data['who']
93
+ content['who'].should_not be_nil
94
+ end
95
+ end
96
+
97
+ end
98
+
99
+ end
100
+
101
+ end
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ describe KnifeSoloDataBag::SoloDataBagList do
4
+ before do
5
+ @knife = subject
6
+ end
7
+
8
+ include_context 'stubbed_out_stdout_and_stderr'
9
+
10
+ describe 'run' do
11
+ include_context 'bag_path_is_not_valid'
12
+
13
+ context 'with valid arguments' do
14
+ before do
15
+ @bags_path = '/var/chef/data_bags'
16
+ @bags = ['bag_1', 'bag_2']
17
+
18
+ FakeFS.activate!
19
+ FileUtils.mkdir_p @bags_path
20
+
21
+ @bags.each do |bag|
22
+ FileUtils.mkdir_p "#{@bags_path}/#{bag}"
23
+ end
24
+ end
25
+
26
+ after do
27
+ FakeFS.deactivate!
28
+ FakeFS::FileSystem.clear
29
+ end
30
+
31
+ it 'should list all of the data bags' do
32
+ @knife.run
33
+ @stdout.string.should match /bag_1/
34
+ @stdout.string.should match /bag_2/
35
+ end
36
+ end
37
+
38
+ end
39
+
40
+ end
@@ -0,0 +1,96 @@
1
+ require 'spec_helper'
2
+
3
+ describe KnifeSoloDataBag::SoloDataBagShow do
4
+ before do
5
+ @knife = subject
6
+ end
7
+
8
+ include_context 'stubbed_out_stdout_and_stderr'
9
+
10
+ describe 'run' do
11
+ include_context 'bag_name_not_provided'
12
+ include_context 'bag_path_is_not_valid'
13
+ include_context 'secret_string_and_secret_file_are_both_provided'
14
+
15
+ context 'with valid arguments' do
16
+ before do
17
+ @bags_path = '/var/chef/data_bags'
18
+ @bag_path = "#{@bags_path}/bag_1"
19
+ @knife.name_args = ['bag_1']
20
+
21
+ FakeFS.activate!
22
+ FileUtils.mkdir_p @bag_path
23
+
24
+ @bag_item_foo = Chef::DataBagItem.from_hash 'id' => 'foo', 'who' => 'bob'
25
+ @bag_item_bar = Chef::DataBagItem.from_hash 'id' => 'bar', 'who' => 'sue'
26
+ Chef::Config[:data_bag_path] = @bags_path
27
+ end
28
+
29
+ after do
30
+ FakeFS.deactivate!
31
+ FakeFS::FileSystem.clear
32
+ end
33
+
34
+ context 'if an item is not specified' do
35
+ before do
36
+ bag_items = {'foo' => @bag_item_foo, 'bar' => @bag_item_bar}
37
+ Chef::DataBag.should_receive(:load).with('bag_1').
38
+ and_return(bag_items)
39
+ end
40
+
41
+ it 'should show the list of items' do
42
+ @knife.run
43
+ @stdout.string.should match /foo/
44
+ @stdout.string.should match /bar/
45
+ end
46
+ end
47
+
48
+ context 'when also specifying an item' do
49
+ before do
50
+ @knife.name_args << 'foo'
51
+ end
52
+
53
+ it 'should show the item' do
54
+ Chef::DataBagItem.should_receive(:load).with('bag_1', 'foo').
55
+ and_return(@bag_item_foo)
56
+ @knife.run
57
+ @stdout.string.should match /id:\s+foo.+who:\s+bob/m
58
+ end
59
+
60
+ context 'when encrypting with -s or --secret' do
61
+ before do
62
+ @knife.config[:secret] = 'SECRET'
63
+ Chef::EncryptedDataBagItem.should_receive(:load).
64
+ with('bag_1', 'foo', 'SECRET').
65
+ and_return(@bag_item_foo)
66
+ end
67
+
68
+ it 'should show the unencrypted item' do
69
+ @knife.run
70
+ @stdout.string.should match /id:\s+foo.+who:\s+bob/m
71
+ end
72
+ end
73
+
74
+ context 'when encrypting with --secret-file' do
75
+ before do
76
+ @knife.config[:secret_file] = '/var/tmp/secret'
77
+ Chef::EncryptedDataBagItem.should_receive(:load_secret).
78
+ with('/var/tmp/secret').
79
+ and_return('abcd')
80
+ Chef::EncryptedDataBagItem.should_receive(:load).
81
+ with('bag_1', 'foo', 'abcd').
82
+ and_return(@bag_item_foo)
83
+ end
84
+
85
+ it 'should show the unencrypted item' do
86
+ @knife.run
87
+ @stdout.string.should match /id:\s+foo.+who:\s+bob/m
88
+ end
89
+ end
90
+
91
+ end
92
+ end
93
+
94
+ end
95
+
96
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: knife-solo_data_bag
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Tommy Bishop
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-17 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: chef
16
+ requirement: &70125975538240 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 0.10.10
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70125975538240
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &70125975537740 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 2.10.0
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70125975537740
36
+ - !ruby/object:Gem::Dependency
37
+ name: fakefs
38
+ requirement: &70125975537280 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 0.4.0
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70125975537280
47
+ description: A knife plugin for working with data bags and chef solo
48
+ email:
49
+ - bishop.thomas@gmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - .rspec
56
+ - CHANGELOG.md
57
+ - Gemfile
58
+ - LICENSE
59
+ - README.md
60
+ - Rakefile
61
+ - knife-solo_data_bag.gemspec
62
+ - lib/chef/knife/helpers.rb
63
+ - lib/chef/knife/solo_data_bag_create.rb
64
+ - lib/chef/knife/solo_data_bag_edit.rb
65
+ - lib/chef/knife/solo_data_bag_list.rb
66
+ - lib/chef/knife/solo_data_bag_show.rb
67
+ - lib/knife-solo_data_bag.rb
68
+ - lib/knife-solo_data_bag/version.rb
69
+ - spec/contexts/bag_name_not_provided.rb
70
+ - spec/contexts/bag_path_is_not_valid.rb
71
+ - spec/contexts/secret_string_and_secret_file_are_both_provided.rb
72
+ - spec/contexts/stubbed_out_stdout_and_stderr.rb
73
+ - spec/spec_helper.rb
74
+ - spec/unit/solo_data_bag_create_spec.rb
75
+ - spec/unit/solo_data_bag_edit_spec.rb
76
+ - spec/unit/solo_data_bag_list_spec.rb
77
+ - spec/unit/solo_data_bag_show_spec.rb
78
+ homepage: https://github.com/thbishop/knife-solo_data_bag
79
+ licenses: []
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ! '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 1.8.15
99
+ signing_key:
100
+ specification_version: 3
101
+ summary: A knife plugin for working with data bags and chef solo
102
+ test_files:
103
+ - spec/contexts/bag_name_not_provided.rb
104
+ - spec/contexts/bag_path_is_not_valid.rb
105
+ - spec/contexts/secret_string_and_secret_file_are_both_provided.rb
106
+ - spec/contexts/stubbed_out_stdout_and_stderr.rb
107
+ - spec/spec_helper.rb
108
+ - spec/unit/solo_data_bag_create_spec.rb
109
+ - spec/unit/solo_data_bag_edit_spec.rb
110
+ - spec/unit/solo_data_bag_list_spec.rb
111
+ - spec/unit/solo_data_bag_show_spec.rb