rubyconfig-vault 1.0.0 → 1.0.3

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: b468333af49d93bac42b51514158c5d3867604ca9aa7c64ac7a6ce02421821b9
4
- data.tar.gz: ea5200b58941e1dab0fdf9cc09fa7532fda3b054702994d0b28081b259be0a5f
3
+ metadata.gz: 0a737c714f6c9e6cdff7bc6ea5ad1cdb864929ea00552b49c0a711bf7985571e
4
+ data.tar.gz: 54d1b1055a1272b021c3b9411fefd41c2c6ee2e6ec2469fec1382e7becbd4502
5
5
  SHA512:
6
- metadata.gz: 7a06193b680915812e77b1632ca04c54aaedea10d99b3894f36737f97028f5321c2e844f5030746285b9bfea08e97c1ee9272764c97b896de546a8485a63c5e3
7
- data.tar.gz: 756beb031fcc04827054e8ddc8b3c8077cb86d9cd5488bed1021f0994bf3477717370650c23f65512e3b880fef9dd9b8d8dd29afa09babfddc79470b313ae46f
6
+ metadata.gz: d6810e8ed8954833fd7de46c76f1ff0102ab8240d7c25b9eed16eee421bd73703c1ae2b6ac75b1a5b36b25c5857e763ef7c73c8641a6aa305702b9716f3ba1ca
7
+ data.tar.gz: f4b0a4031bff1d623978064ddef59656b60ae9cf7071ee0a2e31665d7397b4c15ed63462a9a356b5ee7e82b2b671d2daf6a94b2969f24fb3c7e402f85e005cd6
@@ -4,21 +4,38 @@ module Config
4
4
  module Sources
5
5
  # A vault source for Config
6
6
  class VaultSource
7
- attr_accessor :kv, :root
7
+ attr_accessor :kv, :root, :flatten
8
8
  attr_reader :paths, :client
9
9
 
10
- # Create a new Config source
10
+ # Create a new Config source, all Vault::Client parameters supported
11
11
  #
12
12
  # @param [Hash] opts
13
13
  # @option opts [String, nil] :kv mount point for operations
14
14
  # @option opts [Array<String>, nil] :paths paths for vault secrets
15
- # @option opts [String, Symbol, nil] :root root key for data provided by source
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
16
20
  def initialize(opts = {})
17
21
  client_opts = opts.clone
18
22
  @kv = client_opts.delete(:kv) || ''
19
- @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
20
27
  @root = client_opts.delete(:root)
21
- @client = Vault::Client.new(client_opts)
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
38
+ @client = ::Vault::Client.new(client_opts)
22
39
  end
23
40
 
24
41
  # Add a path to Config source
@@ -28,8 +45,17 @@ module Config
28
45
  # source.load #=> { secrets: { some_key: { test: { secret_data: 2 } } } }
29
46
  #
30
47
  # @param path [String]
31
- def add_path(path)
32
- @paths << path
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
33
59
  end
34
60
 
35
61
  # Remove added paths
@@ -41,13 +67,17 @@ module Config
41
67
  #
42
68
  # @return [Hash]
43
69
  def load
44
- 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
45
76
  end
46
77
 
47
- # Client reference for quick operations
48
- #
49
- # @return [Vault::KV, Vault:Logical]
50
- def client
78
+ private
79
+
80
+ def client_ops
51
81
  unless kv.empty?
52
82
  @client.kv(@kv)
53
83
  else
@@ -55,8 +85,6 @@ module Config
55
85
  end
56
86
  end
57
87
 
58
- private
59
-
60
88
  def process_paths
61
89
  root = {}
62
90
  parsed_paths = @paths.map { |p| process_path(p) }
@@ -67,7 +95,7 @@ module Config
67
95
 
68
96
  def process_path(path)
69
97
  root = {}
70
- subpaths = path.split('/')
98
+ subpaths = path.first.split('/')
71
99
  stack = []
72
100
  stack.push([nil, 0, root])
73
101
 
@@ -75,30 +103,33 @@ module Config
75
103
  query_path, idx, parent = stack.pop
76
104
  sp = subpaths[idx]
77
105
  if sp.nil? || sp.eql?('*')
78
- data = client.read(query_path)&.data
79
- parent.merge!(data || {})
80
- parent.compact!
106
+ data = client_ops.read(query_path)&.data || {}
107
+ node = root if @flatten
108
+ node = parent unless @flatten
109
+ node.merge!(data)
110
+ node.transform_keys! { |key| @map[key] || key }
111
+ node.compact!
81
112
  end
82
113
 
83
114
  if sp.eql?('**') || sp.eql?('*')
84
- subtrees = client.list(query_path)
115
+ subtrees = client_ops.list(query_path)
85
116
  subtrees.each do |st|
86
117
  new_parent = {}
87
118
  new_key = st.split('/').last.downcase.to_sym
88
119
  new_query_path = [query_path, st].join('/')
89
- parent[new_key] = new_parent
120
+ parent[new_key] = new_parent unless @flatten
90
121
  stack.push([new_query_path, idx + 1, new_parent])
91
122
  end
92
123
  elsif sp
93
124
  query_path = [query_path, sp].compact.join('/')
94
125
  idx += 1
95
126
  new_parent = {}
96
- parent[sp.downcase.to_sym] = new_parent
127
+ parent[sp.downcase.to_sym] = new_parent unless @flatten
97
128
  stack.push([query_path, idx, new_parent])
98
129
  end
99
130
  end
100
131
 
101
- if @root
132
+ if path.last
102
133
  { @root => root }
103
134
  else
104
135
  root
@@ -1,5 +1,5 @@
1
1
  module Config
2
2
  module Vault
3
- VERSION = "1.0.0"
3
+ VERSION = "1.0.3"
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: 1.0.0
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Young
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-04-02 00:00:00.000000000 Z
11
+ date: 2022-04-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: vault
@@ -80,7 +80,7 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
- description:
83
+ description:
84
84
  email:
85
85
  - da.young@f5.com
86
86
  executables: []
@@ -97,7 +97,7 @@ metadata:
97
97
  homepage_uri: https://github.com/CrunchwrapSupreme/rubyconfig-vault
98
98
  source_code_uri: https://github.com/CrunchwrapSupreme/rubyconfig-vault
99
99
  documentation_uri: https://www.rubydoc.info/gems/rubyconfig-vault/index
100
- post_install_message:
100
+ post_install_message:
101
101
  rdoc_options: []
102
102
  require_paths:
103
103
  - lib
@@ -112,8 +112,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
112
112
  - !ruby/object:Gem::Version
113
113
  version: '0'
114
114
  requirements: []
115
- rubygems_version: 3.0.3.1
116
- signing_key:
115
+ rubygems_version: 3.1.2
116
+ signing_key:
117
117
  specification_version: 4
118
118
  summary: Implements a ruby config source from vault
119
119
  test_files: []