Files
documentdb/pg_documentdb/include/users.h
T
William Wang fe2f9a7ee7 Merged PR 1639930: Add Extensible Support for Role Privileges in UsersInfo Command
### Does this PR have any customer impact?
Yes
### Type (Feature, Refactoring, Bugfix, DevOps, Testing, Perf, etc)
Feature
### Does it involve schema level changes? (Table, Column, Index, UDF, etc level changes)
No
### Are you introducing any new config? If yes, do you have tests with and without them being set?
No
### ChangeLog (Refer [Template](../oss/CHANGELOG.md))

### Description
We've implemented several key improvements to DocumentDB's user management system, with a focus on enhancing the `usersInfo` command to support privilege information display.

### Feature Overview

- Added support for the `showPrivileges` flag in `usersInfo` command
- When enabled, user information responses now include detailed privilege documents
- Privileges are organized by resource and action, similar to MongoDB's privilege format

### Implementation Details

1. **Privilege Data Structures**
   - Created predefined privilege sets for each role type (readOnly, readWrite, clusterAdmin)
   - Designed efficient merging of privilege sets with hash-based deduplication
2. **GUC Configuration**
   - Added `EnableUsersInfoPrivileges` boolean GUC to control feature availability
3. **Command Flow**
   - Parse `showPrivileges` flag from incoming BSON documents
   - When flag is true and GUC is enabled, append privilege array to user documents
   - Privileges include resource targets (cluster/database/collection) and action lists
4. **Memory Optimization**
   - Implemented hash-based privilege merging to avoid duplicate entries
   - Proper memory management with PostgreSQL allocation functions

This functionality supports the `connectionStatus` command, which requires authenticated user privileges.

We will also support `dbowner` and `root` roles, which will come in a follow-up PR.

Related work items: #3852887
2025-06-05 10:31:32 +08:00

74 lines
1.4 KiB
C

/*-------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* include/users.h
*
* User CRUD functions.
*
*-------------------------------------------------------------------------
*/
#ifndef EXTENSION_USERS_H
#define EXTENSION_USERS_H
#include "postgres.h"
#include "utils/string_view.h"
enum DocumentDB_BuiltInRoles
{
DocumentDB_Role_Read_AnyDatabase = 0x1,
DocumentDB_Role_ReadWrite_AnyDatabase = 0x2,
DocumentDB_Role_Cluster_Admin = 0x4,
};
typedef struct
{
/* "createUser" field */
const char *createUser;
/* "pwd" field */
const char *pwd;
/* "roles" field */
bson_value_t roles;
/* "identityProvider" field*/
bson_value_t identityProviderData;
/* pgRole the passed in role maps to */
char *pgRole;
/* principalType */
char *principalType;
/* has_identity_provider */
bool has_identity_provider;
} CreateUserSpec;
typedef struct
{
/* "updateUser" field */
const char *updateUser;
/* "pwd" field */
const char *pwd;
} UpdateUserSpec;
typedef struct
{
StringView user;
bool showPrivileges;
} GetUserSpec;
/* GUC that controls the blocked role prefix list */
extern char *BlockedRolePrefixList;
/* Method to validate and obtain user role */
char * ValidateAndObtainUserRole(const bson_value_t *rolesDocument);
/* Method to verify if username is valid */
bool IsUserNameInvalid(const char *userName);
#endif