-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathretrieve_token.rb
46 lines (40 loc) · 1.38 KB
/
retrieve_token.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
require 'uri'
require 'json'
Puppet::Functions.create_function(:'influxdb::retrieve_token') do
dispatch :retrieve_token_file do
param 'String', :uri
param 'String', :token_name
param 'Stdlib::Absolutepath', :admin_token_file
end
dispatch :retrieve_token do
param 'String', :uri
param 'String', :token_name
param 'Sensitive', :admin_token
end
def retrieve_token_file(uri, token_name, admin_token_file)
admin_token = File.read(admin_token_file)
retrieve_token(uri, token_name, admin_token)
rescue Errno::EISDIR, Errno::EACCESS, Errno::ENOENT => e
Puppet.err("Unable to retrieve #{token_name}": e.message)
nil
end
def retrieve_token(uri, token_name, admin_token)
if admin_token.is_a?(Puppet::Pops::Types::PSensitiveType::Sensitive)
admin_token = admin_token.unwrap
end
client = Puppet.runtime[:http]
response = client.get(URI(uri + '/api/v2/authorizations'),
headers: { 'Authorization' => "Token #{admin_token}" })
if response.success?
body = JSON.parse(response.body)
token = body['authorizations'].find { |auth| auth['description'] == token_name }
token ? token['token'] : nil
else
Puppet.err("Unable to retrieve #{token_name}": response.body)
nil
end
rescue StandardError => e
Puppet.err("Unable to retrieve #{token_name}": e.message)
nil
end
end