Skip to content

Commit 40c7e9a

Browse files
brainstormmmalenic
andauthored
Filemanager objects apigw API deployment (no app side logic) (#115)
* Refactors and promotes api-gw from SRM to top level. * Fixes all workloads to use the new construct instead of custom ones per service. * Adds AccessLog logGroup. * Explores CloudMap at ApiGw level. --------- Co-authored-by: Marko Malenic <mmalenic1@gmail.com>
1 parent 858a2da commit 40c7e9a

File tree

24 files changed

+554
-314
lines changed

24 files changed

+554
-314
lines changed

.gitignore

+6-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ data/
5555
Brewfile.lock.json
5656
*.xml
5757

58-
target/
59-
6058
.coverage
6159
htmlcov/
60+
61+
# Filemanager-specific
62+
.sqlx
63+
target/
64+
lib/workload/stateful/filemanager/volume
65+
skel/rust-api/Cargo.lock

.vscode/settings.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"rust-analyzer.linkedProjects": [
3-
"lib/workload/stateful/filemanager/Cargo.toml",
3+
"lib/workload/stateless/stacks/filemanager/Cargo.toml",
44
"skel/rust-api/Cargo.toml"
55
]
66
}

config/stacks/fileManager.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FilemanagerConfig } from '../../lib/workload/stateless/stacks/filemanager/deploy/lib/filemanager';
1+
import { FilemanagerConfig } from '../../lib/workload/stateless/stacks/filemanager/deploy/stack';
22
import {
33
AccountName,
44
computeSecurityGroupName,
@@ -9,6 +9,9 @@ import {
99
prodBucket,
1010
stgBucket,
1111
vpcProps,
12+
cognitoPortalAppClientIdParameterName,
13+
cognitoStatusPageAppClientIdParameterName,
14+
cognitoUserPoolIdParameterName,
1215
} from '../constants';
1316

1417
export const getFileManagerStackProps = (n: AccountName): FilemanagerConfig => {
@@ -19,6 +22,9 @@ export const getFileManagerStackProps = (n: AccountName): FilemanagerConfig => {
1922
databaseClusterEndpointHostParameter: dbClusterEndpointHostParameterName,
2023
port: databasePort,
2124
migrateDatabase: true,
25+
cognitoPortalAppClientIdParameterName: cognitoPortalAppClientIdParameterName,
26+
cognitoStatusPageAppClientIdParameterName: cognitoStatusPageAppClientIdParameterName,
27+
cognitoUserPoolIdParameterName: cognitoUserPoolIdParameterName,
2228
};
2329

2430
switch (n) {

config/stacks/postgresManager.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Duration } from 'aws-cdk-lib';
2-
import { FILEMANAGER_SERVICE_NAME } from '../../lib/workload/stateless/stacks/filemanager/deploy/lib/filemanager';
2+
import { FILEMANAGER_SERVICE_NAME } from '../../lib/workload/stateless/stacks/filemanager/deploy/stack';
33
import { PostgresManagerStackProps } from '../../lib/workload/stateless/stacks/postgres-manager/deploy/stack';
44
import { DbAuthType } from '../../lib/workload/stateless/stacks/postgres-manager/function/type';
55
import {
+47-8
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
import { Construct } from 'constructs';
22
import { aws_ssm, Duration } from 'aws-cdk-lib';
33
import { HttpJwtAuthorizer } from 'aws-cdk-lib/aws-apigatewayv2-authorizers';
4-
import { CorsHttpMethod, HttpApi } from 'aws-cdk-lib/aws-apigatewayv2';
4+
import { CorsHttpMethod, HttpApi, CfnStage } from 'aws-cdk-lib/aws-apigatewayv2';
55
import { IStringParameter } from 'aws-cdk-lib/aws-ssm';
6+
import { LogGroup } from 'aws-cdk-lib/aws-logs';
7+
import { Role, ServicePrincipal } from 'aws-cdk-lib/aws-iam';
68

7-
export interface SRMApiGatewayConstructProps {
9+
export interface ApiGatewayConstructProps {
810
region: string;
11+
apiName: string | undefined;
912
cognitoUserPoolIdParameterName: string;
1013
cognitoPortalAppClientIdParameterName: string;
1114
cognitoStatusPageAppClientIdParameterName: string;
1215
}
1316

14-
export class SRMApiGatewayConstruct extends Construct {
17+
export class ApiGatewayConstruct extends Construct {
1518
private readonly _httpApi: HttpApi;
1619

17-
constructor(scope: Construct, id: string, props: SRMApiGatewayConstructProps) {
20+
constructor(scope: Construct, id: string, props: ApiGatewayConstructProps) {
1821
super(scope, id);
1922

2023
this._httpApi = new HttpApi(this, 'HttpApi', {
21-
apiName: 'OrcaBusAPI-SequenceRunManager',
24+
apiName: 'OrcaBusAPI-' + props.apiName,
2225
corsPreflight: {
2326
allowHeaders: ['Authorization'],
2427
allowMethods: [
@@ -34,12 +37,48 @@ export class SRMApiGatewayConstruct extends Construct {
3437
// defaultDomainMapping: ... TODO
3538
});
3639

37-
// TODO Configure access logging. See https://github.com/aws/aws-cdk/issues/11100
40+
// LogGroups
41+
this.setupAccessLogs();
3842

39-
// TODO setup cloud map service discovery perhaps
43+
// CloudMap
44+
// this.setupCloudServiceDiscovery()
4045
}
4146

42-
private getAuthorizer(props: SRMApiGatewayConstructProps): HttpJwtAuthorizer {
47+
// TODO: https://github.com/aws-samples/aws-cdk-service-discovery-example/tree/main
48+
// private setupCloudServiceDiscovery() {
49+
// }
50+
51+
// TODO: Taken from https://github.com/aws/aws-cdk/issues/11100#issuecomment-904627081
52+
// Monitor for higher level CDK construct instead of leveraging CfnStage
53+
private setupAccessLogs() {
54+
const accessLogs = new LogGroup(this, 'OrcaBus-ApiGw-AccessLogs');
55+
const stage = this.httpApi.defaultStage?.node.defaultChild as CfnStage;
56+
stage.accessLogSettings = {
57+
destinationArn: accessLogs.logGroupArn,
58+
format: JSON.stringify({
59+
requestId: '$context.requestId',
60+
userAgent: '$context.identity.userAgent',
61+
sourceIp: '$context.identity.sourceIp',
62+
requestTime: '$context.requestTime',
63+
requestTimeEpoch: '$context.requestTimeEpoch',
64+
httpMethod: '$context.httpMethod',
65+
path: '$context.path',
66+
status: '$context.status',
67+
protocol: '$context.protocol',
68+
responseLength: '$context.responseLength',
69+
domainName: '$context.domainName',
70+
}),
71+
};
72+
73+
// Allow writing access logs, managed
74+
const role = new Role(this, 'AmazonAPIGatewayPushToCloudWatchLogs', {
75+
assumedBy: new ServicePrincipal('apigateway.amazonaws.com'),
76+
});
77+
78+
accessLogs.grantWrite(role);
79+
}
80+
81+
private getAuthorizer(props: ApiGatewayConstructProps): HttpJwtAuthorizer {
4382
/**
4483
* FIXME One fine day in future when we have proper Cognito AAI setup.
4584
* For the moment, we leverage Portal and established Cognito infrastructure.

0 commit comments

Comments
 (0)