-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathpe_ldap_config.rb
executable file
·63 lines (53 loc) · 1.55 KB
/
pe_ldap_config.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
#!/opt/puppetlabs/puppet/bin/ruby
# Puppet Task Name: pe_ldap_config
#
# Update the LDAP configuration
#
require 'json'
require 'net/http'
require 'open3'
def main
params = JSON.parse(STDIN.read)
data = params['ldap_config']
pe_main = params['pe_main']
caf = ['/opt/puppetlabs/bin/puppet', 'config', 'print', 'localcacert']
cafout, cafstatus = Open3.capture2(*caf)
unless cafstatus.success?
raise 'Could not get the CA file path.'
end
cert = ['/opt/puppetlabs/bin/puppet', 'config', 'print', 'hostcert']
certout, certstatus = Open3.capture2(*cert)
unless certstatus.success?
raise 'Could not get the Cert file path.'
end
key = ['/opt/puppetlabs/bin/puppet', 'config', 'print', 'hostprivkey']
keyout, keystatus = Open3.capture2(*key)
unless keystatus.success?
raise 'Could not get the Key file path.'
end
uri = URI("https://#{pe_main}:4433/rbac-api/v1/ds")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
http.ca_file = cafout.strip
http.cert = OpenSSL::X509::Certificate.new(File.read(certout.strip))
http.key = OpenSSL::PKey::RSA.new(File.read(keyout.strip))
req = Net::HTTP::Put.new(uri, 'Content-type' => 'application/json')
req.body = data.to_json
resp = http.request(req)
puts resp.body
raise "API response code #{resp.code}" unless resp.code == '200'
end
begin
main
rescue => e
result = {
'_error' => {
'msg' => e.message,
'kind' => 'RuntimeError',
'details' => e.message,
}
}
puts result.to_json
exit(1)
end