Skip to content

Commit

Permalink
Merge pull request #24 from steveizzle/master
Browse files Browse the repository at this point in the history
Added Credentials from environment variables and iam roles
  • Loading branch information
rahulpsd18 committed Jul 9, 2020
2 parents 2db686d + 8572cab commit 75e6c0a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 27 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ cbr <command> [options]
> `--aws-secret-key` `--secret`: The AWS Secret Key to use. Not to be passed when using `--profile`.
>
> `--delay`: delay in millis between alternate users batch(60) backup, to avoid rate limit error.
>
> `--use-env-vars`: Use AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_SESSION_TOKEN (optional) as environment variables
>
> `--use-ec2-metadata`: Use credentials received from the metadata service on an EC2 instance
![Image showing CLI Usage](gifs/demo.png "CLI Usage")

Expand Down
10 changes: 10 additions & 0 deletions src/cli/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@ export const argv = yargs
describe: dimmed`delay in millis between alternate users batch(60) backup, to avoid rate limit error`,
number: true
})
.option('use-ec2-metadata', {
alias: ['metadata'],
describe: dimmed`Use iam role in ec2 instance.`,
type: 'boolean'
})
.option('use-env-vars', {
alias: ['env'],
describe: dimmed`Use credentials from environment variables.`,
type: 'boolean'
})

// help
.help('help', dimmed`Show help`)
Expand Down
8 changes: 6 additions & 2 deletions src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const orange = chalk.keyword('orange');
(async () => {
let spinner = ora({ spinner: 'dots4', hideCursor: true });
try {
const { mode, profile, region, key, secret, userpool, directory, file, password, passwordModulePath, delay } = await options;
const { mode, profile, region, key, secret, userpool, directory, file, password, passwordModulePath, delay, metadata, env} = await options;

// update the config of aws-sdk based on profile/credentials passed
AWS.config.update({ region });
Expand All @@ -25,7 +25,11 @@ const orange = chalk.keyword('orange');
AWS.config.credentials = new AWS.Credentials({
accessKeyId: key, secretAccessKey: secret
});
}
} else if (env) {
AWS.config.credentials = new AWS.EnvironmentCredentials('AWS');
} else if (metadata) {
AWS.config.credentials = new AWS.EC2MetadataCredentials({});
}

const cognitoISP = new AWS.CognitoIdentityServiceProvider();

Expand Down
55 changes: 30 additions & 25 deletions src/cli/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,6 @@ inquirer.registerPrompt('autocomplete', require('inquirer-autocomplete-prompt'))
inquirer.registerPrompt('filePath', require('inquirer-file-path'));

const greenify = chalk.green;
const credentials = new AWS.IniLoader().loadFrom({});
const savedAWSProfiles = Object.keys(credentials);

const searchAWSProfile = async (_: never, input: string) => {
input = input || '';
const fuzzyResult = fuzzy.filter(input, savedAWSProfiles);
return fuzzyResult.map(el => {
return el.original;
});
};

const searchCognitoRegion = async (_: never, input: string) => {
input = input || '';
Expand All @@ -42,7 +32,7 @@ const searchCognitoRegion = async (_: never, input: string) => {
};

const verifyOptions = async () => {
let { mode, profile, region, key, secret, userpool, directory, file, password, passwordModulePath, delay } = argv;
let { mode, profile, region, key, secret, userpool, directory, file, password, passwordModulePath, delay, metadata, env } = argv;

// choose the mode if not passed through CLI or invalid is passed
if (!mode || !['restore', 'backup'].includes(mode)) {
Expand All @@ -56,19 +46,30 @@ const verifyOptions = async () => {
mode = modeChoice.selected.toLowerCase();
}

// choose your profile from available AWS profiles if not passed through CLI
// only shown in case when no valid profile or no key && secret is passed.
if (!savedAWSProfiles.includes(profile) && (!key || !secret)) {
const awsProfileChoice = await inquirer.prompt({
type: 'autocomplete',
name: 'selected',
message: 'Choose your AWS Profile',
source: searchAWSProfile,
} as inquirer.Question);

profile = awsProfileChoice.selected;
};
if (!metadata && !env) {
const credentials = new AWS.IniLoader().loadFrom({});
const savedAWSProfiles = Object.keys(credentials);

const searchAWSProfile = async (_: never, input: string) => {
input = input || '';
const fuzzyResult = fuzzy.filter(input, savedAWSProfiles);
return fuzzyResult.map(el => {
return el.original;
});
};
// choose your profile from available AWS profiles if not passed through CLI
// only shown in case when no valid profile or no key && secret is passed.
if (!savedAWSProfiles.includes(profile) && (!key || !secret)) {
const awsProfileChoice = await inquirer.prompt({
type: 'autocomplete',
name: 'selected',
message: 'Choose your AWS Profile',
source: searchAWSProfile,
} as inquirer.Question);

profile = awsProfileChoice.selected;
};
}
// choose your region if not passed through CLI
if (!region) {
const awsRegionChoice = await inquirer.prompt({
Expand All @@ -91,7 +92,11 @@ const verifyOptions = async () => {
AWS.config.credentials = new AWS.Credentials({
accessKeyId: key, secretAccessKey: secret
});
}
} else if (env) {
AWS.config.credentials = new AWS.EnvironmentCredentials('AWS');
} else if (metadata) {
AWS.config.credentials = new AWS.EC2MetadataCredentials({});
}

const cognitoISP = new AWS.CognitoIdentityServiceProvider();
const { UserPools } = await cognitoISP.listUserPools({ MaxResults: 60 }).promise();
Expand Down Expand Up @@ -158,7 +163,7 @@ const verifyOptions = async () => {
throw Error(`Cannot load password module path "${passwordModulePath}".`);
}
}
return { mode, profile, region, key, secret, userpool, directory, file, password, passwordModulePath, delay }
return { mode, profile, region, key, secret, userpool, directory, file, password, passwordModulePath, delay, metadata, env }
};


Expand Down

0 comments on commit 75e6c0a

Please sign in to comment.