diff --git a/packages/cli-repl/src/run.ts b/packages/cli-repl/src/run.ts index 5d398f449..53ee5282f 100644 --- a/packages/cli-repl/src/run.ts +++ b/packages/cli-repl/src/run.ts @@ -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'; @@ -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 })); if (repl !== undefined) { repl.bus.emit('mongosh:error', e, 'startup'); } diff --git a/packages/e2e-tests/test/e2e-aws.spec.ts b/packages/e2e-tests/test/e2e-aws.spec.ts index cf56e739b..fa07fae0e 100644 --- a/packages/e2e-tests/test/e2e-aws.spec.ts +++ b/packages/e2e-tests/test/e2e-aws.spec.ts @@ -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'; @@ -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()}` + ); + + 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'); + }); + }); + }); });