-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvault2dotnev.rb
executable file
·60 lines (44 loc) · 1.21 KB
/
vault2dotnev.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env ruby
require 'optparse'
require 'vault'
require 'json'
options = {}
optparse = OptionParser.new do |opts|
opts.banner = "Usage: vault2dotenv.rb [options]"
opts.on('-k', '--key-value KEYVALUEPATH', 'Vault secret path') do |v|
options[:kv] = v
end
opts.on('-e', '--dot-env DOTFILE', '.env file') do |v|
options[:dot_file] = v
end
opts.on('-v', '--vault VAULTURL', 'Vault URL') do |v|
options[:vault] = v
end
opts.on('-u', '--vault-token VAULTTOKEN', 'Vault token') do |v|
options[:vault_token] = v
end
opts.on('--export', 'Export style .nev') do |v|
options[:export] = true
end
end
optparse.parse!
if options[:vault].nil? ||
options[:vault_token].nil? ||
options[:kv].nil?
puts "Missing one of required options"
abort(optparse.help)
end
Vault.configure do |config|
config.address = options[:vault] || ENV["VAULT_ADDR"]
config.token = options[:vault_token] || ENV["VAULT_TOKEN"]
end
vault_keys = Vault.logical.read(options[:kv])
lines = []
vault_keys.data[:data].each do |k,v|
lines << "#{ options[:export] ? 'export ' : '' }#{k}=\"#{v}\""
end
if options[:dot_file].nil?
pust lines.join("\n")
else
File.write(options[:dot_file], lines.join("\n"))
end