lib/3rdParty/composer/aws/aws-sdk-php/src/S3/Crypto/InstructionFileMetadataStrategy.php
raw symbols SCIP| 1 | <?php |
| 2 | namespace Aws\S3\Crypto; |
| 3 | |
| 4 | use \Aws\Crypto\MetadataStrategyInterface; |
| 5 | use \Aws\Crypto\MetadataEnvelope; |
| 6 | use Aws\Exception\CryptoException; |
| 7 | use \Aws\S3\S3Client; |
| 8 | |
| 9 | /** |
| 10 | * Stores and reads encryption MetadataEnvelope information in a file on Amazon |
| 11 | * S3. |
| 12 | * |
| 13 | * A file with the contents of a MetadataEnvelope will be created or read from |
| 14 | * alongside the base file on Amazon S3. The provided client will be used for |
| 15 | * reading or writing this object. A specified suffix (default of '.instruction' |
| 16 | * will be applied to each of the operations involved with the instruction file. |
| 17 | * |
| 18 | * If there is a failure after an instruction file has been uploaded, it will |
| 19 | * not be automatically deleted. |
| 20 | */ |
| 21 | class InstructionFileMetadataStrategy implements MetadataStrategyInterface |
| 22 | { |
| 23 | const DEFAULT_FILE_SUFFIX = '.instruction'; |
| 24 | |
| 25 | private $client; |
| 26 | private $suffix; |
| 27 | |
| 28 | /** |
| 29 | * @param S3Client $client Client for use in uploading the instruction file. |
| 30 | * @param string|null $suffix Optional override suffix for instruction file |
| 31 | * object keys. |
| 32 | */ |
| 33 | public function __construct(S3Client $client, $suffix = null) |
| 34 | { |
| 35 | $this->suffix = empty($suffix) |
| 36 | ? self::DEFAULT_FILE_SUFFIX |
| 37 | : $suffix; |
| 38 | $this->client = $client; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Places the information in the MetadataEnvelope to a location on S3. |
| 43 | * |
| 44 | * @param MetadataEnvelope $envelope Encryption data to save according to |
| 45 | * the strategy. |
| 46 | * @param array $args Starting arguments for PutObject, used for saving |
| 47 | * extra the instruction file. |
| 48 | * |
| 49 | * @return array Updated arguments for PutObject. |
| 50 | */ |
| 51 | public function save(MetadataEnvelope $envelope, array $args) |
| 52 | { |
| 53 | //= ../specification/s3-encryption/data-format/metadata-strategy.md#instruction-file |
| 54 | //# The S3EC MUST support writing some or all (depending on format) content metadata to an Instruction File. |
| 55 | |
| 56 | //= ../specification/s3-encryption/data-format/metadata-strategy.md#v1-v2-instruction-files |
| 57 | //# In the V1/V2 message format, all of the content metadata MUST be stored in the Instruction File. |
| 58 | |
| 59 | //= ../specification/s3-encryption/data-format/content-metadata.md#content-metadata-mapkeys |
| 60 | //= type=implication |
| 61 | //# In the V3 format, the mapkeys "x-amz-c", "x-amz-d", and "x-amz-i" MUST be stored exclusively in the Object Metadata. |
| 62 | if (MetadataEnvelope::isV3Envelope($envelope)) { |
| 63 | //= ../specification/s3-encryption/data-format/metadata-strategy.md#v3-instruction-files |
| 64 | //# - The V3 message format MUST store the mapkey "x-amz-c" and its value in the Object Metadata when writing with an Instruction File. |
| 65 | $args['Metadata'][MetadataEnvelope::CONTENT_CIPHER_V3] = $envelope[MetadataEnvelope::CONTENT_CIPHER_V3]; |
| 66 | //= ../specification/s3-encryption/data-format/metadata-strategy.md#v3-instruction-files |
| 67 | //# - The V3 message format MUST store the mapkey "x-amz-d" and its value in the Object Metadata when writing with an Instruction File. |
| 68 | $args['Metadata'][MetadataEnvelope::KEY_COMMITMENT_V3] = $envelope[MetadataEnvelope::KEY_COMMITMENT_V3]; |
| 69 | //= ../specification/s3-encryption/data-format/metadata-strategy.md#v3-instruction-files |
| 70 | //# - The V3 message format MUST store the mapkey "x-amz-i" and its value in the Object Metadata when writing with an Instruction File. |
| 71 | $args['Metadata'][MetadataEnvelope::MESSAGE_ID_V3] = $envelope[MetadataEnvelope::MESSAGE_ID_V3]; |
| 72 | |
| 73 | //= ../specification/s3-encryption/data-format/metadata-strategy.md#v3-instruction-files |
| 74 | //# - The V3 message format MUST NOT store the mapkey "x-amz-c" and its value in the Instruction File. |
| 75 | unset($envelope[MetadataEnvelope::CONTENT_CIPHER_V3]); |
| 76 | //= ../specification/s3-encryption/data-format/metadata-strategy.md#v3-instruction-files |
| 77 | //# - The V3 message format MUST NOT store the mapkey "x-amz-d" and its value in the Instruction File. |
| 78 | unset($envelope[MetadataEnvelope::KEY_COMMITMENT_V3]); |
| 79 | //= ../specification/s3-encryption/data-format/metadata-strategy.md#v3-instruction-files |
| 80 | //# - The V3 message format MUST NOT store the mapkey "x-amz-i" and its value in the Instruction File. |
| 81 | unset($envelope[MetadataEnvelope::MESSAGE_ID_V3]); |
| 82 | if ( |
| 83 | //= ../specification/s3-encryption/data-format/metadata-strategy.md#v3-instruction-files |
| 84 | //# - The V3 message format MUST store the mapkey "x-amz-3" and its value in the Instruction File. |
| 85 | !isset($envelope[MetadataEnvelope::ENCRYPTED_DATA_KEY_V3]) |
| 86 | //= ../specification/s3-encryption/data-format/metadata-strategy.md#v3-instruction-files |
| 87 | //# - The V3 message format MUST store the mapkey "x-amz-w" and its value in the Instruction File. |
| 88 | || !isset($envelope[MetadataEnvelope::ENCRYPTED_DATA_KEY_ALGORITHM_V3]) |
| 89 | //= ../specification/s3-encryption/data-format/metadata-strategy.md#v3-instruction-files |
| 90 | //# - The V3 message format MUST store the mapkey "x-amz-t" and its value (when present in the content metadata) in the Instruction File. |
| 91 | || !isset($envelope[MetadataEnvelope::ENCRYPTION_CONTEXT_V3]) |
| 92 | ) { |
| 93 | throw new \InvalidArgumentException('Invalid V3 Envelope'); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | //= ../specification/s3-encryption/data-format/metadata-strategy.md#instruction-file |
| 98 | //# The serialized JSON string MUST be the only contents of the Instruction File. |
| 99 | $this->client->putObject([ |
| 100 | 'Bucket' => $args['Bucket'], |
| 101 | 'Key' => $args['Key'] . $this->suffix, |
| 102 | //= ../specification/s3-encryption/data-format/metadata-strategy.md#instruction-file |
| 103 | //= type=implication |
| 104 | //# The content metadata stored in the Instruction File MUST be serialized to a JSON string. |
| 105 | 'Body' => json_encode($envelope) |
| 106 | ]); |
| 107 | |
| 108 | return $args; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Uses the strategy's client to retrieve the instruction file from S3 and generates |
| 113 | * a MetadataEnvelope from its contents. |
| 114 | * |
| 115 | * @param array $args Arguments from Command and Result that contains |
| 116 | * S3 Object information, relevant headers, and command |
| 117 | * configuration. |
| 118 | * |
| 119 | * @return MetadataEnvelope |
| 120 | */ |
| 121 | public function load(array $args) |
| 122 | { |
| 123 | $result = $this->client->getObject([ |
| 124 | 'Bucket' => $args['Bucket'], |
| 125 | 'Key' => $args['Key'] . $this->suffix |
| 126 | ]); |
| 127 | |
| 128 | $metadataHeaders = json_decode($result['Body'], true); |
| 129 | $envelope = new MetadataEnvelope(); |
| 130 | $constantValues = MetadataEnvelope::getConstantValues(); |
| 131 | |
| 132 | foreach ($constantValues as $constant) { |
| 133 | if (isset($metadataHeaders[$constant])) { |
| 134 | // check for a duplicate |
| 135 | if (empty($envelope[$constant])) { |
| 136 | $envelope[$constant] = $metadataHeaders[$constant]; |
| 137 | } else { |
| 138 | throw new CryptoException("Duplicate keys are not allowed" |
| 139 | . " in Instruction Files. " |
| 140 | ); |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | // check if we are reading a V3 object |
| 146 | // if it is a V3 object some data is stored in the object metadata and some |
| 147 | // as in the instruction file |
| 148 | //= ../specification/s3-encryption/data-format/content-metadata.md#content-metadata-mapkeys |
| 149 | //# In the V3 format, the mapkeys "x-amz-c", "x-amz-d", and "x-amz-i" |
| 150 | //# MUST be stored exclusively in the Object Metadata |
| 151 | if (!empty($envelope[MetadataEnvelope::ENCRYPTED_DATA_KEY_V3])) { |
| 152 | // before loading the rest of the v3 metadata, we must check that: |
| 153 | // 1. the following values are not already present in the envelope, if they are |
| 154 | // the instruction file is not correct. |
| 155 | if (!empty($envelope[MetadataEnvelope::CONTENT_CIPHER_V3]) |
| 156 | || !empty($envelope[MetadataEnvelope::KEY_COMMITMENT_V3]) |
| 157 | || !empty($envelope[MetadataEnvelope::MESSAGE_ID_V3]) |
| 158 | ) { |
| 159 | throw new CryptoException("One or more reserved keys found in" |
| 160 | . " Instruction file when they should not be present."); |
| 161 | } |
| 162 | // this data is stored in the original object's metadata |
| 163 | // V3 added x-amz-c, x-amz-d, x-amz-i, x-amz-3, x-amz-w, x-amz-m, x-amz-t |
| 164 | // x-amz-c, x-amz-d, x-amz-i are strictly stored on the object metadata |
| 165 | // the rest are stored in the instruction file |
| 166 | $envelope[MetadataEnvelope::CONTENT_CIPHER_V3] = $args['Metadata'][MetadataEnvelope::CONTENT_CIPHER_V3]; |
| 167 | $envelope[MetadataEnvelope::KEY_COMMITMENT_V3] = $args['Metadata'][MetadataEnvelope::KEY_COMMITMENT_V3]; |
| 168 | $envelope[MetadataEnvelope::MESSAGE_ID_V3] = $args['Metadata'][MetadataEnvelope::MESSAGE_ID_V3]; |
| 169 | |
| 170 | if (!MetadataEnvelope::isV3Envelope($envelope)) { |
| 171 | throw new CryptoException("Expected a V3 envelope but was unable to" |
| 172 | . " constuct one."); |
| 173 | } |
| 174 | return $envelope; |
| 175 | } |
| 176 | // if we are not reading a v3 object then it must be a v2 object |
| 177 | if (!MetadataEnvelope::isV2Envelope($envelope) |
| 178 | && !MetadataEnvelope::isV1Envelope($envelope)) { |
| 179 | throw new CryptoException("Malformed metadata envelope."); |
| 180 | } |
| 181 | |
| 182 | return $envelope; |
| 183 | } |
| 184 | } |