summaryrefslogtreecommitdiff
path: root/jstests/client_encrypt/fle_aws_faults.js
blob: 28b06a55bee26a6820d68ac3319796855a9a740c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/**
 * Verify the AWS KMS implementation can handle a buggy KMS.
 */

load("jstests/client_encrypt/lib/mock_kms.js");
load('jstests/ssl/libs/ssl_helpers.js');

(function() {
"use strict";

const x509_options = {
    sslMode: "requireSSL",
    sslPEMKeyFile: SERVER_CERT,
    sslCAFile: CA_CERT
};

const randomAlgorithm = "AEAD_AES_256_CBC_HMAC_SHA_512-Random";

const conn = MongoRunner.runMongod(x509_options);
const test = conn.getDB("test");
const collection = test.coll;

function runKMS(mock_kms, func) {
    mock_kms.start();

    const awsKMS = {
        accessKeyId: "access",
        secretAccessKey: "secret",
        url: mock_kms.getURL(),
    };

    const clientSideFLEOptions = {
        kmsProviders: {
            aws: awsKMS,
        },
        keyVaultNamespace: "test.coll",
        schemaMap: {}
    };

    const shell = Mongo(conn.host, clientSideFLEOptions);
    const cleanCacheShell = Mongo(conn.host, clientSideFLEOptions);

    collection.drop();

    func(shell, cleanCacheShell);

    mock_kms.stop();
}

function testWrongKeyType() {
    const awsKMS = {accessKeyId: "access", secretAccessKey: "secret", url: "localhost:8000"};

    const clientSideFLEOptions = {
        kmsProviders: {
            aws: awsKMS,
        },
        keyVaultNamespace: "test.coll",
        schemaMap: {}
    };

    const shell = Mongo(conn.host, clientSideFLEOptions);

    collection.drop();

    const keyVault = shell.getKeyVault();

    assert.throws(() => keyVault.createKey(
                      "aws",
                      {"region": "us-east-1", "key": "arn:aws:kms:us-east-1:fake:fake:fake"},
                      ["mongoKey"]));
}

testWrongKeyType();

function testBadEncryptResult(fault) {
    const mock_kms = new MockKMSServerAWS(fault, false);

    runKMS(mock_kms, (shell) => {
        const keyVault = shell.getKeyVault();

        assert.throws(
            () => keyVault.createKey("aws", "arn:aws:kms:us-east-1:fake:fake:fake", ["mongoKey"]));
        assert.eq(keyVault.getKeys("mongoKey").toArray().length, 0);
    });
}

testBadEncryptResult(FAULT_ENCRYPT);
testBadEncryptResult(FAULT_ENCRYPT_WRONG_FIELDS);
testBadEncryptResult(FAULT_ENCRYPT_BAD_BASE64);

function testBadEncryptError() {
    const mock_kms = new MockKMSServerAWS(FAULT_ENCRYPT_CORRECT_FORMAT, false);

    runKMS(mock_kms, (shell) => {
        const keyVault = shell.getKeyVault();

        let error = assert.throws(
            () => keyVault.createKey("aws", "arn:aws:kms:us-east-1:fake:fake:fake", ["mongoKey"]));
        assert.commandFailedWithCode(error, [51224]);
        assert.eq(error,
                  "Error: AWS KMS failed to encrypt: NotFoundException : Error encrypting message");
    });
}

testBadEncryptError();

function testBadDecryptResult(fault) {
    const mock_kms = new MockKMSServerAWS(fault, false);

    runKMS(mock_kms, (shell) => {
        const keyVault = shell.getKeyVault();
        const keyId =
            keyVault.createKey("aws", "arn:aws:kms:us-east-1:fake:fake:fake", ["mongoKey"]);
        const str = "mongo";
        assert.throws(() => {
            const encStr = shell.getClientEncryption().encrypt(keyId, str, randomAlgorithm);
        });
    });
}

testBadDecryptResult(FAULT_DECRYPT);

function testBadDecryptKeyResult(fault) {
    const mock_kms = new MockKMSServerAWS(fault, true);

    runKMS(mock_kms, (shell, cleanCacheShell) => {
        const keyVault = shell.getKeyVault();

        keyVault.createKey("aws", "arn:aws:kms:us-east-1:fake:fake:fake", ["mongoKey"]);
        const keyId = keyVault.getKeys("mongoKey").toArray()[0]._id;
        const str = "mongo";
        const encStr = shell.getClientEncryption().encrypt(keyId, str, randomAlgorithm);

        mock_kms.enableFaults();

        assert.throws(() => {
            var str = cleanCacheShell.decrypt(encStr);
        });
    });
}

testBadDecryptKeyResult(FAULT_DECRYPT_WRONG_KEY);

function testBadDecryptError() {
    const mock_kms = new MockKMSServerAWS(FAULT_DECRYPT_CORRECT_FORMAT, false);

    runKMS(mock_kms, (shell) => {
        const keyVault = shell.getKeyVault();
        keyVault.createKey("aws", "arn:aws:kms:us-east-1:fake:fake:fake", ["mongoKey"]);
        const keyId = keyVault.getKeys("mongoKey").toArray()[0]._id;
        const str = "mongo";
        let error = assert.throws(() => {
            const encStr = shell.getClientEncryption().encrypt(keyId, str, randomAlgorithm);
        });
        assert.commandFailedWithCode(error, [51225]);
        assert.eq(error,
                  "Error: AWS KMS failed to decrypt: NotFoundException : Error decrypting message");
    });
}

testBadDecryptError();

MongoRunner.stopMongod(conn);
}());