Skip to content

fix: bad auth error #178

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
merged 6 commits into from
Apr 30, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const LogId = {

atlasCheckCredentials: mongoLogId(1_001_001),
atlasDeleteDatabaseUserFailure: mongoLogId(1_001_002),
atlasConnectFailure: mongoLogId(1_001_003),

telemetryDisabled: mongoLogId(1_002_001),
telemetryEmitFailure: mongoLogId(1_002_002),
Expand Down
21 changes: 10 additions & 11 deletions src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,25 +69,24 @@ export class Session extends EventEmitter<{
this.emit("disconnect");
return;
}
try {
await this.apiClient.deleteDatabaseUser({
void this.apiClient
.deleteDatabaseUser({
params: {
path: {
groupId: this.connectedAtlasCluster.projectId,
username: this.connectedAtlasCluster.username,
databaseName: "admin",
},
},
})
.catch((err: unknown) => {
const error = err instanceof Error ? err : new Error(String(err));
logger.error(
LogId.atlasDeleteDatabaseUserFailure,
"atlas-connect-cluster",
`Error deleting previous database user: ${error.message}`
);
});
} catch (err: unknown) {
const error = err instanceof Error ? err : new Error(String(err));

logger.error(
LogId.atlasDeleteDatabaseUserFailure,
"atlas-connect-cluster",
`Error deleting previous database user: ${error.message}`
);
}
this.connectedAtlasCluster = undefined;

this.emit("disconnect");
Expand Down
17 changes: 16 additions & 1 deletion src/tools/atlas/metadata/connectCluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { AtlasToolBase } from "../atlasTool.js";
import { ToolArgs, OperationType } from "../../tool.js";
import { randomBytes } from "crypto";
import { promisify } from "util";
import logger, { LogId } from "../../../logger.js";

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

Expand Down Expand Up @@ -100,7 +101,21 @@ export class ConnectClusterTool extends AtlasToolBase {
cn.searchParams.set("authSource", "admin");
const connectionString = cn.toString();

await this.session.connectToMongoDB(connectionString, this.config.connectOptions);
for (let i = 0; i < 20; i++) {
try {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can we add tests for this in a follow-up?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in the integration tests we can't force a user to take longer to be created, we could try with unit tests, but none of the tools are covered in unit at the moment

await this.session.connectToMongoDB(connectionString, this.config.connectOptions);
Copy link
Preview

Copilot AI Apr 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If all retry attempts fail, the connection error is silently swallowed. Consider throwing an error or returning a failure state after exhausting all attempts.

Suggested change
await this.session.connectToMongoDB(connectionString, this.config.connectOptions);
for (let i = 0; i < 20; i++) {
try {
await this.session.connectToMongoDB(connectionString, this.config.connectOptions);
let connected = false;
for (let i = 0; i < 20; i++) {
try {
await this.session.connectToMongoDB(connectionString, this.config.connectOptions);
connected = true;

Copilot uses AI. Check for mistakes.

break;
} catch (err: unknown) {
const error = err instanceof Error ? err : new Error(String(err));
logger.debug(
LogId.atlasConnectFailure,
"atlas-connect-cluster",
`error connecting to cluster: ${error.message}`
);

await new Promise((resolve) => setTimeout(resolve, 500));
}
}

return {
content: [
Expand Down
Loading