-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgitlab2vault.rb
executable file
·90 lines (69 loc) · 2.11 KB
/
gitlab2vault.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env ruby
require 'gitlab'
require 'optparse'
require 'vault'
require 'json'
options = {}
optparse = OptionParser.new do |opts|
opts.banner = "Usage: gitlab2vault.rb [options]"
opts.on('-p', '--project PROJECTID', 'Gitlab project') do |v|
options[:project] = v
end
opts.on('-r', '--group GROUPID', 'Gitlab group') do |v|
options[:group] = v
end
opts.on('-x', '--old-prefix OLDPREFIX', 'Old prefix') do |v|
options[:old_prefix] = v
end
opts.on('-y', '--new-prefix NEWPREFIX', 'New prefix') do |v|
options[:new_prefix] = v
end
opts.on('-k', '--key-value KEYVALUEPATH', 'Vault secret path') do |v|
options[:kv] = v
end
opts.on('-g', '--gitlab GITLABURL', 'Gitlab API URL') do |v|
options[:gitlab] = v
end
opts.on('-q', '--gitlab-token GITLABTOKEN', 'Gitlab token') do |v|
options[:gitlab_token] = 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
end
optparse.parse!
if options[:project].nil? && options[:group].nil?
puts "Missing group or project"
abort(optparse.help)
elsif options[:gitlab].nil? ||
options[:gitlab_token].nil? ||
options[:vault].nil? ||
options[:vault_token].nil? ||
options[:kv].nil?
puts "Missing one of required options"
abort(optparse.help)
end
Gitlab.configure do |config|
config.endpoint = options[:gitlab] || ENV['GITLAB_ADDR']
config.private_token = options[:gitlab_token] || ENV['GITLAB_TOKEN']
end
Vault.configure do |config|
config.address = options[:vault] || ENV["VAULT_ADDR"]
config.token = options[:vault_token] || ENV["VAULT_TOKEN"]
end
if options[:project]
vars = Gitlab.variables(options[:project])
elsif options[:group]
vars = Gitlab.group_variables(options[:group])
end
vault_keys = {}
vars.auto_paginate do |var|
vault_keys[var.key.gsub(options[:old_prefix],options[:new_prefix])] = var.value if var.key.start_with?(options[:old_prefix])
end
vault_keys.each do |k,v|
puts "#{k} -> #{v}"
end
Vault.logical.write(options[:kv], data: vault_keys)