Class: Chef::HTTP::AuthCredentials

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_name = nil, key = nil, use_ssh_agent: false) ⇒ AuthCredentials

Returns a new instance of AuthCredentials.



35
36
37
38
39
# File 'lib/chef/http/auth_credentials.rb', line 35

def initialize(client_name = nil, key = nil, use_ssh_agent: false)
  @client_name = client_name
  @key = key
  @use_ssh_agent = use_ssh_agent
end

Instance Attribute Details

#client_nameObject (readonly)

Returns the value of attribute client_name.



33
34
35
# File 'lib/chef/http/auth_credentials.rb', line 33

def client_name
  @client_name
end

#keyObject (readonly)

Returns the value of attribute key.



33
34
35
# File 'lib/chef/http/auth_credentials.rb', line 33

def key
  @key
end

Instance Method Details

#sign_requests?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/chef/http/auth_credentials.rb', line 41

def sign_requests?
  !!key
end

#signature_headers(request_params = {}) ⇒ Object

Raises:

  • (ArgumentError)


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/chef/http/auth_credentials.rb', line 45

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.trace("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
  request_params[:proto_version] = Chef::Config[:authentication_protocol_version]
  host = request_params.delete(:host) || "localhost"

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