Skip to content

Commit ccb7bf8

Browse files
authored
fix(tests): outdated netwonsoft dep (#2728)
Problem: newtonsoft dependency in the SAM examples has a reported vulnerability which is detected by automation. Solution: - Update the examples to the "dotnet6" template from `sam init`. - Update tests for the new templates.
1 parent 6f97945 commit ccb7bf8

File tree

19 files changed

+176
-233
lines changed

19 files changed

+176
-233
lines changed

src/test/shared/codelens/sampleDotNetSamProgram.ts

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,37 @@
66
import * as vscode from 'vscode'
77

88
/**
9-
* This file emits the Program.cs file contents from the stock SAM CLI app for dotnet, as well as
10-
* the corresponding DocumentSymbols generated by VS Code.
9+
* @deprecated These are not easy to update if the related document changes...
1110
*/
1211

1312
export function getDocumentSymbols(): vscode.DocumentSymbol[] {
1413
const namespaceSymbol: vscode.DocumentSymbol = new vscode.DocumentSymbol(
1514
'HelloWorld',
1615
'',
1716
vscode.SymbolKind.Namespace,
18-
new vscode.Range(14, 0, 51, 1),
19-
new vscode.Range(14, 10, 14, 20)
17+
new vscode.Range(12, 0, 48, 1),
18+
new vscode.Range(12, 10, 12, 20)
2019
)
2120
const classSymbol: vscode.DocumentSymbol = new vscode.DocumentSymbol(
2221
'HelloWorld.Function',
2322
'',
2423
vscode.SymbolKind.Class,
25-
new vscode.Range(17, 4, 50, 5),
26-
new vscode.Range(17, 17, 17, 25)
24+
new vscode.Range(15, 4, 47, 5),
25+
new vscode.Range(15, 17, 15, 25)
2726
)
2827
const privateMethodSymbol: vscode.DocumentSymbol = new vscode.DocumentSymbol(
2928
'GetCallingIP()',
3029
'',
3130
vscode.SymbolKind.Method,
32-
new vscode.Range(22, 8, 31, 9),
33-
new vscode.Range(22, 42, 22, 54)
31+
new vscode.Range(20, 8, 28, 9),
32+
new vscode.Range(20, 42, 20, 54)
3433
)
3534
const publicMethodSymbol: vscode.DocumentSymbol = new vscode.DocumentSymbol(
3635
'FunctionHandler(APIGatewayProxyRequest apigProxyEvent, ILambdaContext context)',
3736
'',
3837
vscode.SymbolKind.Method,
39-
new vscode.Range(33, 8, 49, 9),
40-
new vscode.Range(33, 39, 33, 54)
38+
new vscode.Range(30, 8, 46, 9),
39+
new vscode.Range(30, 51, 30, 66)
4140
)
4241

4342
namespaceSymbol.children.push(classSymbol)
@@ -47,20 +46,21 @@ export function getDocumentSymbols(): vscode.DocumentSymbol[] {
4746
return [namespaceSymbol]
4847
}
4948

49+
/**
50+
* @deprecated
51+
*/
5052
export function getFunctionText(): string {
51-
return String.raw`using System;
53+
return String.raw`
5254
using System.Collections.Generic;
53-
using System.Linq;
54-
using System.Threading.Tasks;
5555
using System.Net.Http;
56-
using System.Net.Http.Headers;
57-
using Newtonsoft.Json;
56+
using System.Text.Json;
57+
using System.Threading.Tasks;
5858
59-
using Amazon.Lambda.Core;
6059
using Amazon.Lambda.APIGatewayEvents;
60+
using Amazon.Lambda.Core;
6161
6262
// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
63-
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
63+
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]
6464
6565
namespace HelloWorld
6666
{
@@ -75,25 +75,24 @@ namespace HelloWorld
7575
client.DefaultRequestHeaders.Accept.Clear();
7676
client.DefaultRequestHeaders.Add("User-Agent", "AWS Lambda .Net Client");
7777
78-
var stringTask = client.GetStringAsync("http://checkip.amazonaws.com/").ConfigureAwait(continueOnCapturedContext:false);
78+
var msg = await client.GetStringAsync("http://checkip.amazonaws.com/").ConfigureAwait(continueOnCapturedContext:false);
7979
80-
var msg = await stringTask;
8180
return msg.Replace("\n","");
8281
}
8382
84-
public APIGatewayProxyResponse FunctionHandler(APIGatewayProxyRequest apigProxyEvent, ILambdaContext context)
83+
public async Task<APIGatewayProxyResponse> FunctionHandler(APIGatewayProxyRequest apigProxyEvent, ILambdaContext context)
8584
{
8685
87-
string location = GetCallingIP().Result;
88-
Dictionary<string, string> body = new Dictionary<string, string>
86+
var location = await GetCallingIP();
87+
var body = new Dictionary<string, string>
8988
{
9089
{ "message", "hello world" },
91-
{ "location", location },
90+
{ "location", location }
9291
};
9392
9493
return new APIGatewayProxyResponse
9594
{
96-
Body = JsonConvert.SerializeObject(body),
95+
Body = JsonSerializer.Serialize(body),
9796
StatusCode = 200,
9897
Headers = new Dictionary<string, string> { { "Content-Type", "application/json" } }
9998
};

src/test/shared/debug/launchConfiguration.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ describe('LaunchConfiguration', function () {
102102
const templateUriPython37 = vscode.Uri.file(
103103
path.join(workspace.uri.fsPath, 'python3.7-plain-sam-app/template.yaml')
104104
)
105-
const templateUriCsharp = vscode.Uri.file(path.join(workspace.uri.fsPath, 'csharp2.1-plain-sam-app/template.yaml'))
105+
const templateUriCsharp = vscode.Uri.file(path.join(workspace.uri.fsPath, 'csharp6-zip/template.yaml'))
106106

107107
beforeEach(async function () {
108108
await globals.templateRegistry.addWatchPattern(TEMPLATE_FILE_GLOB_PATTERN)

src/test/shared/sam/debugger/samDebugConfigProvider.test.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ describe('SamDebugConfigurationProvider', async function () {
310310
const config = await getConfig(
311311
debugConfigProvider,
312312
globals.templateRegistry,
313-
'testFixtures/workspaceFolder/csharp2.1-plain-sam-app/'
313+
'testFixtures/workspaceFolder/csharp6-zip/'
314314
)
315315

316316
// No workspace folder:
@@ -1586,7 +1586,7 @@ describe('SamDebugConfigurationProvider', async function () {
15861586

15871587
it('target=code: dotnet/csharp', async function () {
15881588
const appDir = pathutil.normalize(
1589-
path.join(testutil.getProjectDir(), 'testFixtures/workspaceFolder/csharp2.1-plain-sam-app/')
1589+
path.join(testutil.getProjectDir(), 'testFixtures/workspaceFolder/csharp6-zip/')
15901590
)
15911591
const folder = testutil.getWorkspaceFolder(appDir)
15921592
const input = {
@@ -1599,7 +1599,7 @@ describe('SamDebugConfigurationProvider', async function () {
15991599
projectRoot: 'src/HelloWorld',
16001600
},
16011601
lambda: {
1602-
runtime: 'dotnetcore3.1',
1602+
runtime: 'dotnet6',
16031603
},
16041604
}
16051605
const actual = (await debugConfigProvider.makeConfig(folder, input))! as SamLaunchRequestArgs
@@ -1608,7 +1608,7 @@ describe('SamDebugConfigurationProvider', async function () {
16081608
const expected: SamLaunchRequestArgs = {
16091609
awsCredentials: fakeCredentials,
16101610
request: 'attach', // Input "direct-invoke", output "attach".
1611-
runtime: 'dotnetcore3.1', // lambdaModel.dotNetRuntimes[0],
1611+
runtime: 'dotnet6', // lambdaModel.dotNetRuntimes[0],
16121612
runtimeFamily: lambdaModel.RuntimeFamily.DotNetCore,
16131613
useIkpdb: false,
16141614
type: AWS_SAM_DEBUG_TYPE,
@@ -1675,7 +1675,7 @@ describe('SamDebugConfigurationProvider', async function () {
16751675
Handler: HelloWorld::HelloWorld.Function::FunctionHandler
16761676
CodeUri: >-
16771677
${input.invokeTarget.projectRoot}
1678-
Runtime: dotnetcore3.1
1678+
Runtime: dotnet6
16791679
`
16801680
)
16811681

@@ -1741,7 +1741,7 @@ describe('SamDebugConfigurationProvider', async function () {
17411741

17421742
it('target=template: dotnet/csharp', async function () {
17431743
const appDir = pathutil.normalize(
1744-
path.join(testutil.getProjectDir(), 'testFixtures/workspaceFolder/csharp2.1-plain-sam-app')
1744+
path.join(testutil.getProjectDir(), 'testFixtures/workspaceFolder/csharp6-zip')
17451745
)
17461746
const folder = testutil.getWorkspaceFolder(appDir)
17471747
const input = {
@@ -1774,7 +1774,7 @@ describe('SamDebugConfigurationProvider', async function () {
17741774
const expected: SamLaunchRequestArgs = {
17751775
awsCredentials: fakeCredentials,
17761776
request: 'attach', // Input "direct-invoke", output "attach".
1777-
runtime: 'dotnetcore3.1', // lambdaModel.dotNetRuntimes[0],
1777+
runtime: 'dotnet6', // lambdaModel.dotNetRuntimes[0],
17781778
runtimeFamily: lambdaModel.RuntimeFamily.DotNetCore,
17791779
useIkpdb: false,
17801780
type: AWS_SAM_DEBUG_TYPE,
@@ -1796,7 +1796,7 @@ describe('SamDebugConfigurationProvider', async function () {
17961796
...input.lambda,
17971797
},
17981798
name: input.name,
1799-
architecture: undefined,
1799+
architecture: 'x86_64',
18001800
templatePath: pathutil.normalize(path.join(path.dirname(templatePath.fsPath), 'template.yaml')),
18011801

18021802
//
@@ -1893,7 +1893,7 @@ describe('SamDebugConfigurationProvider', async function () {
18931893

18941894
it('target=template: Image dotnet/csharp', async function () {
18951895
const appDir = pathutil.normalize(
1896-
path.join(testutil.getProjectDir(), 'testFixtures/workspaceFolder/csharp2.1-image-sam-app')
1896+
path.join(testutil.getProjectDir(), 'testFixtures/workspaceFolder/csharp6-image')
18971897
)
18981898
const folder = testutil.getWorkspaceFolder(appDir)
18991899
const input = {
@@ -1906,7 +1906,7 @@ describe('SamDebugConfigurationProvider', async function () {
19061906
logicalId: 'HelloWorldFunction',
19071907
},
19081908
lambda: {
1909-
runtime: 'dotnetcore3.1',
1909+
runtime: 'dotnet6',
19101910
environmentVariables: {
19111911
'test-envvar-1': 'test value 1',
19121912
},
@@ -1927,7 +1927,7 @@ describe('SamDebugConfigurationProvider', async function () {
19271927
const expected: SamLaunchRequestArgs = {
19281928
awsCredentials: fakeCredentials,
19291929
request: 'attach', // Input "direct-invoke", output "attach".
1930-
runtime: 'dotnetcore3.1', // lambdaModel.dotNetRuntimes[0],
1930+
runtime: 'dotnet6', // lambdaModel.dotNetRuntimes[0],
19311931
runtimeFamily: lambdaModel.RuntimeFamily.DotNetCore,
19321932
useIkpdb: false,
19331933
type: AWS_SAM_DEBUG_TYPE,

src/testFixtures/workspaceFolder/.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
"request": "direct-invoke",
9696
"invokeTarget": {
9797
"target": "api",
98-
"templatePath": "csharp2.1-plain-sam-app/template.yaml",
98+
"templatePath": "csharp6-zip/template.yaml",
9999
"logicalId": "HelloWorldFunction"
100100
},
101101
"api": {

src/testFixtures/workspaceFolder/csharp2.1-image-sam-app/omnisharp.json

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/testFixtures/workspaceFolder/csharp2.1-image-sam-app/src/HelloWorld/Dockerfile

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/testFixtures/workspaceFolder/csharp2.1-image-sam-app/src/HelloWorld/HelloWorld.csproj

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/testFixtures/workspaceFolder/csharp2.1-image-sam-app/src/HelloWorld/aws-lambda-tools-defaults.json

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/testFixtures/workspaceFolder/csharp2.1-image-sam-app/template.yaml

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/testFixtures/workspaceFolder/csharp2.1-plain-sam-app/events/event.json

Lines changed: 0 additions & 62 deletions
This file was deleted.

0 commit comments

Comments
 (0)