Skip to content

Commit fd136c6

Browse files
authored
Calls to sam deploy and package no longer use the --profile command line argument. Environment Variables are used for the child process instead. (#879)
1 parent 33697f8 commit fd136c6

File tree

8 files changed

+86
-98
lines changed

8 files changed

+86
-98
lines changed

package.nls.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,10 @@
162162
"AWS.samcli.detect.settings.not.updated": "No settings changes necessary.",
163163
"AWS.samcli.deploy.general.error": "An error occurred while deploying a SAM Application. {0}",
164164
"AWS.samcli.deploy.workflow.init": "Building SAM Application...",
165-
"AWS.samcli.deploy.workflow.packaging": "Packaging SAM Application to S3 Bucket: {0} with profile: {1}",
166-
"AWS.samcli.deploy.workflow.stackName.initiated": "Deploying SAM Application to CloudFormation Stack: {0} with profile: {1}",
165+
"AWS.samcli.deploy.workflow.packaging": "Packaging SAM Application to S3 Bucket: {0}",
166+
"AWS.samcli.deploy.workflow.stackName.initiated": "Deploying SAM Application to CloudFormation Stack: {0}",
167167
"AWS.samcli.deploy.workflow.start": "Starting SAM Application deployment...",
168-
"AWS.samcli.deploy.workflow.success": "Successfully deployed SAM Application to CloudFormation Stack: {0} with profile: {1}",
168+
"AWS.samcli.deploy.workflow.success": "Successfully deployed SAM Application to CloudFormation Stack: {0}",
169169
"AWS.samcli.deploy.workflow.success.general": "SAM Application deployment succeeded.",
170170
"AWS.samcli.deploy.workflow.error": "Failed to deploy SAM application.",
171171
"AWS.samcli.deploy.parameters.mandatoryPrompt.message": "The template {0} contains parameters without default values. In order to deploy, you must provide values for these parameters. Configure them now?",
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*!
2+
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
import { Credentials } from 'aws-sdk'
7+
8+
export function asEnvironmentVariables(credentials: Credentials): NodeJS.ProcessEnv {
9+
const environmentVariables: NodeJS.ProcessEnv = {}
10+
11+
environmentVariables.AWS_ACCESS_KEY = credentials.accessKeyId
12+
environmentVariables.AWS_ACCESS_KEY_ID = credentials.accessKeyId
13+
environmentVariables.AWS_SECRET_KEY = credentials.secretAccessKey
14+
environmentVariables.AWS_SECRET_ACCESS_KEY = credentials.secretAccessKey
15+
environmentVariables.AWS_SESSION_TOKEN = credentials.sessionToken
16+
environmentVariables.AWS_SECURITY_TOKEN = credentials.sessionToken
17+
18+
return environmentVariables
19+
}

src/lambda/commands/deploySamApplication.ts

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import * as path from 'path'
88
import * as vscode from 'vscode'
99
import * as nls from 'vscode-nls'
1010

11+
import { asEnvironmentVariables } from '../../credentials/credentialsUtilities'
1112
import { AwsContext, NoActiveCredentialError } from '../../shared/awsContext'
1213
import { makeTemporaryToolkitFolder } from '../../shared/filesystemUtilities'
1314
import { RegionProvider } from '../../shared/regions/regionProvider'
@@ -26,7 +27,7 @@ const localize = nls.loadMessageBundle()
2627
interface DeploySamApplicationParameters {
2728
sourceTemplatePath: string
2829
deployRootFolder: string
29-
profile: string
30+
environmentVariables: NodeJS.ProcessEnv
3031
region: string
3132
packageBucketName: string
3233
destinationStackName: string
@@ -61,13 +62,13 @@ export async function deploySamApplication(
6162
awsContext,
6263
window = getDefaultWindowFunctions()
6364
}: {
64-
awsContext: Pick<AwsContext, 'getCredentialProfileName'>
65+
awsContext: Pick<AwsContext, 'getCredentials'>
6566
window?: WindowFunctions
6667
}
6768
): Promise<void> {
6869
try {
69-
const profile: string | undefined = awsContext.getCredentialProfileName()
70-
if (!profile) {
70+
const credentials = await awsContext.getCredentials()
71+
if (!credentials) {
7172
throw new NoActiveCredentialError()
7273
}
7374

@@ -84,7 +85,7 @@ export async function deploySamApplication(
8485
destinationStackName: deployWizardResponse.stackName,
8586
packageBucketName: deployWizardResponse.s3Bucket,
8687
parameterOverrides: deployWizardResponse.parameterOverrides,
87-
profile,
88+
environmentVariables: asEnvironmentVariables(credentials),
8889
region: deployWizardResponse.region,
8990
sourceTemplatePath: deployWizardResponse.template.fsPath
9091
}
@@ -156,9 +157,8 @@ async function packageOperation(params: {
156157
}): Promise<void> {
157158
params.channelLogger.info(
158159
'AWS.samcli.deploy.workflow.packaging',
159-
'Packaging SAM Application to S3 Bucket: {0} with profile: {1}',
160-
params.deployParameters.packageBucketName,
161-
params.deployParameters.profile
160+
'Packaging SAM Application to S3 Bucket: {0}',
161+
params.deployParameters.packageBucketName
162162
)
163163

164164
const buildTemplatePath = getBuildTemplatePath(params.deployParameters.deployRootFolder)
@@ -168,7 +168,7 @@ async function packageOperation(params: {
168168
{
169169
sourceTemplateFile: buildTemplatePath,
170170
destinationTemplateFile: packageTemplatePath,
171-
profile: params.deployParameters.profile,
171+
environmentVariables: params.deployParameters.environmentVariables,
172172
region: params.deployParameters.region,
173173
s3Bucket: params.deployParameters.packageBucketName
174174
},
@@ -185,17 +185,16 @@ async function deployOperation(params: {
185185
try {
186186
params.channelLogger.info(
187187
'AWS.samcli.deploy.workflow.stackName.initiated',
188-
'Deploying SAM Application to CloudFormation Stack: {0} with profile: {1}',
189-
params.deployParameters.destinationStackName,
190-
params.deployParameters.profile
188+
'Deploying SAM Application to CloudFormation Stack: {0}',
189+
params.deployParameters.destinationStackName
191190
)
192191

193192
const packageTemplatePath = getPackageTemplatePath(params.deployParameters.deployRootFolder)
194193

195194
await runSamCliDeploy(
196195
{
197196
parameterOverrides: params.deployParameters.parameterOverrides,
198-
profile: params.deployParameters.profile,
197+
environmentVariables: params.deployParameters.environmentVariables,
199198
templateFile: packageTemplatePath,
200199
region: params.deployParameters.region,
201200
stackName: params.deployParameters.destinationStackName
@@ -232,9 +231,8 @@ async function deploy(params: {
232231

233232
params.channelLogger.info(
234233
'AWS.samcli.deploy.workflow.success',
235-
'Successfully deployed SAM Application to CloudFormation Stack: {0} with profile: {1}',
236-
params.deployParameters.destinationStackName,
237-
params.deployParameters.profile
234+
'Successfully deployed SAM Application to CloudFormation Stack: {0}',
235+
params.deployParameters.destinationStackName
238236
)
239237

240238
params.window.showInformationMessage(
@@ -264,9 +262,6 @@ function enhanceAwsCloudFormationInstructions(
264262
)
265263
) {
266264
message += ` --region ${deployParameters.region}`
267-
if (deployParameters.profile) {
268-
message += ` --profile ${deployParameters.profile}`
269-
}
270265
}
271266

272267
return message

src/shared/sam/cli/samCliDeploy.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { logAndThrowIfUnexpectedExitCode, SamCliProcessInvoker } from './samCliI
1010
export interface SamCliDeployParameters {
1111
templateFile: string
1212
parameterOverrides: Map<string, string>
13-
profile: string
13+
environmentVariables: NodeJS.ProcessEnv
1414
region: string
1515
stackName: string
1616
}
@@ -29,16 +29,17 @@ export async function runSamCliDeploy(
2929
'--capabilities',
3030
'CAPABILITY_IAM',
3131
'--region',
32-
deployArguments.region,
33-
'--profile',
34-
deployArguments.profile
32+
deployArguments.region
3533
]
3634
if (deployArguments.parameterOverrides.size > 0) {
3735
const overrides = [...map(deployArguments.parameterOverrides.entries(), ([key, value]) => `${key}=${value}`)]
3836
args.push('--parameter-overrides', ...overrides)
3937
}
4038

41-
const childProcessResult = await invoker.invoke({ arguments: args })
39+
const childProcessResult = await invoker.invoke({
40+
arguments: args,
41+
spawnOptions: { env: deployArguments.environmentVariables }
42+
})
4243

4344
logAndThrowIfUnexpectedExitCode(childProcessResult, 0, logger)
4445
}

src/shared/sam/cli/samCliPackage.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export interface SamCliPackageParameters {
1515
* The SAM Template produced by SAM CLI's packaging
1616
*/
1717
destinationTemplateFile: string
18-
profile: string
18+
environmentVariables: NodeJS.ProcessEnv
1919
region: string
2020
s3Bucket: string
2121
}
@@ -35,10 +35,11 @@ export async function runSamCliPackage(
3535
'--output-template-file',
3636
packageArguments.destinationTemplateFile,
3737
'--region',
38-
packageArguments.region,
39-
'--profile',
40-
packageArguments.profile
41-
]
38+
packageArguments.region
39+
],
40+
spawnOptions: {
41+
env: packageArguments.environmentVariables
42+
}
4243
})
4344

4445
logAndThrowIfUnexpectedExitCode(childProcessResult, 0, logger)

src/test/lambda/commands/deploySamApplication.test.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,10 @@ describe('deploySamApplication', async () => {
119119
}
120120
}
121121

122-
let testProfileName: string | undefined
123-
const awsContext: Pick<AwsContext, 'getCredentialProfileName'> = {
124-
getCredentialProfileName: () => testProfileName
122+
const placeholderCredentials = ({} as any) as AWS.Credentials
123+
let testCredentials: AWS.Credentials | undefined
124+
const awsContext: Pick<AwsContext, 'getCredentials'> = {
125+
getCredentials: async () => testCredentials
125126
}
126127

127128
let channelLogger: FakeChannelLogger
@@ -143,7 +144,7 @@ describe('deploySamApplication', async () => {
143144
const templatePath = path.join(tempToolkitFolder, 'template.yaml')
144145
writeFile(templatePath)
145146

146-
testProfileName = 'profile'
147+
testCredentials = placeholderCredentials
147148
invokerCalledCount = 0
148149
samDeployWizardResponse = {
149150
parameterOverrides: new Map<string, string>(),
@@ -180,7 +181,7 @@ describe('deploySamApplication', async () => {
180181
})
181182

182183
it('informs user of error when user is not logged in', async () => {
183-
testProfileName = undefined
184+
testCredentials = undefined
184185

185186
await deploySamApplication(
186187
{

src/test/shared/sam/cli/samCliDeploy.test.ts

Lines changed: 21 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55

66
import * as assert from 'assert'
7-
import { runSamCliDeploy } from '../../../../shared/sam/cli/samCliDeploy'
7+
import { runSamCliDeploy, SamCliDeployParameters } from '../../../../shared/sam/cli/samCliDeploy'
88
import { getTestLogger } from '../../../globalSetup.test'
99
import { assertThrowsError } from '../../utilities/assertUtils'
1010
import {
@@ -20,7 +20,6 @@ import {
2020
} from './testSamCliProcessInvoker'
2121

2222
describe('runSamCliDeploy', async () => {
23-
const fakeProfile = 'profile'
2423
const fakeRegion = 'region'
2524
const fakeStackName = 'stackName'
2625
const fakeTemplateFile = 'template'
@@ -36,16 +35,7 @@ describe('runSamCliDeploy', async () => {
3635
assertArgNotPresent(args, '--parameter-overrides')
3736
})
3837

39-
await runSamCliDeploy(
40-
{
41-
profile: fakeProfile,
42-
parameterOverrides: new Map<string, string>(),
43-
region: fakeRegion,
44-
stackName: fakeStackName,
45-
templateFile: fakeTemplateFile
46-
},
47-
invoker
48-
)
38+
await runSamCliDeploy(makeSampleSamCliDeployParameters(new Map<string, string>()), invoker)
4939

5040
assert.strictEqual(invokeCount, 1, 'Unexpected invoke count')
5141
})
@@ -62,38 +52,27 @@ describe('runSamCliDeploy', async () => {
6252
})
6353

6454
await runSamCliDeploy(
65-
{
66-
profile: fakeProfile,
67-
parameterOverrides: new Map<string, string>([['key1', 'value1'], ['key2', 'value2']]),
68-
region: fakeRegion,
69-
stackName: fakeStackName,
70-
templateFile: fakeTemplateFile
71-
},
55+
makeSampleSamCliDeployParameters(
56+
new Map<string, string>([
57+
['key1', 'value1'],
58+
['key2', 'value2']
59+
])
60+
),
7261
invoker
7362
)
7463

7564
assert.strictEqual(invokeCount, 1, 'Unexpected invoke count')
7665
})
7766

78-
it('includes a template, stack name, region, and profile ', async () => {
67+
it('includes a template, stack name, and region', async () => {
7968
const invoker = new MockSamCliProcessInvoker(args => {
8069
invokeCount++
8170
assertArgsContainArgument(args, '--template-file', fakeTemplateFile)
8271
assertArgsContainArgument(args, '--stack-name', fakeStackName)
8372
assertArgsContainArgument(args, '--region', fakeRegion)
84-
assertArgsContainArgument(args, '--profile', fakeProfile)
8573
})
8674

87-
await runSamCliDeploy(
88-
{
89-
profile: fakeProfile,
90-
parameterOverrides: new Map<string, string>(),
91-
region: fakeRegion,
92-
stackName: fakeStackName,
93-
templateFile: fakeTemplateFile
94-
},
95-
invoker
96-
)
75+
await runSamCliDeploy(makeSampleSamCliDeployParameters(new Map<string, string>()), invoker)
9776

9877
assert.strictEqual(invokeCount, 1, 'Unexpected invoke count')
9978
})
@@ -103,13 +82,7 @@ describe('runSamCliDeploy', async () => {
10382

10483
const error = await assertThrowsError(async () => {
10584
await runSamCliDeploy(
106-
{
107-
profile: fakeProfile,
108-
parameterOverrides: new Map<string, string>(),
109-
region: fakeRegion,
110-
stackName: fakeStackName,
111-
templateFile: fakeTemplateFile
112-
},
85+
makeSampleSamCliDeployParameters(new Map<string, string>()),
11386
badExitCodeProcessInvoker
11487
)
11588
}, 'Expected an error to be thrown')
@@ -121,4 +94,14 @@ describe('runSamCliDeploy', async () => {
12194
0
12295
)
12396
})
97+
98+
function makeSampleSamCliDeployParameters(parameterOverrides: Map<string, string>): SamCliDeployParameters {
99+
return {
100+
environmentVariables: {},
101+
parameterOverrides: parameterOverrides,
102+
region: fakeRegion,
103+
stackName: fakeStackName,
104+
templateFile: fakeTemplateFile
105+
}
106+
}
124107
})

0 commit comments

Comments
 (0)