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 all 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
31 changes: 30 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 All @@ -15,6 +16,10 @@ async function generateSecurePassword(): Promise<string> {
return pass;
}

function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}

export class ConnectClusterTool extends AtlasToolBase {
protected name = "atlas-connect-cluster";
protected description = "Connect to MongoDB Atlas cluster";
Expand Down Expand Up @@ -100,7 +105,31 @@ export class ConnectClusterTool extends AtlasToolBase {
cn.searchParams.set("authSource", "admin");
const connectionString = cn.toString();

await this.session.connectToMongoDB(connectionString, this.config.connectOptions);
let lastError: Error | undefined = undefined;

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);
lastError = undefined;
break;
} catch (err: unknown) {
const error = err instanceof Error ? err : new Error(String(err));

lastError = error;

logger.debug(
LogId.atlasConnectFailure,
"atlas-connect-cluster",
`error connecting to cluster: ${error.message}`
);

await sleep(500); // wait for 500ms before retrying
}
}

if (lastError) {
throw lastError;
}

return {
content: [
Expand Down
2 changes: 1 addition & 1 deletion src/tools/tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ export abstract class ToolBase {
{
type: "text",
text: `Error running ${this.name}: ${error instanceof Error ? error.message : String(error)}`,
isError: true,
},
],
isError: true,
};
}

Expand Down
Loading