-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRakefile
More file actions
185 lines (161 loc) · 5.82 KB
/
Rakefile
File metadata and controls
185 lines (161 loc) · 5.82 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# frozen_string_literal: true
require 'bundler'
require 'base64'
require 'json'
Bundler.require
require 'fileutils'
require './lib/compiler/generate_factory'
def available_templates(files)
{}.tap do |templates|
files.each do |file|
classification, name, version = File.dirname(file).split('/')[-3..-1]
templates[classification]
templates[classification] ||= []
templates[classification] << "#{version}/#{name.underscore}"
end
end
end
def indent_line(spaces:, line:)
"#{' ' * spaces}#{line}\n"
end
def generate_policy_block(id:, indent:)
[].tap do |response|
response << indent_line(spaces: indent, line: '- !policy')
response << indent_line(spaces: indent + 2, line: "id: #{id}")
response << indent_line(spaces: indent + 2, line: 'body:')
end.join('')
end
# policy_path: conjur/factories
# templates: { 'core' => ['v1/user', 'v1/group'] }
def generate_base_policy(policy_path:, templates:)
indent = 0
[].tap do |response|
policy_path.split('/').each_with_index do |part, index|
indent = 2 * index
response << generate_policy_block(id: part, indent: indent)
end
indent += 2
templates.each do |template, factories|
response << generate_policy_block(id: template, indent: indent)
factories.each do |factory|
response << indent_line(spaces: indent + 2, line: "- !variable #{factory}")
end
end
end.join
end
def api_key
return ENV['API_KEY'] if ENV['API_KEY'].present?
return Conjur::API.login(username, ENV['PASSWORD']).to_s if ENV.key?('PASSWORD')
raise "Conjur `#{username}` user must include either:\n\n - An API key (via `API_KEY` environment variable)\n\n - A password (via `PASSWORD` environment variable)"
end
def username
ENV.fetch('USERNAME', 'admin')
end
def client
@client ||= begin
Conjur.configuration.rest_client_options = {
verify_ssl: false
}
Conjur.configuration.account = account
Conjur.configuration.appliance_url = ENV.fetch('CONJUR_URL', 'https://localhost')
if ENV['CONJUR_AUTH_TOKEN'].present?
Conjur::API.new_from_token(JSON.parse(Base64.decode64(ENV['CONJUR_AUTH_TOKEN'])))
else
Conjur::API.new_from_key(username, api_key)
end
end
end
def account
ENV.fetch('ACCOUNT', 'cucumber')
end
def create_factory(classification:, version:, name:)
version = "v#{version.gsub(/\D/, '')}"
target_directory = "factories/custom/#{classification.underscore}/#{name.underscore}/#{version}"
FileUtils.mkdir_p(target_directory)
if File.exist?("#{target_directory}/policy.yml")
puts "File already exists: '#{target_directory}/policy.yml'"
else
File.open("#{target_directory}/policy.yml", 'w') do |file|
file.write("# Place relevant Conjur Policy here.\n")
end
end
if File.exist?("#{target_directory}/config.json")
puts "File already exists: '#{target_directory}/policy.yml'"
else
File.open("#{target_directory}/config.json", 'w') do |file|
file.write(
JSON.pretty_generate(
{
title: '',
description: '',
variables: {
'variable-1': { required: true, description: '' },
'variable-2': { description: '' }
}
}
)
)
end
end
puts "Factory stubs generated in: '#{target_directory}'"
end
namespace :policy_factory do
task :create, [:classification, :version, :name] do |_, args|
create_factory(
classification: args[:classification],
version: args[:version],
name: args[:name]
)
end
task :inspect, [:path] do |_, args|
factory_file_path = args[:path]
classification, name, version = args[:path].split('/').last(3)
compiled_factory = Compiler::GenerateFactory.new(
classification: classification,
version: version,
name: name
).generate(
policy_template: File.exist?("#{factory_file_path}/policy.yml") ? File.read("#{factory_file_path}/policy.yml") : nil,
configuration: JSON.parse(File.read("#{factory_file_path}/config.json"))
)
factory = JSON.parse(Base64.decode64(compiled_factory))
puts 'Factory Schema:'
puts JSON.pretty_generate(factory)
puts
puts 'Factory Policy:'
puts Base64.decode64(factory['policy'])
end
task :load do
target_policy = ENV.fetch('TARGET_POLICY', 'conjur/factories')
template_folder = ENV.fetch('TEMPLATE_FOLDER', 'default')
templates = available_templates(Dir["#{Dir.pwd}/factories/#{template_folder}/**/*.json"])
if templates.empty?
puts "It looks like there are no templates in 'factories/#{template_folder}'"
exit
end
puts "Loading templates from 'factories/#{template_folder}'\n\n"
puts "Generated Base Template:"
puts generate_base_policy(policy_path: target_policy, templates: templates)
client.load_policy('root', generate_base_policy(policy_path: target_policy, templates: templates))
templates.each do |classification, factories|
factories.each do |factory_version|
version, factory = factory_version.split('/')
factory_file_path = "factories/#{template_folder}/#{classification}/#{factory}/#{version}"
puts " loading template from: '#{factory_file_path}'"
policy_template = File.exist?("#{factory_file_path}/policy.yml") ? File.read("#{factory_file_path}/policy.yml") : nil
compiled_factory = Compiler::GenerateFactory.new(
name: factory,
version: version,
classification: classification
).generate(
policy_template: policy_template,
configuration: JSON.parse(File.read("#{factory_file_path}/config.json"))
)
puts " compiled factory: '#{compiled_factory}'"
client.resource(
"#{account}:variable:#{target_policy}/#{classification}/#{version}/#{factory}"
).add_value(compiled_factory)
end
end
end
end