Skip to content

Commit

Permalink
fix: cleanup keytar and port command (twilio#436)
Browse files Browse the repository at this point in the history
  • Loading branch information
kridai authored and shrutiburman committed Aug 10, 2022
1 parent ef520fd commit d00f7ee
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 371 deletions.
12 changes: 0 additions & 12 deletions src/commands/profiles/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,6 @@ class ProfilesCreate extends BaseCommand {
this.logger.debug(error);
throw new TwilioCliError('Could not create an API Key.');
}
await this.removeKeytarKeysByProfileId(this.profileId);
this.userConfig.addProfile(this.profileId, this.accountSid, this.region, apiKey.sid, apiKey.secret);
const configSavedMessage = await this.configFile.save(this.userConfig);

Expand All @@ -242,17 +241,6 @@ class ProfilesCreate extends BaseCommand {
);
this.logger.info(configSavedMessage);
}

async removeKeytarKeysByProfileId(profileId) {
if (this.userConfig.projects.find((p) => p.id === profileId)) {
const removed = await this.secureStorage.removeCredentials(profileId);
if (removed === true) {
this.logger.info('Deleted key from keytar.');
} else {
this.logger.warn(`Could not delete ${profileId} key from keytar.`);
}
}
}
}

ProfilesCreate.aliases = ['profiles:add', 'login'];
Expand Down
85 changes: 0 additions & 85 deletions src/commands/profiles/port.js

This file was deleted.

21 changes: 3 additions & 18 deletions src/commands/profiles/remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ class ProfilesRemove extends TwilioClientCommand {
const verdict = await this.confirmRemoveProfile();
if (verdict === true) {
const keyVerdict = await this.confirmRemoveKey();
let credentials;
if (!this.userConfig.profiles[deleteProfile.id]) {
credentials = await this.deleteLocalKey(deleteProfile);
}
await this.deleteRemoteKey(deleteProfile, keyVerdict, credentials);
await this.deleteRemoteKey(deleteProfile, keyVerdict);
this.logger.info(`Deleted ${deleteProfile.id} profile.`);
this.userConfig.removeProfile(deleteProfile);
} else {
Expand Down Expand Up @@ -43,21 +39,10 @@ class ProfilesRemove extends TwilioClientCommand {
}
}

async deleteLocalKey(profileDelete) {
const credentials = this.secureStorage.getCredentials(profileDelete.id);
const removed = await this.secureStorage.removeCredentials(profileDelete.id);
if (removed === true) {
this.logger.info('Deleted local key.');
} else {
this.logger.warn('Could not delete local key.');
}
return credentials;
}

async deleteRemoteKey(profileDelete, keyVerdict, credentials) {
async deleteRemoteKey(profileDelete, keyVerdict) {
if (keyVerdict === true) {
try {
const apiKey = profileDelete.apiKey || credentials.apiKey;
const { apiKey } = profileDelete;
await this.twilioClient.api.keys(apiKey).remove();
this.logger.info('The API Key has been deleted from The Twilio console.');
} catch (error) {
Expand Down
11 changes: 5 additions & 6 deletions src/services/post-install-utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ const chalk = require('chalk');
const { Config } = require('@twilio/cli-core').services.config;
const { configureEnv } = require('@twilio/cli-core');

const PORT_WARNING = `Profiles exist with API keys in system keychain. Please run ${chalk.bold(
'twilio profiles:port',
)} to port all keys to the config file`;

const AUTOCOMLETE_WARNING = `If you’re using autocomplete, you’ll need to run '${chalk.bold(
'twilio autocomplete',
)}' after the install and then open a new terminal window. The CLI needs to re-build its cache.`;

const UPDATE_PROFILE_WARNING = `Profiles exist with API keys in system keychain. Please reconfigure profile using ${chalk.bold(
'twilio profiles:create',
)} to add profiles to the config file.`;

class PostInstallDisplayManager {
constructor(configDir, userConfig) {
configureEnv();
Expand Down Expand Up @@ -51,14 +51,13 @@ class PostInstallDisplayManager {
}

if (this.hasProjects()) {
console.warn(chalk.yellowBright(` » ${PORT_WARNING}`));
console.warn(chalk.yellowBright(` » ${UPDATE_PROFILE_WARNING}`));
}
console.warn(chalk.yellowBright(` » ${AUTOCOMLETE_WARNING}`));
}
}

module.exports = {
PostInstallDisplayManager,
PORT_WARNING,
AUTOCOMLETE_WARNING,
};
8 changes: 7 additions & 1 deletion test/commands/phone-numbers/update.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ describe('commands', () => {
return test
.do((ctx) => {
ctx.userConfig = new ConfigData();
ctx.userConfig.addProfile('default', constants.FAKE_ACCOUNT_SID);
ctx.userConfig.addProfile(
'default',
constants.FAKE_ACCOUNT_SID,
'',
constants.FAKE_API_KEY,
constants.FAKE_API_SECRET,
);
ctx.userConfig.setActiveProfile('default');

if (promptAcked) {
Expand Down
9 changes: 1 addition & 8 deletions test/commands/profiles/create.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const helpMessages = require('../../../src/services/messaging/help-messages');
describe('commands', () => {
describe('profiles', () => {
describe('create', () => {
const createTest = (commandArgs = [], { profileId = 'default', addProjects = [], removeCred = true } = {}) =>
const createTest = (commandArgs = [], { profileId = 'default', addProjects = [] } = {}) =>
test
.twilioFakeProfile(ConfigData)
.do((ctx) => {
Expand Down Expand Up @@ -39,11 +39,6 @@ describe('commands', () => {
overwrite: true,
});
ctx.testCmd.inquirer.prompt = fakePrompt;
})
.do((ctx) => {
ctx.testCmd.secureStorage.removeCredentials = () => {
return removeCred;
};
});

const mockSuccess = (api) => {
Expand Down Expand Up @@ -81,7 +76,6 @@ describe('commands', () => {
expect(ctx.stdout).to.equal('');
expect(ctx.stderr).to.contain(helpMessages.AUTH_TOKEN_NOT_SAVED);
expect(ctx.stderr).to.contain('Saved profile1.');
expect(ctx.stderr).to.contain('Deleted key from keytar.');
expect(ctx.stderr).to.contain('configuration saved');
expect(ctx.stderr).to.contain(`Created API Key ${constants.FAKE_API_KEY} and stored the secret in Config.`);
expect(ctx.stderr).to.contain(
Expand All @@ -96,7 +90,6 @@ describe('commands', () => {
expect(ctx.stdout).to.equal('');
expect(ctx.stderr).to.contain(helpMessages.AUTH_TOKEN_NOT_SAVED);
expect(ctx.stderr).to.contain('Saved profile1.');
expect(ctx.stderr).to.contain('Could not delete profile1 key from keytar.');
expect(ctx.stderr).to.contain('configuration saved');
expect(ctx.stderr).to.contain(`Created API Key ${constants.FAKE_API_KEY} and stored the secret in Config.`);
expect(ctx.stderr).to.contain(
Expand Down
20 changes: 1 addition & 19 deletions test/commands/profiles/list.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,25 +143,6 @@ describe('commands', () => {
expect(ctx.stderr).to.equal('');
});

test
.do((ctx) => {
ctx.userConfig = new ConfigData();
ctx.userConfig.addProject('profile1', constants.FAKE_ACCOUNT_SID);
ctx.userConfig.addProject('profile2', constants.FAKE_ACCOUNT_SID);
})
.twilioCliEnv(Config)
.stdout()
.stderr()
.twilioCommand(ProfilesList, [])
.it('runs profiles:list with multiple profiles from projects', (ctx) => {
expect(ctx.stdout).to.contain('profile1');
expect(ctx.stdout).to.contain('profile2');
expect(ctx.stdout).to.contain(constants.FAKE_ACCOUNT_SID);
expect(ctx.stdout).to.not.contain('Region');
expect(ctx.stdout.match(/true/g)).to.have.length(1);
expect(ctx.stderr).to.equal('');
});

test
.do((ctx) => {
ctx.userConfig = new ConfigData();
Expand Down Expand Up @@ -255,6 +236,7 @@ describe('commands', () => {
constants.FAKE_API_SECRET,
);
ctx.userConfig.addProject('profile2', constants.FAKE_ACCOUNT_SID);
ctx.userConfig.activeProfile = 'profile1';
})
.twilioCliEnv(Config)
.stdout()
Expand Down
128 changes: 0 additions & 128 deletions test/commands/profiles/port.test.js

This file was deleted.

Loading

0 comments on commit d00f7ee

Please sign in to comment.