Debian Codegraph @

lib/3rdParty/composer/aws/aws-sdk-php/src/S3/Crypto/S3EncryptionMultipartUploaderV3.php

raw symbols SCIP
1 <?php
2 namespace Aws\S3\Crypto;
3
4 use Aws\Crypto\AbstractCryptoClientV3;
5 use Aws\Crypto\EncryptionTraitV3;
6 use Aws\Crypto\AlgorithmSuite;
7 use Aws\Crypto\MaterialsProviderInterfaceV3;
8 use Aws\Crypto\MetadataEnvelope;
9 use Aws\Crypto\Cipher\CipherBuilderTrait;
10 use Aws\Exception\CryptoException;
11 use Aws\S3\MultipartUploader;
12 use Aws\S3\S3ClientInterface;
13 use GuzzleHttp\Promise;
14
15 //= ../specification/s3-encryption/client.md#optional-api-operations
16 //= type=implication
17 //# - CreateMultipartUpload MAY be implemented by the S3EC.
18 /**
19 * Encapsulates the execution of a multipart upload of an encrypted object to S3.
20 *
21 */
22 class S3EncryptionMultipartUploaderV3 extends MultipartUploader
23 {
24 use CipherBuilderTrait;
25 use CryptoParamsTraitV3;
26 use EncryptionTraitV3;
27 use UserAgentTrait;
28
29 CONST CRYPTO_VERSION = '3.0';
30
31 // MaterialsProvider to be used with this client
32 private MaterialsProviderInterfaceV3 $provider;
33 // Commitment Policy to use with this client
34 private string $keyCommitmentPolicy;
35 // Algorithm Suite to use with this client
36 private AlgorithmSuite $algorithmSuite;
37 // Suffix to use when using a InstructionFileMetadata strategy
38 private ?string $instructionFileSuffix;
39 // Metadata Strategy to use when uploading objects
40 private $strategy;
41
42 /**
43 * Returns if the passed cipher name is supported for encryption by the SDK.
44 *
45 * @param string $cipherName The name of a cipher to verify is registered.
46 *
47 * @return bool If the cipher passed is in our supported list.
48 */
49 public static function isSupportedCipher($cipherName)
50 {
51 return in_array($cipherName, AbstractCryptoClientV3::$supportedCiphers);
52 }
53
54 /**
55 * Creates a multipart upload for an S3 object after encrypting it.
56 *
57 * The required configuration options are as follows:
58 *
59 * - @MaterialsProvider: (MaterialsProviderV3) Provides Cek, Iv, and Cek
60 * encrypting/decrypting for encryption metadata.
61 * - @CommitmentPolicy: (string) Must be set to 'FORBID_ENCRYPT_ALLOW_DECRYPT',
62 * 'REQUIRE_ENCRYPT_ALLOW_DECRYPT', or 'REQUIRE_ENCRYPT_REQUIRE_DECRYPT'.
63 * - 'FORBID_ENCRYPT_ALLOW_DECRYPT' indicates that the client is configured
64 * to write messages without key commitment and read messages encrypted
65 * with key commitment or without key commitment.
66 * - 'REQUIRE_ENCRYPT_ALLOW_DECRYPT' indicates that the client is configured
67 * to write messages with key commitment and read messages encrypted
68 * with key commitment or without key commitment.
69 * - 'REQUIRE_ENCRYPT_REQUIRE_DECRYPT' indicates that the client is configured
70 * to write messages with key commitment and read messages encrypted
71 * with key commitment.
72 * - @CipherOptions: (array) Cipher options for encrypting data. A Cipher
73 * is required. Accepts the following options:
74 * - Cipher: (string) gcm
75 * See also: AbstractCryptoClientV3::$supportedCiphers
76 * - KeySize: (int) 256
77 * See also: MaterialsProviderV3::$supportedKeySizes
78 * - Aad: (string) Additional authentication data. This option is
79 * passed directly to OpenSSL when using gcm.
80 * - @KmsEncryptionContext: (array) Only required if using
81 * KmsMaterialsProviderV3. An associative array of key-value
82 * pairs to be added to the encryption context for KMS key encryption. An
83 * empty array may be passed if no additional context is desired.
84 * - bucket: (string) Name of the bucket to which the object is
85 * being uploaded.
86 * - key: (string) Key to use for the object being uploaded.
87 *
88 * The optional configuration arguments are as follows:
89 *
90 * - @MetadataStrategy: (MetadataStrategy|string|null) Strategy for storing
91 * MetadataEnvelope information. Defaults to using a
92 * HeadersMetadataStrategy. Can either be a class implementing
93 * MetadataStrategy, a class name of a predefined strategy, or empty/null
94 * to default.
95 * - @InstructionFileSuffix: (string|null) Suffix used when writing to an
96 * instruction file if an using an InstructionFileMetadataHandler was
97 * determined.
98 * - acl: (string) ACL to set on the object being upload. Objects are
99 * private by default.
100 * - before_complete: (callable) Callback to invoke before the
101 * `CompleteMultipartUpload` operation. The callback should have a
102 * function signature like `function (Aws\Command $command) {...}`.
103 * - before_initiate: (callable) Callback to invoke before the
104 * `CreateMultipartUpload` operation. The callback should have a function
105 * signature like `function (Aws\Command $command) {...}`.
106 * - before_upload: (callable) Callback to invoke before any `UploadPart`
107 * operations. The callback should have a function signature like
108 * `function (Aws\Command $command) {...}`.
109 * - concurrency: (int, default=int(5)) Maximum number of concurrent
110 * `UploadPart` operations allowed during the multipart upload.
111 * - params: (array) An array of key/value parameters that will be applied
112 * to each of the sub-commands run by the uploader as a base.
113 * Auto-calculated options will override these parameters. If you need
114 * more granularity over parameters to each sub-command, use the before_*
115 * options detailed above to update the commands directly.
116 * - part_size: (int, default=int(5242880)) Part size, in bytes, to use when
117 * doing a multipart upload. This must between 5 MB and 5 GB, inclusive.
118 * - state: (Aws\Multipart\UploadState) An object that represents the state
119 * of the multipart upload and that is used to resume a previous upload.
120 * When this option is provided, the `bucket`, `key`, and `part_size`
121 * options are ignored.
122 *
123 * @param S3ClientInterface $client Client used for the upload.
124 * @param mixed $source Source of the data to upload.
125 * @param array $config Configuration used to perform the upload.
126 */
127 public function __construct(
128 S3ClientInterface $client,
129 $source,
130 array $config = []
131 ) {
132 $this->appendUserAgent($client, 'feat/s3-encrypt/' . self::CRYPTO_VERSION);
133 $this->client = $client;
134 $config['params'] = [];
135 if (!empty($config['bucket'])) {
136 $config['params']['Bucket'] = $config['bucket'];
137 }
138 if (!empty($config['key'])) {
139 $config['params']['Key'] = $config['key'];
140 }
141
142 $this->provider = $this->getMaterialsProvider($config);
143 unset($config['@MaterialsProvider']);
144
145 $this->keyCommitmentPolicy = $this->getKeyCommitmentPolicy($config);
146 unset($config['@CommitmentPolicy']);
147
148 $this->instructionFileSuffix = $this->getInstructionFileSuffix($config);
149 unset($config['@InstructionFileSuffix']);
150 $this->strategy = $this->getMetadataStrategy(
151 $config,
152 $this->instructionFileSuffix
153 );
154 if ($this->strategy === null) {
155 $this->strategy = self::getDefaultStrategy();
156 }
157 unset($config['@MetadataStrategy']);
158
159 $options = array_change_key_case($config);
160 $cipherOptions = array_intersect_key(
161 $options['@cipheroptions'],
162 self::$allowedOptions
163 );
164 if (empty($cipherOptions['Cipher'])) {
165 throw new \InvalidArgumentException('An encryption cipher must be'
166 . ' specified in @CipherOptions["Cipher"].');
167 }
168
169 $this->algorithmSuite = AlgorithmSuite::validateCommitmentPolicyOnEncrypt(
170 $cipherOptions,
171 $this->keyCommitmentPolicy
172 );
173
174 //= ../specification/s3-encryption/client.md#optional-api-operations
175 //= type=implication
176 //# - If implemented, CreateMultipartUpload MUST initiate a multipart upload.
177 $config['prepare_data_source'] = $this->getEncryptingDataPreparer();
178
179 parent::__construct($client, $source, $config);
180
181 if (!extension_loaded('openssl')) {
182 throw new CryptoException("Unable to load `openssl` extension.");
183 }
184 }
185
186 private static function getDefaultStrategy()
187 {
188 return new HeadersMetadataStrategy();
189 }
190
191 private function getEncryptingDataPreparer()
192 {
193 return function() {
194 // Defer encryption work until promise is executed
195 $envelope = new MetadataEnvelope();
196
197 list($this->source, $params) = Promise\Create::promiseFor($this->encrypt(
198 $this->source,
199 $this->algorithmSuite,
200 $this->config ?: [],
201 $this->provider,
202 $envelope
203 ))->then(
204 function ($bodyStream) use ($envelope) {
205 $params = $this->strategy->save(
206 $envelope,
207 $this->config['params']
208 );
209 return [$bodyStream, $params];
210 }
211 )->wait();
212
213 $this->source->rewind();
214 $this->config['params'] = $params;
215 };
216 }
217 }