MCPcopy Create free account
hub / github.com/documentdb/documentdb / ValidateAndObtainUserRole

Function ValidateAndObtainUserRole

pg_documentdb/src/commands/users.c:1427–1518  ·  view source on GitHub ↗

* At the moment we only allow ApiAdminRole and ApiReadOnlyRole * 1. ApiAdminRole corresponds to * roles: [ * { role: "clusterAdmin", db: "admin" }, * { role: "readWriteAnyDatabase", db: "admin" } * ] * * 2. ApiReadOnlyRole corresponds to * roles: [ * { role: "readAnyDatabase", db: "admin" } * ] * * Reject all other combinations. */

Source from the content-addressed store, hash-verified

1425 * Reject all other combinations.
1426 */
1427static char *
1428ValidateAndObtainUserRole(const bson_value_t *rolesDocument)
1429{
1430 bson_iter_t rolesIterator;
1431 BsonValueInitIterator(rolesDocument, &rolesIterator);
1432 int userRoles = 0;
1433
1434 while (bson_iter_next(&rolesIterator))
1435 {
1436 bson_iter_t roleIterator;
1437
1438 BsonValueInitIterator(bson_iter_value(&rolesIterator), &roleIterator);
1439 while (bson_iter_next(&roleIterator))
1440 {
1441 const char *key = bson_iter_key(&roleIterator);
1442
1443 if (strcmp(key, "role") == 0)
1444 {
1445 EnsureTopLevelFieldType(key, &roleIterator, BSON_TYPE_UTF8);
1446 uint32_t strLength = 0;
1447 const char *role = bson_iter_utf8(&roleIterator, &strLength);
1448 if (strcmp(role, "readAnyDatabase") == 0)
1449 {
1450 /*This would indicate the ApiReadOnlyRole provided the db is "admin" */
1451 userRoles |= DocumentDB_Role_Read_AnyDatabase;
1452 }
1453 else if (strcmp(role, "readWriteAnyDatabase") == 0)
1454 {
1455 /*This would indicate the ApiAdminRole provided the db is "admin" and there is another role "clusterAdmin" */
1456 userRoles |= DocumentDB_Role_ReadWrite_AnyDatabase;
1457 }
1458 else if (strcmp(role, "clusterAdmin") == 0)
1459 {
1460 /*This would indicate the ApiAdminRole provided the db is "admin" and there is another role "readWriteAnyDatabase" */
1461 userRoles |= DocumentDB_Role_Cluster_Admin;
1462 }
1463 else
1464 {
1465 ereport(ERROR, (errcode(ERRCODE_DOCUMENTDB_ROLENOTFOUND),
1466 errmsg(
1467 "The specified value for the role is invalid: '%s'.",
1468 role),
1469 errdetail_log(
1470 "The specified value for the role is invalid: '%s'.",
1471 role)));
1472 }
1473 }
1474 else if (strcmp(key, "db") == 0 || strcmp(key, "$db") == 0)
1475 {
1476 EnsureTopLevelFieldType(key, &roleIterator, BSON_TYPE_UTF8);
1477 uint32_t strLength = 0;
1478 const char *db = bson_iter_utf8(&roleIterator, &strLength);
1479 if (strcmp(db, "admin") != 0)
1480 {
1481 ereport(ERROR, (errcode(ERRCODE_DOCUMENTDB_BADVALUE), errmsg(
1482 "Unsupported value specified for db. Only 'admin' is allowed.")));
1483 }
1484 }

Callers 1

ParseCreateUserSpecFunction · 0.85

Calls 2

BsonValueInitIteratorFunction · 0.85
EnsureTopLevelFieldTypeFunction · 0.85

Tested by

no test coverage detected