|
| 1 | +/* |
| 2 | + * Copyright (c) 2020 Temporal Technologies, Inc. All Rights Reserved |
| 3 | + * |
| 4 | + * Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 5 | + * |
| 6 | + * Modifications copyright (C) 2017 Uber Technologies, Inc. |
| 7 | + * |
| 8 | + * Licensed under the Apache License, Version 2.0 (the "License"). You may not |
| 9 | + * use this file except in compliance with the License. A copy of the License is |
| 10 | + * located at |
| 11 | + * |
| 12 | + * http://aws.amazon.com/apache2.0 |
| 13 | + * |
| 14 | + * or in the "license" file accompanying this file. This file is distributed on |
| 15 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 16 | + * express or implied. See the License for the specific language governing |
| 17 | + * permissions and limitations under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package io.temporal.samples.keymanagementencryption.awsencryptionsdk; |
| 21 | + |
| 22 | +import com.amazonaws.encryptionsdk.AwsCrypto; |
| 23 | +import com.google.protobuf.ByteString; |
| 24 | +import com.google.protobuf.InvalidProtocolBufferException; |
| 25 | +import io.temporal.api.common.v1.Payload; |
| 26 | +import io.temporal.common.converter.EncodingKeys; |
| 27 | +import io.temporal.payload.codec.PayloadCodec; |
| 28 | +import io.temporal.payload.context.ActivitySerializationContext; |
| 29 | +import io.temporal.payload.context.HasWorkflowSerializationContext; |
| 30 | +import io.temporal.payload.context.SerializationContext; |
| 31 | +import io.temporal.workflow.unsafe.WorkflowUnsafe; |
| 32 | +import java.nio.charset.StandardCharsets; |
| 33 | +import java.util.Collections; |
| 34 | +import java.util.List; |
| 35 | +import java.util.Map; |
| 36 | +import java.util.stream.Collectors; |
| 37 | +import javax.annotation.Nonnull; |
| 38 | +import javax.annotation.Nullable; |
| 39 | +import org.jetbrains.annotations.NotNull; |
| 40 | +import software.amazon.cryptography.materialproviders.IKeyring; |
| 41 | + |
| 42 | +/** |
| 43 | + * KeyringCodec is a {@link PayloadCodec} that encrypts and decrypts payloads using the AWS |
| 44 | + * Encryption SDK. It uses the provided {@link IKeyring} to encrypt and decrypt payloads. It can |
| 45 | + * optionally support using a {@link SerializationContext}. |
| 46 | + */ |
| 47 | +class KeyringCodec implements PayloadCodec { |
| 48 | + // Metadata encoding key for the AWS Encryption SDK |
| 49 | + static final ByteString METADATA_ENCODING = |
| 50 | + ByteString.copyFrom("awsencriptionsdk/binary/encrypted", StandardCharsets.UTF_8); |
| 51 | + |
| 52 | + private final AwsCrypto crypto; |
| 53 | + private final IKeyring kmsKeyring; |
| 54 | + private final boolean useSerializationContext; |
| 55 | + @Nullable private final SerializationContext serializationContext; |
| 56 | + |
| 57 | + /** |
| 58 | + * Constructs a new KeyringCodec with the provided {@link IKeyring}. The codec will not use a |
| 59 | + * {@link SerializationContext}. |
| 60 | + * |
| 61 | + * @param kmsKeyring the keyring to use for encryption and decryption. |
| 62 | + */ |
| 63 | + public KeyringCodec(IKeyring kmsKeyring) { |
| 64 | + this.crypto = AwsCrypto.standard(); |
| 65 | + this.kmsKeyring = kmsKeyring; |
| 66 | + this.useSerializationContext = false; |
| 67 | + this.serializationContext = null; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Constructs a new KeyringCodec with the provided {@link IKeyring}. |
| 72 | + * |
| 73 | + * @param crypto the AWS Crypto object to use for encryption and decryption. |
| 74 | + * @param kmsKeyring the keyring to use for encryption and decryption. |
| 75 | + * @param useSerializationContext whether to use a {@link SerializationContext} for encoding and |
| 76 | + * decoding payloads. |
| 77 | + */ |
| 78 | + public KeyringCodec(AwsCrypto crypto, IKeyring kmsKeyring, boolean useSerializationContext) { |
| 79 | + this.crypto = crypto; |
| 80 | + this.kmsKeyring = kmsKeyring; |
| 81 | + this.useSerializationContext = useSerializationContext; |
| 82 | + this.serializationContext = null; |
| 83 | + } |
| 84 | + |
| 85 | + private KeyringCodec( |
| 86 | + AwsCrypto crypto, IKeyring kmsKeyring, SerializationContext serializationContext) { |
| 87 | + this.crypto = crypto; |
| 88 | + this.kmsKeyring = kmsKeyring; |
| 89 | + this.useSerializationContext = true; |
| 90 | + this.serializationContext = serializationContext; |
| 91 | + } |
| 92 | + |
| 93 | + @NotNull |
| 94 | + @Override |
| 95 | + public List<Payload> encode(@NotNull List<Payload> payloads) { |
| 96 | + // Disable deadlock detection for encoding payloads because this may make a network call |
| 97 | + // to encrypt the data. |
| 98 | + return WorkflowUnsafe.deadlockDetectorOff( |
| 99 | + () -> payloads.stream().map(this::encodePayload).collect(Collectors.toList())); |
| 100 | + } |
| 101 | + |
| 102 | + @NotNull |
| 103 | + @Override |
| 104 | + public List<Payload> decode(@NotNull List<Payload> payloads) { |
| 105 | + // Disable deadlock detection for decoding payloads because this may make a network call |
| 106 | + // to decrypt the data. |
| 107 | + return WorkflowUnsafe.deadlockDetectorOff( |
| 108 | + () -> payloads.stream().map(this::decodePayload).collect(Collectors.toList())); |
| 109 | + } |
| 110 | + |
| 111 | + @NotNull |
| 112 | + @Override |
| 113 | + public PayloadCodec withContext(@Nonnull SerializationContext context) { |
| 114 | + if (!useSerializationContext) { |
| 115 | + return this; |
| 116 | + } |
| 117 | + return new KeyringCodec(crypto, kmsKeyring, context); |
| 118 | + } |
| 119 | + |
| 120 | + private Map<String, String> getEncryptionContext() { |
| 121 | + // If we are not using a serialization context, return an empty map |
| 122 | + // There may not be a serialization context if certain cases, such as when the codec is used |
| 123 | + // for encoding/decoding payloads for a Nexus operation. |
| 124 | + if (!useSerializationContext |
| 125 | + || serializationContext == null |
| 126 | + || !(serializationContext instanceof HasWorkflowSerializationContext)) { |
| 127 | + return Collections.emptyMap(); |
| 128 | + } |
| 129 | + String workflowId = ((HasWorkflowSerializationContext) serializationContext).getWorkflowId(); |
| 130 | + String activityType = null; |
| 131 | + if (serializationContext instanceof ActivitySerializationContext) { |
| 132 | + activityType = ((ActivitySerializationContext) serializationContext).getActivityType(); |
| 133 | + } |
| 134 | + String signature = activityType != null ? workflowId + activityType : workflowId; |
| 135 | + return Collections.singletonMap("signature", signature); |
| 136 | + } |
| 137 | + |
| 138 | + private Payload encodePayload(Payload payload) { |
| 139 | + byte[] plaintext = payload.toByteArray(); |
| 140 | + byte[] ciphertext = |
| 141 | + crypto.encryptData(kmsKeyring, plaintext, getEncryptionContext()).getResult(); |
| 142 | + return Payload.newBuilder() |
| 143 | + .setData(ByteString.copyFrom(ciphertext)) |
| 144 | + .putMetadata(EncodingKeys.METADATA_ENCODING_KEY, METADATA_ENCODING) |
| 145 | + .build(); |
| 146 | + } |
| 147 | + |
| 148 | + private Payload decodePayload(Payload payload) { |
| 149 | + if (METADATA_ENCODING.equals( |
| 150 | + payload.getMetadataOrDefault(EncodingKeys.METADATA_ENCODING_KEY, null))) { |
| 151 | + byte[] ciphertext = payload.getData().toByteArray(); |
| 152 | + byte[] plaintext = |
| 153 | + crypto.decryptData(kmsKeyring, ciphertext, getEncryptionContext()).getResult(); |
| 154 | + try { |
| 155 | + return Payload.parseFrom(plaintext); |
| 156 | + } catch (InvalidProtocolBufferException e) { |
| 157 | + throw new RuntimeException(e); |
| 158 | + } |
| 159 | + } |
| 160 | + return payload; |
| 161 | + } |
| 162 | +} |
0 commit comments