configuration_service-provider-vault 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gemspec +27 -0
- data/.gitignore +9 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/README.md +77 -0
- data/Rakefile +77 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/configuration_service/provider/vault.rb +98 -0
- data/lib/configuration_service/provider/vault/index_helper.rb +25 -0
- data/lib/configuration_service/provider/vault/version.rb +13 -0
- metadata +139 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b6fbf02628baa05b01e86460e79683b31a4614ed
|
4
|
+
data.tar.gz: 38593989390fd441064766c7d22b3c8a7643a5c1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: acc301f249f02ec8189bb905258dce1b22c55d13e794bca1628697d9c8c4b0b25f5704d8793f74e5ac7f7f17b85aef7fc28ae3bfcded3a8081688213afb967f6
|
7
|
+
data.tar.gz: ec587b1fea57526fbdc0adbdddbeeb53d10cb210eef6449acb41d66797ace6c80066d4665a95957d8e2621d717e98f1204b6c299d993dbc9954aca984e124e83
|
data/.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'configuration_service/provider/vault/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "configuration_service-provider-vault"
|
8
|
+
spec.version = ConfigurationService::Provider::Vault::VERSION
|
9
|
+
spec.authors = ["Sheldon Hearn"]
|
10
|
+
spec.email = ["sheldonh@starjuice.net"]
|
11
|
+
|
12
|
+
spec.summary = %q{Vault provider for Configuration Service}
|
13
|
+
spec.description = %q{A HashiCorp Vault provider for the Configuration Service}
|
14
|
+
spec.homepage = "http://www.hetzner.co.za"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "vault", "~> 0.1"
|
22
|
+
spec.add_dependency "configuration_service", "~> 1.1.0"
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
spec.add_development_dependency "cucumber", "~> 2.0"
|
26
|
+
spec.add_development_dependency "rspec-expectations", "~> 3.3"
|
27
|
+
end
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
# ConfigurationService::Provider::Vault
|
2
|
+
|
3
|
+
A service provider for the Ruby [ConfigurationService API](https://rubygems.org/gems/configuration_service).
|
4
|
+
|
5
|
+
|
6
|
+
## Usage
|
7
|
+
|
8
|
+
The recommended approach to creating a configuration service client is to use a factory
|
9
|
+
from the [configuration_service](https://rubygems.org/gems/configuration_service) gem.
|
10
|
+
See the documentation for the ConfigurationService::Factory package for a list of
|
11
|
+
available factories.
|
12
|
+
|
13
|
+
For example, we can use the ConfigurationService::Factory::EnvironmentContext factory
|
14
|
+
to create and configure a configuration service client backed by the vault provider as
|
15
|
+
follows.
|
16
|
+
|
17
|
+
Our `main.rb` (or `config.ru` or whatever) is simple:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
require 'bundler'
|
21
|
+
Bundler.require(:default)
|
22
|
+
|
23
|
+
service = ConfigurationService::Factory::EnvironmentContext.create
|
24
|
+
if configuraton = service.request_configuration
|
25
|
+
AcmeApplication.new(configuration.data).run
|
26
|
+
else
|
27
|
+
raise "configuration not found"
|
28
|
+
end
|
29
|
+
```
|
30
|
+
|
31
|
+
This relies on a [bundler](http://bundler.io) Gemfile to provide the
|
32
|
+
configuration\_service-provider-vault gem:
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
source 'https://rubygems.org'
|
36
|
+
|
37
|
+
gem 'configuration_service-provider-vault'
|
38
|
+
gem 'acme_application'
|
39
|
+
```
|
40
|
+
|
41
|
+
Now we use the process environment to configure the EnvironmentContext factory:
|
42
|
+
|
43
|
+
```shell
|
44
|
+
CFGSRV_IDENTIFIER="acme" \
|
45
|
+
CFGSRV_TOKEN="0b2a80f4-54ce-45f4-8267-f6558fee64af" \
|
46
|
+
CFGSRV_PROVIDER="vault" \
|
47
|
+
CFGSRV_PROVIDER_ADDRESS="http://127.0.0.1:8200" \
|
48
|
+
bundle exec main.rb
|
49
|
+
```
|
50
|
+
|
51
|
+
Note that `main.rb` is completely decoupled from the selection of provider and
|
52
|
+
provider configuration. We could swap and/or reconfigure the provider by
|
53
|
+
manipulating only the Gemfile and the environment.
|
54
|
+
|
55
|
+
If you insist on hard-coding everything, or if your strategy for bootstrapping
|
56
|
+
the configuration service isn't expressed by an existing factory yet, you can
|
57
|
+
construct the service yourself:
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
# Bad example
|
61
|
+
|
62
|
+
require 'configuration_service/provider/vault'
|
63
|
+
require 'acme_application'
|
64
|
+
|
65
|
+
service = ConfigurationService.new(
|
66
|
+
"acme",
|
67
|
+
"0b2a80f4-54ce-45f4-8267-f6558fee64af",
|
68
|
+
ConfigurationService::Provider::Vault.new(
|
69
|
+
address: "http://127.0.0.1:8200"
|
70
|
+
)
|
71
|
+
)
|
72
|
+
if configuraton = service.request_configuration
|
73
|
+
AcmeApplication.new(configuration.data).run
|
74
|
+
else
|
75
|
+
raise "configuration not found"
|
76
|
+
end
|
77
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
|
3
|
+
desc "Test the Vault provider for Configuration Service against a Vault development server"
|
4
|
+
task :test do
|
5
|
+
with_devserver do
|
6
|
+
Rake::Task["just_test"].invoke
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
desc "Run cucumber without starting a Vault development server"
|
11
|
+
task :just_test do
|
12
|
+
gem = Gem::Specification.find_by_name("configuration_service")
|
13
|
+
features_path = gem.full_gem_path + "/features"
|
14
|
+
sh %{TEST_ORCHESTRATION_PROVIDER=vault bundle exec cucumber -r #{features_path} -r ./features/support #{features_path}}
|
15
|
+
end
|
16
|
+
|
17
|
+
def with_devserver
|
18
|
+
assert_no_vault_server
|
19
|
+
devserver_start
|
20
|
+
begin
|
21
|
+
yield
|
22
|
+
ensure
|
23
|
+
devserver_stop
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def assert_no_vault_server
|
28
|
+
require "socket"
|
29
|
+
|
30
|
+
begin
|
31
|
+
Socket.tcp('127.0.0.1', 8200).close
|
32
|
+
raise "can't start devserver; localhost already listening on TCP port 8200"
|
33
|
+
rescue Errno::ECONNREFUSED
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def devserver_start
|
38
|
+
require "open3"
|
39
|
+
|
40
|
+
channel, notify = IO.pipe
|
41
|
+
|
42
|
+
fork do
|
43
|
+
channel.close
|
44
|
+
begin
|
45
|
+
_, stdout, _, wait_thr = Open3.popen3("vault server -dev")
|
46
|
+
rescue
|
47
|
+
notify.puts("Process.exit(0)")
|
48
|
+
raise
|
49
|
+
end
|
50
|
+
|
51
|
+
vault_pid = wait_thr[:pid]
|
52
|
+
notify.puts("ENV['VAULT_PID']='#{vault_pid}'")
|
53
|
+
while line = stdout.gets
|
54
|
+
line.chomp!
|
55
|
+
if line =~ /export VAULT_ADDR='([^']+)'/
|
56
|
+
notify.puts("ENV['VAULT_ADDR']='#{$1}'")
|
57
|
+
elsif line =~ /^Root Token: (.+)/
|
58
|
+
notify.puts("ENV['VAULT_TOKEN']='#{$1}'")
|
59
|
+
notify.close
|
60
|
+
break
|
61
|
+
end
|
62
|
+
end
|
63
|
+
Process.detach(vault_pid)
|
64
|
+
Process.exit(0)
|
65
|
+
end
|
66
|
+
|
67
|
+
notify.close
|
68
|
+
while line = channel.gets
|
69
|
+
line.chomp!
|
70
|
+
eval line
|
71
|
+
end
|
72
|
+
%w[VAULT_PID VAULT_ADDR VAULT_TOKEN].each { |s| $stderr.puts "export #{s}='#{ENV[s]}'" }
|
73
|
+
end
|
74
|
+
|
75
|
+
def devserver_stop
|
76
|
+
Process.kill("TERM", ENV['VAULT_PID'].to_i)
|
77
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "configuration_service/vault"
|
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
|
data/bin/setup
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
require "configuration_service"
|
2
|
+
require "vault"
|
3
|
+
require "json"
|
4
|
+
require "time"
|
5
|
+
|
6
|
+
require_relative "vault/index_helper"
|
7
|
+
|
8
|
+
module ConfigurationService
|
9
|
+
|
10
|
+
module Provider
|
11
|
+
|
12
|
+
class Vault
|
13
|
+
|
14
|
+
def initialize(address:)
|
15
|
+
@vault = ::Vault::Client.new(address: address)
|
16
|
+
end
|
17
|
+
|
18
|
+
def request_configuration(identifier, token)
|
19
|
+
authenticate(token)
|
20
|
+
|
21
|
+
adapt_exceptions do
|
22
|
+
if revision = get_latest_revision(identifier)
|
23
|
+
index = build_index(identifier, revision)
|
24
|
+
if response = @vault.logical.read(index)
|
25
|
+
data, metadata = JSON.parse(response.data[:data]), JSON.parse(response.data[:metadata])
|
26
|
+
ConfigurationService::Configuration.new(identifier, data, metadata)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def publish_configuration(configuration, token)
|
33
|
+
authenticate(token)
|
34
|
+
|
35
|
+
identifier, data, metadata = configuration.identifier, configuration.data, configuration.metadata
|
36
|
+
revision = metadata["revision"] or raise "can't publish configuration without revision in metadata"
|
37
|
+
|
38
|
+
adapt_exceptions do
|
39
|
+
index = build_index(identifier, revision)
|
40
|
+
@vault.logical.write(index, data: JSON.generate(data), metadata: JSON.generate(metadata), format: "json")
|
41
|
+
set_latest_revision(identifier, metadata["revision"])
|
42
|
+
ConfigurationService::Configuration.new(identifier, data, metadata)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def key(identifier)
|
47
|
+
self.class.key(identifier)
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
# We explicitly disallow a nil token to defeat ::Vault::Client's default behaviour
|
53
|
+
# of reading ENV['VAULT_TOKEN'] and ~/.vault-token, which makes testing harder.
|
54
|
+
#
|
55
|
+
def authenticate(token)
|
56
|
+
token or raise ConfigurationService::AuthorizationError, "non-nil token required"
|
57
|
+
@vault.token = token
|
58
|
+
end
|
59
|
+
|
60
|
+
def adapt_exceptions
|
61
|
+
yield
|
62
|
+
rescue ::Vault::MissingTokenError
|
63
|
+
raise ConfigurationService::AuthorizationError, "missing token"
|
64
|
+
rescue ::Vault::HTTPError => ex
|
65
|
+
if ex.errors.include?("permission denied")
|
66
|
+
raise ConfigurationService::AuthorizationError, "permission denied"
|
67
|
+
else
|
68
|
+
raise
|
69
|
+
end
|
70
|
+
rescue ::Vault::VaultError => ex
|
71
|
+
raise ConfigurationService::Error, ex.message
|
72
|
+
end
|
73
|
+
|
74
|
+
def get_latest_revision(identifier)
|
75
|
+
if response = @vault.logical.read(latest_index(identifier))
|
76
|
+
response.data[:revision]
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def set_latest_revision(identifier, revision)
|
81
|
+
@vault.logical.write(latest_index(identifier), revision: revision)
|
82
|
+
end
|
83
|
+
|
84
|
+
def latest_index(identifier)
|
85
|
+
IndexHelper.index(identifier)
|
86
|
+
end
|
87
|
+
|
88
|
+
def build_index(identifier, revision)
|
89
|
+
IndexHelper.index(identifier, revision)
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
95
|
+
|
96
|
+
end
|
97
|
+
|
98
|
+
ConfigurationService::ProviderRegistry.instance.register("vault", ConfigurationService::Provider::Vault)
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module ConfigurationService
|
2
|
+
|
3
|
+
module Provider
|
4
|
+
|
5
|
+
class Vault
|
6
|
+
|
7
|
+
module IndexHelper
|
8
|
+
|
9
|
+
PREFIX = "secret/config/v1"
|
10
|
+
|
11
|
+
def self.index(identifier, revision = "latest")
|
12
|
+
"#{policy_index(identifier)}/#{revision}"
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.policy_index(identifier)
|
16
|
+
"#{PREFIX}/#{identifier}"
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,139 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: configuration_service-provider-vault
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sheldon Hearn
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-08-19 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.1'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.1'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: configuration_service
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.1.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 1.1.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.10'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.10'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: cucumber
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '2.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '2.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec-expectations
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '3.3'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '3.3'
|
97
|
+
description: A HashiCorp Vault provider for the Configuration Service
|
98
|
+
email:
|
99
|
+
- sheldonh@starjuice.net
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".gemspec"
|
105
|
+
- ".gitignore"
|
106
|
+
- ".travis.yml"
|
107
|
+
- Gemfile
|
108
|
+
- README.md
|
109
|
+
- Rakefile
|
110
|
+
- bin/console
|
111
|
+
- bin/setup
|
112
|
+
- lib/configuration_service/provider/vault.rb
|
113
|
+
- lib/configuration_service/provider/vault/index_helper.rb
|
114
|
+
- lib/configuration_service/provider/vault/version.rb
|
115
|
+
homepage: http://www.hetzner.co.za
|
116
|
+
licenses: []
|
117
|
+
metadata: {}
|
118
|
+
post_install_message:
|
119
|
+
rdoc_options: []
|
120
|
+
require_paths:
|
121
|
+
- lib
|
122
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
123
|
+
requirements:
|
124
|
+
- - ">="
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: '0'
|
127
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
requirements: []
|
133
|
+
rubyforge_project:
|
134
|
+
rubygems_version: 2.4.8
|
135
|
+
signing_key:
|
136
|
+
specification_version: 4
|
137
|
+
summary: Vault provider for Configuration Service
|
138
|
+
test_files: []
|
139
|
+
has_rdoc:
|