Sha256: 1bc7a676f82fd6356ee0a96aa24a066d02432b862860cf4ce145a30416d52342

Contents?: true

Size: 1.3 KB

Versions: 2

Compression:

Stored size: 1.3 KB

Contents

require 'mixlib/authentication/signedheaderauth'

module Ridley
  module Middleware
    # @author Jamie Winsor <jamie@vialstudios.com>
    class ChefAuth < Faraday::Middleware
      attr_reader :client_name
      attr_reader :client_key

      def initialize(app, client_name, client_key)
        super(app)
        @client_name = client_name
        @client_key = OpenSSL::PKey::RSA.new(File.read(client_key))
      end

      def call(env)
        sign_obj = Mixlib::Authentication::SignedHeaderAuth.signing_object(
          http_method: env[:method],
          host: env[:url].host,
          path: env[:url].path,
          body: env[:body] || '',
          timestamp: Time.now.utc.iso8601,
          user_id: client_name
        )
        authentication_headers = sign_obj.sign(client_key)
        env[:request_headers] = env[:request_headers].merge(authentication_headers).merge(default_headers)
        env[:request_headers] = env[:request_headers].merge('Content-Length' => env[:body].bytesize.to_s) if env[:body]

        Ridley.log.debug(env)

        @app.call(env)
      end

      private

        def default_headers
          {
            'Accept' => 'application/json',
            'Content-Type' => 'application/json',
            'X-Chef-Version' => Ridley::CHEF_VERSION
          }
        end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ridley-0.0.2 lib/ridley/middleware/chef_auth.rb
ridley-0.0.1 lib/ridley/middleware/chef_auth.rb