forked from aws/aws-toolkit-vscode
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial flow for entering credentials profile (aws#109)
* Initial flow for entering credentials profile * When a user tries to connect to AWS, and no credentials profiles can be found... * if they have no credentials/config file, they are prompted to enter credentials * if they have credentials and/or config files, these are opened in the editor for the user to modify * New Comand Palette action allows user to Create a credential profile, which follows the same flow as above * When user enters credentials through a prompt, they are validated with a call to STS getCallerIdentity * compile step now runs typescript compiler before linting
- Loading branch information
1 parent
708d99b
commit 5677997
Showing
17 changed files
with
3,198 additions
and
2,368 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Amazon Web Services Credentials File used by AWS CLI, SDKs, and tools | ||
# This file was created by the AWS Toolkit for Visual Studio Code extension. | ||
# | ||
# Your AWS credentials are represented by access keys associated with IAM users. | ||
# For information about how to create and manage AWS access keys for a user, see: | ||
# https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_access-keys.html | ||
# | ||
# This credential file can store multiple access keys by placing each one in a | ||
# named "profile". For information about how to change the access keys in a | ||
# profile or to add a new profile with a different access key, see: | ||
# https://docs.aws.amazon.com/cli/latest/userguide/cli-config-files.html | ||
# | ||
[{{profileName}}] | ||
# The access key and secret key pair identify your account and grant access to AWS. | ||
aws_access_key_id = {{accessKey}} | ||
# Treat your secret key like a password. Never share your secret key with anyone. Do | ||
# not post it in online forums, or store it in a source control system. If your secret | ||
# key is ever disclosed, immediately use IAM to delete the access key and secret key | ||
# and create a new key pair. Then, update this file with the replacement key details. | ||
aws_secret_access_key = {{secretKey}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
/*! | ||
* Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
'use strict' | ||
|
||
import * as handlebars from 'handlebars' | ||
import * as path from 'path' | ||
import * as filesystem from '../filesystem' | ||
|
||
import { STS } from 'aws-sdk' | ||
import { ServiceConfigurationOptions } from 'aws-sdk/lib/service' | ||
import { EnvironmentVariables } from '../environmentVariables' | ||
import { SystemUtilities } from '../systemUtilities' | ||
|
||
/** | ||
* The payload used to fill in the handlebars template | ||
* for the simple credentials file. | ||
*/ | ||
export interface CredentialsTemplateContext { | ||
profileName: string | ||
accessKey: string | ||
secretKey: string | ||
} | ||
|
||
export interface CredentialsValidationResult { | ||
isValid: boolean, | ||
invalidMessage?: string | ||
} | ||
|
||
export class UserCredentialsUtils { | ||
|
||
/** | ||
* @description Determines which credentials related files | ||
* exist, and returns their filenames. | ||
* | ||
* @returns array of filenames for files found. | ||
*/ | ||
public static async findExistingCredentialsFilenames(): Promise<string[]> { | ||
const candidateFiles: string[] = [ | ||
this.getCredentialsFilename(), | ||
this.getConfigFilename() | ||
] | ||
|
||
const existsResults: boolean[] = await Promise.all( | ||
candidateFiles.map(async filename => await SystemUtilities.fileExists(filename)) | ||
) | ||
|
||
return candidateFiles.filter((filename, index) => existsResults[index]) | ||
} | ||
|
||
/** | ||
* @returns Filename for the credentials file | ||
*/ | ||
public static getCredentialsFilename(): string { | ||
const env = process.env as EnvironmentVariables | ||
|
||
return env.AWS_SHARED_CREDENTIALS_FILE | ||
|| path.join(SystemUtilities.getHomeDirectory(), '.aws', 'credentials') | ||
} | ||
|
||
/** | ||
* @returns Filename for the config file | ||
*/ | ||
public static getConfigFilename(): string { | ||
const env = process.env as EnvironmentVariables | ||
|
||
return env.AWS_CONFIG_FILE | ||
|| path.join(SystemUtilities.getHomeDirectory(), '.aws', 'config') | ||
} | ||
|
||
/** | ||
* @description Produces a credentials file from a template | ||
* containing a single profile based on the given information | ||
* | ||
* @param credentialsContext the profile to create in the file | ||
*/ | ||
public static async generateCredentialsFile( | ||
extensionPath: string, | ||
credentialsContext: CredentialsTemplateContext | ||
): Promise<void> { | ||
const templatePath: string = path.join(extensionPath, 'resources', 'newUserCredentialsFile') | ||
|
||
const credentialsTemplate: string = await filesystem.readFileAsyncAsString(templatePath, 'utf-8') | ||
|
||
const handlebarTemplate = handlebars.compile(credentialsTemplate) | ||
const credentialsFileContents = handlebarTemplate(credentialsContext) | ||
|
||
// Make a final check | ||
if (await SystemUtilities.fileExists(this.getCredentialsFilename())) { | ||
throw new Error('Credentials file exists. Not overwriting it.') | ||
} | ||
|
||
await filesystem.writeFileAsync(this.getCredentialsFilename(), credentialsFileContents, 'utf8') | ||
} | ||
|
||
/** | ||
* @description Tests if the given credentials are valid by making a request to AWS | ||
* | ||
* @param accessKey access key of credentials to validate | ||
* @param secretKey secret key of credentials to validate | ||
* @param sts (Optional) STS Service Client | ||
* | ||
* @returns a validation result, indicating whether or not credentials are valid, and if not, | ||
* an error message. | ||
*/ | ||
public static async validateCredentials( | ||
accessKey: string, | ||
secretKey: string, | ||
sts?: STS | ||
): Promise<CredentialsValidationResult> { | ||
try { | ||
if (!sts) { | ||
const awsServiceOpts: ServiceConfigurationOptions = { | ||
accessKeyId: accessKey, | ||
secretAccessKey: secretKey | ||
} | ||
|
||
sts = new STS(awsServiceOpts) | ||
} | ||
|
||
await sts.getCallerIdentity().promise() | ||
|
||
return { isValid: true } | ||
|
||
} catch (err) { | ||
|
||
let reason: string | ||
|
||
if (err instanceof Error) { | ||
const error = err as Error | ||
console.error(error.message) | ||
reason = error.message | ||
} else { | ||
reason = err as string | ||
} | ||
|
||
return { isValid: false, invalidMessage: reason } | ||
} | ||
} | ||
} |
Oops, something went wrong.