Class: Ridley::Middleware::ChefAuth
- Inherits:
-
Faraday::Middleware
- Object
- Faraday::Middleware
- Ridley::Middleware::ChefAuth
- Extended by:
- Mixlib::Authentication
- Includes:
- Logging
- Defined in:
- lib/ridley/middleware/chef_auth.rb
Instance Attribute Summary collapse
-
#client_key ⇒ Object
readonly
Returns the value of attribute client_key.
-
#client_name ⇒ Object
readonly
Returns the value of attribute client_name.
Class Method Summary collapse
-
.authentication_headers(client_name, client_key, options = {}) ⇒ Object
Generate authentication headers for a request to a Chef Server.
-
.signing_object(client_name, options = {}) ⇒ SigningObject
Create a signing object for a Request to a Chef Server.
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, client_name, client_key) ⇒ ChefAuth
constructor
A new instance of ChefAuth.
Methods included from Logging
Constructor Details
#initialize(app, client_name, client_key) ⇒ ChefAuth
Returns a new instance of ChefAuth.
53 54 55 56 57 |
# File 'lib/ridley/middleware/chef_auth.rb', line 53 def initialize(app, client_name, client_key) super(app) @client_name = client_name @client_key = client_key end |
Instance Attribute Details
#client_key ⇒ Object (readonly)
Returns the value of attribute client_key.
51 52 53 |
# File 'lib/ridley/middleware/chef_auth.rb', line 51 def client_key @client_key end |
#client_name ⇒ Object (readonly)
Returns the value of attribute client_name.
50 51 52 |
# File 'lib/ridley/middleware/chef_auth.rb', line 50 def client_name @client_name end |
Class Method Details
.authentication_headers(client_name, client_key, options = {}) ⇒ Object
Generate authentication headers for a request to a Chef Server
18 19 20 21 22 23 24 |
# File 'lib/ridley/middleware/chef_auth.rb', line 18 def authentication_headers(client_name, client_key, = {}) contents = File.exists?(client_key) ? File.read(client_key) : client_key.to_s rsa_key = OpenSSL::PKey::RSA.new(contents) headers = signing_object(client_name, ).sign(rsa_key).merge(host: [:host]) headers.inject({}) { |memo, kv| memo["#{kv[0].to_s.upcase}"] = kv[1];memo } end |
.signing_object(client_name, options = {}) ⇒ SigningObject
Create a signing object for a Request to a Chef Server
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/ridley/middleware/chef_auth.rb', line 36 def signing_object(client_name, = {}) = .reverse_merge( body: String.new, timestamp: Time.now.utc.iso8601 ) [:user_id] = client_name [:proto_version] = "1.0" SignedHeaderAuth.signing_object() end |
Instance Method Details
#call(env) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/ridley/middleware/chef_auth.rb', line 59 def call(env) = { http_method: env[:method], host: "#{env[:url].host}:#{env[:url].port}", path: env[:url].path, body: env[:body] || '' } authentication_headers = self.class.authentication_headers(client_name, client_key, ) env[:request_headers] = default_headers.merge(env[:request_headers]).merge(authentication_headers) env[:request_headers] = env[:request_headers].merge('Content-Length' => env[:body].bytesize.to_s) if env[:body] log.debug { "==> performing authenticated Chef request as '#{client_name}'"} log.debug { "request env: #{env}"} @app.call(env) end |