Skip to content

Commit

Permalink
fix: source temp aws creds and ec2 instance credentials using general…
Browse files Browse the repository at this point in the history
… level config (#12181)

* fix: added a test for general profile

* fix: added config change

* fix: exp1

* fix: running only single test

* fix: added aplify error for missing creds

* fix: fixes e2e

* fix: cci config file

* chore: fixes lint

* chore: fixes ci config again

* chore: running test in specific branches

* fix: running test after verification

* fix: address comments

* fix: e2e tests config

* chore: removes extra comments

* fix: fixes codeql warnings

* fix: config file

---------

Co-authored-by: Akshay Upadhyay <[email protected]>
  • Loading branch information
akshbhu and Akshay Upadhyay authored Mar 21, 2023
1 parent f5cebf5 commit 68d267c
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 7 deletions.
44 changes: 44 additions & 0 deletions .circleci/config.base.yml
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,36 @@ jobs:
path: ~/repo/packages/amplify-migration-tests/amplify-migration-reports
working_directory: ~/repo

amplify_general_config_tests:
parameters:
os:
type: executor
default: l_large
executor: << parameters.os >>
environment:
AMPLIFY_PATH: /home/circleci/.npm-global/lib/node_modules/@aws-amplify/cli/bin/amplify
steps:
- restore_cache:
key: amplify-cli-repo-{{ .Branch }}-{{ .Revision }}
- restore_cache:
key: amplify-cli-yarn-deps-{{ .Branch }}-{{ checksum "yarn.lock" }}
- restore_cache:
key: amplify-pkg-binaries-{{ .Branch }}-{{ .Revision }}
- run:
name: Run tests using general profile
command: |
source .circleci/local_publish_helpers.sh
changeNpmGlobalPath
cd packages/amplify-e2e-tests
retry yarn run general-config-e2e --no-cache --maxWorkers=3 --forceExit $TEST_SUITE
no_output_timeout: 90m
- run: *scan_e2e_test_artifacts
- store_test_results:
path: ~/repo/packages/amplify-e2e-tests/
- store_artifacts:
path: ~/repo/packages/amplify-e2e-tests/amplify-e2e-reports
working_directory: ~/repo

amplify_console_integration_tests:
parameters:
os:
Expand Down Expand Up @@ -1156,6 +1186,20 @@ workflows:
- /run-e2e\/.*/
requires:
- upload_pkg_binaries
- amplify_general_config_tests:
context:
- e2e-auth-credentials
- cleanup-resources
- e2e-test-context
filters:
branches:
only:
- dev
- /run-e2e-with-rc\/.*/
- /tagged-release\/.*/
- /run-e2e\/.*/
requires:
- upload_pkg_binaries
- amplify_console_integration_tests:
context:
- e2e-auth-credentials
Expand Down
27 changes: 22 additions & 5 deletions packages/amplify-e2e-core/src/init/non-interactive-init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { CategoriesConfig, AwsProviderConfig } from './headless-types';
export const nonInteractiveInitAttach = async (
projRoot: string,
amplifyInitConfig: AmplifyInitConfig,
awsProviderConfig: AwsProviderConfig | AwsProviderGeneralConfig,
categoriesConfig?: CategoriesConfig,
awsProviderConfig = getAwsProviderConfig(),
): Promise<void> => {
const args = [
'init',
Expand Down Expand Up @@ -67,10 +67,23 @@ export const getAmplifyInitConfig = (projectName: string, envName: string): Ampl
/**
* Returns a default AwsProviderConfig
*/
export const getAwsProviderConfig = (): AwsProviderConfig => ({
configLevel: 'project',
useProfile: true,
profileName: TEST_PROFILE_NAME,
export const getAwsProviderConfig = (profileType?: string): AwsProviderConfig | AwsProviderGeneralConfig => {
if (profileType === 'general') {
return getAwsProviderGeneralConfig();
} else {
return {
configLevel: 'project',
useProfile: true,
profileName: TEST_PROFILE_NAME,
};
}
};

/**
* Returns a general AwsProviderConfig
*/
export const getAwsProviderGeneralConfig = (): AwsProviderGeneralConfig => ({
configLevel: 'general',
});

/**
Expand All @@ -82,3 +95,7 @@ export type AmplifyInitConfig = {
defaultEditor: string;
frontend?: string;
};

export type AwsProviderGeneralConfig = {
configLevel: string;
};
1 change: 1 addition & 0 deletions packages/amplify-e2e-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"private": true,
"scripts": {
"e2e": "npm run setup-profile && jest --verbose",
"general-config-e2e": "jest src/__tests__/general-config/general-config-headless-init.test.ts --verbose",
"build-tests": "tsc --build tsconfig.tests.json",
"setup-profile": "ts-node ./src/configure_tests.ts",
"clean-e2e-resources": "ts-node ./src/cleanup-e2e-resources.ts"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Tests for headless init/pull workflows on git-cloned projects
* These tests exercise workflows that hosting executes during backend builds
*/

import {
createNewProjectDir,
deleteProject,
deleteProjectDir,
getAmplifyInitConfig,
getAwsProviderConfig,
nonInteractiveInitAttach,
} from '@aws-amplify/amplify-e2e-core';
import {} from '@aws-amplify/amplify-e2e-core';

describe('attach amplify to git-cloned project', () => {
const envName = 'test';
const projectName = 'initGeneral';
let projRoot: string;
beforeAll(async () => {
projRoot = await createNewProjectDir('clone-test');
});

afterAll(async () => {
await deleteProject(projRoot);
deleteProjectDir(projRoot);
});

test('headless init works with general profile', async () => {
// execute headless init
await nonInteractiveInitAttach(projRoot, getAmplifyInitConfig(projectName, envName), getAwsProviderConfig('general'));
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
deleteProjectDir,
getAmplifyInitConfig,
getAmplifyPullConfig,
getAwsProviderConfig,
getProjectConfig,
getSocialProviders,
getTeamProviderInfo,
Expand Down Expand Up @@ -76,7 +77,7 @@ describe('attach amplify to git-cloned project', () => {
region: importBucketRegion,
},
};
await nonInteractiveInitAttach(projRoot, getAmplifyInitConfig(projectName, envName), categoriesConfig);
await nonInteractiveInitAttach(projRoot, getAmplifyInitConfig(projectName, envName), getAwsProviderConfig(), categoriesConfig);
await buildOverrides(projRoot);

// expect no file changes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,32 @@ describe('init', () => {
`[ProjectInitError: You have reached the Amplify App limit for this account and region]`,
);
});

it('throws Configutation error if config level is general and soorcing wrong credentials', async () => {
const amplifyClientGeneralConfigStub = {
createApp: jest.fn().mockReturnValue({
promise: jest.fn().mockRejectedValue({
code: 'ConfigError',
message: 'Missing Region in Config',
}),
}),
} as unknown as Amplify;
getConfiguredAmplifyClientMock.mockClear();
getConfiguredAmplifyClientMock.mockResolvedValue(amplifyClientGeneralConfigStub);

const amplifyServiceParamsStub = {
context: {
exeInfo: {
awsConfigInfo: {
configLevel: 'general',
},
},
},
awsConfigInfo: {},
projectName: 'test-project',
envName: 'test',
stackName: 'test-stack-name',
};
await expect(init(amplifyServiceParamsStub)).rejects.toMatchInlineSnapshot(`[ConfigurationError: Missing Region in Config]`);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,14 @@ export async function init(amplifyServiceParams) {
e,
);
}
if (context?.exeInfo?.awsConfigInfo?.configLevel === 'general' && e.code === 'ConfigError') {
throw new AmplifyError('ConfigurationError', {
code: e.code,
message: e.message,
resolution: 'https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/setting-credentials-node.html',
});
}
// default fault
throw new AmplifyFault(
'ProjectInitFault',
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -859,7 +859,7 @@ async function determineAuthFlow(context: $TSContext, projectConfig?: ProjectCon
useProfile = useProfile ?? projectConfig?.config?.useProfile;
profileName = profileName ?? projectConfig?.config?.profileName;

const generalCreds = projectConfig?.configLevel === 'general';
const generalCreds = projectConfig?.configLevel === 'general' || cfnParams?.configLevel === 'general';

if (generalCreds) {
return { type: 'general' };
Expand Down

0 comments on commit 68d267c

Please sign in to comment.