chef-encrypted-data-bag-url 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e7e3df4197b52b4f1814ca6b50037e917e19af20
4
+ data.tar.gz: d7ecb27d3bb9409c762f301197016b631ca06669
5
+ SHA512:
6
+ metadata.gz: 9577009736ec0ecf4458c8c1e5ce57d8a79e11d66aec852bf49699317cb7284cf8818c4241fadaede1171b4bea2327af1d6add07c42bc06408c294bdbd90ba19
7
+ data.tar.gz: c2d9417c6dd7ff06c9cafda4df9e2fdc85760e41c64a26bf1022540e49511a1fa8690ab53c88cbfa43d606c7013798dae43b07f1d2ce78dde2e0815fc86e10c0
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.13.6
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at joe.g.yates@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in chef-encrypted-data-bag-url.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Joe Yates
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,86 @@
1
+ # Chef::EncryptedDataBagUrl
2
+
3
+ Ease collaboration on your Chef kitchen's secrets by storing the data online,
4
+ for example in private Gists.
5
+
6
+ The principle use case for this gem is when you ue Chef Solo. In this case
7
+ there is no Chef Server instance where users can go and edit secrets via a web
8
+ interface. You have to check out the kitchen's repo, obtain a copy of the
9
+ `secret_file` and edit the data bag with the 'knife-solo_data_bag' gem.
10
+
11
+ With this gem, you create a private Gist (or any other online secret file)
12
+ and write your secrets in JSON format, e.g.:
13
+
14
+ ```json
15
+ {
16
+ "foo_api_token": "fffuuuuuuuuuuuuuuu"
17
+ "database": {
18
+ "password": "secret"
19
+ }
20
+ }
21
+ ```
22
+
23
+ then put the URL of the **raw** Gist data in your encrypted data bag:
24
+
25
+ ```json
26
+ {
27
+ "id": "ciao",
28
+ "data_url": "https://gist.githubusercontent.com/myuser/f95499c75cbe04f8cd7c42732729167d24928009/raw/7a05c4f72e41c1022d9b8d9067cc9aba601ea99e/secrets.json"
29
+ }
30
+ ```
31
+
32
+ In your Chef cookbooks, instead of using
33
+ `Chef::EncryptedDataBagItem.load("foo", "bar").to_hash` to load your data,
34
+ use `Chef::EncryptedDataBagUrl`.
35
+
36
+ The result is a deep merge of the data in the data bag itself with the data
37
+ from the supplied URL.
38
+
39
+ Note that data bag contents are treated as defaults, and the data URL contents
40
+ override specific values.
41
+
42
+ ## Installation
43
+
44
+ Add this line to your application's Gemfile:
45
+
46
+ ```ruby
47
+ gem 'chef-encrypted-data-bag-url'
48
+ ```
49
+
50
+ And then execute:
51
+
52
+ $ bundle
53
+
54
+ Or install it yourself as:
55
+
56
+ $ gem install chef-encrypted-data-bag-url
57
+
58
+ ## Usage
59
+
60
+ TODO: Write usage instructions here
61
+
62
+ ## Development
63
+
64
+ After checking out the repo, run `bin/setup` to install dependencies.
65
+ Then, run `rake spec` to run the tests.
66
+ You can also run `bin/console` for an interactive prompt that will
67
+ allow you to experiment.
68
+
69
+ To install this gem onto your local machine, run `bundle exec rake install`.
70
+ To release a new version, update the version number in `version.rb`,
71
+ and then run `bundle exec rake release`, which will create a git tag for
72
+ the version, push git commits and tags, and push the `.gem` file to
73
+ [rubygems.org](https://rubygems.org).
74
+
75
+ ## Contributing
76
+
77
+ Bug reports and pull requests are welcome on GitHub at
78
+ https://github.com/joeyates/chef-encrypted-data-bag-url.
79
+ This project is intended to be a safe, welcoming space for collaboration,
80
+ and contributors are expected to adhere to the
81
+ [Contributor Covenant](http://contributor-covenant.org) code of conduct.
82
+
83
+ ## License
84
+
85
+ The gem is available as open source under the terms of the
86
+ [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "chef/encrypted/data/bag/url"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("lib", __dir__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "chef/encrypted_data_bag_url"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "chef-encrypted-data-bag-url"
8
+ spec.version = Chef::EncryptedDataBagUrl::VERSION
9
+ spec.authors = ["Joe Yates"]
10
+ spec.email = ["joe.g.yates@gmail.com"]
11
+
12
+ spec.summary = %q{Use online data with Chef data bags}
13
+ spec.description = <<-EOT
14
+ Ease collaboration on your Chef kitchen's secrets by storing the data online,
15
+ for example in private Gists.
16
+ EOT
17
+
18
+ spec.homepage = "https://github.com/joeyates/chef-encrypted-data-bag-url"
19
+ spec.license = "MIT"
20
+
21
+ spec.files = `git ls-files -z`.split("\x0")
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.13"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.0"
29
+ end
@@ -0,0 +1,77 @@
1
+ require "chef/encrypted_data_bag_item"
2
+ require "json"
3
+ require "net/http"
4
+
5
+ class Chef::EncryptedDataBagUrl
6
+ VERSION = "0.1.0"
7
+
8
+ def self.load(data_bag, name, secret = nil)
9
+ encrypted_data = Chef::DataBagItem.load(data_bag, name)
10
+ secret_key = secret || Chef::EncryptedDataBagItem.load_secret
11
+ new(encrypted_data, secret_key)
12
+ end
13
+
14
+ def initialize(encrypted_data, secret_key)
15
+ @encrypted_data = encrypted_data
16
+ @secret_key = secret_key
17
+ @encrypted_data_bag_item = nil
18
+ @online_data = nil
19
+ end
20
+
21
+ def to_hash
22
+ deep_merge(data_bag_hash, online_data)
23
+ end
24
+
25
+ private
26
+
27
+ attr_reader :encrypted_data
28
+ attr_reader :secret_key
29
+
30
+ def encrypted_data_bag_item
31
+ @encrypted_data_bag_item ||= Chef::EncryptedDataBagItem.new(
32
+ encrypted_data, secret_key
33
+ )
34
+ end
35
+
36
+ def data_bag_hash
37
+ encrypted_data_bag_item.to_hash
38
+ end
39
+
40
+ def data_url
41
+ data_bag_hash["data_url"]
42
+ end
43
+
44
+ def online_data
45
+ return @online_data if @online_data
46
+
47
+ if !data_url
48
+ @online_data = {}
49
+ return @online_data
50
+ end
51
+
52
+ online_json = Net::HTTP.get(URI.parse(data_url))
53
+ @online_data = JSON.parse(online_json)
54
+ end
55
+
56
+ # Adapted from
57
+ # http://stackoverflow.com/questions/9381553/ruby-merge-nested-hash/30225093#30225093
58
+ def deep_merge(first, second)
59
+ merger = ->(key, v1, v2) do
60
+ if Hash === v1 && Hash === v2
61
+ v1.merge(v2, &merger)
62
+ else
63
+ if Array === v1 && Array === v2
64
+ v1 | v2
65
+ else
66
+ if [:undefined, nil, :nil].include?(v2)
67
+ v1
68
+ else
69
+ v2
70
+ end
71
+ end
72
+ end
73
+ end
74
+
75
+ first.merge(second.to_h, &merger)
76
+ end
77
+ end
@@ -0,0 +1,123 @@
1
+ require "spec_helper"
2
+
3
+ class FooError < StandardError; end
4
+
5
+ describe Chef::EncryptedDataBagUrl do
6
+ let(:encrypted_data) { "encrypted_data" }
7
+ let(:secret_key) { "secret_key" }
8
+ let(:data_bag) { "data_bag" }
9
+ let(:name) { "name" }
10
+ let(:encrypted_data_bag_item) do
11
+ instance_double(Chef::EncryptedDataBagItem, to_hash: data_bag_contents)
12
+ end
13
+
14
+ subject { described_class.load(data_bag, name) }
15
+
16
+ before do
17
+ allow(Chef::DataBagItem).to receive(:load).
18
+ with(data_bag, name) { encrypted_data }
19
+ allow(Chef::EncryptedDataBagItem).to receive(:load_secret) { secret_key }
20
+ allow(Chef::EncryptedDataBagItem).
21
+ to receive(:new) { encrypted_data_bag_item }
22
+ end
23
+
24
+ context "when the data bag contains a `data_url`" do
25
+ let(:data_bag_contents) do
26
+ {"data_url" => data_url}
27
+ end
28
+ let(:data_url) { "http:/example.com" }
29
+
30
+ context "when the online data is available" do
31
+ before do
32
+ allow(Net::HTTP).to receive(:get) { online_json }
33
+ end
34
+
35
+ context "when the online data is well-formed JSON" do
36
+ let(:online_json) { online_data.to_json }
37
+ let(:online_data) { {"ciao" => "hello"} }
38
+
39
+ it "merges online data with data bag contents" do
40
+ expect(subject.to_hash).to eq(data_bag_contents.merge(online_data))
41
+ end
42
+
43
+ context "when the data bag has common keys with the online data" do
44
+ let(:data_bag_contents) do
45
+ {
46
+ "data_url" => data_url,
47
+ "shared" => {"db" => 1, "common" => "db"},
48
+ "data_bag_only" => "ok",
49
+ }
50
+ end
51
+ let(:online_data) do
52
+ {
53
+ "shared" => {"on" => 1, "common" => "on"},
54
+ "online_only" => "ok",
55
+ }
56
+ end
57
+ let(:expected) do
58
+ {
59
+ "data_url" => data_url,
60
+ "shared" => {"db" => 1, "on" => 1, "common" => "on"},
61
+ "data_bag_only" => "ok",
62
+ "online_only" => "ok",
63
+ }
64
+ end
65
+
66
+ it "deep merges the data" do
67
+ expect(subject.to_hash).to eq(expected)
68
+ end
69
+ end
70
+ end
71
+
72
+ context "when the online data is not well-formed JSON" do
73
+ let(:online_json) { "Not JSON" }
74
+
75
+ it "fails" do
76
+ expect do
77
+ subject.to_hash
78
+ end.to raise_error(JSON::ParserError)
79
+ end
80
+ end
81
+ end
82
+
83
+ context "when the online data is unavailable" do
84
+ before do
85
+ allow(Net::HTTP).to receive(:get).and_raise(FooError.new)
86
+ end
87
+
88
+ it "fails" do
89
+ expect do
90
+ subject.to_hash
91
+ end.to raise_error(FooError)
92
+ end
93
+ end
94
+ end
95
+
96
+ context "when the data bag doesn't contain a `data_url`" do
97
+ let(:data_bag_contents) { {"data_bag_contents" => "foo"} }
98
+
99
+ it "returns the data bag's contents" do
100
+ expect(subject.to_hash).to eq(data_bag_contents)
101
+ end
102
+ end
103
+
104
+ context "#encrypted_data" do
105
+ it "is private" do
106
+ expect do
107
+ subject.encrypted_data
108
+ end.to raise_error(NoMethodError, /private method .*?\bencrypted_data\b/)
109
+ end
110
+ end
111
+
112
+ context "#secret_key" do
113
+ it "is private" do
114
+ expect do
115
+ subject.secret_key
116
+ end.to raise_error(NoMethodError, /private method .*?\bsecret_key\b/)
117
+ end
118
+ end
119
+
120
+ it "has a version number" do
121
+ expect(Chef::EncryptedDataBagUrl::VERSION).not_to be_nil
122
+ end
123
+ end
@@ -0,0 +1,3 @@
1
+ $LOAD_PATH.unshift File.expand_path("../lib", __dir__)
2
+
3
+ require "chef/encrypted_data_bag_url"
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chef-encrypted-data-bag-url
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Joe Yates
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-12-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: |2
56
+ Ease collaboration on your Chef kitchen's secrets by storing the data online,
57
+ for example in private Gists.
58
+ email:
59
+ - joe.g.yates@gmail.com
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - ".rspec"
66
+ - ".travis.yml"
67
+ - CODE_OF_CONDUCT.md
68
+ - Gemfile
69
+ - LICENSE.txt
70
+ - README.md
71
+ - Rakefile
72
+ - bin/console
73
+ - bin/setup
74
+ - chef-encrypted-data-bag-url.gemspec
75
+ - lib/chef/encrypted_data_bag_url.rb
76
+ - spec/chef/encrypted_data_bag_url_spec.rb
77
+ - spec/spec_helper.rb
78
+ homepage: https://github.com/joeyates/chef-encrypted-data-bag-url
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.4.8
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: Use online data with Chef data bags
102
+ test_files: []