aws-sdk-s3 1.75.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/aws-sdk-s3.rb +73 -0
- data/lib/aws-sdk-s3/bucket.rb +861 -0
- data/lib/aws-sdk-s3/bucket_acl.rb +277 -0
- data/lib/aws-sdk-s3/bucket_cors.rb +262 -0
- data/lib/aws-sdk-s3/bucket_lifecycle.rb +264 -0
- data/lib/aws-sdk-s3/bucket_lifecycle_configuration.rb +283 -0
- data/lib/aws-sdk-s3/bucket_logging.rb +251 -0
- data/lib/aws-sdk-s3/bucket_notification.rb +293 -0
- data/lib/aws-sdk-s3/bucket_policy.rb +242 -0
- data/lib/aws-sdk-s3/bucket_region_cache.rb +81 -0
- data/lib/aws-sdk-s3/bucket_request_payment.rb +236 -0
- data/lib/aws-sdk-s3/bucket_tagging.rb +251 -0
- data/lib/aws-sdk-s3/bucket_versioning.rb +312 -0
- data/lib/aws-sdk-s3/bucket_website.rb +292 -0
- data/lib/aws-sdk-s3/client.rb +11818 -0
- data/lib/aws-sdk-s3/client_api.rb +3014 -0
- data/lib/aws-sdk-s3/customizations.rb +34 -0
- data/lib/aws-sdk-s3/customizations/bucket.rb +162 -0
- data/lib/aws-sdk-s3/customizations/multipart_upload.rb +44 -0
- data/lib/aws-sdk-s3/customizations/object.rb +389 -0
- data/lib/aws-sdk-s3/customizations/object_summary.rb +85 -0
- data/lib/aws-sdk-s3/customizations/types/list_object_versions_output.rb +13 -0
- data/lib/aws-sdk-s3/encryption.rb +21 -0
- data/lib/aws-sdk-s3/encryption/client.rb +375 -0
- data/lib/aws-sdk-s3/encryption/decrypt_handler.rb +190 -0
- data/lib/aws-sdk-s3/encryption/default_cipher_provider.rb +65 -0
- data/lib/aws-sdk-s3/encryption/default_key_provider.rb +40 -0
- data/lib/aws-sdk-s3/encryption/encrypt_handler.rb +61 -0
- data/lib/aws-sdk-s3/encryption/errors.rb +15 -0
- data/lib/aws-sdk-s3/encryption/io_auth_decrypter.rb +58 -0
- data/lib/aws-sdk-s3/encryption/io_decrypter.rb +36 -0
- data/lib/aws-sdk-s3/encryption/io_encrypter.rb +71 -0
- data/lib/aws-sdk-s3/encryption/key_provider.rb +31 -0
- data/lib/aws-sdk-s3/encryption/kms_cipher_provider.rb +75 -0
- data/lib/aws-sdk-s3/encryption/materials.rb +60 -0
- data/lib/aws-sdk-s3/encryption/utils.rb +81 -0
- data/lib/aws-sdk-s3/encryptionV2/client.rb +388 -0
- data/lib/aws-sdk-s3/encryptionV2/decrypt_handler.rb +198 -0
- data/lib/aws-sdk-s3/encryptionV2/default_cipher_provider.rb +103 -0
- data/lib/aws-sdk-s3/encryptionV2/default_key_provider.rb +38 -0
- data/lib/aws-sdk-s3/encryptionV2/encrypt_handler.rb +66 -0
- data/lib/aws-sdk-s3/encryptionV2/errors.rb +13 -0
- data/lib/aws-sdk-s3/encryptionV2/io_auth_decrypter.rb +56 -0
- data/lib/aws-sdk-s3/encryptionV2/io_decrypter.rb +35 -0
- data/lib/aws-sdk-s3/encryptionV2/io_encrypter.rb +71 -0
- data/lib/aws-sdk-s3/encryptionV2/key_provider.rb +29 -0
- data/lib/aws-sdk-s3/encryptionV2/kms_cipher_provider.rb +99 -0
- data/lib/aws-sdk-s3/encryptionV2/materials.rb +58 -0
- data/lib/aws-sdk-s3/encryptionV2/utils.rb +116 -0
- data/lib/aws-sdk-s3/encryption_v2.rb +20 -0
- data/lib/aws-sdk-s3/errors.rb +115 -0
- data/lib/aws-sdk-s3/event_streams.rb +69 -0
- data/lib/aws-sdk-s3/file_downloader.rb +142 -0
- data/lib/aws-sdk-s3/file_part.rb +78 -0
- data/lib/aws-sdk-s3/file_uploader.rb +70 -0
- data/lib/aws-sdk-s3/legacy_signer.rb +189 -0
- data/lib/aws-sdk-s3/multipart_file_uploader.rb +227 -0
- data/lib/aws-sdk-s3/multipart_stream_uploader.rb +173 -0
- data/lib/aws-sdk-s3/multipart_upload.rb +401 -0
- data/lib/aws-sdk-s3/multipart_upload_error.rb +18 -0
- data/lib/aws-sdk-s3/multipart_upload_part.rb +423 -0
- data/lib/aws-sdk-s3/object.rb +1422 -0
- data/lib/aws-sdk-s3/object_acl.rb +333 -0
- data/lib/aws-sdk-s3/object_copier.rb +101 -0
- data/lib/aws-sdk-s3/object_multipart_copier.rb +182 -0
- data/lib/aws-sdk-s3/object_summary.rb +1181 -0
- data/lib/aws-sdk-s3/object_version.rb +550 -0
- data/lib/aws-sdk-s3/plugins/accelerate.rb +87 -0
- data/lib/aws-sdk-s3/plugins/bucket_arn.rb +212 -0
- data/lib/aws-sdk-s3/plugins/bucket_dns.rb +91 -0
- data/lib/aws-sdk-s3/plugins/bucket_name_restrictions.rb +45 -0
- data/lib/aws-sdk-s3/plugins/dualstack.rb +74 -0
- data/lib/aws-sdk-s3/plugins/expect_100_continue.rb +28 -0
- data/lib/aws-sdk-s3/plugins/get_bucket_location_fix.rb +25 -0
- data/lib/aws-sdk-s3/plugins/http_200_errors.rb +55 -0
- data/lib/aws-sdk-s3/plugins/iad_regional_endpoint.rb +62 -0
- data/lib/aws-sdk-s3/plugins/location_constraint.rb +35 -0
- data/lib/aws-sdk-s3/plugins/md5s.rb +84 -0
- data/lib/aws-sdk-s3/plugins/redirects.rb +45 -0
- data/lib/aws-sdk-s3/plugins/s3_host_id.rb +30 -0
- data/lib/aws-sdk-s3/plugins/s3_signer.rb +222 -0
- data/lib/aws-sdk-s3/plugins/sse_cpk.rb +70 -0
- data/lib/aws-sdk-s3/plugins/streaming_retry.rb +118 -0
- data/lib/aws-sdk-s3/plugins/url_encoded_keys.rb +97 -0
- data/lib/aws-sdk-s3/presigned_post.rb +686 -0
- data/lib/aws-sdk-s3/presigner.rb +253 -0
- data/lib/aws-sdk-s3/resource.rb +117 -0
- data/lib/aws-sdk-s3/types.rb +13154 -0
- data/lib/aws-sdk-s3/waiters.rb +243 -0
- metadata +184 -0
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'stringio'
|
|
4
|
+
require 'tempfile'
|
|
5
|
+
|
|
6
|
+
module Aws
|
|
7
|
+
module S3
|
|
8
|
+
module Encryption
|
|
9
|
+
|
|
10
|
+
# Provides an IO wrapper encrpyting a stream of data.
|
|
11
|
+
# It is possible to use this same object for decrypting. You must
|
|
12
|
+
# initialize it with a decryptiion cipher in that case and the
|
|
13
|
+
# IO object must contain cipher text instead of plain text.
|
|
14
|
+
# @api private
|
|
15
|
+
class IOEncrypter
|
|
16
|
+
|
|
17
|
+
# @api private
|
|
18
|
+
ONE_MEGABYTE = 1024 * 1024
|
|
19
|
+
|
|
20
|
+
def initialize(cipher, io)
|
|
21
|
+
@encrypted = io.size <= ONE_MEGABYTE ?
|
|
22
|
+
encrypt_to_stringio(cipher, io.read) :
|
|
23
|
+
encrypt_to_tempfile(cipher, io)
|
|
24
|
+
@size = @encrypted.size
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# @return [Integer]
|
|
28
|
+
attr_reader :size
|
|
29
|
+
|
|
30
|
+
def read(bytes = nil, output_buffer = nil)
|
|
31
|
+
if Tempfile === @encrypted && @encrypted.closed?
|
|
32
|
+
@encrypted.open
|
|
33
|
+
@encrypted.binmode
|
|
34
|
+
end
|
|
35
|
+
@encrypted.read(bytes, output_buffer)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def rewind
|
|
39
|
+
@encrypted.rewind
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# @api private
|
|
43
|
+
def close
|
|
44
|
+
@encrypted.close if Tempfile === @encrypted
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
private
|
|
48
|
+
|
|
49
|
+
def encrypt_to_stringio(cipher, plain_text)
|
|
50
|
+
if plain_text.empty?
|
|
51
|
+
StringIO.new(cipher.final)
|
|
52
|
+
else
|
|
53
|
+
StringIO.new(cipher.update(plain_text) + cipher.final)
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def encrypt_to_tempfile(cipher, io)
|
|
58
|
+
encrypted = Tempfile.new(self.object_id.to_s)
|
|
59
|
+
encrypted.binmode
|
|
60
|
+
while chunk = io.read(ONE_MEGABYTE)
|
|
61
|
+
encrypted.write(cipher.update(chunk))
|
|
62
|
+
end
|
|
63
|
+
encrypted.write(cipher.final)
|
|
64
|
+
encrypted.rewind
|
|
65
|
+
encrypted
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Aws
|
|
4
|
+
module S3
|
|
5
|
+
module Encryption
|
|
6
|
+
|
|
7
|
+
# This module defines the interface required for a {Client#key_provider}.
|
|
8
|
+
# A key provider is any object that:
|
|
9
|
+
#
|
|
10
|
+
# * Responds to {#encryption_materials} with an {Materials} object.
|
|
11
|
+
#
|
|
12
|
+
# * Responds to {#key_for}, receiving a JSON document String,
|
|
13
|
+
# returning an encryption key. The returned encryption key
|
|
14
|
+
# must be one of:
|
|
15
|
+
#
|
|
16
|
+
# * `OpenSSL::PKey::RSA` - for asymmetric encryption
|
|
17
|
+
# * `String` - 32, 24, or 16 bytes long, for symmetric encryption
|
|
18
|
+
#
|
|
19
|
+
module KeyProvider
|
|
20
|
+
|
|
21
|
+
# @return [Materials]
|
|
22
|
+
def encryption_materials; end
|
|
23
|
+
|
|
24
|
+
# @param [String<JSON>] materials_description
|
|
25
|
+
# @return [OpenSSL::PKey::RSA, String] encryption_key
|
|
26
|
+
def key_for(materials_description); end
|
|
27
|
+
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'base64'
|
|
4
|
+
|
|
5
|
+
module Aws
|
|
6
|
+
module S3
|
|
7
|
+
module Encryption
|
|
8
|
+
# @api private
|
|
9
|
+
class KmsCipherProvider
|
|
10
|
+
|
|
11
|
+
def initialize(options = {})
|
|
12
|
+
@kms_key_id = options[:kms_key_id]
|
|
13
|
+
@kms_client = options[:kms_client]
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# @return [Array<Hash,Cipher>] Creates an returns a new encryption
|
|
17
|
+
# envelope and encryption cipher.
|
|
18
|
+
def encryption_cipher
|
|
19
|
+
encryption_context = { "kms_cmk_id" => @kms_key_id }
|
|
20
|
+
key_data = @kms_client.generate_data_key(
|
|
21
|
+
key_id: @kms_key_id,
|
|
22
|
+
encryption_context: encryption_context,
|
|
23
|
+
key_spec: 'AES_256',
|
|
24
|
+
)
|
|
25
|
+
cipher = Utils.aes_encryption_cipher(:CBC)
|
|
26
|
+
cipher.key = key_data.plaintext
|
|
27
|
+
envelope = {
|
|
28
|
+
'x-amz-key-v2' => encode64(key_data.ciphertext_blob),
|
|
29
|
+
'x-amz-iv' => encode64(cipher.iv = cipher.random_iv),
|
|
30
|
+
'x-amz-cek-alg' => 'AES/CBC/PKCS5Padding',
|
|
31
|
+
'x-amz-wrap-alg' => 'kms',
|
|
32
|
+
'x-amz-matdesc' => Json.dump(encryption_context)
|
|
33
|
+
}
|
|
34
|
+
[envelope, cipher]
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# @return [Cipher] Given an encryption envelope, returns a
|
|
38
|
+
# decryption cipher.
|
|
39
|
+
def decryption_cipher(envelope)
|
|
40
|
+
encryption_context = Json.load(envelope['x-amz-matdesc'])
|
|
41
|
+
key = @kms_client.decrypt(
|
|
42
|
+
ciphertext_blob: decode64(envelope['x-amz-key-v2']),
|
|
43
|
+
encryption_context: encryption_context,
|
|
44
|
+
).plaintext
|
|
45
|
+
iv = decode64(envelope['x-amz-iv'])
|
|
46
|
+
block_mode =
|
|
47
|
+
case envelope['x-amz-cek-alg']
|
|
48
|
+
when 'AES/CBC/PKCS5Padding'
|
|
49
|
+
:CBC
|
|
50
|
+
when 'AES/CBC/PKCS7Padding'
|
|
51
|
+
:CBC
|
|
52
|
+
when 'AES/GCM/NoPadding'
|
|
53
|
+
:GCM
|
|
54
|
+
else
|
|
55
|
+
type = envelope['x-amz-cek-alg'].inspect
|
|
56
|
+
msg = "unsupported content encrypting key (cek) format: #{type}"
|
|
57
|
+
raise Errors::DecryptionError, msg
|
|
58
|
+
end
|
|
59
|
+
Utils.aes_decryption_cipher(block_mode, key, iv)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
private
|
|
63
|
+
|
|
64
|
+
def encode64(str)
|
|
65
|
+
Base64.encode64(str).split("\n") * ""
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def decode64(str)
|
|
69
|
+
Base64.decode64(str)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'base64'
|
|
4
|
+
|
|
5
|
+
module Aws
|
|
6
|
+
module S3
|
|
7
|
+
module Encryption
|
|
8
|
+
class Materials
|
|
9
|
+
|
|
10
|
+
# @option options [required, OpenSSL::PKey::RSA, String] :key
|
|
11
|
+
# The master key to use for encrypting/decrypting all objects.
|
|
12
|
+
#
|
|
13
|
+
# @option options [String<JSON>] :description ('{}')
|
|
14
|
+
# The encryption materials description. This is must be
|
|
15
|
+
# a JSON document string.
|
|
16
|
+
#
|
|
17
|
+
def initialize(options = {})
|
|
18
|
+
@key = validate_key(options[:key])
|
|
19
|
+
@description = validate_desc(options[:description])
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# @return [OpenSSL::PKey::RSA, String]
|
|
23
|
+
attr_reader :key
|
|
24
|
+
|
|
25
|
+
# @return [String<JSON>]
|
|
26
|
+
attr_reader :description
|
|
27
|
+
|
|
28
|
+
private
|
|
29
|
+
|
|
30
|
+
def validate_key(key)
|
|
31
|
+
case key
|
|
32
|
+
when OpenSSL::PKey::RSA then key
|
|
33
|
+
when String
|
|
34
|
+
if [32, 24, 16].include?(key.bytesize)
|
|
35
|
+
key
|
|
36
|
+
else
|
|
37
|
+
msg = 'invalid key, symmetric key required to be 16, 24, or '\
|
|
38
|
+
'32 bytes in length, saw length ' + key.bytesize.to_s
|
|
39
|
+
raise ArgumentError, msg
|
|
40
|
+
end
|
|
41
|
+
else
|
|
42
|
+
msg = 'invalid encryption key, expected an OpenSSL::PKey::RSA key '\
|
|
43
|
+
'(for asymmetric encryption) or a String (for symmetric '\
|
|
44
|
+
'encryption).'
|
|
45
|
+
raise ArgumentError, msg
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def validate_desc(description)
|
|
50
|
+
Json.load(description)
|
|
51
|
+
description
|
|
52
|
+
rescue Json::ParseError, EncodingError
|
|
53
|
+
msg = 'expected description to be a valid JSON document string'
|
|
54
|
+
raise ArgumentError, msg
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'openssl'
|
|
4
|
+
|
|
5
|
+
module Aws
|
|
6
|
+
module S3
|
|
7
|
+
module Encryption
|
|
8
|
+
# @api private
|
|
9
|
+
module Utils
|
|
10
|
+
|
|
11
|
+
UNSAFE_MSG = "unsafe encryption, data is longer than key length"
|
|
12
|
+
|
|
13
|
+
class << self
|
|
14
|
+
|
|
15
|
+
def encrypt(key, data)
|
|
16
|
+
case key
|
|
17
|
+
when OpenSSL::PKey::RSA # asymmetric encryption
|
|
18
|
+
warn(UNSAFE_MSG) if key.public_key.n.num_bits < cipher_size(data)
|
|
19
|
+
key.public_encrypt(data)
|
|
20
|
+
when String # symmetric encryption
|
|
21
|
+
warn(UNSAFE_MSG) if cipher_size(key) < cipher_size(data)
|
|
22
|
+
cipher = aes_encryption_cipher(:ECB, key)
|
|
23
|
+
cipher.update(data) + cipher.final
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def decrypt(key, data)
|
|
28
|
+
begin
|
|
29
|
+
case key
|
|
30
|
+
when OpenSSL::PKey::RSA # asymmetric decryption
|
|
31
|
+
key.private_decrypt(data)
|
|
32
|
+
when String # symmetric Decryption
|
|
33
|
+
cipher = aes_cipher(:decrypt, :ECB, key, nil)
|
|
34
|
+
cipher.update(data) + cipher.final
|
|
35
|
+
end
|
|
36
|
+
rescue OpenSSL::Cipher::CipherError
|
|
37
|
+
msg = 'decryption failed, possible incorrect key'
|
|
38
|
+
raise Errors::DecryptionError, msg
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# @param [String] block_mode "CBC" or "ECB"
|
|
43
|
+
# @param [OpenSSL::PKey::RSA, String, nil] key
|
|
44
|
+
# @param [String, nil] iv The initialization vector
|
|
45
|
+
def aes_encryption_cipher(block_mode, key = nil, iv = nil)
|
|
46
|
+
aes_cipher(:encrypt, block_mode, key, iv)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# @param [String] block_mode "CBC" or "ECB"
|
|
50
|
+
# @param [OpenSSL::PKey::RSA, String, nil] key
|
|
51
|
+
# @param [String, nil] iv The initialization vector
|
|
52
|
+
def aes_decryption_cipher(block_mode, key = nil, iv = nil)
|
|
53
|
+
aes_cipher(:decrypt, block_mode, key, iv)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# @param [String] mode "encrypt" or "decrypt"
|
|
57
|
+
# @param [String] block_mode "CBC" or "ECB"
|
|
58
|
+
# @param [OpenSSL::PKey::RSA, String, nil] key
|
|
59
|
+
# @param [String, nil] iv The initialization vector
|
|
60
|
+
def aes_cipher(mode, block_mode, key, iv)
|
|
61
|
+
cipher = key ?
|
|
62
|
+
OpenSSL::Cipher.new("aes-#{cipher_size(key)}-#{block_mode.downcase}") :
|
|
63
|
+
OpenSSL::Cipher.new("aes-256-#{block_mode.downcase}")
|
|
64
|
+
cipher.send(mode) # encrypt or decrypt
|
|
65
|
+
cipher.key = key if key
|
|
66
|
+
cipher.iv = iv if iv
|
|
67
|
+
cipher
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# @param [String] key
|
|
71
|
+
# @return [Integer]
|
|
72
|
+
# @raise ArgumentError
|
|
73
|
+
def cipher_size(key)
|
|
74
|
+
key.bytesize * 8
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
@@ -0,0 +1,388 @@
|
|
|
1
|
+
require 'forwardable'
|
|
2
|
+
|
|
3
|
+
module Aws
|
|
4
|
+
module S3
|
|
5
|
+
|
|
6
|
+
# Provides an encryption client that encrypts and decrypts data client-side,
|
|
7
|
+
# storing the encrypted data in Amazon S3. The EncryptionV2::Client
|
|
8
|
+
# provides improved security over the Encryption::Client by using more
|
|
9
|
+
# modern and secure algorithms. The EncryptionV2::Client maintains
|
|
10
|
+
# backwards compatibility: Using the EncryptionV2::Client you can decrypt
|
|
11
|
+
# objects encrypted with the Encryption::Client. However, objects you
|
|
12
|
+
# encrypt using the EncryptionV2::Client cannot be decrypted using the
|
|
13
|
+
# Encryption::Client.
|
|
14
|
+
#
|
|
15
|
+
# This client uses a process called "envelope encryption". Your private
|
|
16
|
+
# encryption keys and your data's plain-text are **never** sent to
|
|
17
|
+
# Amazon S3. **If you lose you encryption keys, you will not be able to
|
|
18
|
+
# decrypt your data.**
|
|
19
|
+
#
|
|
20
|
+
# ## Envelope Encryption Overview
|
|
21
|
+
#
|
|
22
|
+
# The goal of envelope encryption is to combine the performance of
|
|
23
|
+
# fast symmetric encryption while maintaining the secure key management
|
|
24
|
+
# that asymmetric keys provide.
|
|
25
|
+
#
|
|
26
|
+
# A one-time-use symmetric key (envelope key) is generated client-side.
|
|
27
|
+
# This is used to encrypt the data client-side. This key is then
|
|
28
|
+
# encrypted by your master key and stored alongside your data in Amazon
|
|
29
|
+
# S3.
|
|
30
|
+
#
|
|
31
|
+
# When accessing your encrypted data with the encryption client,
|
|
32
|
+
# the encrypted envelope key is retrieved and decrypted client-side
|
|
33
|
+
# with your master key. The envelope key is then used to decrypt the
|
|
34
|
+
# data client-side.
|
|
35
|
+
#
|
|
36
|
+
# One of the benefits of envelope encryption is that if your master key
|
|
37
|
+
# is compromised, you have the option of just re-encrypting the stored
|
|
38
|
+
# envelope symmetric keys, instead of re-encrypting all of the
|
|
39
|
+
# data in your account.
|
|
40
|
+
#
|
|
41
|
+
# ## Basic Usage
|
|
42
|
+
#
|
|
43
|
+
# The encryption client requires an {Aws::S3::Client}. If you do not
|
|
44
|
+
# provide a `:client`, then a client will be constructed for you.
|
|
45
|
+
#
|
|
46
|
+
# require 'openssl'
|
|
47
|
+
# key = OpenSSL::PKey::RSA.new(1024)
|
|
48
|
+
#
|
|
49
|
+
# # encryption client
|
|
50
|
+
# s3 = Aws::S3::EncryptionV2::Client.new(encryption_key: key)
|
|
51
|
+
#
|
|
52
|
+
# # round-trip an object, encrypted/decrypted locally
|
|
53
|
+
# s3.put_object(bucket:'aws-sdk', key:'secret', body:'handshake')
|
|
54
|
+
# s3.get_object(bucket:'aws-sdk', key:'secret').body.read
|
|
55
|
+
# #=> 'handshake'
|
|
56
|
+
#
|
|
57
|
+
# # reading encrypted object without the encryption client
|
|
58
|
+
# # results in the getting the cipher text
|
|
59
|
+
# Aws::S3::Client.new.get_object(bucket:'aws-sdk', key:'secret').body.read
|
|
60
|
+
# #=> "... cipher text ..."
|
|
61
|
+
#
|
|
62
|
+
# ## Keys
|
|
63
|
+
#
|
|
64
|
+
# For client-side encryption to work, you must provide one of the following:
|
|
65
|
+
#
|
|
66
|
+
# * An encryption key
|
|
67
|
+
# * A {KeyProvider}
|
|
68
|
+
# * A KMS encryption key id
|
|
69
|
+
#
|
|
70
|
+
# ### An Encryption Key
|
|
71
|
+
#
|
|
72
|
+
# You can pass a single encryption key. This is used as a master key
|
|
73
|
+
# encrypting and decrypting all object keys.
|
|
74
|
+
#
|
|
75
|
+
# key = OpenSSL::Cipher.new("AES-256-ECB").random_key # symmetric key
|
|
76
|
+
# key = OpenSSL::PKey::RSA.new(1024) # asymmetric key pair
|
|
77
|
+
#
|
|
78
|
+
# s3 = Aws::S3::EncryptionV2::Client.new(encryption_key: key)
|
|
79
|
+
#
|
|
80
|
+
# ### Key Provider
|
|
81
|
+
#
|
|
82
|
+
# Alternatively, you can use a {KeyProvider}. A key provider makes
|
|
83
|
+
# it easy to work with multiple keys and simplifies key rotation.
|
|
84
|
+
#
|
|
85
|
+
# ### KMS Encryption Key Id
|
|
86
|
+
#
|
|
87
|
+
# If you pass the id to an AWS Key Management Service (KMS) key,
|
|
88
|
+
# then KMS will be used to generate, encrypt and decrypt object keys.
|
|
89
|
+
#
|
|
90
|
+
# # keep track of the kms key id
|
|
91
|
+
# kms = Aws::KMS::Client.new
|
|
92
|
+
# key_id = kms.create_key.key_metadata.key_id
|
|
93
|
+
#
|
|
94
|
+
# Aws::S3::EncryptionV2::Client.new(
|
|
95
|
+
# kms_key_id: key_id,
|
|
96
|
+
# kms_client: kms,
|
|
97
|
+
# )
|
|
98
|
+
#
|
|
99
|
+
# ## Custom Key Providers
|
|
100
|
+
#
|
|
101
|
+
# A {KeyProvider} is any object that responds to:
|
|
102
|
+
#
|
|
103
|
+
# * `#encryption_materials`
|
|
104
|
+
# * `#key_for(materials_description)`
|
|
105
|
+
#
|
|
106
|
+
# Here is a trivial implementation of an in-memory key provider.
|
|
107
|
+
# This is provided as a demonstration of the key provider interface,
|
|
108
|
+
# and should not be used in production:
|
|
109
|
+
#
|
|
110
|
+
# class KeyProvider
|
|
111
|
+
#
|
|
112
|
+
# def initialize(default_key_name, keys)
|
|
113
|
+
# @keys = keys
|
|
114
|
+
# @encryption_materials = Aws::S3::EncryptionV2::Materials.new(
|
|
115
|
+
# key: @keys[default_key_name],
|
|
116
|
+
# description: JSON.dump(key: default_key_name),
|
|
117
|
+
# )
|
|
118
|
+
# end
|
|
119
|
+
#
|
|
120
|
+
# attr_reader :encryption_materials
|
|
121
|
+
#
|
|
122
|
+
# def key_for(matdesc)
|
|
123
|
+
# key_name = JSON.load(matdesc)['key']
|
|
124
|
+
# if key = @keys[key_name]
|
|
125
|
+
# key
|
|
126
|
+
# else
|
|
127
|
+
# raise "encryption key not found for: #{matdesc.inspect}"
|
|
128
|
+
# end
|
|
129
|
+
# end
|
|
130
|
+
# end
|
|
131
|
+
#
|
|
132
|
+
# Given the above key provider, you can create an encryption client that
|
|
133
|
+
# chooses the key to use based on the materials description stored with
|
|
134
|
+
# the encrypted object. This makes it possible to use multiple keys
|
|
135
|
+
# and simplifies key rotation.
|
|
136
|
+
#
|
|
137
|
+
# # uses "new-key" for encrypting objects, uses either for decrypting
|
|
138
|
+
# keys = KeyProvider.new('new-key', {
|
|
139
|
+
# "old-key" => Base64.decode64("kM5UVbhE/4rtMZJfsadYEdm2vaKFsmV2f5+URSeUCV4="),
|
|
140
|
+
# "new-key" => Base64.decode64("w1WLio3agRWRTSJK/Ouh8NHoqRQ6fn5WbSXDTHjXMSo="),
|
|
141
|
+
# }),
|
|
142
|
+
#
|
|
143
|
+
# # chooses the key based on the materials description stored
|
|
144
|
+
# # with the encrypted object
|
|
145
|
+
# s3 = Aws::S3::EncryptionV2::Client.new(key_provider: keys)
|
|
146
|
+
#
|
|
147
|
+
# ## Materials Description
|
|
148
|
+
#
|
|
149
|
+
# A materials description is JSON document string that is stored
|
|
150
|
+
# in the metadata (or instruction file) of an encrypted object.
|
|
151
|
+
# The {DefaultKeyProvider} uses the empty JSON document `"{}"`.
|
|
152
|
+
#
|
|
153
|
+
# When building a key provider, you are free to store whatever
|
|
154
|
+
# information you need to identify the master key that was used
|
|
155
|
+
# to encrypt the object.
|
|
156
|
+
#
|
|
157
|
+
# ## Envelope Location
|
|
158
|
+
#
|
|
159
|
+
# By default, the encryption client store the encryption envelope
|
|
160
|
+
# with the object, as metadata. You can choose to have the envelope
|
|
161
|
+
# stored in a separate "instruction file". An instruction file
|
|
162
|
+
# is an object, with the key of the encrypted object, suffixed with
|
|
163
|
+
# `".instruction"`.
|
|
164
|
+
#
|
|
165
|
+
# Specify the `:envelope_location` option as `:instruction_file` to
|
|
166
|
+
# use an instruction file for storing the envelope.
|
|
167
|
+
#
|
|
168
|
+
# # default behavior
|
|
169
|
+
# s3 = Aws::S3::EncryptionV2::Client.new(
|
|
170
|
+
# key_provider: ...,
|
|
171
|
+
# envelope_location: :metadata,
|
|
172
|
+
# )
|
|
173
|
+
#
|
|
174
|
+
# # store envelope in a separate object
|
|
175
|
+
# s3 = Aws::S3::EncryptionV2::Client.new(
|
|
176
|
+
# key_provider: ...,
|
|
177
|
+
# envelope_location: :instruction_file,
|
|
178
|
+
# instruction_file_suffix: '.instruction' # default
|
|
179
|
+
# )
|
|
180
|
+
#
|
|
181
|
+
# When using an instruction file, multiple requests are made when
|
|
182
|
+
# putting and getting the object. **This may cause issues if you are
|
|
183
|
+
# issuing concurrent PUT and GET requests to an encrypted object.**
|
|
184
|
+
#
|
|
185
|
+
module EncryptionV2
|
|
186
|
+
class Client
|
|
187
|
+
|
|
188
|
+
extend Deprecations
|
|
189
|
+
extend Forwardable
|
|
190
|
+
def_delegators :@client, :config, :delete_object, :head_object, :build_request
|
|
191
|
+
|
|
192
|
+
# Creates a new encryption client. You must provide one of the following
|
|
193
|
+
# options:
|
|
194
|
+
#
|
|
195
|
+
# * `:encryption_key`
|
|
196
|
+
# * `:kms_key_id`
|
|
197
|
+
# * `:key_provider`
|
|
198
|
+
#
|
|
199
|
+
# You may also pass any other options accepted by `Client#initialize`.
|
|
200
|
+
#
|
|
201
|
+
# @option options [S3::Client] :client A basic S3 client that is used
|
|
202
|
+
# to make api calls. If a `:client` is not provided, a new {S3::Client}
|
|
203
|
+
# will be constructed.
|
|
204
|
+
#
|
|
205
|
+
# @option options [OpenSSL::PKey::RSA, String] :encryption_key The master
|
|
206
|
+
# key to use for encrypting/decrypting all objects.
|
|
207
|
+
#
|
|
208
|
+
# @option options [String] :kms_key_id When you provide a `:kms_key_id`,
|
|
209
|
+
# then AWS Key Management Service (KMS) will be used to manage the
|
|
210
|
+
# object encryption keys. By default a {KMS::Client} will be
|
|
211
|
+
# constructed for KMS API calls. Alternatively, you can provide
|
|
212
|
+
# your own via `:kms_client`.
|
|
213
|
+
#
|
|
214
|
+
# @option options [#key_for] :key_provider Any object that responds
|
|
215
|
+
# to `#key_for`. This method should accept a materials description
|
|
216
|
+
# JSON document string and return return an encryption key.
|
|
217
|
+
#
|
|
218
|
+
# @option options [Symbol] :envelope_location (:metadata) Where to
|
|
219
|
+
# store the envelope encryption keys. By default, the envelope is
|
|
220
|
+
# stored with the encrypted object. If you pass `:instruction_file`,
|
|
221
|
+
# then the envelope is stored in a separate object in Amazon S3.
|
|
222
|
+
#
|
|
223
|
+
# @option options [String] :instruction_file_suffix ('.instruction')
|
|
224
|
+
# When `:envelope_location` is `:instruction_file` then the
|
|
225
|
+
# instruction file uses the object key with this suffix appended.
|
|
226
|
+
#
|
|
227
|
+
# @option options [KMS::Client] :kms_client A default {KMS::Client}
|
|
228
|
+
# is constructed when using KMS to manage encryption keys.
|
|
229
|
+
#
|
|
230
|
+
def initialize(options = {})
|
|
231
|
+
@client = extract_client(options)
|
|
232
|
+
@cipher_provider = cipher_provider(options)
|
|
233
|
+
@envelope_location = extract_location(options)
|
|
234
|
+
@instruction_file_suffix = extract_suffix(options)
|
|
235
|
+
end
|
|
236
|
+
|
|
237
|
+
# @return [S3::Client]
|
|
238
|
+
attr_reader :client
|
|
239
|
+
|
|
240
|
+
# @return [KeyProvider, nil] Returns `nil` if you are using
|
|
241
|
+
# AWS Key Management Service (KMS).
|
|
242
|
+
attr_reader :key_provider
|
|
243
|
+
|
|
244
|
+
# @return [Symbol<:metadata, :instruction_file>]
|
|
245
|
+
attr_reader :envelope_location
|
|
246
|
+
|
|
247
|
+
# @return [String] When {#envelope_location} is `:instruction_file`,
|
|
248
|
+
# the envelope is stored in the object with the object key suffixed
|
|
249
|
+
# by this string.
|
|
250
|
+
attr_reader :instruction_file_suffix
|
|
251
|
+
|
|
252
|
+
# Uploads an object to Amazon S3, encrypting data client-side.
|
|
253
|
+
# See {S3::Client#put_object} for documentation on accepted
|
|
254
|
+
# request parameters.
|
|
255
|
+
# @option params [Hash] :kms_encryption_context Additional encryption
|
|
256
|
+
# context to use with KMS. Applies only when KMS is used. In order
|
|
257
|
+
# to decrypt the object you will need to provide the identical
|
|
258
|
+
# :kms_encryption_context to `get_object`.
|
|
259
|
+
# @option (see S3::Client#put_object)
|
|
260
|
+
# @return (see S3::Client#put_object)
|
|
261
|
+
# @see S3::Client#put_object
|
|
262
|
+
def put_object(params = {})
|
|
263
|
+
kms_encryption_context = params.delete(:kms_encryption_context)
|
|
264
|
+
req = @client.build_request(:put_object, params)
|
|
265
|
+
req.handlers.add(EncryptHandler, priority: 95)
|
|
266
|
+
req.context[:encryption] = {
|
|
267
|
+
cipher_provider: @cipher_provider,
|
|
268
|
+
envelope_location: @envelope_location,
|
|
269
|
+
instruction_file_suffix: @instruction_file_suffix,
|
|
270
|
+
kms_encryption_context: kms_encryption_context
|
|
271
|
+
}
|
|
272
|
+
req.send_request
|
|
273
|
+
end
|
|
274
|
+
|
|
275
|
+
# Gets an object from Amazon S3, decrypting data locally.
|
|
276
|
+
# See {S3::Client#get_object} for documentation on accepted
|
|
277
|
+
# request parameters.
|
|
278
|
+
# @option params [String] :instruction_file_suffix The suffix
|
|
279
|
+
# used to find the instruction file containing the encryption
|
|
280
|
+
# envelope. You should not set this option when the envelope
|
|
281
|
+
# is stored in the object metadata. Defaults to
|
|
282
|
+
# {#instruction_file_suffix}.
|
|
283
|
+
# @option params [Hash] :kms_encryption_context Additional encryption
|
|
284
|
+
# context to use with KMS. Applies only when KMS is used.
|
|
285
|
+
# @option params [String] :instruction_file_suffix
|
|
286
|
+
# @option (see S3::Client#get_object)
|
|
287
|
+
# @return (see S3::Client#get_object)
|
|
288
|
+
# @see S3::Client#get_object
|
|
289
|
+
# @note The `:range` request parameter is not yet supported.
|
|
290
|
+
def get_object(params = {}, &block)
|
|
291
|
+
if params[:range]
|
|
292
|
+
raise NotImplementedError, '#get_object with :range not supported'
|
|
293
|
+
end
|
|
294
|
+
envelope_location, instruction_file_suffix = envelope_options(params)
|
|
295
|
+
kms_encryption_context = params.delete(:kms_encryption_context)
|
|
296
|
+
req = @client.build_request(:get_object, params)
|
|
297
|
+
req.handlers.add(DecryptHandler)
|
|
298
|
+
req.context[:encryption] = {
|
|
299
|
+
cipher_provider: @cipher_provider,
|
|
300
|
+
envelope_location: envelope_location,
|
|
301
|
+
instruction_file_suffix: instruction_file_suffix,
|
|
302
|
+
kms_encryption_context: kms_encryption_context
|
|
303
|
+
}
|
|
304
|
+
req.send_request(target: block)
|
|
305
|
+
end
|
|
306
|
+
|
|
307
|
+
private
|
|
308
|
+
|
|
309
|
+
def extract_client(options)
|
|
310
|
+
options[:client] || begin
|
|
311
|
+
options = options.dup
|
|
312
|
+
options.delete(:kms_key_id)
|
|
313
|
+
options.delete(:kms_client)
|
|
314
|
+
options.delete(:key_provider)
|
|
315
|
+
options.delete(:encryption_key)
|
|
316
|
+
options.delete(:envelope_location)
|
|
317
|
+
options.delete(:instruction_file_suffix)
|
|
318
|
+
S3::Client.new(options)
|
|
319
|
+
end
|
|
320
|
+
end
|
|
321
|
+
|
|
322
|
+
def kms_client(options)
|
|
323
|
+
options[:kms_client] || begin
|
|
324
|
+
KMS::Client.new(
|
|
325
|
+
region: @client.config.region,
|
|
326
|
+
credentials: @client.config.credentials,
|
|
327
|
+
)
|
|
328
|
+
end
|
|
329
|
+
end
|
|
330
|
+
|
|
331
|
+
def cipher_provider(options)
|
|
332
|
+
if options[:kms_key_id]
|
|
333
|
+
KmsCipherProvider.new(
|
|
334
|
+
kms_key_id: options[:kms_key_id],
|
|
335
|
+
kms_client: kms_client(options),
|
|
336
|
+
)
|
|
337
|
+
else
|
|
338
|
+
@key_provider = extract_key_provider(options)
|
|
339
|
+
DefaultCipherProvider.new(key_provider: @key_provider)
|
|
340
|
+
end
|
|
341
|
+
end
|
|
342
|
+
|
|
343
|
+
def extract_key_provider(options)
|
|
344
|
+
if options[:key_provider]
|
|
345
|
+
options[:key_provider]
|
|
346
|
+
elsif options[:encryption_key]
|
|
347
|
+
DefaultKeyProvider.new(options)
|
|
348
|
+
else
|
|
349
|
+
msg = 'you must pass a :kms_key_id, :key_provider, or :encryption_key'
|
|
350
|
+
raise ArgumentError, msg
|
|
351
|
+
end
|
|
352
|
+
end
|
|
353
|
+
|
|
354
|
+
def envelope_options(params)
|
|
355
|
+
location = params.delete(:envelope_location) || @envelope_location
|
|
356
|
+
suffix = params.delete(:instruction_file_suffix)
|
|
357
|
+
if suffix
|
|
358
|
+
[:instruction_file, suffix]
|
|
359
|
+
else
|
|
360
|
+
[location, @instruction_file_suffix]
|
|
361
|
+
end
|
|
362
|
+
end
|
|
363
|
+
|
|
364
|
+
def extract_location(options)
|
|
365
|
+
location = options[:envelope_location] || :metadata
|
|
366
|
+
if [:metadata, :instruction_file].include?(location)
|
|
367
|
+
location
|
|
368
|
+
else
|
|
369
|
+
msg = ':envelope_location must be :metadata or :instruction_file '\
|
|
370
|
+
"got #{location.inspect}"
|
|
371
|
+
raise ArgumentError, msg
|
|
372
|
+
end
|
|
373
|
+
end
|
|
374
|
+
|
|
375
|
+
def extract_suffix(options)
|
|
376
|
+
suffix = options[:instruction_file_suffix] || '.instruction'
|
|
377
|
+
if suffix.is_a? String
|
|
378
|
+
suffix
|
|
379
|
+
else
|
|
380
|
+
msg = ':instruction_file_suffix must be a String'
|
|
381
|
+
raise ArgumentError, msg
|
|
382
|
+
end
|
|
383
|
+
end
|
|
384
|
+
|
|
385
|
+
end
|
|
386
|
+
end
|
|
387
|
+
end
|
|
388
|
+
end
|