|
| 1 | +package software.amazon.encryption.s3.internal; |
| 2 | + |
| 3 | +import software.amazon.encryption.s3.S3EncryptionClientException; |
| 4 | +import software.amazon.encryption.s3.materials.AesKeyring; |
| 5 | +import software.amazon.encryption.s3.materials.KmsKeyring; |
| 6 | +import software.amazon.encryption.s3.materials.RsaKeyring; |
| 7 | +import software.amazon.encryption.s3.materials.S3Keyring; |
| 8 | + |
| 9 | +/** Request object for re-encrypting instruction files. |
| 10 | + * Supports both AES and RSA keyring with different instruction file suffixes. |
| 11 | + */ |
| 12 | +public class ReEncryptInstructionFileRequest { |
| 13 | + private final String bucket; |
| 14 | + private final String key; |
| 15 | + private final S3Keyring newKeyring; |
| 16 | + private final String instructionFileSuffix; |
| 17 | + |
| 18 | + private ReEncryptInstructionFileRequest(Builder builder) { |
| 19 | + bucket = builder.bucket; |
| 20 | + key = builder.key; |
| 21 | + newKeyring = builder.newKeyring; |
| 22 | + instructionFileSuffix = builder.instructionFileSuffix; |
| 23 | + } |
| 24 | + public static Builder builder() { |
| 25 | + return new Builder(); |
| 26 | + } |
| 27 | + public static class Builder { |
| 28 | + private static final String DEFAULT_INSTRUCTION_FILE_SUFFIX = ".instruction"; |
| 29 | + private String bucket; |
| 30 | + private String key; |
| 31 | + private S3Keyring newKeyring; |
| 32 | + private String instructionFileSuffix = DEFAULT_INSTRUCTION_FILE_SUFFIX; |
| 33 | + |
| 34 | + public Builder bucket(String bucket) { |
| 35 | + this.bucket = bucket; |
| 36 | + return this; |
| 37 | + } |
| 38 | + |
| 39 | + public Builder key(String key) { |
| 40 | + this.key = key; |
| 41 | + return this; |
| 42 | + } |
| 43 | + |
| 44 | + public Builder newKeyring(S3Keyring newKeyring) { |
| 45 | + this.newKeyring = newKeyring; |
| 46 | + return this; |
| 47 | + } |
| 48 | + |
| 49 | + public Builder instructionFileSuffix(String instructionFileSuffix) { |
| 50 | + this.instructionFileSuffix = instructionFileSuffix; |
| 51 | + return this; |
| 52 | + } |
| 53 | + |
| 54 | + public ReEncryptInstructionFileRequest build() { |
| 55 | + if (bucket == null || bucket.isEmpty()) { |
| 56 | + throw new S3EncryptionClientException("Bucket must be provided!"); |
| 57 | + } |
| 58 | + if (key == null || key.isEmpty()) { |
| 59 | + throw new S3EncryptionClientException("Key must be provided!"); |
| 60 | + } |
| 61 | + if (newKeyring == null) { |
| 62 | + throw new S3EncryptionClientException("New keyring must be provided!"); |
| 63 | + } |
| 64 | + if (newKeyring instanceof AesKeyring) { |
| 65 | + if (!instructionFileSuffix.equals(DEFAULT_INSTRUCTION_FILE_SUFFIX)) { |
| 66 | + throw new S3EncryptionClientException("Instruction file suffix is not applicable for AES keyring!"); |
| 67 | + } |
| 68 | + } else if (newKeyring instanceof RsaKeyring) { |
| 69 | + if (instructionFileSuffix.equals(DEFAULT_INSTRUCTION_FILE_SUFFIX)) { |
| 70 | + throw new S3EncryptionClientException("Instruction file suffix must be different than the default one for RSA keyring!"); |
| 71 | + } |
| 72 | + } else if (newKeyring instanceof KmsKeyring){ |
| 73 | + throw new S3EncryptionClientException("KMS keyring is not supported for re-encrypting instruction file!"); |
| 74 | + } |
| 75 | + return new ReEncryptInstructionFileRequest(this); |
| 76 | + } |
| 77 | + |
| 78 | + } |
| 79 | + |
| 80 | +} |
0 commit comments