summaryrefslogtreecommitdiff
path: root/jstests/sharding/compact_structured_encryption_data_coordinator.js
blob: 33c107463687f4b0b208b42759cfae5da74a63fb (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
// Basic test that the CompactStructuredEncryptionDataCoordinator runs.
// @tags: [
//     requires_sharding,
//     requires_fcv_70,
// ]

(function() {
'use strict';

load('jstests/fle2/libs/encrypted_client_util.js');

const options = {
    mongos: 1,
    shards: 1,
    rs: {nodes: [{}]},
};

if (!TestData.configShard) {
    // Setting config options will override shard options in config shard mode, which doesn't set
    // the right audit node options on the config shard.
    options.config = 1;
}

const kHaveAuditing = buildInfo().modules.includes("enterprise");
if (kHaveAuditing) {
    jsTest.log('Including test for audit events since this is an enterprise build');
    const nodeOpts = options.rs.nodes[0];
    nodeOpts.auditDestination = 'file';
    nodeOpts.auditPath = MongoRunner.dataPath + '/audit.log';
    nodeOpts.auditFormat = 'JSON';
}

const st = new ShardingTest(options);

const admin = st.s0.getDB('admin');

// Setup collection with encrypted fields and a compactible metadata collection.
const encryptedFields = {
    fields: [
        {path: "firstName", bsonType: "string", queries: {"queryType": "equality"}},
        {path: "a.b.c", bsonType: "int", queries: {"queryType": "equality"}},
    ]
};

const client = new EncryptedClient(st.s0, 'test');
const test = client.getDB('test');

assert.commandWorked(
    client.createEncryptionCollection("encrypted", {encryptedFields: encryptedFields}));
assert.commandWorked(test.createCollection("unencrypted"));

assert.commandFailedWithCode(test.unencrypted.compact(), 6346807);

const reply = assert.commandWorked(test.encrypted.compact());
jsTest.log(reply);

// Validate dummy data we expect the placeholder compaction algorithm to return.
assert.eq(reply.stats.ecoc.read, 0);
assert.eq(reply.stats.ecoc.deleted, 0);

assert.eq(reply.stats.esc.read, 0);
assert.eq(reply.stats.esc.inserted, 0);
assert.eq(reply.stats.esc.updated, 0);
assert.eq(reply.stats.esc.deleted, 0);

if (kHaveAuditing) {
    jsTest.log('Verifying audit contents');

    // Check the audit log for the rename/drop events.
    const audit = cat(options.rs.nodes[0].auditPath)
                      .split('\n')
                      .filter((l) => l !== '')
                      .map((l) => JSON.parse(l));
    jsTest.log('Audit Log: ' + tojson(audit));

    const renameEvents = audit.filter((ev) => (ev.atype === 'renameCollection') &&
                                          (ev.param.old === 'test.enxcol_.encrypted.ecoc'));
    assert.eq(renameEvents.length, 1, 'Invalid number of rename events: ' + tojson(renameEvents));
    assert.eq(renameEvents[0].result, ErrorCodes.OK);
    const tempNSS = renameEvents[0].param.new;

    const dropEvents =
        audit.filter((ev) => (ev.atype === 'dropCollection') && (ev.param.ns === tempNSS));
    assert.eq(dropEvents.length, 1, 'Invalid number of drop events: ' + tojson(dropEvents));
    assert.eq(dropEvents[0].result, ErrorCodes.OK);
}

st.stop();

jsTest.log(reply);
})();