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 / S3EncryptionMultipartUploader.php

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

142 lines 6.5 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\AbstractCryptoClient;
6 use Dudlewebs\WPMCS\s3\Aws\Crypto\EncryptionTrait;
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 * Legacy implementation using older encryption workflow. Use
16 * S3EncryptionMultipartUploaderV2 if possible.
17 *
18 * @deprecated
19 */
20 class S3EncryptionMultipartUploader extends MultipartUploader
21 {
22 use CipherBuilderTrait;
23 use CryptoParamsTrait;
24 use EncryptionTrait;
25 use UserAgentTrait;
26 const CRYPTO_VERSION = '1n';
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, AbstractCryptoClient::$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 * The required configuration options are as follows:
45 *
46 * - @MaterialsProvider: (MaterialsProvider) Provides Cek, Iv, and Cek
47 * encrypting/decrypting for encryption metadata.
48 * - @CipherOptions: (array) Cipher options for encrypting data. A Cipher
49 * is required. Accepts the following options:
50 * - Cipher: (string) cbc|gcm
51 * See also: AbstractCryptoClient::$supportedCiphers. Note that
52 * cbc is deprecated and gcm should be used when possible.
53 * - KeySize: (int) 128|192|256
54 * See also: MaterialsProvider::$supportedKeySizes
55 * - Aad: (string) Additional authentication data. This option is
56 * passed directly to OpenSSL when using gcm. It is ignored when
57 * using cbc.
58 * - bucket: (string) Name of the bucket to which the object is
59 * being uploaded.
60 * - key: (string) Key to use for the object being uploaded.
61 *
62 * The optional configuration arguments are as follows:
63 *
64 * - @MetadataStrategy: (MetadataStrategy|string|null) Strategy for storing
65 * MetadataEnvelope information. Defaults to using a
66 * HeadersMetadataStrategy. Can either be a class implementing
67 * MetadataStrategy, a class name of a predefined strategy, or empty/null
68 * to default.
69 * - @InstructionFileSuffix: (string|null) Suffix used when writing to an
70 * instruction file if an using an InstructionFileMetadataHandler was
71 * determined.
72 * - acl: (string) ACL to set on the object being upload. Objects are
73 * private by default.
74 * - before_complete: (callable) Callback to invoke before the
75 * `CompleteMultipartUpload` operation. The callback should have a
76 * function signature like `function (Aws\Command $command) {...}`.
77 * - before_initiate: (callable) Callback to invoke before the
78 * `CreateMultipartUpload` operation. The callback should have a function
79 * signature like `function (Aws\Command $command) {...}`.
80 * - before_upload: (callable) Callback to invoke before any `UploadPart`
81 * operations. The callback should have a function signature like
82 * `function (Aws\Command $command) {...}`.
83 * - concurrency: (int, default=int(5)) Maximum number of concurrent
84 * `UploadPart` operations allowed during the multipart upload.
85 * - params: (array) An array of key/value parameters that will be applied
86 * to each of the sub-commands run by the uploader as a base.
87 * Auto-calculated options will override these parameters. If you need
88 * more granularity over parameters to each sub-command, use the before_*
89 * options detailed above to update the commands directly.
90 * - part_size: (int, default=int(5242880)) Part size, in bytes, to use when
91 * doing a multipart upload. This must between 5 MB and 5 GB, inclusive.
92 * - state: (Aws\Multipart\UploadState) An object that represents the state
93 * of the multipart upload and that is used to resume a previous upload.
94 * When this option is provided, the `bucket`, `key`, and `part_size`
95 * options are ignored.
96 *
97 * @param S3ClientInterface $client Client used for the upload.
98 * @param mixed $source Source of the data to upload.
99 * @param array $config Configuration used to perform the upload.
100 */
101 public function __construct(S3ClientInterface $client, $source, array $config = [])
102 {
103 $this->appendUserAgent($client, 'feat/s3-encrypt/' . self::CRYPTO_VERSION);
104 $this->client = $client;
105 $config['params'] = [];
106 if (!empty($config['bucket'])) {
107 $config['params']['Bucket'] = $config['bucket'];
108 }
109 if (!empty($config['key'])) {
110 $config['params']['Key'] = $config['key'];
111 }
112 $this->provider = $this->getMaterialsProvider($config);
113 unset($config['@MaterialsProvider']);
114 $this->instructionFileSuffix = $this->getInstructionFileSuffix($config);
115 unset($config['@InstructionFileSuffix']);
116 $this->strategy = $this->getMetadataStrategy($config, $this->instructionFileSuffix);
117 if ($this->strategy === null) {
118 $this->strategy = self::getDefaultStrategy();
119 }
120 unset($config['@MetadataStrategy']);
121 $config['prepare_data_source'] = $this->getEncryptingDataPreparer();
122 parent::__construct($client, $source, $config);
123 }
124 private static function getDefaultStrategy()
125 {
126 return new HeadersMetadataStrategy();
127 }
128 private function getEncryptingDataPreparer()
129 {
130 return function () {
131 // Defer encryption work until promise is executed
132 $envelope = new MetadataEnvelope();
133 list($this->source, $params) = Promise\Create::promiseFor($this->encrypt($this->source, $this->config['@cipheroptions'] ?: [], $this->provider, $envelope))->then(function ($bodyStream) use($envelope) {
134 $params = $this->strategy->save($envelope, $this->config['params']);
135 return [$bodyStream, $params];
136 })->wait();
137 $this->source->rewind();
138 $this->config['params'] = $params;
139 };
140 }
141 }
142