Skip to content

Commit

Permalink
feat: add authentication to apigw
Browse files Browse the repository at this point in the history
  • Loading branch information
TheOrangePuff committed Mar 25, 2024
1 parent 0777062 commit 852c3a9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
9 changes: 8 additions & 1 deletion packages/graphql-mesh-server/lib/graphql-mesh-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ export type MeshHostingProps = {
* @default true
*/
enableMaintenanceMode?: boolean;

/**
* Maintenance auth key
* @default true
*/
maintenanceAuthKey?: string;
};

export class MeshHosting extends Construct {
Expand Down Expand Up @@ -214,7 +220,8 @@ export class MeshHosting extends Construct {
new Maintenance(this, "maintenance", {
...props,
vpc: this.vpc,
fargateService: this.service
fargateService: this.service,
authKey: props.maintenanceAuthKey,
});
}

Expand Down
36 changes: 29 additions & 7 deletions packages/graphql-mesh-server/lib/maintenance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ interface MaintenanceProps {
* @default '/mnt/efs0'
*/
mountPath?: string;

/**
* Authentication key for the maintenance API
*
* @default randomly generated key
*/
authKey?: string;
}

export class Maintenance extends Construct {
Expand Down Expand Up @@ -100,6 +107,17 @@ export class Maintenance extends Construct {
);

const api = new apigateway.RestApi(this, "maintenance-apigw");
const apiKey = api.addApiKey("maintenance-api-key", {
value: props.authKey,
});
const usagePlan = api.addUsagePlan("maintenance-usage-plan", {
apiStages: [{ api: api, stage: api.deploymentStage }],
});
usagePlan.addApiKey(apiKey);

const methodOptions: apigateway.MethodOptions = {
apiKeyRequired: true,
};

const maintenance = api.root.addResource("maintenance");
const maintenanceLambda = new NodejsFunction(this, "maintenance-lambda", {
Expand All @@ -121,10 +139,14 @@ export class Maintenance extends Construct {
vpc: props.vpc,
});
const maintenanceInt = new apigateway.LambdaIntegration(maintenanceLambda);
maintenance.addMethod("GET", maintenanceInt);
maintenance.addMethod("POST", maintenanceInt);
maintenance.addResource("enable").addMethod("POST", maintenanceInt);
maintenance.addResource("disable").addMethod("POST", maintenanceInt);
maintenance.addMethod("GET", maintenanceInt, methodOptions);
maintenance.addMethod("POST", maintenanceInt, methodOptions);
maintenance
.addResource("enable")
.addMethod("POST", maintenanceInt, methodOptions);
maintenance
.addResource("disable")
.addMethod("POST", maintenanceInt, methodOptions);

const whitelist = maintenance.addResource("whitelist");
const whitelistLambda = new NodejsFunction(this, "whitelist-lambda", {
Expand All @@ -146,8 +168,8 @@ export class Maintenance extends Construct {
vpc: props.vpc,
});
const whitelistInt = new apigateway.LambdaIntegration(whitelistLambda);
whitelist.addMethod("GET", whitelistInt);
whitelist.addMethod("PUT", whitelistInt);
whitelist.addMethod("PATCH", whitelistInt);
whitelist.addMethod("GET", whitelistInt, methodOptions);
whitelist.addMethod("PUT", whitelistInt, methodOptions);
whitelist.addMethod("PATCH", whitelistInt, methodOptions);
}
}

0 comments on commit 852c3a9

Please sign in to comment.