Skip to content

Commit 632fc54

Browse files
committed
fix: atlas connectCluster defaults to readOnly DB roles
1 parent ca68195 commit 632fc54

File tree

1 file changed

+34
-16
lines changed

1 file changed

+34
-16
lines changed

src/tools/atlas/connect/connectCluster.ts

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { LogId } from "../../../common/logger.js";
77
import { inspectCluster } from "../../../common/atlas/cluster.js";
88
import { ensureCurrentIpInAccessList } from "../../../common/atlas/accessListUtils.js";
99
import { AtlasClusterConnectionInfo } from "../../../common/connectionManager.js";
10+
import { DatabaseUserRole } from "../../../common/atlas/openapi.js";
1011

1112
const EXPIRY_MS = 1000 * 60 * 60 * 12; // 12 hours
1213

@@ -72,16 +73,7 @@ export class ConnectClusterTool extends AtlasToolBase {
7273
const password = await generateSecurePassword();
7374

7475
const expiryDate = new Date(Date.now() + EXPIRY_MS);
75-
76-
const readOnly =
77-
this.config.readOnly ||
78-
(this.config.disabledTools?.includes("create") &&
79-
this.config.disabledTools?.includes("update") &&
80-
this.config.disabledTools?.includes("delete") &&
81-
!this.config.disabledTools?.includes("read") &&
82-
!this.config.disabledTools?.includes("metadata"));
83-
84-
const roleName = readOnly ? "readAnyDatabase" : "readWriteAnyDatabase";
76+
const role = this.getRoleFromConfig();
8577

8678
await this.session.apiClient.createDatabaseUser({
8779
params: {
@@ -92,12 +84,7 @@ export class ConnectClusterTool extends AtlasToolBase {
9284
body: {
9385
databaseName: "admin",
9486
groupId: projectId,
95-
roles: [
96-
{
97-
roleName,
98-
databaseName: "admin",
99-
},
100-
],
87+
roles: [role],
10188
scopes: [{ type: "CLUSTER", name: clusterName }],
10289
username,
10390
password,
@@ -258,4 +245,35 @@ export class ConnectClusterTool extends AtlasToolBase {
258245
],
259246
};
260247
}
248+
249+
/**
250+
* @description Get the role name for the database user based on the Atlas Admin API https://www.mongodb.com/docs/atlas/mongodb-users-roles-and-privileges/
251+
* @returns The role name for the database user
252+
*/
253+
private getRoleFromConfig(): DatabaseUserRole {
254+
if (this.config.readOnly) {
255+
return {
256+
roleName: "readAnyDatabase",
257+
databaseName: "admin",
258+
};
259+
}
260+
261+
// If all write tools are enabled, use readWriteAnyDatabase
262+
if (
263+
!this.config.disabledTools?.includes("create") &&
264+
!this.config.disabledTools?.includes("update") &&
265+
!this.config.disabledTools?.includes("delete") &&
266+
!this.config.disabledTools?.includes("metadata")
267+
) {
268+
return {
269+
roleName: "readWriteAnyDatabase",
270+
databaseName: "admin",
271+
};
272+
}
273+
274+
return {
275+
roleName: "readAnyDatabase",
276+
databaseName: "admin",
277+
};
278+
}
261279
}

0 commit comments

Comments
 (0)