-
-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathruby.rb
More file actions
313 lines (270 loc) · 9.58 KB
/
ruby.rb
File metadata and controls
313 lines (270 loc) · 9.58 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# frozen_string_literal: true
Puppet::Type.type(:elastic_stack_keystore).provide(
:elastic_stack_keystore
) do
desc 'Provider for both `elasticsearch-keystore` and `kibana-keystore` based secret management.'
mk_resource_methods
def self.defaults_dir
@defaults_dir ||= case Facter.value(:os)['family']
when 'RedHat'
'/etc/sysconfig'
else
'/etc/default'
end
end
def self.root_dir
@root_dir ||= case Facter.value(:os)['family']
when 'OpenBSD'
'/usr/local'
else
'/usr/share'
end
end
def self.home_dir_kibana
@home_dir_kibana ||= File.join(root_dir, 'kibana')
end
def self.home_dir_elasticsearch
@home_dir_elasticsearch ||= File.join(root_dir, 'elasticsearch')
end
def self.elastic_keystore_password_file
keystore_env = get_envvar('elasticsearch', 'ES_KEYSTORE_PASSPHRASE_FILE')
@elastic_keystore_password_file ||= keystore_env.empty? ? "#{configdir('elasticsearch')}/.elasticsearch-keystore-password" : keystore_env
end
def self.elastic_keystore_password(password = '')
if File.file?(elastic_keystore_password_file)
@elastic_keystore_password ||= File.open(elastic_keystore_password_file, &:readline).strip
else
@elastic_keystore_password = password.empty? ? @elastic_keystore_password : password
end
end
def self.elastic_keystore_password_file_bak
@elastic_keystore_password_file_bak ||= "#{elastic_keystore_password_file}.puppet-bak"
end
def self.elastic_keystore_password_bak
@elastic_keystore_password_bak ||= File.file?(elastic_keystore_password_file_bak) ? File.open(elastic_keystore_password_file_bak, &:readline).strip : ''
end
attr_accessor :defaults_dir, :root_dir, :home_dir_kibana, :home_dir_elasticsearch, :elastic_keystore_password_file, :elastic_keystore_password, :elastic_keystore_password_file_bak, :elastic_keystore_password_bak
optional_commands kibana_keystore: "#{home_dir_kibana}/bin/kibana-keystore"
optional_commands elasticsearch_keystore: "#{home_dir_elasticsearch}/bin/elasticsearch-keystore"
def self.run_keystore(args, service, stdin = nil)
options = {
uid: service.to_s,
gid: service.to_s,
failonfail: true
}
password = case service
when 'elasticsearch'
File.file?(elastic_keystore_password_file_bak) ? elastic_keystore_password_bak : elastic_keystore_password
else
''
end
cmd = [command("#{service}_keystore")]
if args[0] == 'create' || args[0] == 'has-passwd'
options[:failonfail] = false
options[:combine] = true
elsif args[0] == 'passwd'
options[:combine] = true
stdin = File.file?(elastic_keystore_password_file_bak) ? "#{elastic_keystore_password_bak}\n#{elastic_keystore_password}\n#{elastic_keystore_password}" : "#{elastic_keystore_password}\n#{elastic_keystore_password}"
end
unless args[0] == 'passwd' || args[0] == 'has-passwd'
stdin = stdin.nil? ? password : "#{password}\n#{stdin}"
end
unless stdin.nil?
stdinfile = Tempfile.new("#{service}-keystore")
stdinfile << stdin
stdinfile.flush
options[:stdinfile] = stdinfile.path
end
begin
stdout = execute(cmd + args, options)
ensure
unless stdin.nil?
stdinfile.close
stdinfile.unlink
end
end
if stdout.exitstatus.zero?
stdout
else
options[:failonfail] ? raise(Puppet::Error, stdout) : stdout
end
end
def self.present_keystores(configdir, service, password = '')
keystore_file = File.join(configdir, "#{service}.keystore")
if File.file?(keystore_file)
current_password = case service
when 'elasticsearch'
if passwd?(service) && File.file?(elastic_keystore_password_file_bak)
elastic_keystore_password_bak
elsif passwd?(service)
elastic_keystore_password(password.value)
else
elastic_keystore_password(password.value)
''
end
else
''
end
settings = {}
run_keystore(['list'], service).split("\n").each do |setting|
settings[setting] = service == 'kibana' ? '' : run_keystore(['show', setting], service)
end
[{
name: service,
ensure: :present,
provider: name,
settings: settings,
password: current_password,
}]
else
[]
end
end
def self.configdir(service)
dir = get_envvar(service, '(ES|KBN)_PATH_CONF')
if dir.empty?
File.join('/etc', service)
else
dir
end
end
def self.get_envvar(service, env)
defaults_file = File.join(defaults_dir, service)
val = ''
if File.file?(defaults_file)
File.readlines(defaults_file).each do |line|
next if line =~ %r{^#}
key, value = line.split '='
val = value.gsub(%r{"}, '').strip if key =~ %r{#{env}}
end
end
val
end
def self.instances(password = '')
keystores = []
%w[kibana elasticsearch].each do |service|
keystores.concat(present_keystores(configdir(service), service, password))
end
keystores.map do |keystore|
new keystore
end
end
def self.passwd?(service)
has_passwd = run_keystore(['has-passwd'], service).split("\n").last
has_passwd.match?(%r{^Keystore is password-protected})
end
def self.keystore_password_management(service)
if passwd?(service)
run_keystore(['passwd'], service) unless elastic_keystore_password_bak.strip.empty? || elastic_keystore_password == elastic_keystore_password_bak
else
run_keystore(['passwd'], service) unless elastic_keystore_password.empty?
end
end
def self.prefetch(resources)
password = resources.key?(:elasticsearch) ? resources[:elasticsearch].parameters[:password] : ''
keystores = instances(password)
resources.each_key do |name|
provider = keystores.find { |keystore| keystore.name.to_sym == name }
resources[name].provider = provider if provider
end
end
def initialize(value = {})
super(value)
@property_flush = {}
end
def flush
configdir = self.class.configdir(resource[:service].to_s)
service = resource[:service].to_s
case @property_flush[:ensure]
when :present
debug(self.class.run_keystore(['create', '-s'], service, 'N'))
@property_flush[:settings] = resource[:settings]
when :absent
File.delete(File.join([
configdir, "#{resource[:service]}.keystore"
]))
return
end
# Note that since the property is :array_matching => :all, we have to
# expect that the hash is wrapped in an array.
if @property_flush.key?(:settings) && !(@property_flush[:settings].empty? && @property_hash.nil? && @property_hash[:settings].nil?)
# Flush properties that _should_ be present
@property_flush[:settings].each do |setting, value|
next if @property_hash.key?(:settings) && @property_hash[:settings].key?(setting) \
&& @property_hash[:settings][setting] == value
args = ['add', '--force']
args << '--stdin' if service == 'kibana'
args << setting
debug(self.class.run_keystore(args, service, value))
end
# Remove properties that are no longer present
if resource[:purge]
(@property_hash[:settings].keys.sort - @property_flush[:settings].keys.sort).each do |setting|
debug(self.class.run_keystore(
['remove', setting], service
))
end
end
end
keystore_settings = {}
self.class.run_keystore(['list'], service).split("\n").each do |setting|
keystore_settings[setting] = service == 'kibana' ? '' : self.class.run_keystore(['show', setting], service)
end
# if service == 'elasticsearch' && @property_flush.key?(:password)
if service == 'elasticsearch'
# set and update keystore password if needed
self.class.keystore_password_management(service)
# unlink backup file containing keystore password (synced)
File.unlink(self.class.elastic_keystore_password_file_bak) if File.file?(self.class.elastic_keystore_password_file_bak)
end
@property_hash = {
name: service,
ensure: :present,
provider: resource[:name],
settings: keystore_settings,
password: self.class.elastic_keystore_password,
}
end
# settings property setter
#
# @return [Hash] settings
def settings=(new_settings)
@property_flush[:settings] = new_settings
end
# settings property getter
#
# @return [Hash] settings
def settings
@property_hash[:settings]
end
# settings property setter
#
# @return [String] password
def password=(new_password)
@property_flush[:password] = new_password
end
# settings property getter
#
# @return [Hash] password
def password
@property_hash[:password]
end
# Sets the ensure property in the @property_flush hash.
#
# @return [Symbol] :present
def create
@property_flush[:ensure] = :present
end
# Determine whether this resource is present on the system.
#
# @return [Boolean]
def exists?
@property_hash[:ensure] == :present
end
# Set flushed ensure property to absent.
#
# @return [Symbol] :absent
def destroy
@property_flush[:ensure] = :absent
end
end