123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340 |
- <?php
- namespace Aws\S3\Crypto;
- use Aws\Crypto\DecryptionTrait;
- use Aws\HashingStream;
- use Aws\PhpHash;
- use Aws\Crypto\AbstractCryptoClient;
- use Aws\Crypto\EncryptionTrait;
- use Aws\Crypto\MetadataEnvelope;
- use Aws\Crypto\MaterialsProvider;
- use Aws\Crypto\Cipher\CipherBuilderTrait;
- use Aws\S3\S3Client;
- use GuzzleHttp\Promise;
- use GuzzleHttp\Promise\PromiseInterface;
- use GuzzleHttp\Psr7;
- class S3EncryptionClient extends AbstractCryptoClient
- {
- use CipherBuilderTrait;
- use CryptoParamsTrait;
- use DecryptionTrait;
- use EncryptionTrait;
- use UserAgentTrait;
- const CRYPTO_VERSION = '1n';
- private $client;
- private $instructionFileSuffix;
-
- public function __construct(
- S3Client $client,
- $instructionFileSuffix = null
- ) {
- $this->appendUserAgent($client, 'feat/s3-encrypt/' . self::CRYPTO_VERSION);
- $this->client = $client;
- $this->instructionFileSuffix = $instructionFileSuffix;
- }
- private static function getDefaultStrategy()
- {
- return new HeadersMetadataStrategy();
- }
-
- public function putObjectAsync(array $args)
- {
- $provider = $this->getMaterialsProvider($args);
- unset($args['@MaterialsProvider']);
- $instructionFileSuffix = $this->getInstructionFileSuffix($args);
- unset($args['@InstructionFileSuffix']);
- $strategy = $this->getMetadataStrategy($args, $instructionFileSuffix);
- unset($args['@MetadataStrategy']);
- $envelope = new MetadataEnvelope();
- return Promise\Create::promiseFor($this->encrypt(
- Psr7\Utils::streamFor($args['Body']),
- $args['@CipherOptions'] ?: [],
- $provider,
- $envelope
- ))->then(
- function ($encryptedBodyStream) use ($args) {
- $hash = new PhpHash('sha256');
- $hashingEncryptedBodyStream = new HashingStream(
- $encryptedBodyStream,
- $hash,
- self::getContentShaDecorator($args)
- );
- return [$hashingEncryptedBodyStream, $args];
- }
- )->then(
- function ($putObjectContents) use ($strategy, $envelope) {
- list($bodyStream, $args) = $putObjectContents;
- if ($strategy === null) {
- $strategy = self::getDefaultStrategy();
- }
- $updatedArgs = $strategy->save($envelope, $args);
- $updatedArgs['Body'] = $bodyStream;
- return $updatedArgs;
- }
- )->then(
- function ($args) {
- unset($args['@CipherOptions']);
- return $this->client->putObjectAsync($args);
- }
- );
- }
- private static function getContentShaDecorator(&$args)
- {
- return function ($hash) use (&$args) {
- $args['ContentSHA256'] = bin2hex($hash);
- };
- }
-
- public function putObject(array $args)
- {
- return $this->putObjectAsync($args)->wait();
- }
-
- public function getObjectAsync(array $args)
- {
- $provider = $this->getMaterialsProvider($args);
- unset($args['@MaterialsProvider']);
- $instructionFileSuffix = $this->getInstructionFileSuffix($args);
- unset($args['@InstructionFileSuffix']);
- $strategy = $this->getMetadataStrategy($args, $instructionFileSuffix);
- unset($args['@MetadataStrategy']);
- $saveAs = null;
- if (!empty($args['SaveAs'])) {
- $saveAs = $args['SaveAs'];
- }
- $promise = $this->client->getObjectAsync($args)
- ->then(
- function ($result) use (
- $provider,
- $instructionFileSuffix,
- $strategy,
- $args
- ) {
- if ($strategy === null) {
- $strategy = $this->determineGetObjectStrategy(
- $result,
- $instructionFileSuffix
- );
- }
- $envelope = $strategy->load($args + [
- 'Metadata' => $result['Metadata']
- ]);
- $provider = $provider->fromDecryptionEnvelope($envelope);
- $result['Body'] = $this->decrypt(
- $result['Body'],
- $provider,
- $envelope,
- isset($args['@CipherOptions'])
- ? $args['@CipherOptions']
- : []
- );
- return $result;
- }
- )->then(
- function ($result) use ($saveAs) {
- if (!empty($saveAs)) {
- file_put_contents(
- $saveAs,
- (string)$result['Body'],
- LOCK_EX
- );
- }
- return $result;
- }
- );
- return $promise;
- }
-
- public function getObject(array $args)
- {
- return $this->getObjectAsync($args)->wait();
- }
- }
|