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
|
# frozen_string_literal: true
require 'uri'
require 'openssl'
module Aws
module S3
module Plugins
class SseCpk < Seahorse::Client::Plugin
option(:require_https_for_sse_cpk,
default: true,
doc_type: 'Boolean',
docstring: <<-DOCS)
When `true`, the endpoint **must** be HTTPS for all operations
where server-side-encryption is used with customer-provided keys.
This should only be disabled for local testing.
DOCS
class Handler < Seahorse::Client::Handler
def call(context)
compute_key_md5(context) if context.params.is_a?(Hash)
@handler.call(context)
end
private
def compute_key_md5(context)
params = context.params
if key = params[:sse_customer_key]
require_https(context)
params[:sse_customer_key] = base64(key)
params[:sse_customer_key_md5] = base64(md5(key))
end
if key = params[:copy_source_sse_customer_key]
require_https(context)
params[:copy_source_sse_customer_key] = base64(key)
params[:copy_source_sse_customer_key_md5] = base64(md5(key))
end
end
def require_https(context)
unless URI::HTTPS === context.config.endpoint
msg = <<-MSG.strip.gsub("\n", ' ')
Attempting to send customer-provided-keys for S3
server-side-encryption over HTTP; Please configure a HTTPS
endpoint. If you are attempting to use a test endpoint,
you can disable this check via `:require_https_for_sse_cpk`
MSG
raise ArgumentError, msg
end
end
def md5(str)
OpenSSL::Digest::MD5.digest(str)
end
def base64(str)
Base64.encode64(str).strip
end
end
handler(Handler, step: :initialize)
end
end
end
end
|