summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIan Boros <ian.boros@mongodb.com>2021-08-06 14:09:37 -0400
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2021-08-06 20:33:43 +0000
commit06d4cb4869e332d7bce197f3f50f9c71e8febca9 (patch)
tree5abdf17fd23ac63dcffca9353d8b6cf5b66bb38b
parentcbec187266a9f902b3906ae8ccef2bbda0c5b27b (diff)
downloadmongo-06d4cb4869e332d7bce197f3f50f9c71e8febca9.tar.gz
Fix lint
-rw-r--r--jstests/aggregation/range.js603
-rw-r--r--jstests/auth/applyOps_privilege.js165
-rw-r--r--jstests/auth/lib/commands_lib.js12
3 files changed, 394 insertions, 386 deletions
diff --git a/jstests/aggregation/range.js b/jstests/aggregation/range.js
index 8041d0ab53c..7ac445c16df 100644
--- a/jstests/aggregation/range.js
+++ b/jstests/aggregation/range.js
@@ -2,298 +2,313 @@
* Tests general $range functionality.
*/
(function() {
-"use strict";
-
-load("jstests/aggregation/extras/utils.js");
-
-const coll = db.range;
-coll.drop();
-
-assert.commandWorked(coll.insert([
- {city: "San Jose", distance: NumberInt(42)},
- {city: "Sacramento", distance: NumberInt(88)},
- {city: "Reno", distance: NumberInt(218)},
- {city: "Los Angeles", distance: NumberInt(383)},
-]));
-
-const positiveRangeExpectedResult = [
- {"city": "San Jose", "Rest stops": [0, 25]},
- {"city": "Sacramento", "Rest stops": [0, 25, 50, 75]},
- {"city": "Reno", "Rest stops": [0, 25, 50, 75, 100, 125, 150, 175, 200]},
- {
- "city": "Los Angeles",
- "Rest stops": [0, 25, 50, 75, 100, 125, 150, 175, 200, 225, 250, 275, 300, 325, 350, 375]
- },
-];
-
-// Expecting the results to have an "Rest stops" array with positive elements.
-const positiveRangeResult =
- coll.aggregate([{
- $project: {
- _id: 0,
- city: 1,
- "Rest stops": {$range: [NumberInt(0), "$distance", NumberInt(25)]}
- }
- }])
- .toArray();
-
-assert(arrayEq(positiveRangeExpectedResult, positiveRangeResult));
-
-// Expecting same result when Int64 is used as long as it's value is representable in Int32.
-const positiveRangeResult2 =
- coll.aggregate([{
- $project: {
- _id: 0,
- city: 1,
- "Rest stops": {$range: [NumberLong(0), "$distance", NumberLong(25)]}
- }
- }])
- .toArray();
-
-assert(arrayEq(positiveRangeExpectedResult, positiveRangeResult2));
-
-const negativeRangeExpectedResult = [
- {"city": "San Jose", "Rest stops": [0, -25]},
- {"city": "Sacramento", "Rest stops": [0, -25, -50, -75]},
- {"city": "Reno", "Rest stops": [0, -25, -50, -75, -100, -125, -150, -175, -200]},
- {
- "city": "Los Angeles",
- "Rest stops": [
- 0,
- -25,
- -50,
- -75,
- -100,
- -125,
- -150,
- -175,
- -200,
- -225,
- -250,
- -275,
- -300,
- -325,
- -350,
- -375
- ]
- },
-];
-
-// Expecting the results to have an "Rest stops" array with negative elements.
-const negativeRangeResult =
- coll.aggregate([{
- $project: {
- _id: 0,
- city: 1,
- "Rest stops":
- {$range: [NumberInt(0), {"$multiply": ["$distance", -1]}, NumberInt(-25)]}
- }
- }])
- .toArray();
-
-assert(arrayEq(negativeRangeExpectedResult, negativeRangeResult));
-
-const nothingRangeExpectedResult = [
- {"city": "San Jose", "Rest stops": []},
- {"city": "Sacramento", "Rest stops": []},
- {"city": "Reno", "Rest stops": []},
- {"city": "Los Angeles", "Rest stops": []},
-];
-
-// Expecting the results to have an empty "Rest stops" array.
-const nothingRangeResult =
- coll.aggregate([{
- $project: {
- _id: 0,
- city: 1,
- "Rest stops":
- {$range: [NumberInt(0), {"$multiply": ["$distance", -1]}, NumberInt(25)]}
- }
- }])
- .toArray();
-
-assert(arrayEq(nothingRangeExpectedResult, nothingRangeResult));
-
-// Expecting the results to have an empty "Rest stops" array.
-const nothingRangeResult2 =
- coll.aggregate([{
- $project:
- {_id: 0, city: 1, "Rest stops": {$range: ["$distance", "$distance", NumberInt(25)]}}
- }])
- .toArray();
-
-assert(arrayEq(nothingRangeExpectedResult, nothingRangeResult2));
-
-// Testing default step.
-coll.drop();
-
-assert.commandWorked(coll.insert([
- {city: "San Jose", distance: NumberInt(5)},
- {city: "Sacramento", distance: NumberInt(8)},
- {city: "Reno", distance: NumberInt(2)},
- {city: "Los Angeles", distance: NumberInt(1)},
-]));
-
-const rangeDefaultStepExpectedResult = [
- {"city": "San Jose", "Rest stops": [0, 1, 2, 3, 4]},
- {"city": "Sacramento", "Rest stops": [0, 1, 2, 3, 4, 5, 6, 7]},
- {"city": "Reno", "Rest stops": [0, 1]},
- {"city": "Los Angeles", "Rest stops": [0]},
-];
-
-// Expecting the results to have an "Rest stops" array with positive elements starting from 0 to the
-// distance value.
-const rangeDefaultStepResult =
- coll.aggregate(
- [{$project: {_id: 0, city: 1, "Rest stops": {$range: [NumberInt(0), "$distance"]}}}])
- .toArray();
-
-assert(arrayEq(rangeDefaultStepExpectedResult, rangeDefaultStepResult));
-
-// Expecting the results to have an empty "Rest stops" array.
-const nothingRangeDefaultStepResult =
- coll.aggregate([{
- $project: {
- _id: 0,
- city: 1,
- "Rest stops": {$range: [NumberInt(0), {"$multiply": ["$distance", NumberInt(-1)]}]}
- }
- }])
- .toArray();
-
-assert(arrayEq(nothingRangeExpectedResult, nothingRangeDefaultStepResult));
-
-// Expecting the results to have an empty "Rest stops" array.
-const nothingRangeDefaultStepResult2 =
- coll.aggregate(
- [{$project: {_id: 0, city: 1, "Rest stops": {$range: ["$distance", "$distance"]}}}])
- .toArray();
-
-assert(arrayEq(nothingRangeExpectedResult, nothingRangeDefaultStepResult2));
-
-coll.drop();
-
-assert.commandWorked(coll.insert([
- {city: "San Jose", distance: NumberInt(100)},
-]));
-
-// Testing overflow errors due to $range start and end taking int32 values.
-// Example: {$range: [100, 2147483647, 1073741824]}
-// Output will OOM because array will look like this:
-// [ 100, 1073741924, -2147483548, -1073741724, 100, 1073741924, -2147483548, -1073741724, 100, … so
-// on and so forth ]
-const overflowRangeExpectedResult = [{"city": "San Jose", "Rest stops": [100, 1073741924]}];
-
-const overflowRangeResult =
- coll.aggregate([{
- $project: {
- _id: 0,
- city: 1,
- "Rest stops": {$range: ["$distance", NumberInt(2147483647), NumberInt(1073741824)]}
- }
- }])
- .toArray();
-
-assert(arrayEq(overflowRangeExpectedResult, overflowRangeResult));
-
-// Testing int32 representable errors (Arguments to $range must be int32 representable).
-let pipeline;
-
-// Start value is too big.
-pipeline = [
- {$project: {_id: 0, city: 1, "Rest stops": {$range: [NumberLong("12147483647"), "$distance"]}}}
-];
-assertErrorCode(coll, pipeline, 34444);
-
-// Start value is a decimal.
-pipeline =
- [{$project: {_id: 0, city: 1, "Rest stops": {$range: [NumberDecimal("0.35"), "$distance"]}}}];
-assertErrorCode(coll, pipeline, 34444);
-
-// Start value is not a number.
-pipeline = [
- {$project: {_id: 0, city: 1, "Rest stops": {$range: ["String is not a number", "$distance"]}}}
-];
-assertErrorCode(coll, pipeline, 34443);
-
-// Start value is null.
-pipeline = [{$project: {_id: 0, city: 1, "Rest stops": {$range: [null, "$distance"]}}}];
-assertErrorCode(coll, pipeline, 34443);
-
-// End value is too big.
-pipeline = [
- {$project: {_id: 0, city: 1, "Rest stops": {$range: ["$distance", NumberLong("12147483647")]}}}
-];
-assertErrorCode(coll, pipeline, 34446);
-
-// End value is a decimal.
-pipeline =
- [{$project: {_id: 0, city: 1, "Rest stops": {$range: ["$distance", NumberDecimal("0.35")]}}}];
-assertErrorCode(coll, pipeline, 34446);
-
-// End value is not a number.
-pipeline = [
- {$project: {_id: 0, city: 1, "Rest stops": {$range: ["$distance", "String is not a number"]}}}
-];
-assertErrorCode(coll, pipeline, 34445);
-
-// End value is null.
-pipeline = [{$project: {_id: 0, city: 1, "Rest stops": {$range: ["$distance", null]}}}];
-assertErrorCode(coll, pipeline, 34445);
-
-// Step value is too big.
-pipeline = [{
- $project: {
- _id: 0,
- city: 1,
- "Rest stops": {$range: ["$distance", NumberInt(100), NumberLong("12147483647")]}
- }
-}];
-assertErrorCode(coll, pipeline, 34448);
-
-// Step value is a decimal.
-pipeline = [{
- $project: {
- _id: 0,
- city: 1,
- "Rest stops": {$range: ["$distance", NumberInt(100), NumberDecimal("0.35")]}
- }
-}];
-assertErrorCode(coll, pipeline, 34448);
-
-// Step value is not a number.
-pipeline = [{
- $project: {
- _id: 0,
- city: 1,
- "Rest stops": {$range: ["$distance", NumberInt(100), "String is not a number"]}
- }
-}];
-assertErrorCode(coll, pipeline, 34447);
-
-// Step value is null.
-pipeline =
- [{$project: {_id: 0, city: 1, "Rest stops": {$range: ["$distance", NumberInt(100), null]}}}];
-assertErrorCode(coll, pipeline, 34447);
-
-// Step value is zero.
-pipeline = [{$project: {_id: 0, city: 1, "Rest stops": {$range: [0, "$distance", 0]}}}];
-assertErrorCode(coll, pipeline, 34449);
-
-// Testing decimal values that are Int32 representable.
-const decimalRangeExpectedResult = [{"city": "San Jose", "Rest stops": [100, 200]}];
-
-// Expecting results same as if decimals were Int32.
-const decimalRangeResult =
- coll.aggregate([{
- $project: {
- _id: 0,
- city: 1,
- "Rest stops":
- {$range: ["$distance", NumberDecimal("201.0"), NumberDecimal("100.0")]}
- }
- }])
- .toArray();
-
-assert(arrayEq(decimalRangeExpectedResult, decimalRangeResult));
+ "use strict";
+
+ load("jstests/aggregation/extras/utils.js");
+
+ const coll = db.range;
+ coll.drop();
+
+ assert.commandWorked(coll.insert([
+ {city: "San Jose", distance: NumberInt(42)},
+ {city: "Sacramento", distance: NumberInt(88)},
+ {city: "Reno", distance: NumberInt(218)},
+ {city: "Los Angeles", distance: NumberInt(383)},
+ ]));
+
+ const positiveRangeExpectedResult = [
+ {"city": "San Jose", "Rest stops": [0, 25]},
+ {"city": "Sacramento", "Rest stops": [0, 25, 50, 75]},
+ {"city": "Reno", "Rest stops": [0, 25, 50, 75, 100, 125, 150, 175, 200]},
+ {
+ "city": "Los Angeles",
+ "Rest stops":
+ [0, 25, 50, 75, 100, 125, 150, 175, 200, 225, 250, 275, 300, 325, 350, 375]
+ },
+ ];
+
+ // Expecting the results to have an "Rest stops" array with positive elements.
+ const positiveRangeResult =
+ coll.aggregate([{
+ $project: {
+ _id: 0,
+ city: 1, "Rest stops": {$range: [NumberInt(0), "$distance", NumberInt(25)]}
+ }
+ }])
+ .toArray();
+
+ assert(arrayEq(positiveRangeExpectedResult, positiveRangeResult));
+
+ // Expecting same result when Int64 is used as long as it's value is representable in Int32.
+ const positiveRangeResult2 =
+ coll.aggregate([{
+ $project: {
+ _id: 0,
+ city: 1,
+ "Rest stops": {$range: [NumberLong(0), "$distance", NumberLong(25)]}
+ }
+ }])
+ .toArray();
+
+ assert(arrayEq(positiveRangeExpectedResult, positiveRangeResult2));
+
+ const negativeRangeExpectedResult = [
+ {"city": "San Jose", "Rest stops": [0, -25]},
+ {"city": "Sacramento", "Rest stops": [0, -25, -50, -75]},
+ {"city": "Reno", "Rest stops": [0, -25, -50, -75, -100, -125, -150, -175, -200]},
+ {
+ "city": "Los Angeles",
+ "Rest stops": [
+ 0,
+ -25,
+ -50,
+ -75,
+ -100,
+ -125,
+ -150,
+ -175,
+ -200,
+ -225,
+ -250,
+ -275,
+ -300,
+ -325,
+ -350,
+ -375
+ ]
+ },
+ ];
+
+ // Expecting the results to have an "Rest stops" array with negative elements.
+ const negativeRangeResult =
+ coll.aggregate([{
+ $project: {
+ _id: 0,
+ city: 1, "Rest stops": {
+ $range:
+ [NumberInt(0), {"$multiply": ["$distance", -1]}, NumberInt(-25)]
+ }
+ }
+ }])
+ .toArray();
+
+ assert(arrayEq(negativeRangeExpectedResult, negativeRangeResult));
+
+ const nothingRangeExpectedResult = [
+ {"city": "San Jose", "Rest stops": []},
+ {"city": "Sacramento", "Rest stops": []},
+ {"city": "Reno", "Rest stops": []},
+ {"city": "Los Angeles", "Rest stops": []},
+ ];
+
+ // Expecting the results to have an empty "Rest stops" array.
+ const nothingRangeResult =
+ coll.aggregate([{
+ $project: {
+ _id: 0,
+ city: 1, "Rest stops": {
+ $range: [NumberInt(0), {"$multiply": ["$distance", -1]}, NumberInt(25)]
+ }
+ }
+ }])
+ .toArray();
+
+ assert(arrayEq(nothingRangeExpectedResult, nothingRangeResult));
+
+ // Expecting the results to have an empty "Rest stops" array.
+ const nothingRangeResult2 =
+ coll.aggregate([{
+ $project: {
+ _id: 0,
+ city: 1, "Rest stops": {$range: ["$distance", "$distance", NumberInt(25)]}
+ }
+ }])
+ .toArray();
+
+ assert(arrayEq(nothingRangeExpectedResult, nothingRangeResult2));
+
+ // Testing default step.
+ coll.drop();
+
+ assert.commandWorked(coll.insert([
+ {city: "San Jose", distance: NumberInt(5)},
+ {city: "Sacramento", distance: NumberInt(8)},
+ {city: "Reno", distance: NumberInt(2)},
+ {city: "Los Angeles", distance: NumberInt(1)},
+ ]));
+
+ const rangeDefaultStepExpectedResult = [
+ {"city": "San Jose", "Rest stops": [0, 1, 2, 3, 4]},
+ {"city": "Sacramento", "Rest stops": [0, 1, 2, 3, 4, 5, 6, 7]},
+ {"city": "Reno", "Rest stops": [0, 1]},
+ {"city": "Los Angeles", "Rest stops": [0]},
+ ];
+
+ // Expecting the results to have an "Rest stops" array with positive elements starting from 0 to
+ // the
+ // distance value.
+ const rangeDefaultStepResult =
+ coll.aggregate([
+ {$project: {_id: 0, city: 1, "Rest stops": {$range: [NumberInt(0), "$distance"]}}}
+ ])
+ .toArray();
+
+ assert(arrayEq(rangeDefaultStepExpectedResult, rangeDefaultStepResult));
+
+ // Expecting the results to have an empty "Rest stops" array.
+ const nothingRangeDefaultStepResult =
+ coll.aggregate([{
+ $project: {
+ _id: 0,
+ city: 1,
+ "Rest stops":
+ {$range: [NumberInt(0), {"$multiply": ["$distance", NumberInt(-1)]}]}
+ }
+ }])
+ .toArray();
+
+ assert(arrayEq(nothingRangeExpectedResult, nothingRangeDefaultStepResult));
+
+ // Expecting the results to have an empty "Rest stops" array.
+ const nothingRangeDefaultStepResult2 =
+ coll.aggregate(
+ [{$project: {_id: 0, city: 1, "Rest stops": {$range: ["$distance", "$distance"]}}}])
+ .toArray();
+
+ assert(arrayEq(nothingRangeExpectedResult, nothingRangeDefaultStepResult2));
+
+ coll.drop();
+
+ assert.commandWorked(coll.insert([
+ {city: "San Jose", distance: NumberInt(100)},
+ ]));
+
+ // Testing overflow errors due to $range start and end taking int32 values.
+ // Example: {$range: [100, 2147483647, 1073741824]}
+ // Output will OOM because array will look like this:
+ // [ 100, 1073741924, -2147483548, -1073741724, 100, 1073741924, -2147483548, -1073741724, 100,
+ // … so
+ // on and so forth ]
+ const overflowRangeExpectedResult = [{"city": "San Jose", "Rest stops": [100, 1073741924]}];
+
+ const overflowRangeResult =
+ coll.aggregate([{
+ $project: {
+ _id: 0,
+ city: 1,
+ "Rest stops":
+ {$range: ["$distance", NumberInt(2147483647), NumberInt(1073741824)]}
+ }
+ }])
+ .toArray();
+
+ assert(arrayEq(overflowRangeExpectedResult, overflowRangeResult));
+
+ // Testing int32 representable errors (Arguments to $range must be int32 representable).
+ let pipeline;
+
+ // Start value is too big.
+ pipeline = [{
+ $project:
+ {_id: 0, city: 1, "Rest stops": {$range: [NumberLong("12147483647"), "$distance"]}}
+ }];
+ assertErrorCode(coll, pipeline, 34444);
+
+ // Start value is a decimal.
+ pipeline = [{
+ $project: {_id: 0, city: 1, "Rest stops": {$range: [NumberDecimal("0.35"), "$distance"]}}
+ }];
+ assertErrorCode(coll, pipeline, 34444);
+
+ // Start value is not a number.
+ pipeline = [{
+ $project:
+ {_id: 0, city: 1, "Rest stops": {$range: ["String is not a number", "$distance"]}}
+ }];
+ assertErrorCode(coll, pipeline, 34443);
+
+ // Start value is null.
+ pipeline = [{$project: {_id: 0, city: 1, "Rest stops": {$range: [null, "$distance"]}}}];
+ assertErrorCode(coll, pipeline, 34443);
+
+ // End value is too big.
+ pipeline = [{
+ $project:
+ {_id: 0, city: 1, "Rest stops": {$range: ["$distance", NumberLong("12147483647")]}}
+ }];
+ assertErrorCode(coll, pipeline, 34446);
+
+ // End value is a decimal.
+ pipeline = [{
+ $project: {_id: 0, city: 1, "Rest stops": {$range: ["$distance", NumberDecimal("0.35")]}}
+ }];
+ assertErrorCode(coll, pipeline, 34446);
+
+ // End value is not a number.
+ pipeline = [{
+ $project:
+ {_id: 0, city: 1, "Rest stops": {$range: ["$distance", "String is not a number"]}}
+ }];
+ assertErrorCode(coll, pipeline, 34445);
+
+ // End value is null.
+ pipeline = [{$project: {_id: 0, city: 1, "Rest stops": {$range: ["$distance", null]}}}];
+ assertErrorCode(coll, pipeline, 34445);
+
+ // Step value is too big.
+ pipeline = [{
+ $project: {
+ _id: 0,
+ city: 1,
+ "Rest stops": {$range: ["$distance", NumberInt(100), NumberLong("12147483647")]}
+ }
+ }];
+ assertErrorCode(coll, pipeline, 34448);
+
+ // Step value is a decimal.
+ pipeline = [{
+ $project: {
+ _id: 0,
+ city: 1,
+ "Rest stops": {$range: ["$distance", NumberInt(100), NumberDecimal("0.35")]}
+ }
+ }];
+ assertErrorCode(coll, pipeline, 34448);
+
+ // Step value is not a number.
+ pipeline = [{
+ $project: {
+ _id: 0,
+ city: 1,
+ "Rest stops": {$range: ["$distance", NumberInt(100), "String is not a number"]}
+ }
+ }];
+ assertErrorCode(coll, pipeline, 34447);
+
+ // Step value is null.
+ pipeline = [
+ {$project: {_id: 0, city: 1, "Rest stops": {$range: ["$distance", NumberInt(100), null]}}}
+ ];
+ assertErrorCode(coll, pipeline, 34447);
+
+ // Step value is zero.
+ pipeline = [{$project: {_id: 0, city: 1, "Rest stops": {$range: [0, "$distance", 0]}}}];
+ assertErrorCode(coll, pipeline, 34449);
+
+ // Testing decimal values that are Int32 representable.
+ const decimalRangeExpectedResult = [{"city": "San Jose", "Rest stops": [100, 200]}];
+
+ // Expecting results same as if decimals were Int32.
+ const decimalRangeResult =
+ coll.aggregate([{
+ $project: {
+ _id: 0,
+ city: 1,
+ "Rest stops":
+ {$range: ["$distance", NumberDecimal("201.0"), NumberDecimal("100.0")]}
+ }
+ }])
+ .toArray();
+
+ assert(arrayEq(decimalRangeExpectedResult, decimalRangeResult));
}());
diff --git a/jstests/auth/applyOps_privilege.js b/jstests/auth/applyOps_privilege.js
index 26423c52b24..ca3eba9cf24 100644
--- a/jstests/auth/applyOps_privilege.js
+++ b/jstests/auth/applyOps_privilege.js
@@ -1,92 +1,93 @@
// Tests that a user can only run a applyops while having applyOps privilege.
(function() {
-"use strict";
-
-// Special privilege required to run applyOps command.
-// Role dbAdminAnyDatabase has this privilege.
-const applyOps_priv = {
- resource: {cluster: true},
- actions: ["applyOps"]
-};
-
-const testUser = "testUser";
-const testUserWithDbAdminAnyDatabaseRole = "testUserWithDbAdminAnyDatabaseRole";
-const testRole = "testRole";
-const testDBName = "test_applyOps_auth";
-const adminDbName = "admin";
-const authErrCode = 13;
-
-const command = {
- applyOps: [{
- "op": "c",
- "ns": testDBName + ".$cmd",
- "o": {
- "create": "x",
- }
- }]
-};
-
-function createUsers(conn) {
- let adminDb = conn.getDB(adminDbName);
- // Create the admin user.
- assert.commandWorked(
- adminDb.runCommand({createUser: "admin", pwd: "password", roles: ["__system"]}));
-
- assert(adminDb.auth("admin", "password"));
- assert.commandWorked(adminDb.runCommand({createRole: testRole, privileges: [], roles: []}));
-
- let testDb = adminDb.getSiblingDB(testDBName);
- assert.commandWorked(testDb.runCommand(
- {createUser: testUser, pwd: "password", roles: [{role: testRole, db: adminDbName}]}));
-
- assert.commandWorked(testDb.runCommand({
- createUser: testUserWithDbAdminAnyDatabaseRole,
- pwd: "password",
- roles: [{role: "dbAdminAnyDatabase", db: adminDbName}, {role: testRole, db: adminDbName}]
- }));
-
- adminDb.logout();
-}
-
-function testAuthorization(conn, privileges, user, shouldSucceed) {
- let testDb = conn.getDB(testDBName);
- let adminDb = conn.getDB(adminDbName);
-
- assert(adminDb.auth("admin", "password"));
- assert.commandWorked(adminDb.runCommand({updateRole: testRole, privileges: privileges}));
- adminDb.logout();
-
- assert(testDb.auth(user, "password"));
- if (shouldSucceed) {
- assert.commandWorked(testDb.runCommand(command));
- } else {
- var res = testDb.runCommand(command);
- if (res.ok == 1 || res.code != authErrCode) {
- let msg = "expected authorization failure " +
- " but received " + tojson(res) + " with privileges " + tojson(privileges);
- assert(false, msg);
- }
+ "use strict";
+
+ // Special privilege required to run applyOps command.
+ // Role dbAdminAnyDatabase has this privilege.
+ const applyOps_priv = {resource: {cluster: true}, actions: ["applyOps"]};
+
+ const testUser = "testUser";
+ const testUserWithDbAdminAnyDatabaseRole = "testUserWithDbAdminAnyDatabaseRole";
+ const testRole = "testRole";
+ const testDBName = "test_applyOps_auth";
+ const adminDbName = "admin";
+ const authErrCode = 13;
+
+ const command = {
+ applyOps: [{
+ "op": "c",
+ "ns": testDBName + ".$cmd",
+ "o": {
+ "create": "x",
+ }
+ }]
+ };
+
+ function createUsers(conn) {
+ let adminDb = conn.getDB(adminDbName);
+ // Create the admin user.
+ assert.commandWorked(
+ adminDb.runCommand({createUser: "admin", pwd: "password", roles: ["__system"]}));
+
+ assert(adminDb.auth("admin", "password"));
+ assert.commandWorked(adminDb.runCommand({createRole: testRole, privileges: [], roles: []}));
+
+ let testDb = adminDb.getSiblingDB(testDBName);
+ assert.commandWorked(testDb.runCommand(
+ {createUser: testUser, pwd: "password", roles: [{role: testRole, db: adminDbName}]}));
+
+ assert.commandWorked(testDb.runCommand({
+ createUser: testUserWithDbAdminAnyDatabaseRole,
+ pwd: "password",
+ roles: [
+ {role: "dbAdminAnyDatabase", db: adminDbName},
+ {role: testRole, db: adminDbName}
+ ]
+ }));
+
+ adminDb.logout();
}
- testDb.logout();
-}
+ function testAuthorization(conn, privileges, user, shouldSucceed) {
+ let testDb = conn.getDB(testDBName);
+ let adminDb = conn.getDB(adminDbName);
+
+ assert(adminDb.auth("admin", "password"));
+ assert.commandWorked(adminDb.runCommand({updateRole: testRole, privileges: privileges}));
+ adminDb.logout();
+
+ assert(testDb.auth(user, "password"));
+ if (shouldSucceed) {
+ assert.commandWorked(testDb.runCommand(command));
+ } else {
+ var res = testDb.runCommand(command);
+ if (res.ok == 1 || res.code != authErrCode) {
+ let msg = "expected authorization failure " + " but received " + tojson(res) +
+ " with privileges " + tojson(privileges);
+ assert(false, msg);
+ }
+ }
+
+ testDb.logout();
+ }
-function runTest(conn) {
- createUsers(conn);
- let privileges = [{resource: {db: testDBName, collection: "x"}, actions: ["createCollection"]}];
+ function runTest(conn) {
+ createUsers(conn);
+ let privileges =
+ [{resource: {db: testDBName, collection: "x"}, actions: ["createCollection"]}];
- // Test applyOps failed without applyOps privilege or dbAdminAnyDatabase role.
- testAuthorization(conn, privileges, testUser, false);
+ // Test applyOps failed without applyOps privilege or dbAdminAnyDatabase role.
+ testAuthorization(conn, privileges, testUser, false);
- // Test applyOps succeed with applyOps privilege.
- testAuthorization(conn, privileges.concat(applyOps_priv), testUser, true);
+ // Test applyOps succeed with applyOps privilege.
+ testAuthorization(conn, privileges.concat(applyOps_priv), testUser, true);
- // Test applyOps succeed with dbAdminAnyDatabase role.
- testAuthorization(conn, privileges, testUserWithDbAdminAnyDatabaseRole, true);
-}
+ // Test applyOps succeed with dbAdminAnyDatabase role.
+ testAuthorization(conn, privileges, testUserWithDbAdminAnyDatabaseRole, true);
+ }
-// Run the test on a standalone.
-let conn = MongoRunner.runMongod({auth: ""});
-runTest(conn);
-MongoRunner.stopMongod(conn);
+ // Run the test on a standalone.
+ let conn = MongoRunner.runMongod({auth: ""});
+ runTest(conn);
+ MongoRunner.stopMongod(conn);
}());
diff --git a/jstests/auth/lib/commands_lib.js b/jstests/auth/lib/commands_lib.js
index 36ff03aac01..ce0ab88e27e 100644
--- a/jstests/auth/lib/commands_lib.js
+++ b/jstests/auth/lib/commands_lib.js
@@ -296,11 +296,7 @@ var authCommandsLib = {
testcases: [
{
runOnDb: adminDbName,
- roles: {
- dbAdminAnyDatabase: 1,
- root: 1,
- __system: 1
- },
+ roles: {dbAdminAnyDatabase: 1, root: 1, __system: 1},
privileges: [
{resource: {db: firstDbName, collection: "x"}, actions: ["createCollection"]},
{resource: {cluster: true}, actions: ["applyOps"]},
@@ -385,11 +381,7 @@ var authCommandsLib = {
testcases: [
{
runOnDb: adminDbName,
- roles: {
- dbAdminAnyDatabase: 1,
- root: 1,
- __system: 1
- },
+ roles: {dbAdminAnyDatabase: 1, root: 1, __system: 1},
privileges: [
{resource: {db: firstDbName, collection: "x"}, actions: ["dropCollection"]},
{resource: {cluster: true}, actions: ["applyOps"]},