vault-consul 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4d8af969a5e2ab0c082c66fb9a86bf3a455202e68e28662f58d9606f62abb3c8
4
+ data.tar.gz: 928ce9d52127099d964cd5b7630b2bfecb74a8fcc0424b53d7e01ba4dc6e92f3
5
+ SHA512:
6
+ metadata.gz: 62e1739ed119862d31e8211eb66d3c71f149df3a8b11b03a92f794af4904f0259d94428bb5d2da66be5707390409e512a0d6cb010d44edac5408cec077bf7ca4
7
+ data.tar.gz: 6abaef9e90ad4901fa03ff09f84740dfae383a19b358178f1d564eba74be5ec1a7fcc9de8bbd50c71c913c2c3e1c914ee8044a2260e352aa47ac9177cb2c0c57
@@ -0,0 +1 @@
1
+ .idea
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
@@ -0,0 +1,31 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ vault-consul (0.1.0)
5
+ diplomat
6
+ psych
7
+ vault
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ aws-sigv4 (1.0.3)
13
+ diplomat (2.0.2)
14
+ faraday (~> 0.9)
15
+ json
16
+ faraday (0.15.3)
17
+ multipart-post (>= 1.2, < 3)
18
+ json (2.1.0)
19
+ multipart-post (2.0.0)
20
+ psych (3.0.3)
21
+ vault (0.12.0)
22
+ aws-sigv4
23
+
24
+ PLATFORMS
25
+ ruby
26
+
27
+ DEPENDENCIES
28
+ vault-consul!
29
+
30
+ BUNDLED WITH
31
+ 1.16.2
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 Viki
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 all
13
+ 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 THE
21
+ SOFTWARE.
@@ -0,0 +1,2 @@
1
+ # vault-consul-gem
2
+ Vault &amp; Consul gem for Viki
@@ -0,0 +1,43 @@
1
+ require 'vault-consul/errors'
2
+ require 'vault-consul/config_handler'
3
+ require 'vault'
4
+ require 'diplomat'
5
+
6
+ module VaultConsul
7
+ ENV_LIST = %w[TEAM ENV SERVICE CONSUL_HTTP_ADDR CONSUL_HTTP_TOKEN VAULT_ADDR VAULT_TOKEN]
8
+ class << self
9
+ def parse(path)
10
+ unless ['local', 'test'].include?(ENV['ENV'])
11
+ ENV_LIST.each do |env|
12
+ raise ConfigError, "Missing ENV variable: #{env}" if ENV[env].nil? || ENV[env] == ''
13
+ end
14
+ end
15
+
16
+ yaml = File.read(path)
17
+ handler = VaultConsul::ConfigHandler.new
18
+ parser = Psych::Parser.new(handler)
19
+ parser.parse(yaml)
20
+ handler.root.to_ruby.first
21
+ end
22
+
23
+ def read_vault(vault_key, json_key)
24
+ Vault.with_retries(Vault::HTTPConnectionError) do
25
+ key = "secret/data/#{ENV['TEAM']}/#{ENV['ENV']}/#{ENV['SERVICE']}/#{vault_key}"
26
+ val = Vault.logical.read(key)
27
+ raise "VaultError: No secret was stored for #{key}" if val.nil? || val.data.nil? || val.data[:data].nil?
28
+ val.data[:data][json_key.to_sym]
29
+ end
30
+ end
31
+
32
+ def read_consul(consul_key)
33
+ Diplomat.configure do |config|
34
+ config.url = ENV['CONSUL_HTTP_ADDR']
35
+ config.acl_token = ENV['CONSUL_HTTP_TOKEN']
36
+ end
37
+ key = "#{ENV['TEAM']}/#{ENV['ENV']}/#{ENV['SERVICE']}/#{consul_key}"
38
+ val = Diplomat::Kv.get(key)
39
+ raise "ConsulError: No value was stored for #{key}" if val.nil? || val == ''
40
+ val
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,27 @@
1
+ require 'psych'
2
+ module VaultConsul
3
+ class ConfigHandler < Psych::TreeBuilder
4
+ def scalar value, anchor, tag, plain, quoted, style
5
+ vault_regex = /vault:/
6
+ consul_regex = /consul:/
7
+ env_regex = /env:/
8
+ translated = if value.match(vault_regex)
9
+ vault_tag = value.gsub(vault_regex, '')
10
+ vault_tag_holder = vault_tag.split('.')
11
+ vault_key = vault_tag_holder[0]
12
+ json_key = vault_tag_holder[1]
13
+ VaultConsul.read_vault(vault_key, json_key)
14
+ elsif value.match(consul_regex)
15
+ consul_key = value.gsub(consul_regex, '')
16
+ VaultConsul.read_consul(consul_key)
17
+ elsif value.match(env_regex)
18
+ ENV[value.gsub(env_regex, '')]
19
+ else
20
+ value
21
+ end
22
+
23
+ raise ConfigError, "Missing config: #{value}" if translated.nil? || translated == ''
24
+ super translated, anchor, tag, plain, quoted, style
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,2 @@
1
+ class ConfigError < RuntimeError
2
+ end
@@ -0,0 +1,3 @@
1
+ module VaultConsul
2
+ VERSION = "0.1.2"
3
+ end
@@ -0,0 +1,36 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "vault-consul/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "vault-consul"
8
+ spec.version = VaultConsul::VERSION
9
+ spec.authors = ["Viki-Platform"]
10
+ spec.email = ["platform@viki.com"]
11
+
12
+ spec.summary = %q{Viki vault & consul client}
13
+ spec.description = %q{Parse your yaml config file with consul/ vault variables}
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_runtime_dependency "psych"
34
+ spec.add_runtime_dependency "diplomat"
35
+ spec.add_runtime_dependency "vault"
36
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vault-consul
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Viki-Platform
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-11-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: psych
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: diplomat
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: vault
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Parse your yaml config file with consul/ vault variables
56
+ email:
57
+ - platform@viki.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - Gemfile.lock
65
+ - LICENSE
66
+ - README.md
67
+ - lib/vault-consul.rb
68
+ - lib/vault-consul/config_handler.rb
69
+ - lib/vault-consul/errors.rb
70
+ - lib/vault-consul/version.rb
71
+ - vault-consul.gemspec
72
+ homepage: ''
73
+ licenses:
74
+ - MIT
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.7.7
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: Viki vault & consul client
96
+ test_files: []