Class: Chef::Readbag::AuthCredentials

Inherits:
Object
  • Object
show all
Defined in:
lib/chef-readbag/auth_credentials.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_name, key_file_path) ⇒ AuthCredentials

Returns a new instance of AuthCredentials.



8
9
10
11
12
13
# File 'lib/chef-readbag/auth_credentials.rb', line 8

def initialize(client_name, key_file_path)
  @client_name = client_name
  @key_file    = File.expand_path(key_file_path)

  load_signing_key if sign_requests?
end

Instance Attribute Details

#client_nameObject (readonly)

Returns the value of attribute client_name.



6
7
8
# File 'lib/chef-readbag/auth_credentials.rb', line 6

def client_name
  @client_name
end

#keyObject (readonly)

Returns the value of attribute key.



6
7
8
# File 'lib/chef-readbag/auth_credentials.rb', line 6

def key
  @key
end

#key_fileObject (readonly)

Returns the value of attribute key_file.



6
7
8
# File 'lib/chef-readbag/auth_credentials.rb', line 6

def key_file
  @key_file
end

#raw_keyObject (readonly)

Returns the value of attribute raw_key.



6
7
8
# File 'lib/chef-readbag/auth_credentials.rb', line 6

def raw_key
  @raw_key
end

Instance Method Details

#sign_requests?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/chef-readbag/auth_credentials.rb', line 15

def sign_requests?
  !!key_file
end

#signature_headers(request_params = {}) ⇒ Object

Raises:

  • (ArgumentError)


19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/chef-readbag/auth_credentials.rb', line 19

def signature_headers(request_params={})
  raise ArgumentError, "Cannot sign the request without a client name, check that :node_name is assigned" if client_name.nil?

  # params_in = {:http_method => :GET, :path => "/clients", :body => "", :host => "localhost"}
  request_params             = request_params.dup
  request_params[:timestamp] = Time.now.utc.iso8601
  request_params[:user_id]   = @client_name
  host = request_params.delete(:host) || "localhost"

  sign_obj = Mixlib::Authentication::SignedHeaderAuth.signing_object(request_params)
  signed =  sign_obj.sign(key).merge({:host => host})
  signed.inject({}){|memo, kv| memo["#{kv[0].to_s.upcase}"] = kv[1];memo}
end