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: #7441 - init from git prompts for credentials twice #7682

Merged
merged 1 commit into from
Jul 12, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,15 @@ export class SSMClientWrapper {

const getSSMClient = async (context: $TSContext) => {
const spinner = ora('Initializing SSM Client');
spinner.start();
const { client } = await context.amplify.invokePluginMethod(context, 'awscloudformation', undefined, 'getConfiguredSSMClient', [context]);
spinner.stop();
return client as aws.SSM;
try {
spinner.start();

const { client } = await context.amplify.invokePluginMethod(context, 'awscloudformation', undefined, 'getConfiguredSSMClient', [
context,
]);

return client as aws.SSM;
} finally {
spinner.stop();
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ export async function deleteProject(context) {
}
}
} catch (ex) {
spinner.fail('Project delete failed');
spinner.fail('Project delete failed.');
throw ex;
}
spinner.succeed('Project deleted in the cloud');
spinner.succeed('Project deleted in the cloud.');
// Remove amplify dir
const { frontend } = context.amplify.getProjectConfig();
const frontendPlugins = getFrontendPlugins(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export async function removeEnvFromCloud(context, envName, deleteS3) {
const providerPlugins = getProviderPlugins(context);
const providerPromises: (() => Promise<any>)[] = [];
context.print.info('');
context.print.info(`Deleting env:${envName}`);
context.print.info(`Deleting env: ${envName}.`);

// Pinpoint attaches an IAM policy to several roles, which blocks CFN from
// deleting the roles. Work around that by deleting Pinpoint first.
Expand All @@ -28,7 +28,7 @@ export async function removeEnvFromCloud(context, envName, deleteS3) {
await raiseIntenralOnlyPostEnvRemoveEvent(context, envName);
} catch (e) {
context.print.info('');
context.print.error(`Error in deleting env:${envName}`);
context.print.error(`Error occurred while deleting env: ${envName}.`);
context.print.info(e.message);
throw e;
}
Expand Down
18 changes: 16 additions & 2 deletions packages/amplify-cli/src/init-steps/preInitSetup.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { $TSContext, exitOnNextTick, getPackageManager, NonEmptyDirectoryError } from 'amplify-cli-core';
import { $TSContext, exitOnNextTick, getPackageManager, NonEmptyDirectoryError, pathManager } from 'amplify-cli-core';
import { execSync } from 'child_process';
import * as fs from 'fs-extra';
import * as url from 'url';
Expand All @@ -12,6 +12,7 @@ export async function preInitSetup(context: $TSContext) {

await validateGithubRepo(context, repoUrl);
await cloneRepo(context, repoUrl);
cleanAmplifyArtifacts();
await installPackage();
await setLocalEnvDefaults(context);
}
Expand Down Expand Up @@ -93,5 +94,18 @@ async function setLocalEnvDefaults(context: $TSContext) {

context.exeInfo.inputParams.amplify.envName = envName;

await generateLocalEnvInfoFile(context);
generateLocalEnvInfoFile(context);
}

/**
* After cloning a project, remove the environment specific, perhaps accidentally checked in Amplify state files
* to make sure further commands will run correctly, like 'amplify delete'
*/
function cleanAmplifyArtifacts() {
const projectPath = process.cwd();

fs.removeSync(pathManager.getAmplifyMetaFilePath(projectPath));
fs.removeSync(pathManager.getTeamProviderInfoFilePath(projectPath));
fs.removeSync(pathManager.getLocalAWSInfoFilePath(projectPath));
fs.removeSync(pathManager.getLocalEnvFilePath(projectPath));
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ const { loadConfigurationForEnv } = require('./configuration-manager');
const logger = fileLogger('amplify-service-manager');

async function init(amplifyServiceParams) {
const { context, awsConfig, projectName, envName, stackName } = amplifyServiceParams;
const { context, awsConfigInfo, projectName, envName, stackName } = amplifyServiceParams;

let amplifyAppId;
let verifiedStackName = stackName;
let deploymentBucketName = `${stackName}-deployment`;

const amplifyClient = await getConfiguredAmplifyClient(context, awsConfig);
const amplifyClient = await getConfiguredAmplifyClient(context, awsConfigInfo);
if (!amplifyClient) {
// This happens when the Amplify service is not available in the region
return {
Expand Down Expand Up @@ -74,7 +74,7 @@ async function init(amplifyServiceParams) {
for (let env of envList) {
if (
env !== envName &&
teamProviderInfo[env][ProviderName].Region === awsConfig.region &&
teamProviderInfo[env][ProviderName].Region === awsConfigInfo.region &&
teamProviderInfo[env][ProviderName][AmplifyAppIdLabel] &&
!appIdsInTheSameLocalProjectAndRegion.includes(teamProviderInfo[env][ProviderName][AmplifyAppIdLabel])
) {
Expand Down Expand Up @@ -108,7 +108,7 @@ async function init(amplifyServiceParams) {
if (appIdsInTheSameLocalProjectAndRegion.length === 1) {
amplifyAppId = appIdsInTheSameLocalProjectAndRegion[0]; // eslint-disable-line
} else if (appIdsInTheSameLocalProjectAndRegion.length > 1) {
context.print.info(`Your project is associated with multiple Amplify Service Apps in the region ${awsConfig.region}`);
context.print.info(`Your project is associated with multiple Amplify Service Apps in the region ${awsConfigInfo.region}`);
amplifyAppId = await SelectFromExistingAppId(context, appIdsInTheSameLocalProjectAndRegion);
}
}
Expand Down Expand Up @@ -204,7 +204,7 @@ async function init(amplifyServiceParams) {
};
}

async function deleteEnv(context, envName, awsConfig) {
async function deleteEnv(context, envName, awsConfigInfo) {
if (stateManager.teamProviderInfoExists()) {
const teamProviderInfo = stateManager.getTeamProviderInfo();
if (
Expand All @@ -213,7 +213,7 @@ async function deleteEnv(context, envName, awsConfig) {
teamProviderInfo[envName][ProviderName][AmplifyAppIdLabel]
) {
const envConfig = await loadConfigurationForEnv(context, envName);
const amplifyClient = await getConfiguredAmplifyClient(context, { ...awsConfig, ...envConfig });
const amplifyClient = await getConfiguredAmplifyClient(context, { ...awsConfigInfo, ...envConfig });
if (!amplifyClient) {
// This happens when the Amplify service is not available in the region
return;
Expand All @@ -235,7 +235,12 @@ async function deleteEnv(context, envName, awsConfig) {
await amplifyClient.deleteBackendEnvironment(deleteEnvParams).promise();
} catch (ex) {
log(ex);
throw ex;

if (ex.code === 'NotFoundException') {
context.print.warning(ex.message);
} else {
throw ex;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ async function run(context) {
let projectDetails;
let currentAmplifyMetaFilePath;
let currentAmplifyMeta;
let awsConfig;
let awsConfigInfo;

let isProjectFullySetUp = false;

try {
projectDetails = context.amplify.getProjectDetails();
currentAmplifyMetaFilePath = context.amplify.pathManager.getCurrentAmplifyMetaFilePath();
currentAmplifyMeta = context.amplify.readJsonFile(currentAmplifyMetaFilePath);
awsConfig = await configurationManager.getAwsConfig(context);
awsConfigInfo = await configurationManager.getAwsConfig(context);
isProjectFullySetUp = true;
} catch (e) {
isProjectFullySetUp = false;
Expand All @@ -38,10 +38,10 @@ async function run(context) {
return;
}

const amplifyClient = await getConfiguredAmplifyClient(context, awsConfig);
const amplifyClient = await getConfiguredAmplifyClient(context, awsConfigInfo);
if (!amplifyClient) {
// This happens when the Amplify service is not available in the region
const message = `Amplify service is not available in the region ${awsConfig.region ? awsConfig.region : ''}`;
const message = `Amplify service is not available in the region ${awsConfigInfo.region ? awsConfigInfo.region : ''}`;
context.print.error(message);
throw new Error(message);
}
Expand Down
28 changes: 14 additions & 14 deletions packages/amplify-provider-awscloudformation/src/attach-backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const logger = fileLogger('attach-backend');

async function run(context) {
let appId;
let awsConfig;
let awsConfigInfo;
let isAdminApp = false;
try {
appId = resolveAppId(context);
Expand All @@ -44,20 +44,20 @@ async function run(context) {
}

if (isAdminApp) {
context.exeInfo.awsConfig = {
context.exeInfo.awsConfigInfo = {
configLevel: 'amplifyAdmin',
config: {},
};
awsConfig = await configurationManager.loadConfigurationForEnv(context, envName, appId);
awsConfigInfo = await configurationManager.loadConfigurationForEnv(context, envName, appId);
} else {
await configurationManager.init(context);
awsConfig = await configurationManager.getAwsConfig(context);
awsConfigInfo = await configurationManager.getAwsConfig(context);
}

const amplifyClient = await getConfiguredAmplifyClient(context, awsConfig);
const amplifyClient = await getConfiguredAmplifyClient(context, awsConfigInfo);
if (!amplifyClient) {
// This happens when the Amplify service is not available in the region
const region = awsConfig && awsConfig.region ? awsConfig.region : '<unknown>';
const region = awsConfigInfo && awsConfigInfo.region ? awsConfigInfo.region : '<unknown>';
const message = `Amplify service is not available in the region ${region}`;
context.print.error(message);
throw new Error(message);
Expand All @@ -74,15 +74,15 @@ async function run(context) {

const backendEnv = await getBackendEnv(context, amplifyClient, amplifyApp);

await downloadBackend(context, backendEnv, awsConfig);
const currentAmplifyMeta = await ensureAmplifyMeta(context, amplifyApp, awsConfig);
await downloadBackend(context, backendEnv, awsConfigInfo);
const currentAmplifyMeta = await ensureAmplifyMeta(context, amplifyApp, awsConfigInfo);

context.exeInfo.projectConfig.projectName = amplifyApp.name;
context.exeInfo.localEnvInfo.envName = backendEnv.environmentName;
context.exeInfo.teamProviderInfo[backendEnv.environmentName] = currentAmplifyMeta.providers;
}

async function ensureAmplifyMeta(context, amplifyApp, awsConfig) {
async function ensureAmplifyMeta(context, amplifyApp, awsConfigInfo) {
// check if appId is present in the provider section of the metadata
// if not, it's a migration case and we need to
// 1. insert the appId
Expand All @@ -99,15 +99,15 @@ async function ensureAmplifyMeta(context, amplifyApp, awsConfig) {
fs.writeFileSync(amplifyMetaFilePath, jsonString, 'utf8');

const { DeploymentBucketName } = currentAmplifyMeta.providers[constants.ProviderName];
await storeArtifactsForAmplifyService(context, awsConfig, DeploymentBucketName);
await storeArtifactsForAmplifyService(context, awsConfigInfo, DeploymentBucketName);
}

return currentAmplifyMeta;
}

async function storeArtifactsForAmplifyService(context, awsConfig, deploymentBucketName) {
async function storeArtifactsForAmplifyService(context, awsConfigInfo, deploymentBucketName) {
const projectPath = process.cwd();
const s3Client = new aws.S3(awsConfig);
const s3Client = new aws.S3(awsConfigInfo);
const amplifyMetaFilePath = context.amplify.pathManager.getCurrentAmplifyMetaFilePath(projectPath);
const backendConfigFilePath = context.amplify.pathManager.getCurrentBackendConfigFilePath(projectPath);
await uploadFile(s3Client, deploymentBucketName, amplifyMetaFilePath);
Expand Down Expand Up @@ -297,7 +297,7 @@ async function getBackendEnv(context, amplifyClient, amplifyApp) {
throw ex;
}

async function downloadBackend(context, backendEnv, awsConfig) {
async function downloadBackend(context, backendEnv, awsConfigInfo) {
if (!backendEnv) {
return;
}
Expand All @@ -308,7 +308,7 @@ async function downloadBackend(context, backendEnv, awsConfig) {
const backendDir = context.amplify.pathManager.getBackendDirPath(projectPath);
const zipFileName = constants.S3BackendZipFileName;

const s3Client = new aws.S3(awsConfig);
const s3Client = new aws.S3(awsConfigInfo);
const deploymentBucketName = backendEnv.deploymentArtifacts;

const params = {
Expand Down
Loading