-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Adopt SDK-standard behavior when no environment is specified #128
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* IMPORTANT: This **must** be required _before_ 'aws-sdk' is. | ||
* | ||
* This ensures the correct environment is set-up so the AWS SDK properly | ||
* loads up configruation stored in the shared credentials file (usually | ||
* found at ~/.aws/credentials) and the aws config file (usually found at | ||
* ~/.aws/config), if either is present. | ||
* | ||
* @see https://github.com/awslabs/aws-cdk/pull/128 | ||
*/ | ||
|
||
import * as fs from 'fs'; | ||
import * as os from 'os'; | ||
import * as path from 'path'; | ||
|
||
const sharedCredentialsFile = | ||
process.env.AWS_SHARED_CREDENTIALS_FILE ? process.env.AWS_SHARED_CREDENTIALS_FILE | ||
: path.join(os.homedir(), '.aws', 'credentials'); | ||
const awsConfigFile = | ||
process.env.AWS_CONFIG_FILE ? process.env.AWS_CONFIG_FILE | ||
: path.join(os.homedir(), '.aws', 'config'); | ||
|
||
if (fs.existsSync(awsConfigFile) && !fs.existsSync(sharedCredentialsFile)) { | ||
/* | ||
* Write an empty credentials file if there's a config file, otherwise the SDK will simply bail out, | ||
* since the credentials file is loaded before the config file is. | ||
*/ | ||
fs.writeFileSync(sharedCredentialsFile, ''); | ||
} | ||
if (fs.existsSync(sharedCredentialsFile)) { | ||
// Ensures that region is loaded from ~/.aws/config (https://github.com/aws/aws-sdk-js/pull/1391) | ||
process.env.AWS_SDK_LOAD_CONFIG = '1'; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,3 @@ | ||
import * as fs from 'fs'; | ||
import * as os from 'os'; | ||
import * as path from 'path'; | ||
|
||
if (fs.existsSync(path.join(os.homedir(), ".aws", "credentials")) && fs.existsSync(path.join(os.homedir(), ".aws", "config"))) { | ||
// Ensures that region is loaded from ~/.aws/config (https://github.com/aws/aws-sdk-js/pull/1391) | ||
|
||
// Only set this value if if the requisite files exist, otherwise this is | ||
// just going to throw an unhelpful error. | ||
process.env.AWS_SDK_LOAD_CONFIG = '1'; | ||
} | ||
|
||
import { Environment} from '@aws-cdk/cx-api'; | ||
import { CloudFormation, config, CredentialProviderChain , EC2, S3, SSM, STS } from 'aws-sdk'; | ||
import { debug } from '../../logging'; | ||
|
@@ -28,12 +16,9 @@ import { CredentialProviderSource, Mode } from '../aws-auth/credentials'; | |
export class SDK { | ||
private defaultAccountFetched = false; | ||
private defaultAccountId?: string = undefined; | ||
private credentialSources: CredentialProviderSource[]; | ||
private readonly userAgent: string; | ||
|
||
constructor() { | ||
this.credentialSources = PluginHost.instance.credentialProviderSources; | ||
|
||
// Find the package.json from the main toolkit | ||
const pkg = (require.main as any).require('../package.json'); | ||
this.userAgent = `${pkg.name}/${pkg.version}`; | ||
|
@@ -72,15 +57,7 @@ export class SDK { | |
} | ||
|
||
public defaultRegion() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we need to read the account ID anyway by issuing a request to AWS, wouldn't it make more sense to read the region this way as well and avoid duplicating the SDKs behavior? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Which region to I configure in order to build the service endpoint URL that I will call in order to get the region? OH WAIT! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Obvsiouly you shouldn't configure a region. I believe the SDKs have a default region and we want the toolkit to behave exactly like them. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah right. It didn't work when I tested but that was because of initialization order. Now only using |
||
if (process.env.AWS_DEFAULT_REGION) { | ||
debug('Obtaining default region from environment'); | ||
return process.env.AWS_DEFAULT_REGION; | ||
} | ||
if (config.region) { | ||
debug('Obtaining default region from AWS configuration'); | ||
return config.region; | ||
} | ||
return undefined; | ||
return config.region; | ||
} | ||
|
||
public async defaultAccount() { | ||
|
@@ -113,7 +90,7 @@ export class SDK { | |
const triedSources: CredentialProviderSource[] = []; | ||
|
||
// Otherwise, inspect the various credential sources we have | ||
for (const source of this.credentialSources) { | ||
for (const source of PluginHost.instance.credentialProviderSources) { | ||
if (!(await source.isAvailable())) { | ||
debug('Credentials source %s is not available, ignoring it.', source.name); | ||
continue; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add the URL of this pull request here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK