rubyconfig-vault 0.1.2 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6472a1998cf169f1dca30ea8106dc75b21f2faf9a480ee1bf382d324e2c79c19
4
- data.tar.gz: d25f00cd1684191891ae98689db23e3aace676d9188b26e6ad09a2a199e04d82
3
+ metadata.gz: 63279ad2f29fabd8006dd8bdf3a69e49dabd2a1d5e3f89c1c695c9e6528f5154
4
+ data.tar.gz: 734327aae2db0747633a5a06101e2c2117ad54b0b441011bf4d58348da871c46
5
5
  SHA512:
6
- metadata.gz: d07012ba1f5d6e89a29ff6797c6cbbb94277e9766d1771f051a7ba408ae929dbd58ea8bf82cc1dd6e348352a9d8df0d68dd8bf227a329e2b55775278bace5077
7
- data.tar.gz: 6881a30a2b2a9cddf3d0c7753f744eef1b809d72fa669cbf89f4347234a44be47c150e404754e54ced347d449ff6e5467c260f185ac1d4226ce7a90946d8f26e
6
+ metadata.gz: 4b6aac24bcb3781bae89ef0bc3e77fe5da3f5ada28308c711e5f8117e193cedcf12aaf5a6576ab080521f138325052c16c5e2246011fb086b38c7407cd32a839
7
+ data.tar.gz: 8455068a25f114a7a9a430e8daf25421c253f39788c673bcfd996ba208bfee086f7c5fb7d3dfd0c2f93c7349eb6792d4851b082c0753b60ebf729c04db2f2379
@@ -2,31 +2,82 @@ require 'vault'
2
2
 
3
3
  module Config
4
4
  module Sources
5
+ # A vault source for Config
5
6
  class VaultSource
6
7
  attr_accessor :kv, :root
7
8
  attr_reader :paths, :client
8
9
 
10
+ # Create a new Config source, all Vault::Client parameters supported
11
+ #
12
+ # @param [Hash] opts
13
+ # @option opts [String, nil] :kv mount point for operations
14
+ # @option opts [Array<String>, nil] :paths paths for vault secrets
15
+ # @option opts [String, Symbol, nil] :root default root key for data provided by source
16
+ # @option opts [Integer] :attempts number of attempts to try and resolve Vault::HTTPError
17
+ # @option opts [Number] :base interval for exponential backoff
18
+ # @option opts [Number] :max_wait maximum weight time for exponential backoff
19
+ # @option opts [Boolean] :flatten flatten the resulting hash. Preserves root option
9
20
  def initialize(opts = {})
10
21
  client_opts = opts.clone
11
22
  @kv = client_opts.delete(:kv) || ''
12
- @paths = client_opts.delete(:paths) || []
23
+ @paths = []
24
+ @attempts = client_opts.delete(:attempts) || 5
25
+ @base = client_opts.delete(:base) || 0.5
26
+ @max_wait = client_opts.delete(:max_wait) || 2.5
13
27
  @root = client_opts.delete(:root)
28
+ @flatten = client_opts.delete(:flatten)
29
+ @paths << client_opts.delete(:paths) if client_opts.key?(:paths)
30
+ @map = {}
31
+ @paths.map! do |p|
32
+ if p.is_a?(Array)
33
+ p
34
+ else
35
+ [p, @root]
36
+ end
37
+ end
14
38
  @client = Vault::Client.new(client_opts)
15
39
  end
16
40
 
17
- def add_path(path)
18
- @paths << path
41
+ # Add a path to Config source
42
+ #
43
+ # @example Use glob operators
44
+ # source.add_path('secrets/**/test/*')
45
+ # source.load #=> { secrets: { some_key: { test: { secret_data: 2 } } } }
46
+ #
47
+ # @param path [String]
48
+ # @param root [String] optional root
49
+ def add_path(path, root = nil)
50
+ root ||= @root
51
+ @paths << [path, root]
52
+ end
53
+
54
+ # Re-map individual key names
55
+ #
56
+ # @param hsh [Hash] mappings for keys
57
+ def map(hsh)
58
+ @map = hsh
19
59
  end
20
60
 
61
+ # Remove added paths
21
62
  def clear_paths
22
63
  @paths = []
23
64
  end
24
65
 
66
+ # Load data from source into hash
67
+ #
68
+ # @return [Hash]
25
69
  def load
26
- process_paths
70
+ Vault.with_retries(Vault::HTTPError,
71
+ attempts: @attempts,
72
+ base: @base,
73
+ max_wait: @max_wait) do
74
+ process_paths
75
+ end
27
76
  end
28
77
 
29
- def client
78
+ private
79
+
80
+ def client_ops
30
81
  unless kv.empty?
31
82
  @client.kv(@kv)
32
83
  else
@@ -34,8 +85,6 @@ module Config
34
85
  end
35
86
  end
36
87
 
37
- private
38
-
39
88
  def process_paths
40
89
  root = {}
41
90
  parsed_paths = @paths.map { |p| process_path(p) }
@@ -46,7 +95,7 @@ module Config
46
95
 
47
96
  def process_path(path)
48
97
  root = {}
49
- subpaths = path.split('/')
98
+ subpaths = path.first.split('/')
50
99
  stack = []
51
100
  stack.push([nil, 0, root])
52
101
 
@@ -54,13 +103,14 @@ module Config
54
103
  query_path, idx, parent = stack.pop
55
104
  sp = subpaths[idx]
56
105
  if sp.nil? || sp.eql?('*')
57
- data = client.read(query_path)&.data
58
- parent.merge!(data || {})
106
+ data = client_ops.read(query_path)&.data || {}
107
+ parent.merge!(data)
108
+ parent.transform_keys! { |key| @map[key] || key }
59
109
  parent.compact!
60
110
  end
61
111
 
62
112
  if sp.eql?('**') || sp.eql?('*')
63
- subtrees = client.list(query_path)
113
+ subtrees = client_ops.list(query_path)
64
114
  subtrees.each do |st|
65
115
  new_parent = {}
66
116
  new_key = st.split('/').last.downcase.to_sym
@@ -77,7 +127,8 @@ module Config
77
127
  end
78
128
  end
79
129
 
80
- if @root
130
+ root = root.flatten if @flatten
131
+ if path.last
81
132
  { @root => root }
82
133
  else
83
134
  root
@@ -1,5 +1,5 @@
1
- module Rubyconfig
1
+ module Config
2
2
  module Vault
3
- VERSION = "0.1.2"
3
+ VERSION = "1.0.2"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubyconfig-vault
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Young
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-04-02 00:00:00.000000000 Z
11
+ date: 2022-04-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: vault
@@ -96,6 +96,7 @@ metadata:
96
96
  allowed_push_host: https://rubygems.org
97
97
  homepage_uri: https://github.com/CrunchwrapSupreme/rubyconfig-vault
98
98
  source_code_uri: https://github.com/CrunchwrapSupreme/rubyconfig-vault
99
+ documentation_uri: https://www.rubydoc.info/gems/rubyconfig-vault/index
99
100
  post_install_message:
100
101
  rdoc_options: []
101
102
  require_paths: