poise-citadel 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +11 -0
  3. data/.kitchen.yml +22 -0
  4. data/.travis.yml +33 -0
  5. data/CHANGELOG.md +14 -0
  6. data/Gemfile +32 -0
  7. data/LICENSE +201 -0
  8. data/README.md +179 -0
  9. data/Rakefile +17 -0
  10. data/chef/attributes/default.rb +24 -0
  11. data/lib/citadel.rb +80 -0
  12. data/lib/citadel/chef_dsl.rb +29 -0
  13. data/lib/citadel/cheftie.rb +37 -0
  14. data/lib/citadel/error.rb +33 -0
  15. data/lib/citadel/s3.rb +84 -0
  16. data/lib/citadel/safe_node.rb +41 -0
  17. data/lib/citadel/version.rb +21 -0
  18. data/poise-citadel.gemspec +41 -0
  19. data/test/cookbooks/citadel_test/attributes/default.rb +17 -0
  20. data/test/cookbooks/citadel_test/metadata.rb +20 -0
  21. data/test/cookbooks/citadel_test/recipes/default.rb +19 -0
  22. data/test/gemfiles/chef-12.0.gemfile +19 -0
  23. data/test/gemfiles/chef-12.1.gemfile +19 -0
  24. data/test/gemfiles/chef-12.10.gemfile +19 -0
  25. data/test/gemfiles/chef-12.2.gemfile +19 -0
  26. data/test/gemfiles/chef-12.3.gemfile +19 -0
  27. data/test/gemfiles/chef-12.4.gemfile +21 -0
  28. data/test/gemfiles/chef-12.5.gemfile +19 -0
  29. data/test/gemfiles/chef-12.6.gemfile +19 -0
  30. data/test/gemfiles/chef-12.7.gemfile +19 -0
  31. data/test/gemfiles/chef-12.8.gemfile +19 -0
  32. data/test/gemfiles/chef-12.9.gemfile +19 -0
  33. data/test/gemfiles/chef-12.gemfile +19 -0
  34. data/test/gemfiles/master.gemfile +22 -0
  35. data/test/integration/attr/serverspec/default_spec.rb +23 -0
  36. data/test/integration/iam/serverspec/default_spec.rb +23 -0
  37. data/test/spec/citadel_spec.rb +70 -0
  38. data/test/spec/s3_spec.rb +70 -0
  39. data/test/spec/spec_helper.rb +19 -0
  40. metadata +149 -0
@@ -0,0 +1,17 @@
1
+ #
2
+ # Copyright 2016, Noah Kantrowitz
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ require 'poise_boiler/rakefile'
@@ -0,0 +1,24 @@
1
+ #
2
+ # Copyright 2013-2016, Balanced, Inc.
3
+ # Copyright 2016, Noah Kantrowitz
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+
18
+ # Default S3 bucket to use for Citadel data
19
+ default['citadel']['bucket'] = nil
20
+ default['citadel']['region'] = 'us-east-1'
21
+
22
+ # Override these for use in Vagrant or other development environments
23
+ default['citadel']['access_key_id'] = nil
24
+ default['citadel']['secret_access_key'] = nil
@@ -0,0 +1,80 @@
1
+ #
2
+ # Copyright 2013-2016, Balanced, Inc.
3
+ # Copyright 2016, Noah Kantrowitz
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+
18
+ require 'chef/http'
19
+ require 'chef/json_compat'
20
+
21
+
22
+ # Helper to access files in a private S3 bucket using an interface like Chef
23
+ # node attributes.
24
+ #
25
+ # @since 1.0.0
26
+ # @example
27
+ # template '/etc/myapp.conf' do
28
+ # variables password: citadel['myapp/password']
29
+ # end
30
+ class Citadel
31
+ autoload :ChefDSL, 'citadel/chef_dsl'
32
+ autoload :CitadelError, 'citadel/error'
33
+ autoload :S3, 'citadel/s3'
34
+ autoload :VERSION, 'citadel/version'
35
+
36
+ attr_reader :bucket, :region, :credentials
37
+
38
+ def initialize(node, bucket=nil, region=nil)
39
+ @node = node
40
+ @bucket = bucket || node['citadel']['bucket']
41
+ @region = region || node['citadel']['region']
42
+ @credentials = find_credentials
43
+ end
44
+
45
+ def find_credentials
46
+ if @node['citadel']['access_key_id']
47
+ {
48
+ access_key_id: @node['citadel']['access_key_id'],
49
+ secret_access_key: @node['citadel']['secret_access_key'],
50
+ token: @node['citadel']['token'],
51
+ }
52
+ elsif @node['ec2']
53
+ role_creds = if @node['ec2']['iam'] && @node['ec2']['iam']['security-credentials']
54
+ # Creds loaded from Ohai.
55
+ @node['ec2']['iam']['security-credentials'].values.first
56
+ else
57
+ metadata_service = Chef::HTTP.new('http://169.254.169.254')
58
+ iam_role = metadata_service.get('latest/meta-data/iam/security-credentials/')
59
+ if iam_role.nil? || iam_role.empty?
60
+ raise 'Unable to find IAM role for node from EC2 metadata'
61
+ else
62
+ creds_json = metadata_service.get("latest/meta-data/iam/security-credentials/#{iam_role}")
63
+ Chef::JSONCompat.parse(creds_json)
64
+ end
65
+ end
66
+ {
67
+ access_key_id: role_creds['AccessKeyId'],
68
+ secret_access_key: role_creds['SecretAccessKey'],
69
+ token: role_creds['Token'],
70
+ }
71
+ else
72
+ raise 'Unable to find S3 credentials'
73
+ end
74
+ end
75
+
76
+ def [](key)
77
+ Chef::Log.debug("citadel: Retrieving #{@bucket}/#{key}")
78
+ Citadel::S3.get(bucket: @bucket, path: key, region: @region, **@credentials).to_s
79
+ end
80
+ end
@@ -0,0 +1,29 @@
1
+ #
2
+ # Copyright 2013-2016, Balanced, Inc.
3
+ # Copyright 2016, Noah Kantrowitz
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+
18
+
19
+ class Citadel
20
+ # Helper module for the DSL extension.
21
+ #
22
+ # @since 1.0.0
23
+ # @api private
24
+ module ChefDSL
25
+ def citadel(bucket=nil, region=nil)
26
+ Citadel.new(node, bucket, region)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,37 @@
1
+ #
2
+ # Copyright 2013-2016, Balanced, Inc.
3
+ # Copyright 2016, Noah Kantrowitz
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+
18
+ require 'citadel'
19
+ require 'citadel/safe_node'
20
+
21
+
22
+ # Patch our DSL extension into Chef.
23
+ # @api private
24
+ class Chef
25
+ class Recipe
26
+ include Citadel::ChefDSL
27
+ end
28
+
29
+ class Resource
30
+ include Citadel::ChefDSL
31
+ end
32
+
33
+ class Provider
34
+ include Citadel::ChefDSL
35
+ end
36
+ end
37
+
@@ -0,0 +1,33 @@
1
+ #
2
+ # Copyright 2012-2016, Brandon Adams and other contributors.
3
+ # Copyright 2013-2016, Balanced, Inc.
4
+ # Copyright 2016, Noah Kantrowitz
5
+ #
6
+ # Permission is hereby granted, free of charge, to any person obtaining
7
+ # a copy of this software and associated documentation files (the
8
+ # "Software"), to deal in the Software without restriction, including
9
+ # without limitation the rights to use, copy, modify, merge, publish,
10
+ # distribute, sublicense, and/or sell copies of the Software, and to
11
+ # permit persons to whom the Software is furnished to do so, subject to
12
+ # the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be
15
+ # included in all copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
+
25
+
26
+ class Citadel
27
+ # Base class for Citadell errors.
28
+ #
29
+ # @since 1.0.0
30
+ # @api private
31
+ class CitadelError < Exception
32
+ end
33
+ end
@@ -0,0 +1,84 @@
1
+ #
2
+ # Copyright 2012-2016, Brandon Adams and other contributors.
3
+ # Copyright 2013-2016, Balanced, Inc.
4
+ # Copyright 2016, Noah Kantrowitz
5
+ #
6
+ # Permission is hereby granted, free of charge, to any person obtaining
7
+ # a copy of this software and associated documentation files (the
8
+ # "Software"), to deal in the Software without restriction, including
9
+ # without limitation the rights to use, copy, modify, merge, publish,
10
+ # distribute, sublicense, and/or sell copies of the Software, and to
11
+ # permit persons to whom the Software is furnished to do so, subject to
12
+ # the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be
15
+ # included in all copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
+
25
+ require 'time'
26
+ require 'openssl'
27
+ require 'base64'
28
+
29
+ require 'chef/http'
30
+
31
+ require 'citadel/error'
32
+
33
+
34
+ class Citadel
35
+ # Simple read-only S3 client.
36
+ #
37
+ # @since 1.0.0
38
+ # @api private
39
+ module S3
40
+ extend self
41
+
42
+ # Get an object from S3.
43
+ #
44
+ # @param bucket [String] Name of the bucket to use.
45
+ # @param path [String] Path to the object.
46
+ # @param access_key_id [String] AWS access key ID.
47
+ # @param secret_access_key [String] AWS secret access key.
48
+ # @param token [String, nil] AWS IAM token.
49
+ # @param region [String] S3 bucket region.
50
+ # @return [Net::HTTPResponse]
51
+ def get(bucket:, path:, access_key_id:, secret_access_key:, token: nil, region: nil)
52
+ region ||= 'us-east-1' # Most buckets.
53
+ path = path[1..-1] if path[0] == '/'
54
+ now = Time.now().utc.strftime('%a, %d %b %Y %H:%M:%S GMT')
55
+
56
+ string_to_sign = "GET\n\n\n#{now}\n"
57
+ string_to_sign << "x-amz-security-token:#{token}\n" if token
58
+ string_to_sign << "/#{bucket}/#{path}"
59
+
60
+ signed = OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha1'), secret_access_key, string_to_sign)
61
+ signed_base64 = Base64.encode64(signed)
62
+
63
+ headers = {
64
+ 'date' => now,
65
+ 'authorization' => "AWS #{access_key_id}:#{signed_base64}",
66
+ }
67
+ headers['x-amz-security-token'] = token if token
68
+
69
+ hostname = case region
70
+ when 'us-east-1'
71
+ 's3.amazonaws.com'
72
+ else
73
+ "s3-#{region}.amazonaws.com"
74
+ end
75
+
76
+ begin
77
+ Chef::HTTP.new("https://#{hostname}").get("#{bucket}/#{path}", headers)
78
+ rescue Net::HTTPServerException => e
79
+ raise CitadelError.new("Unable to download #{path}: #{e}")
80
+ end
81
+ end
82
+
83
+ end
84
+ end
@@ -0,0 +1,41 @@
1
+ #
2
+ # Copyright 2013-2016, Balanced, Inc.
3
+ # Copyright 2016, Noah Kantrowitz
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+
18
+
19
+ # Block the IAM credentials from being stored to the Chef server.
20
+ # @api private
21
+ class Chef
22
+ class Node
23
+ old_save = instance_method(:save)
24
+
25
+ define_method(:save) do
26
+ security_credentials = nil
27
+ if automatic_attrs['ec2'] && automatic_attrs['ec2']['iam'] && automatic_attrs['ec2']['iam']['security-credentials']
28
+ security_credentials = automatic_attrs['ec2']['iam']['security-credentials']
29
+ automatic_attrs['ec2']['iam']['security-credentials'] = {}
30
+ end
31
+ begin
32
+ old_save.bind(self).call
33
+ ensure
34
+ unless security_credentials.nil?
35
+ automatic_attrs['ec2']['iam']['security-credentials'] = security_credentials
36
+ end
37
+ end
38
+ end
39
+
40
+ end
41
+ end
@@ -0,0 +1,21 @@
1
+ #
2
+ # Copyright 2016, Noah Kantrowitz
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+
18
+ class Citadel
19
+ # Citadel gem version.
20
+ VERSION = '1.1.0'
21
+ end
@@ -0,0 +1,41 @@
1
+ #
2
+ # Copyright 2016, Noah Kantrowitz
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ lib = File.expand_path('../lib', __FILE__)
18
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
19
+ require 'citadel/version'
20
+
21
+ Gem::Specification.new do |spec|
22
+ spec.name = 'poise-citadel'
23
+ spec.version = Citadel::VERSION
24
+ spec.authors = ['Noah Kantrowitz']
25
+ spec.email = %w{noah@coderanger.net}
26
+ spec.description = 'DSL for accessing secret data stored on S3 using IAM roles.'
27
+ spec.summary = spec.description
28
+ spec.homepage = 'https://github.com/poise/citadel'
29
+ spec.license = 'Apache 2.0'
30
+ spec.metadata['halite_name'] = 'citadel'
31
+
32
+ spec.files = `git ls-files`.split($/)
33
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
34
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
35
+ spec.require_paths = %w{lib}
36
+
37
+ spec.add_dependency 'halite', '~> 1.2'
38
+
39
+ spec.add_development_dependency 'poise-boiler', '~> 1.7'
40
+ spec.add_development_dependency 'kitchen-ec2', '~> 1.0'
41
+ end
@@ -0,0 +1,17 @@
1
+ #
2
+ # Copyright 2016, Noah Kantrowitz
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ override['citadel']['bucket'] = 'citadel-kitchen'
@@ -0,0 +1,20 @@
1
+ #
2
+ # Copyright 2013-2016, Balanced, Inc.
3
+ # Copyright 2016, Noah Kantrowitz
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+
18
+ name 'citadel_test'
19
+
20
+ depends 'citadel'