-
Notifications
You must be signed in to change notification settings - Fork 132
fix: atlas connectCluster defaults to readOnly DB roles - MCP-125 #471
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from 3 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
632fc54
fix: atlas connectCluster defaults to readOnly DB roles
blva 439daab
decouple and add unit test
blva c84caf1
add missing file
blva 3c92f7c
minor fix: rename test
blva 34051b7
address comment: add description to the DB User
blva 271a678
address comment: keep readWrite if any write tool is enabled but not …
blva 981dcc3
Merge remote-tracking branch 'origin/main' into MCP-125
blva 3ab7beb
apply new lint
blva fa45e6f
fix new lint
blva ac80a9c
correct unit test
blva 97c2556
address comments
blva 903825d
address comment: description
blva File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { UserConfig } from "../config.js"; | ||
import { DatabaseUserRole } from "./openapi.js"; | ||
|
||
/** | ||
* Get the default role name for the database user based on the Atlas Admin API | ||
* https://www.mongodb.com/docs/atlas/mongodb-users-roles-and-privileges/ | ||
*/ | ||
export function getDefaultRoleFromConfig(config: UserConfig): DatabaseUserRole { | ||
if (config.readOnly) { | ||
return { | ||
roleName: "readAnyDatabase", | ||
databaseName: "admin", | ||
}; | ||
} | ||
|
||
// If all write tools are enabled, use readWriteAnyDatabase | ||
if ( | ||
!config.disabledTools?.includes("create") && | ||
!config.disabledTools?.includes("update") && | ||
!config.disabledTools?.includes("delete") && | ||
!config.disabledTools?.includes("metadata") | ||
blva marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
) { | ||
return { | ||
roleName: "readWriteAnyDatabase", | ||
databaseName: "admin", | ||
}; | ||
blva marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
|
||
return { | ||
roleName: "readAnyDatabase", | ||
databaseName: "admin", | ||
}; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { describe, it, expect } from "vitest"; | ||
import { getDefaultRoleFromConfig } from "../../../src/common/atlas/roles.js"; | ||
import { defaultUserConfig, UserConfig } from "../../../src/common/config.js"; | ||
|
||
describe("getDefaultRoleFromConfig", () => { | ||
const defaultConfig: UserConfig = { | ||
...defaultUserConfig, | ||
}; | ||
|
||
const readOnlyConfig: UserConfig = { | ||
...defaultConfig, | ||
readOnly: true, | ||
}; | ||
|
||
const readWriteConfig: UserConfig = { | ||
...defaultConfig, | ||
readOnly: false, | ||
disabledTools: [], | ||
}; | ||
|
||
it("should return the correct role for a read-only config", () => { | ||
const role = getDefaultRoleFromConfig(readOnlyConfig); | ||
expect(role).toEqual({ | ||
roleName: "readAnyDatabase", | ||
databaseName: "admin", | ||
}); | ||
}); | ||
|
||
it("should return the correct role for a read-write config", () => { | ||
const role = getDefaultRoleFromConfig(readWriteConfig); | ||
expect(role).toEqual({ | ||
roleName: "readWriteAnyDatabase", | ||
databaseName: "admin", | ||
}); | ||
}); | ||
|
||
it("should return the correct role for a read-write config with all tools enabled", () => { | ||
const role = getDefaultRoleFromConfig(readWriteConfig); | ||
expect(role).toEqual({ | ||
roleName: "readWriteAnyDatabase", | ||
databaseName: "admin", | ||
}); | ||
}); | ||
|
||
// loop with each disabled tool | ||
for (const tool of ["create", "update", "delete", "metadata"]) { | ||
it(`should return the correct role for a read-write config with ${tool} disabled`, () => { | ||
const config = { ...readWriteConfig, disabledTools: [tool] }; | ||
const role = getDefaultRoleFromConfig(config); | ||
expect(role).toEqual({ | ||
roleName: "readAnyDatabase", | ||
databaseName: "admin", | ||
}); | ||
}); | ||
} | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a bit hard to follow due to the double negation, but I don't think this is the right condition to check - if any write tool is enabled, we should default to readWrite, otherwise we're risking some tools to not work as intended. For example, if I want to be able to insert new data, but not delete existing documents, I may disable delete tools, but I'll still need the readWriteAnyDatabase user.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, got it! I thought that we were trying to avoid any writes if any of them are disabled.
Fair, I thought we wanted to be more strict, but I agree that this would just cause people to enable all tools