Class: Chef::REST::AuthCredentials

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_name = nil, key_file = nil) ⇒ AuthCredentials

Returns a new instance of AuthCredentials.



31
32
33
34
# File 'lib/chef/rest/auth_credentials.rb', line 31

def initialize(client_name=nil, key_file=nil)
  @client_name, @key_file = client_name, key_file
  load_signing_key if sign_requests?
end

Instance Attribute Details

#client_nameObject (readonly)

Returns the value of attribute client_name.



29
30
31
# File 'lib/chef/rest/auth_credentials.rb', line 29

def client_name
  @client_name
end

#keyObject (readonly)

Returns the value of attribute key.



29
30
31
# File 'lib/chef/rest/auth_credentials.rb', line 29

def key
  @key
end

#key_fileObject (readonly)

Returns the value of attribute key_file.



29
30
31
# File 'lib/chef/rest/auth_credentials.rb', line 29

def key_file
  @key_file
end

#raw_keyObject (readonly)

Returns the value of attribute raw_key.



29
30
31
# File 'lib/chef/rest/auth_credentials.rb', line 29

def raw_key
  @raw_key
end

Instance Method Details

#sign_requests?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/chef/rest/auth_credentials.rb', line 36

def sign_requests?
  !!key_file
end

#signature_headers(request_params = {}) ⇒ Object

Raises:

  • (ArgumentError)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/chef/rest/auth_credentials.rb', line 40

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?
  Chef::Log.debug("Signing the request as #{client_name}")

  # 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