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

fix(amplify-provider-awscloudformation): ask auth flow type for new envs #6569

Merged
merged 1 commit into from
Feb 8, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ async function run(context) {
}

if (isAdminApp) {
awsConfig = await configurationManager.loadConfigurationForEnv(context, envName, appId);
context.exeInfo.awsConfig = {
configLevel: 'amplifyAdmin',
config: {},
};
awsConfig = await configurationManager.loadConfigurationForEnv(context, envName, appId);
} else {
await configurationManager.init(context);
awsConfig = await configurationManager.getAwsConfig(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -456,11 +456,11 @@ function persistLocalEnvConfig(context: $TSContext) {

function getCurrentConfig(context: $TSContext) {
const { envName }: { envName: string } = context.amplify.getEnvInfo();
return getConfigForEnv(envName);
return getConfigForEnv(context, envName);
}

function getConfigForEnv(envName: string) {
const projectConfigInfo: ProjectConfig = {
function getConfigForEnv(context: $TSContext, envName: string) {
const projectConfigInfo: ProjectConfig = context?.exeInfo?.awsConfig || {
configLevel: 'general',
config: {},
};
Expand Down Expand Up @@ -545,7 +545,7 @@ export async function loadConfigurationForEnv(context: $TSContext, env: string,
return awsConfigInfo.config;
}

const projectConfigInfo = getConfigForEnv(env);
const projectConfigInfo = getConfigForEnv(context, env);
const authType = await determineAuthFlow(context, projectConfigInfo);
const { print, usageData } = context;
let awsConfig: AwsSdkConfig;
Expand Down Expand Up @@ -745,22 +745,11 @@ async function determineAuthFlow(context: $TSContext, projectConfig?: ProjectCon
useProfile = useProfile ?? projectConfig?.config?.useProfile;
profileName = profileName ?? projectConfig?.config?.profileName;

if (context?.exeInfo?.inputParams?.yes) {
if (process.env.AWS_SDK_LOAD_CONFIG) {
useProfile = useProfile === undefined ? true : useProfile;
profileName = profileName || process.env.AWS_PROFILE || 'default';
} else {
accessKeyId = accessKeyId || process.env.AWS_ACCESS_KEY_ID;
secretAccessKey = secretAccessKey || process.env.AWS_SECRET_ACCESS_KEY;
}
region = region || resolveRegion();
}

if (useProfile && profileName) {
return { type: 'profile', profileName };
}

if (accessKeyId && secretAccessKey) {
if (accessKeyId && secretAccessKey && region) {
return { type: 'accessKeys', accessKeyId, region, secretAccessKey };
}

Expand All @@ -769,29 +758,43 @@ async function determineAuthFlow(context: $TSContext, projectConfig?: ProjectCon
return { ...awsConfig, type: 'accessKeys' };
}

if (context?.exeInfo?.inputParams?.yes) {
const errorMessage = 'Failed to resolve AWS access keys with --yes flag.';
const docsUrl = 'https://docs.amplify.aws/cli/usage/headless';
context.print.error(errorMessage);
context.print.info(`Access keys for continuous integration can be configured with headless paramaters: ${chalk.green(docsUrl)}`);
await context.usageData.emitError(errorMessage);
exitOnNextTick(1);
}

let appId: string;
let adminAppConfig: { isAdminApp?: boolean; region?: string };
try {
appId = resolveAppId(context);
if (appId) {
adminAppConfig = await isAmplifyAdminApp(appId);
if (adminAppConfig.isAdminApp && doAdminTokensExist(appId)) {
if (adminAppConfig.isAdminApp && doAdminTokensExist(appId) && projectConfig?.configLevel === 'amplifyAdmin') {
return { type: 'admin', appId, region: adminAppConfig.region };
}
}
} catch (e) {
// do nothing, appId might not be defined for a new project
}

if (context?.exeInfo?.inputParams?.yes) {
if (process.env.AWS_SDK_LOAD_CONFIG) {
profileName = profileName || process.env.AWS_PROFILE || 'default';
return { type: 'profile', profileName };
} else {
accessKeyId = accessKeyId || process.env.AWS_ACCESS_KEY_ID;
secretAccessKey = secretAccessKey || process.env.AWS_SECRET_ACCESS_KEY;
region = region || resolveRegion();
if (accessKeyId && secretAccessKey && region) {
return { type: 'accessKeys', accessKeyId, region, secretAccessKey };
}
}
}

if (context?.exeInfo?.inputParams?.yes) {
const errorMessage = 'Failed to resolve AWS credentials with --yes flag.';
const docsUrl = 'https://docs.amplify.aws/cli/usage/headless';
context.print.error(errorMessage);
context.print.info(`Access keys for continuous integration can be configured with headless paramaters: ${chalk.green(docsUrl)}`);
await context.usageData.emitError(errorMessage);
exitOnNextTick(1);
}

const authType = await askAuthType(adminAppConfig?.isAdminApp);
return { type: authType };
}
Expand Down