PluginProbe
Media Cloud Sync / 1.3.11
Media Cloud Sync v1.3.11
1.4.1 1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 All 35 releases
media-cloud-sync / includes / sdk / s3 / Aws / S3 / Crypto / S3EncryptionMultipartUploaderV2.php

S3EncryptionMultipartUploaderV2.php in Media Cloud Sync 1.3.11, at includes/sdk/s3/Aws/S3/Crypto/S3EncryptionMultipartUploaderV2.php

149 lines 7.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Dudlewebs\WPMCS\s3\Aws\S3\Crypto;
4
5 use Dudlewebs\WPMCS\s3\Aws\Crypto\AbstractCryptoClientV2;
6 use Dudlewebs\WPMCS\s3\Aws\Crypto\EncryptionTraitV2;
7 use Dudlewebs\WPMCS\s3\Aws\Crypto\MetadataEnvelope;
8 use Dudlewebs\WPMCS\s3\Aws\Crypto\Cipher\CipherBuilderTrait;
9 use Dudlewebs\WPMCS\s3\Aws\S3\MultipartUploader;
10 use Dudlewebs\WPMCS\s3\Aws\S3\S3ClientInterface;
11 use Dudlewebs\WPMCS\s3\GuzzleHttp\Promise;
12 /**
13 * Encapsulates the execution of a multipart upload of an encrypted object to S3.
14 *
15 * Note that for PHP versions of < 7.1, this class uses an AES-GCM polyfill
16 * for encryption since there is no native PHP support. The performance for large
17 * inputs will be a lot slower than for PHP 7.1+, so upgrading older PHP version
18 * environments may be necessary to use this effectively.
19 */
20 class S3EncryptionMultipartUploaderV2 extends MultipartUploader
21 {
22 use CipherBuilderTrait;
23 use CryptoParamsTraitV2;
24 use EncryptionTraitV2;
25 use UserAgentTrait;
26 const CRYPTO_VERSION = '2.1';
27 /**
28 * Returns if the passed cipher name is supported for encryption by the SDK.
29 *
30 * @param string $cipherName The name of a cipher to verify is registered.
31 *
32 * @return bool If the cipher passed is in our supported list.
33 */
34 public static function isSupportedCipher($cipherName)
35 {
36 return \in_array($cipherName, AbstractCryptoClientV2::$supportedCiphers);
37 }
38 private $provider;
39 private $instructionFileSuffix;
40 private $strategy;
41 /**
42 * Creates a multipart upload for an S3 object after encrypting it.
43 *
44 * Note that for PHP versions of < 7.1, this class uses an AES-GCM polyfill
45 * for encryption since there is no native PHP support. The performance for
46 * large inputs will be a lot slower than for PHP 7.1+, so upgrading older
47 * PHP version environments may be necessary to use this effectively.
48 *
49 * The required configuration options are as follows:
50 *
51 * - @MaterialsProvider: (MaterialsProviderV2) Provides Cek, Iv, and Cek
52 * encrypting/decrypting for encryption metadata.
53 * - @CipherOptions: (array) Cipher options for encrypting data. A Cipher
54 * is required. Accepts the following options:
55 * - Cipher: (string) gcm
56 * See also: AbstractCryptoClientV2::$supportedCiphers
57 * - KeySize: (int) 128|256
58 * See also: MaterialsProvider::$supportedKeySizes
59 * - Aad: (string) Additional authentication data. This option is
60 * passed directly to OpenSSL when using gcm.
61 * - @KmsEncryptionContext: (array) Only required if using
62 * KmsMaterialsProviderV2. An associative array of key-value
63 * pairs to be added to the encryption context for KMS key encryption. An
64 * empty array may be passed if no additional context is desired.
65 * - bucket: (string) Name of the bucket to which the object is
66 * being uploaded.
67 * - key: (string) Key to use for the object being uploaded.
68 *
69 * The optional configuration arguments are as follows:
70 *
71 * - @MetadataStrategy: (MetadataStrategy|string|null) Strategy for storing
72 * MetadataEnvelope information. Defaults to using a
73 * HeadersMetadataStrategy. Can either be a class implementing
74 * MetadataStrategy, a class name of a predefined strategy, or empty/null
75 * to default.
76 * - @InstructionFileSuffix: (string|null) Suffix used when writing to an
77 * instruction file if an using an InstructionFileMetadataHandler was
78 * determined.
79 * - acl: (string) ACL to set on the object being upload. Objects are
80 * private by default.
81 * - before_complete: (callable) Callback to invoke before the
82 * `CompleteMultipartUpload` operation. The callback should have a
83 * function signature like `function (Aws\Command $command) {...}`.
84 * - before_initiate: (callable) Callback to invoke before the
85 * `CreateMultipartUpload` operation. The callback should have a function
86 * signature like `function (Aws\Command $command) {...}`.
87 * - before_upload: (callable) Callback to invoke before any `UploadPart`
88 * operations. The callback should have a function signature like
89 * `function (Aws\Command $command) {...}`.
90 * - concurrency: (int, default=int(5)) Maximum number of concurrent
91 * `UploadPart` operations allowed during the multipart upload.
92 * - params: (array) An array of key/value parameters that will be applied
93 * to each of the sub-commands run by the uploader as a base.
94 * Auto-calculated options will override these parameters. If you need
95 * more granularity over parameters to each sub-command, use the before_*
96 * options detailed above to update the commands directly.
97 * - part_size: (int, default=int(5242880)) Part size, in bytes, to use when
98 * doing a multipart upload. This must between 5 MB and 5 GB, inclusive.
99 * - state: (Aws\Multipart\UploadState) An object that represents the state
100 * of the multipart upload and that is used to resume a previous upload.
101 * When this option is provided, the `bucket`, `key`, and `part_size`
102 * options are ignored.
103 *
104 * @param S3ClientInterface $client Client used for the upload.
105 * @param mixed $source Source of the data to upload.
106 * @param array $config Configuration used to perform the upload.
107 */
108 public function __construct(S3ClientInterface $client, $source, array $config = [])
109 {
110 $this->appendUserAgent($client, 'feat/s3-encrypt/' . self::CRYPTO_VERSION);
111 $this->client = $client;
112 $config['params'] = [];
113 if (!empty($config['bucket'])) {
114 $config['params']['Bucket'] = $config['bucket'];
115 }
116 if (!empty($config['key'])) {
117 $config['params']['Key'] = $config['key'];
118 }
119 $this->provider = $this->getMaterialsProvider($config);
120 unset($config['@MaterialsProvider']);
121 $this->instructionFileSuffix = $this->getInstructionFileSuffix($config);
122 unset($config['@InstructionFileSuffix']);
123 $this->strategy = $this->getMetadataStrategy($config, $this->instructionFileSuffix);
124 if ($this->strategy === null) {
125 $this->strategy = self::getDefaultStrategy();
126 }
127 unset($config['@MetadataStrategy']);
128 $config['prepare_data_source'] = $this->getEncryptingDataPreparer();
129 parent::__construct($client, $source, $config);
130 }
131 private static function getDefaultStrategy()
132 {
133 return new HeadersMetadataStrategy();
134 }
135 private function getEncryptingDataPreparer()
136 {
137 return function () {
138 // Defer encryption work until promise is executed
139 $envelope = new MetadataEnvelope();
140 list($this->source, $params) = Promise\Create::promiseFor($this->encrypt($this->source, $this->config ?: [], $this->provider, $envelope))->then(function ($bodyStream) use($envelope) {
141 $params = $this->strategy->save($envelope, $this->config['params']);
142 return [$bodyStream, $params];
143 })->wait();
144 $this->source->rewind();
145 $this->config['params'] = $params;
146 };
147 }
148 }
149