Skip to content

Commit 45bf540

Browse files
committed
Correct auth header generation, log API access
1 parent c303852 commit 45bf540

File tree

9 files changed

+41
-24
lines changed

9 files changed

+41
-24
lines changed

.github/workflows/cloud.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ defaults:
1515
working-directory: ./cloud
1616

1717
jobs:
18-
code-checks:
18+
lint-format:
1919
runs-on: ubuntu-latest
2020
strategy:
2121
matrix:

cloud/.eslintrc.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module.exports = {
1616
},
1717
plugins: ['@typescript-eslint'],
1818
root: true,
19-
ignorePatterns: ['node_modules', 'cdk.out'],
19+
ignorePatterns: ['node_modules', 'cdk*.out'],
2020
rules: {
2121
eqeqeq: 'error',
2222
'func-style': ['error', 'expression', { allowArrowFunctions: true }],

cloud/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ node_modules
55

66
# CDK asset staging directory
77
.cdk.staging
8-
cdk.out
8+
cdk*.out

cloud/cdk.context.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,13 @@
55
"eu-north-1b",
66
"eu-north-1c"
77
],
8-
"ami:account=992382568770:filters.image-type.0=machine:filters.name.0=fck-nat-al2023-*-x86_64-ebs:filters.state.0=available:owners.0=568608671756:region=eu-north-1": "ami-0acc1d35233e84277"
8+
"ami:account=992382568770:filters.image-type.0=machine:filters.name.0=fck-nat-al2023-*-x86_64-ebs:filters.state.0=available:owners.0=568608671756:region=eu-north-1": "ami-0acc1d35233e84277",
9+
"cloudfront-prefix-list:account=992382568770:region=eu-north-1": "pl-fab65393",
10+
"availability-zones:account=992382568770:region=eu-west-1": [
11+
"eu-west-1a",
12+
"eu-west-1b",
13+
"eu-west-1c"
14+
],
15+
"ami:account=992382568770:filters.image-type.0=machine:filters.name.0=fck-nat-al2023-*-x86_64-ebs:filters.state.0=available:owners.0=568608671756:region=eu-west-1": "ami-085252d6cf1b51097",
16+
"cloudfront-prefix-list:account=992382568770:region=eu-west-1": "pl-4fa04526"
917
}

cloud/lib/api-stack.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,11 @@ export class ApiStack extends Stack {
7070
directory: join(__dirname, '../../backend/'),
7171
});
7272

73-
// TODO Look into IPv6 Routing, so no need for NAT Gateway or Instance!
73+
// Look into IPv6 Routing, so no need for NAT Instance:
7474
// https://www.turbogeek.co.uk/aws-cdk-ipv6-v/
7575
// https://docs.aws.amazon.com/vpc/latest/userguide/egress-only-internet-gateway.html
76+
// NOTE: api.openai.com does not have an IPv6 address yet :(
77+
// https://community.openai.com/t/ipv6-address-for-api-openai-com/339025
7678

7779
// AMI courtesy of fck-nat: https://fck-nat.dev/stable/deploying/#cdk
7880
const natGatewayProvider = NatInstanceProviderV2.instanceV2({
@@ -175,7 +177,11 @@ export class ApiStack extends Stack {
175177
priority: 1,
176178
});
177179
listener.connections.allowDefaultPortFrom(
178-
Peer.prefixList('pl-fab65393'),
180+
Peer.prefixList(
181+
this.node.tryGetContext(
182+
`cloudfront-prefix-list:account=${env.account}:region=${region}`
183+
) as string
184+
),
179185
'Allow incoming traffic only from CloudFront'
180186
);
181187

cloud/lib/auth-stack.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
import { StringParameter } from 'aws-cdk-lib/aws-ssm';
1111
import { CfnOutput, Duration, RemovalPolicy, Stack, StackProps, Tags } from 'aws-cdk-lib/core';
1212
import { Construct } from 'constructs';
13+
import { randomUUID } from 'node:crypto';
1314

1415
import { appName, resourceId, stageName } from './resourceNamingUtils';
1516

@@ -19,7 +20,7 @@ type AuthStackProps = StackProps & {
1920

2021
export class AuthStack extends Stack {
2122
public readonly customAuthHeaderName = 'X-Origin-Verified';
22-
public readonly customAuthHeaderValue = 'todo-generate-uuid-in-pipeline';
23+
public readonly customAuthHeaderValue: string;
2324
public readonly parameterNameUserPoolId: string;
2425
public readonly parameterNameUserPoolClient: string;
2526
public readonly userPoolId: CfnOutput;
@@ -37,6 +38,9 @@ export class AuthStack extends Stack {
3738
throw new Error('Region not defined in stack env, cannot continue!');
3839
}
3940

41+
// Regenerated each time we deploy the stacks:
42+
this.customAuthHeaderValue = randomUUID();
43+
4044
/*
4145
User Pool - including attribute claims and password policy
4246
*/

cloud/lib/lambdas/verifyAuth/index.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type {
66
CognitoVerifyProperties,
77
CognitoJwtVerifierSingleUserPool,
88
} from 'aws-jwt-verify/cognito-verifier';
9+
import type { CognitoAccessTokenPayload } from 'aws-jwt-verify/jwt-model';
910
import type { CloudFrontRequestEvent, CloudFrontResponse } from 'aws-lambda';
1011

1112
// Leave these alone! They are replaced verbatim by esbuild, see ui-stack
@@ -98,9 +99,16 @@ export const handler = async (event: CloudFrontRequestEvent) => {
9899
}
99100

100101
try {
101-
// Maybe change to using cookie for tokens?
102-
const accessToken = request.headers.authorization[0].value;
103-
await verifier.verify(accessToken, {} as CognitoVerifyProperties);
102+
const accessToken = request.headers.authorization[0]?.value;
103+
if (!accessToken) return unauthorizedResponse;
104+
105+
const jwt = (await verifier.verify(accessToken, {
106+
tokenUse: 'access',
107+
} as CognitoVerifyProperties)) as CognitoAccessTokenPayload;
108+
// Maybe insert custom header for username? We could then log that at ALB,
109+
// and generate metrics for user access.
110+
console.log(`Access verified for [${jwt.username}]`);
111+
104112
return request;
105113
} catch (err: unknown) {
106114
console.log('Unable to verify access token', err);

cloud/package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,18 @@
66
},
77
"scripts": {
88
"cdk:synth": "cdk synth -q \"*\"",
9-
"cdk:synth:prod": "cdk synth --context STAGE=prod -q \"*\"",
9+
"cdk:synth:prod": "cdk synth --context STAGE=prod",
1010
"cdk:diff": "cdk diff --app cdk.out",
1111
"cdk:deploy": "cdk deploy --app cdk.out",
1212
"cdk:deploy:all": "cdk deploy --app cdk.out --all",
1313
"cdk:destroy": "cdk destroy --app cdk.out",
1414
"cdk:destroy:all": "cdk destroy --app cdk.out --all",
1515
"cdk:clean": "rimraf cdk.out",
1616
"cdk:test:synth": "cdk synth -o cdk.test.out -a \"npx ts-node --prefer-ts-exts -r dotenv/config bin/application.ts\"",
17-
"cdk:test:deploy": "cdk deploy --app cdk.test.out --all",
18-
"cdk:test:destroy": "cdk destroy --app cdk.test.out --all",
17+
"cdk:test:deploy": "cdk deploy --app cdk.test.out",
18+
"cdk:test:deploy:all": "cdk deploy --app cdk.test.out --all",
19+
"cdk:test:destroy": "cdk destroy --app cdk.test.out",
20+
"cdk:test:destroy:all": "cdk destroy --app cdk.test.out --all",
1921
"cdk:test:clean": "rimraf cdk.test.out",
2022
"codecheck": "concurrently \"npm run lint:check\" \"npm run format:check\"",
2123
"format": "prettier . --write",

frontend/src/models/overlay.ts

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

0 commit comments

Comments
 (0)