Class: ConsulACLTokenClient

Inherits:
PuppetX::Consul::ACLBase::BaseClient
  • Object
show all
Defined in:
lib/puppet/provider/consul_token/default.rb

Instance Method Summary collapse

Instance Method Details

#create_token(accessor_id, description, policies_by_name, policies_by_id, tries, secret_id = nil) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
# File 'lib/puppet/provider/consul_token/default.rb', line 142

def create_token(accessor_id, description, policies_by_name, policies_by_id, tries, secret_id = nil)
  begin
    body = encode_body(accessor_id, description, policies_by_name, policies_by_id, secret_id)
    response = put('/token', body, tries)
  rescue StandardError => e
    Puppet.warning("Unable to create token #{description}: #{e.message}")
    return nil
  end

  ConsulToken.new(response['AccessorID'], response['SecretID'], description, parse_policies(response['Policies']))
end

#delete_token(accessor_id) ⇒ Object



166
167
168
169
170
171
172
173
# File 'lib/puppet/provider/consul_token/default.rb', line 166

def delete_token(accessor_id)
  response = delete('/token/' + accessor_id)

  raise 'Consul API returned false as response' if response == 'false'
rescue StandardError => e
  Puppet.warning("Unable to delete token #{accessor_id}: #{e.message}")
  nil
end

#encode_body(accessor_id, description, policies_by_name, policies_by_id, secret_id = nil) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/puppet/provider/consul_token/default.rb', line 186

def encode_body(accessor_id, description, policies_by_name, policies_by_id, secret_id = nil)
  policies = []
  policies_by_name.each do |name|
    policies.push({ 'Name' => name })
  end

  policies_by_id.each do |id|
    policies.push({ 'ID' => id })
  end

  body = {}
  body.store('AccessorID', accessor_id)
  body.store('Description', description)
  body.store('Local', false)
  body.store('Policies', policies)

  body.store('SecretID', secret_id) if !secret_id.nil? && !secret_id.to_s.strip.empty?

  body
end

#get_token_list(tries) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/puppet/provider/consul_token/default.rb', line 126

def get_token_list(tries)
  begin
    response = get('/tokens', tries)
  rescue StandardError => e
    Puppet.warning("Cannot retrieve ACL token list: #{e.message}")
    response = {}
  end

  collection = []
  response.each do |item|
    collection.push(ConsulToken.new(item['AccessorID'], item['SecretID'], item['Description'], parse_policies(item['Policies'])))
  end

  collection
end

#parse_policies(response) ⇒ Object



175
176
177
178
179
180
181
182
183
184
# File 'lib/puppet/provider/consul_token/default.rb', line 175

def parse_policies(response)
  return [] unless response

  policy_links = []
  response.each do |policy|
    policy_links.push(ConsulTokenPolicyLink.new(policy['ID'], policy['Name']))
  end

  policy_links
end

#update_token(accessor_id, description, policies_by_name, policies_by_id) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
# File 'lib/puppet/provider/consul_token/default.rb', line 154

def update_token(accessor_id, description, policies_by_name, policies_by_id)
  begin
    body = encode_body(accessor_id, description, policies_by_name, policies_by_id, nil)
    response = put('/token/' + accessor_id, body)
  rescue StandardError => e
    Puppet.warning("Unable to update token #{description} (Accessor ID: #{accessor_id}): #{e.message}")
    return nil
  end

  parse_policies(response['Policies'])
end