1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
# frozen_string_literal: true
require 'aws-sigv4'
module Aws
module Plugins
# @api private
class Sign < Seahorse::Client::Plugin
# These once had defaults. But now they are used as overrides to
# new endpoint and auth resolution.
option(:sigv4_signer)
option(:sigv4_name)
option(:sigv4_region)
option(:unsigned_operations, default: [])
supported_auth_types = %w[sigv4 bearer none]
supported_auth_types += ['sigv4a'] if Aws::Sigv4::Signer.use_crt?
SUPPORTED_AUTH_TYPES = supported_auth_types.freeze
def add_handlers(handlers, cfg)
operations = cfg.api.operation_names - cfg.unsigned_operations
handlers.add(Handler, step: :sign, operations: operations)
end
# @api private
# Return a signer with the `sign(context)` method
def self.signer_for(auth_scheme, config, region_override = nil)
case auth_scheme['name']
when 'sigv4', 'sigv4a'
SignatureV4.new(auth_scheme, config, region_override)
when 'bearer'
Bearer.new
else
NullSigner.new
end
end
class Handler < Seahorse::Client::Handler
def call(context)
signer = Sign.signer_for(
context[:auth_scheme],
context.config,
context[:sigv4_region]
)
signer.sign(context)
@handler.call(context)
end
end
# @api private
class Bearer
def initialize
end
def sign(context)
if context.http_request.endpoint.scheme != 'https'
raise ArgumentError,
'Unable to use bearer authorization on non https endpoint.'
end
token_provider = context.config.token_provider
raise Errors::MissingBearerTokenError unless token_provider&.set?
context.http_request.headers['Authorization'] =
"Bearer #{token_provider.token.token}"
end
def presign_url(*args)
raise ArgumentError, 'Bearer auth does not support presigned urls'
end
def sign_event(*args)
raise ArgumentError, 'Bearer auth does not support event signing'
end
end
# @api private
class SignatureV4
def initialize(auth_scheme, config, region_override = nil)
scheme_name = auth_scheme['name']
unless %w[sigv4 sigv4a].include?(scheme_name)
raise ArgumentError,
"Expected sigv4 or sigv4a auth scheme, got #{scheme_name}"
end
region = if scheme_name == 'sigv4a'
auth_scheme['signingRegionSet'].first
else
auth_scheme['signingRegion']
end
begin
@signer = Aws::Sigv4::Signer.new(
service: config.sigv4_name || auth_scheme['signingName'],
region: region_override || config.sigv4_region || region,
credentials_provider: config.credentials,
signing_algorithm: scheme_name.to_sym,
uri_escape_path: !!!auth_scheme['disableDoubleEncoding'],
unsigned_headers: %w[content-length user-agent x-amzn-trace-id]
)
rescue Aws::Sigv4::Errors::MissingCredentialsError
raise Aws::Errors::MissingCredentialsError
end
end
def sign(context)
req = context.http_request
apply_authtype(context, req)
reset_signature(req)
apply_clock_skew(context, req)
# compute the signature
begin
signature = @signer.sign_request(
http_method: req.http_method,
url: req.endpoint,
headers: req.headers,
body: req.body
)
rescue Aws::Sigv4::Errors::MissingCredentialsError
# Necessary for when credentials is explicitly set to nil
raise Aws::Errors::MissingCredentialsError
end
# apply signature headers
req.headers.update(signature.headers)
# add request metadata with signature components for debugging
context[:canonical_request] = signature.canonical_request
context[:string_to_sign] = signature.string_to_sign
end
def presign_url(*args)
@signer.presign_url(*args)
end
def sign_event(*args)
@signer.sign_event(*args)
end
private
def apply_authtype(context, req)
if context.operation['authtype'].eql?('v4-unsigned-body') &&
req.endpoint.scheme.eql?('https')
req.headers['X-Amz-Content-Sha256'] ||= 'UNSIGNED-PAYLOAD'
end
end
def reset_signature(req)
# in case this request is being re-signed
req.headers.delete('Authorization')
req.headers.delete('X-Amz-Security-Token')
req.headers.delete('X-Amz-Date')
req.headers.delete('x-Amz-Region-Set')
end
def apply_clock_skew(context, req)
if context.config.respond_to?(:clock_skew) &&
context.config.clock_skew &&
context.config.correct_clock_skew
endpoint = context.http_request.endpoint
skew = context.config.clock_skew.clock_correction(endpoint)
if skew.abs.positive?
req.headers['X-Amz-Date'] =
(Time.now.utc + skew).strftime('%Y%m%dT%H%M%SZ')
end
end
end
end
# @api private
class NullSigner
def sign(context)
end
def presign_url(*args)
end
def sign_event(*args)
end
end
end
end
end
|