vaml 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: be6b490176d6f15d95fcc857f59070092e8b6a4d
4
+ data.tar.gz: 508da870ab0559220448c30b2a23879b185242ef
5
+ SHA512:
6
+ metadata.gz: eed59d3d4c072a25d87a3a906e9b5df3a35c15f645a3d7c164eb47386c8b75f353692f9ab4041f23007c1e96b4e40d6f7d76b2440d71a9bf85ce6d91642bd297
7
+ data.tar.gz: a1ed95d6cd38d82f3bef07ddf9a2b0b9ab1df483ebead2a0b6c2ba8ed416549c736d4e821308ba0a83b063334d1ef94b6bd5e364f8749a1bb4af31206e4ac894
@@ -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/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in vaml.gemspec
4
+ gemspec
5
+ gem 'psych'
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Dipesh
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,149 @@
1
+ # Vaml
2
+
3
+ Vaml is Vault with YAML, and a little sweet of Github. It helps you manage your app's secrets from your yml files.
4
+ It also provides support for Github Auth Integration, so you can easily control key access to developers.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'vaml'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install vaml
21
+
22
+ ## Usage
23
+
24
+ ### Configuration
25
+ ```
26
+ Vaml.configure do |v|
27
+ v.host = "http://127.0.0.1:8200"
28
+ v.organization = 'xxx'
29
+ v.token = ENV['VAULT_TOKEN']
30
+ end
31
+ ```
32
+
33
+ or
34
+
35
+ `Vaml.configure(host: 'xxx', organization: 'xxx', token: 'xxx')`
36
+
37
+
38
+ ## Adding secrets
39
+ The gem provides a rake task that you can use to add secrets. As a developer, if you want to add a new secret:
40
+
41
+ * Use the following syntax in you YML. It is not necessary to follow the syntax, but it is highly recommended.
42
+ `access_id: vault:/secret/staging/aws/access_id`
43
+
44
+ * Then, add your secret with `rake vaml:add_secret`. You will need to set an `ENV['VAULT_TOKEN']` to run this command. If Github integration is activated, Go your Developer Settings > Personal Access Token and select the `admin:org` scope. You can now use the token generated by Github with the command.
45
+
46
+ `VAULT_TOKEN=my_token rake vaml:add_secret /secrets/staging/aws/access_id AXXSAWEDFSF`
47
+
48
+ * After you configure vault, you can also add secrets from the rails console using the same token.
49
+
50
+ ```
51
+ Vaml::Github.auth(my_token)
52
+ Vaml.write(key, value)
53
+ ```
54
+
55
+ If you have been given proper access rights, you will be able to successfully write the secret.
56
+
57
+ ### YAML Integration
58
+
59
+ Given, you have an input yml that looks like:
60
+ ```
61
+ development:
62
+ aws:
63
+ access_id: 'XXX'
64
+ staging:
65
+ aws:
66
+ access_id: vault:/secret/staging/aws/access_id
67
+ production:
68
+ aws:
69
+ access_id: vault:/secret/production/aws/access_id
70
+ ```
71
+
72
+ and you write these secrets with:
73
+ ```
74
+ VAULT_TOKEN=my_token rake vaml:add_secret /secret/staging/aws/access_id ABC
75
+ VAULT_TOKEN=my_token rake vaml:add_secret /secret/production/aws/access_id DEF
76
+ ```
77
+
78
+ When you access these secrets from vault,
79
+
80
+ `Vaml.from_yaml(File.read('input_yml.yml'))`
81
+
82
+ Vaml gives you back a ruby hash that looks like:
83
+
84
+ ```
85
+ {
86
+ "development" => {"aws"=>{"access_id"=>"XXX"}},
87
+ "staging" => {"aws"=>{"access_id"=>"ABC"}},
88
+ "production" => {"aws"=>{"access_id"=>"ABC"}}
89
+ }
90
+ ```
91
+ Note that this does not actually write back to the file, and it is upto you to use this result as you want.
92
+ One strategy is to store these into the Rails configuration object when Rails initializes, so you will have it available to your process, but you won't need to write any secret anywhere. Bye bye to leaky ENV variables!
93
+
94
+
95
+
96
+ ### Policies
97
+
98
+ You can add and list Policies
99
+
100
+ * List policies
101
+
102
+ `Vaml.list_policies`
103
+
104
+ * Add policies
105
+
106
+ ```
107
+ # policy_definition = <<-EOH
108
+ # path "sys" {
109
+ # policy = "deny"
110
+ # }
111
+ # EOH
112
+
113
+ Vaml.add_policy(policy_name, policy_definition)
114
+ ```
115
+ Internally all this does is the original call to Vault.
116
+
117
+ `Vault.sys.put_policy("dev", policy)`
118
+
119
+
120
+ ## Github Integration
121
+
122
+ ```
123
+ Vaml::Github.enable_auth(organization)
124
+ Vaml::Github.grant_policy(team_name, policy_name)
125
+ ```
126
+
127
+ ## Using Vault in your Local System
128
+
129
+ OSX
130
+
131
+ ```
132
+ brew install vault
133
+ vault server --dev
134
+ ```
135
+ And Follow the official Vault documentation.
136
+
137
+ ## Development
138
+
139
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
140
+
141
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
142
+
143
+ ## Contributing
144
+
145
+ Bug reports and pull requests are welcome on GitHub at https://github.com/dgtm/vaml.
146
+
147
+ ## License
148
+
149
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "vaml"
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(__FILE__)
@@ -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,3 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "vaml"
@@ -0,0 +1,20 @@
1
+ desc 'Add a secret to Vault'
2
+ namespace :vaml do
3
+ task :add_secret, [:arg1, :arg2] do |t, args|
4
+ key, value = ARGV[1], ARGV[2]
5
+ unless key && value
6
+ puts "Usage: VAULT_HOST=xxx VAULT_TOKEN=xxx rake vaml:add_secret /secret/development/xxx value"
7
+ raise
8
+ end
9
+ Vaml.configure(host: ENV['VAULT_HOST'], token: ENV['VAULT_TOKEN'])
10
+ Vaml.write_string(key, value)
11
+ puts "the rake task did something"
12
+ exit
13
+ end
14
+
15
+ task :read_secret do
16
+ Vaml.configure(host: ENV['VAULT_HOST'], token: ENV['VAULT_TOKEN'])
17
+ puts Vaml.read_string(ARGV[1])
18
+ exit
19
+ end
20
+ end
@@ -0,0 +1,79 @@
1
+ require 'psych'
2
+ require 'yaml'
3
+ require 'vaml/config_handler'
4
+ require 'vaml/version'
5
+ require 'vaml/vault_config'
6
+ require 'vaml/configuration'
7
+ require 'vaml/github'
8
+ require 'vaml/railtie' if defined?(Rails)
9
+ require 'pry'
10
+
11
+ module Vaml
12
+
13
+ class << self
14
+ attr_accessor :configuration
15
+
16
+ # @param [Hash] options {host: '0.0.0.0:8200', token: ENV["VTOKEN"], organization: ''}
17
+ def configure(options)
18
+ options[:host] ||= 'http://127.0.0.1:8200'
19
+ options[:token] ||= ENV['VAULT_TOKEN']
20
+
21
+ self.configuration ||= Configuration.new(options)
22
+ yield configuration if block_given?
23
+
24
+ # Configures Vault itself.
25
+ Vaml::VaultConfig.configure!
26
+ self
27
+ end
28
+
29
+ def write_string(key, value)
30
+ write(key, {value: value})
31
+ end
32
+
33
+ def read_string(key)
34
+ read(key).data[:value]
35
+ end
36
+
37
+ def from_yaml(yml)
38
+ handler = Vaml::ConfigHandler.new
39
+ parser = Psych::Parser.new(handler)
40
+ parser.parse(yml)
41
+ handler.root.to_ruby.first
42
+ end
43
+
44
+ def read(query)
45
+ Vault.with_retries(Vault::HTTPConnectionError) do
46
+ val = Vault.logical.read(query)
47
+ raise "VamlError: No secret was stored for #{query}" unless val
48
+ val
49
+ end
50
+ end
51
+
52
+
53
+ def write(key, value)
54
+ Vault.with_retries(Vault::HTTPConnectionError) do
55
+ Vault.logical.write(key, value)
56
+ end
57
+ end
58
+
59
+ def auth_with_github(token)
60
+ Vaml::Github.auth(token)
61
+ end
62
+
63
+ # policy = <<-EOH
64
+ # path "sys" {
65
+ # policy = "deny"
66
+ # }
67
+ # EOH
68
+ # Vault.sys.put_policy("dev", policy)
69
+ def add_policy(policy_name, policy_definition)
70
+ Vault.sys.put_policy(policy_name, policy_definition)
71
+ end
72
+
73
+ def list_policies
74
+ Vault.sys.policies.map do |name|
75
+ Vault.sys.policy(name)
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,14 @@
1
+ module Vaml
2
+ class ConfigHandler < Psych::TreeBuilder
3
+ def scalar value, anchor, tag, plain, quoted, style
4
+ vault_regex = /vault:/
5
+ translated = if value.match(vault_regex)
6
+ query = value.gsub(vault_regex, '')
7
+ Vaml.read_string(query)
8
+ else
9
+ value
10
+ end
11
+ super translated, anchor, tag, plain, quoted, style
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,10 @@
1
+ module Vaml
2
+ class Configuration
3
+ attr_accessor :organization, :host, :token
4
+ def initialize(options)
5
+ @host = options[:host]
6
+ @token = options[:token]
7
+ @organization = options[:organization]
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,19 @@
1
+ module Vaml
2
+ module Github
3
+ def self.enable_auth(org = Vaml.configuration.organization)
4
+ puts "Enabling auth for #{org} ... "
5
+ Vault.sys.enable_auth("github", "github")
6
+ Vault.logical.write("auth/github/config", organization: org)
7
+ end
8
+
9
+ def self.grant_policy(team_name, policy_name)
10
+ Vault.client.post("/v1/auth/github/map/teams/#{team_name}", policy_name)
11
+ # Vault.logical.write("auth/github/map/teams/#{team_name}", policy_name)
12
+ end
13
+
14
+ def self.auth(token)
15
+ puts "Authenticating to Github ... "
16
+ Vault.auth.github(token)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,10 @@
1
+ require 'rails'
2
+ module Vaml
3
+ class Railtie < Rails::Railtie
4
+ railtie_name :vaml
5
+
6
+ rake_tasks do
7
+ load "tasks/add_secret.rake"
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,16 @@
1
+ require 'vault'
2
+ module Vaml
3
+ module VaultConfig
4
+ def self.configure!
5
+ ::Vault.configure do |config|
6
+ config.address = Vaml.configuration.host
7
+ config.token = Vaml.configuration.token
8
+ config.ssl_verify = false
9
+ config.timeout = 30
10
+ config.ssl_timeout = 5
11
+ config.open_timeout = 5
12
+ config.read_timeout = 30
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ module Vaml
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,38 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "vaml/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "vaml"
8
+ spec.version = Vaml::VERSION
9
+ spec.authors = ["Dipesh"]
10
+ spec.email = ["dipeshgtm@gmail.com"]
11
+
12
+ spec.summary = %q{Do not expose your secret keys}
13
+ spec.description = %q{Get your secrets from vault}
14
+ spec.homepage = ""
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against " \
23
+ "public gem pushes."
24
+ end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_dependency "vault", "~> 0.10"
34
+
35
+ spec.add_development_dependency "bundler", "~> 1.15"
36
+ spec.add_development_dependency "rake", "~> 10.0"
37
+ spec.add_development_dependency "pry"
38
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vaml
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dipesh
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-07-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: vault
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.10'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.15'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.15'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Get your secrets from vault
70
+ email:
71
+ - dipeshgtm@gmail.com
72
+ executables:
73
+ - vaml
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - bin/console
83
+ - bin/setup
84
+ - exe/vaml
85
+ - lib/tasks/add_secret.rake
86
+ - lib/vaml.rb
87
+ - lib/vaml/config_handler.rb
88
+ - lib/vaml/configuration.rb
89
+ - lib/vaml/github.rb
90
+ - lib/vaml/railtie.rb
91
+ - lib/vaml/vault_config.rb
92
+ - lib/vaml/version.rb
93
+ - vaml.gemspec
94
+ homepage: ''
95
+ licenses:
96
+ - MIT
97
+ metadata:
98
+ allowed_push_host: https://rubygems.org
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 2.4.5
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: Do not expose your secret keys
119
+ test_files: []