Skip to content
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

chore: add tests for connecting with a profile from an aws credentials file MONGOSH-1609 #1733

Closed
wants to merge 5 commits into from
Closed
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
2 changes: 2 additions & 0 deletions packages/cli-repl/src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ function enableFipsIfRequested() {
}
enableFipsIfRequested();

import util from 'util';
import { CliRepl } from './cli-repl';
import { parseCliArgs } from './arg-parser';
import { runSmokeTests } from './smoke-tests';
Expand Down Expand Up @@ -220,6 +221,7 @@ async function main() {
});
} catch (e: any) {
console.error(`${e?.name}: ${e?.message}`);
console.error('full error: ' + util.inspect(e, { depth: null }));
Copy link
Contributor

Choose a reason for hiding this comment

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

Leftover? (Fwiw, I do this a ton too…)

Copy link
Contributor Author

@lerouxb lerouxb Nov 9, 2023

Choose a reason for hiding this comment

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

yeah. There's nothing more there anyway ;) Hoped there would at least be an original error or something..

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Would be nice if we could access the most recent error from the test somehow.

if (repl !== undefined) {
repl.bus.emit('mongosh:error', e, 'startup');
}
Expand Down
85 changes: 85 additions & 0 deletions packages/e2e-tests/test/e2e-aws.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import os from 'os';
import path from 'path';
import { promises as fs } from 'fs';
import { expect } from 'chai';
import { spawnSync } from 'child_process';
import { TestShell } from './test-shell';
Expand Down Expand Up @@ -295,4 +298,86 @@ describe('e2e AWS AUTH', function () {
});
});
});

context('with aws profile', function () {
const awsConfigPath = path.join(
os.tmpdir(),
`aws-config-${new Date().getTime()}-${Math.random()}`
);
Copy link
Contributor

Choose a reason for hiding this comment

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

useTmpdir() from ./repl-helpers.ts?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Didn't know about that. copy/pasted this from another test.


before(async function () {
const contents = `
[default]
aws_access_key_id=${AWS_ACCESS_KEY_ID}
aws_secret_access_key=${AWS_SECRET_ACCESS_KEY}

[user1]
aws_access_key_id=${AWS_ACCESS_KEY_ID}
aws_secret_access_key=${AWS_SECRET_ACCESS_KEY}

[invalid]
aws_access_key_id=invalid
aws_secret_access_key=invalid
`;
await fs.writeFile(awsConfigPath, contents, { encoding: 'utf8' });
});

after(async function () {
await fs.unlink(awsConfigPath);
});

context('with default aws profile', function () {
it('connects for the IAM user', async function () {
const shell = TestShell.start({
args: [getConnectionString()],
env: {
...process.env,
AWS_SHARED_CREDENTIALS_FILE: awsConfigPath,
},
});
const result = await shell.waitForPromptOrExit();
expect(result.state).to.equal('prompt');

const connectionStatus = await shell.executeLine(
'db.runCommand({ connectionStatus: 1 })'
);
expect(connectionStatus).to.contain(`user: '${AWS_IAM_USER_ARN}'`);
});
});

context('with AWS_PROFILE=user1 env var', function () {
it('connects for the IAM user', async function () {
const shell = TestShell.start({
args: [getConnectionString()],
env: {
...process.env,
AWS_SHARED_CREDENTIALS_FILE: awsConfigPath,
AWS_PROFILE: 'user1',
},
});
const result = await shell.waitForPromptOrExit();
expect(result.state).to.equal('prompt');

const connectionStatus = await shell.executeLine(
'db.runCommand({ connectionStatus: 1 })'
);
expect(connectionStatus).to.contain(`user: '${AWS_IAM_USER_ARN}'`);
});
});

context('with AWS_PROFILE=invalid env var', function () {
it('fails to connect', async function () {
const shell = TestShell.start({
args: [getConnectionString()],
env: {
...process.env,
AWS_SHARED_CREDENTIALS_FILE: awsConfigPath,
AWS_PROFILE: 'invalid',
},
});
const result = await shell.waitForPromptOrExit();
expect(result.state).to.equal('exit');
});
});
});
});