Skip to content

Commit 514a82a

Browse files
committed
try resolve circular dependency
1 parent ea00d73 commit 514a82a

File tree

3 files changed

+79
-113
lines changed

3 files changed

+79
-113
lines changed

infrastructure/terraform/components/app/module_kms.tf

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,15 @@ data "aws_iam_policy_document" "kms" {
6767
]
6868

6969
condition {
70-
test = "StringEquals"
71-
variable = "AWS:SourceArn"
70+
test = "StringLike"
71+
variable = "aws:SourceArn"
72+
values = ["arn:aws:cloudfront::${var.aws_account_id}:distribution/*"]
73+
}
7274

73-
values = [
74-
aws_cloudfront_distribution.main.arn,
75-
]
75+
condition {
76+
test = "StringEquals"
77+
variable = "aws:ResourceTag/Environment"
78+
values = [var.environment]
7679
}
7780
}
7881
}
Lines changed: 71 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
1-
import type { APIGatewayRequestAuthorizerEvent, Context } from 'aws-lambda';
1+
import type { CloudFrontRequestEvent } from 'aws-lambda';
22
import { mock } from 'jest-mock-extended';
33
import { logger } from 'nhs-notify-web-template-management-utils/logger';
44
import { handler } from '../index';
55
import { LambdaCognitoAuthorizer } from 'nhs-notify-web-template-management-utils/lambda-cognito-authorizer';
66
import { CognitoIdentityProviderClient } from '@aws-sdk/client-cognito-identity-provider';
77

8-
const requestContext = {
9-
accountId: '000000000000',
10-
apiId: 'api-id',
11-
stage: 'stage',
12-
};
13-
14-
const methodArn = 'arn:aws:execute-api:eu-west-2:000000000000:api-id/stage/*';
15-
168
jest.mock('nhs-notify-web-template-management-utils/logger');
179
const mockLogger = jest.mocked(logger);
1810

@@ -29,66 +21,41 @@ jest
2921
.mocked(CognitoIdentityProviderClient)
3022
.mockImplementation(() => cognitoClientMock);
3123

32-
const allowPolicy = {
33-
principalId: 'api-caller',
34-
policyDocument: {
35-
Version: '2012-10-17',
36-
Statement: [
37-
{
38-
Action: 'execute-api:Invoke',
39-
Effect: 'Allow',
40-
Resource: methodArn,
41-
},
42-
],
43-
},
44-
context: {
45-
user: 'sub',
46-
},
47-
};
48-
49-
const denyPolicy = {
50-
principalId: 'api-caller',
51-
policyDocument: {
52-
Version: '2012-10-17',
53-
Statement: [
54-
{
55-
Action: 'execute-api:Invoke',
56-
Effect: 'Deny',
57-
Resource: methodArn,
58-
},
59-
],
60-
},
61-
};
62-
63-
const originalEnv = { ...process.env };
64-
6524
beforeEach(() => {
6625
jest.clearAllMocks();
67-
process.env.USER_POOL_ID = 'user-pool-id';
68-
process.env.USER_POOL_CLIENT_ID = 'user-pool-client-id';
6926
});
7027

71-
afterEach(() => {
72-
process.env = originalEnv;
73-
});
28+
describe('download authorizer handler', () => {});
7429

75-
test('returns Allow policy on valid token', async () => {
30+
test('returns request, when request is valid', async () => {
7631
lambdaCognitoAuthorizer.authorize.mockResolvedValue({
7732
success: true,
7833
subject: 'sub',
7934
});
8035

8136
const res = await handler(
82-
mock<APIGatewayRequestAuthorizerEvent>({
83-
requestContext,
84-
headers: { Authorization: 'jwt' },
85-
type: 'REQUEST',
86-
}),
87-
mock<Context>(),
88-
jest.fn()
37+
mock<CloudFrontRequestEvent>({
38+
Records: [
39+
{
40+
cf: {
41+
request: {
42+
uri: '',
43+
headers: {
44+
cookies: [{ value: '' }],
45+
},
46+
origin: {
47+
s3: {
48+
customHeaders: {},
49+
},
50+
},
51+
},
52+
},
53+
},
54+
],
55+
})
8956
);
9057

91-
expect(res).toEqual(allowPolicy);
58+
expect(res).toEqual({});
9259
expect(mockLogger.warn).not.toHaveBeenCalled();
9360
expect(mockLogger.error).not.toHaveBeenCalled();
9461

@@ -99,51 +66,51 @@ test('returns Allow policy on valid token', async () => {
9966
);
10067
});
10168

102-
test('returns Deny policy on lambda misconfiguration', async () => {
103-
process.env.USER_POOL_ID = '';
104-
105-
const res = await handler(
106-
mock<APIGatewayRequestAuthorizerEvent>({
107-
requestContext,
108-
headers: { Authorization: '123' },
109-
type: 'REQUEST',
110-
}),
111-
mock<Context>(),
112-
jest.fn()
113-
);
114-
115-
expect(res).toEqual(denyPolicy);
116-
expect(mockLogger.error).toHaveBeenCalledWith('Lambda misconfiguration');
117-
});
118-
119-
test('returns Deny policy if no Authorization token in header', async () => {
120-
const res = await handler(
121-
mock<APIGatewayRequestAuthorizerEvent>({
122-
requestContext,
123-
headers: { Authorization: undefined },
124-
type: 'REQUEST',
125-
}),
126-
mock<Context>(),
127-
jest.fn()
128-
);
129-
130-
expect(res).toEqual(denyPolicy);
131-
});
132-
133-
test('returns Deny policy when authorization fails', async () => {
134-
lambdaCognitoAuthorizer.authorize.mockResolvedValue({
135-
success: false,
136-
});
137-
138-
const res = await handler(
139-
mock<APIGatewayRequestAuthorizerEvent>({
140-
requestContext,
141-
headers: { Authorization: 'jwt' },
142-
type: 'REQUEST',
143-
}),
144-
mock<Context>(),
145-
jest.fn()
146-
);
147-
148-
expect(res).toEqual(denyPolicy);
149-
});
69+
// test('returns Deny policy on lambda misconfiguration', async () => {
70+
// process.env.USER_POOL_ID = '';
71+
72+
// const res = await handler(
73+
// mock<APIGatewayRequestAuthorizerEvent>({
74+
// requestContext,
75+
// headers: { Authorization: '123' },
76+
// type: 'REQUEST',
77+
// }),
78+
// mock<Context>(),
79+
// jest.fn()
80+
// );
81+
82+
// expect(res).toEqual(denyPolicy);
83+
// expect(mockLogger.error).toHaveBeenCalledWith('Lambda misconfiguration');
84+
// });
85+
86+
// test('returns Deny policy if no Authorization token in header', async () => {
87+
// const res = await handler(
88+
// mock<APIGatewayRequestAuthorizerEvent>({
89+
// requestContext,
90+
// headers: { Authorization: undefined },
91+
// type: 'REQUEST',
92+
// }),
93+
// mock<Context>(),
94+
// jest.fn()
95+
// );
96+
97+
// expect(res).toEqual(denyPolicy);
98+
// });
99+
100+
// test('returns Deny policy when authorization fails', async () => {
101+
// lambdaCognitoAuthorizer.authorize.mockResolvedValue({
102+
// success: false,
103+
// });
104+
105+
// const res = await handler(
106+
// mock<APIGatewayRequestAuthorizerEvent>({
107+
// requestContext,
108+
// headers: { Authorization: 'jwt' },
109+
// type: 'REQUEST',
110+
// }),
111+
// mock<Context>(),
112+
// jest.fn()
113+
// );
114+
115+
// expect(res).toEqual(denyPolicy);
116+
// });

utils/test-helper-utils/src/aws-events.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,3 @@ export const makeGuardDutyMalwareScanResultNotificationEvent = (
112112
'detail-type': 'GuardDuty Malware Protection Object Scan Result',
113113
detail: makeGuardDutyMalwareScanResultNotificationEventDetail(event.detail),
114114
});
115-
116-
// type MakeCloudFrontRequestParams = Partial<SQSRecord> & Pick<SQSRecord, 'body'>;
117-
118-
// export const makeCloudFrontRequest = ()

0 commit comments

Comments
 (0)