Skip to content

fix: allow multi-tenant queries with allow_plaintext #1240

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module QueryTransform {
import DDB = ComAmazonawsDynamodbTypes
import opened AwsCryptographyDbEncryptionSdkDynamoDbTransformsTypes
import EncTypes = AwsCryptographyDbEncryptionSdkDynamoDbItemEncryptorTypes
import Seq
import EncOps = AwsCryptographyDbEncryptionSdkDynamoDbItemEncryptorOperations

method Input(config: Config, input: QueryInputTransformInput)
returns (output: Result<QueryInputTransformOutput, Error>)
Expand Down Expand Up @@ -90,10 +90,19 @@ module QueryTransform {
//# with the resulting decrypted [DynamoDB Item](./decrypt-item.md#dynamodb-item-1).
var decryptInput := EncTypes.DecryptItemInput(encryptedItem := encryptedItems[x]);
var decryptRes := tableConfig.itemEncryptor.DecryptItem(decryptInput);

var decrypted :- MapError(decryptRes);
if keyId.KeyId? {
:- Need(decrypted.parsedHeader.Some?, E("Decrypted query result has no parsed header."));

// If the decrypted result was plaintext, i.e. has no parsedHeader
// then this is expected IFF the table config allows plaintext read
assert decrypted.parsedHeader.None? ==>
&& EncOps.IsPlaintextItem(encryptedItems[x])
&& !tableConfig.plaintextOverride.FORBID_PLAINTEXT_WRITE_FORBID_PLAINTEXT_READ?
&& (
|| tableConfig.plaintextOverride.FORBID_PLAINTEXT_WRITE_ALLOW_PLAINTEXT_READ?
|| tableConfig.plaintextOverride.FORCE_PLAINTEXT_WRITE_ALLOW_PLAINTEXT_READ?
);

if keyId.KeyId? && decrypted.parsedHeader.Some? {
:- Need(|decrypted.parsedHeader.value.encryptedDataKeys| == 1, E("Query result has more than one Encrypted Data Key"));
if decrypted.parsedHeader.value.encryptedDataKeys[0].keyProviderInfo == keyIdUtf8 {
decryptedItems := decryptedItems + [decrypted.plaintextItem];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module ScanTransform {
import DDB = ComAmazonawsDynamodbTypes
import opened AwsCryptographyDbEncryptionSdkDynamoDbTransformsTypes
import EncTypes = AwsCryptographyDbEncryptionSdkDynamoDbItemEncryptorTypes
import Seq
import EncOps = AwsCryptographyDbEncryptionSdkDynamoDbItemEncryptorOperations

method Input(config: Config, input: ScanInputTransformInput)
returns (output: Result<ScanInputTransformOutput, Error>)
Expand Down Expand Up @@ -88,10 +88,19 @@ module ScanTransform {

var decryptInput := EncTypes.DecryptItemInput(encryptedItem := encryptedItems[x]);
var decryptRes := tableConfig.itemEncryptor.DecryptItem(decryptInput);

var decrypted :- MapError(decryptRes);
if keyId.KeyId? {
:- Need(decrypted.parsedHeader.Some?, E("Decrypted scan result has no parsed header."));

// If the decrypted result was plaintext, i.e. has no parsedHeader
// then this is expected IFF the table config allows plaintext read
assert decrypted.parsedHeader.None? ==>
&& EncOps.IsPlaintextItem(encryptedItems[x])
&& !tableConfig.plaintextOverride.FORBID_PLAINTEXT_WRITE_FORBID_PLAINTEXT_READ?
&& (
|| tableConfig.plaintextOverride.FORBID_PLAINTEXT_WRITE_ALLOW_PLAINTEXT_READ?
|| tableConfig.plaintextOverride.FORCE_PLAINTEXT_WRITE_ALLOW_PLAINTEXT_READ?
);
Comment on lines +95 to +101
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: since this assert is the same as the one in QueryTransform, it would be nice to split into a predicate and just call it.


if keyId.KeyId? && decrypted.parsedHeader.Some? {
:- Need(|decrypted.parsedHeader.value.encryptedDataKeys| == 1, E("Scan result has more than one Encrypted Data Key"));
if decrypted.parsedHeader.value.encryptedDataKeys[0].keyProviderInfo == keyIdUtf8 {
decryptedItems := decryptedItems + [decrypted.plaintextItem];
Expand Down
Loading