summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/queryStats/documentSourceQueryStats_redaction_parameters.js
blob: 8facb10607254b2cd8ba0fb85a07af79390a6ce3 (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
/**
 * Test the $queryStats hmac properties.
 * @tags: [featureFlagQueryStats]
 */

load("jstests/aggregation/extras/utils.js");  // For assertAdminDBErrCodeAndErrMsgContains.
load("jstests/libs/telemetry_utils.js");

(function() {
"use strict";

// Assert the expected telemetry key with no hmac.
function assertTelemetryKeyWithoutHmac(telemetryKey) {
    assert.eq(telemetryKey.filter, {"foo": {"$lte": "?number"}});
    assert.eq(telemetryKey.sort, {"bar": -1});
    assert.eq(telemetryKey.limit, "?number");
}

function runTest(conn) {
    const testDB = conn.getDB('test');
    var coll = testDB[jsTestName()];
    coll.drop();

    coll.insert({foo: 1});
    coll.find({foo: {$lte: 2}}).sort({bar: -1}).limit(2).toArray();
    // Default is no hmac.
    assertTelemetryKeyWithoutHmac(getTelemetry(conn)[0].key.queryShape);

    // Turning on hmac should apply hmac to all field names on all entries, even previously cached
    // ones.
    const telemetryKey = getTelemetryRedacted(conn)[0]["key"];
    assert.eq(telemetryKey.queryShape.filter,
              {"fNWkKfogMv6MJ77LpBcuPrO7Nq+R+7TqtD+Lgu3Umc4=": {"$lte": "?number"}});
    assert.eq(telemetryKey.queryShape.sort, {"CDDQIXZmDehLKmQcRxtdOQjMqoNqfI2nGt2r4CgJ52o=": -1});
    assert.eq(telemetryKey.queryShape.limit, "?number");

    // Turning hmac back off should preserve field names on all entries, even previously cached
    // ones.
    assertTelemetryKeyWithoutHmac(getTelemetry(conn)[0]["key"].queryShape);

    // Explicitly set applyHmacToIdentifiers to false.
    assertTelemetryKeyWithoutHmac(getTelemetryRedacted(conn, false)[0]["key"].queryShape);

    // Wrong parameter name throws error.
    let pipeline = [{$queryStats: {redactFields: true}}];
    assertAdminDBErrCodeAndErrMsgContains(
        coll,
        pipeline,
        ErrorCodes.FailedToParse,
        "$queryStats parameters object may only contain 'applyHmacToIdentifiers' or 'hmacKey' options. Found: redactFields");

    // Wrong parameter type throws error.
    pipeline = [{$queryStats: {applyHmacToIdentifiers: 1}}];
    assertAdminDBErrCodeAndErrMsgContains(
        coll,
        pipeline,
        ErrorCodes.FailedToParse,
        "$queryStats applyHmacToIdentifiers parameter must be boolean. Found type: double");

    pipeline = [{$queryStats: {hmacKey: 1}}];
    assertAdminDBErrCodeAndErrMsgContains(
        coll,
        pipeline,
        ErrorCodes.FailedToParse,
        "$queryStats hmacKey parameter must be bindata of length 32 or greater. Found type: double");

    // Parameter object with unrecognized key throws error.
    pipeline = [{$queryStats: {applyHmacToIdentifiers: true, hmacStrategy: "on"}}];
    assertAdminDBErrCodeAndErrMsgContains(
        coll,
        pipeline,
        ErrorCodes.FailedToParse,
        "$queryStats parameters object may only contain 'applyHmacToIdentifiers' or 'hmacKey' options. Found: hmacStrategy");
}

const conn = MongoRunner.runMongod({
    setParameter: {
        internalQueryStatsSamplingRate: -1,
        featureFlagQueryStats: true,
    }
});
runTest(conn);
MongoRunner.stopMongod(conn);

const st = new ShardingTest({
    mongos: 1,
    shards: 1,
    config: 1,
    rs: {nodes: 1},
    mongosOptions: {
        setParameter: {
            internalQueryStatsSamplingRate: -1,
            featureFlagQueryStats: true,
            'failpoint.skipClusterParameterRefresh': "{'mode':'alwaysOn'}"
        }
    },
});
runTest(st.s);
st.stop();
}());