Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade typescript, format the code and add license header #54

Merged
merged 1 commit into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions infrastructure/canary/nodejs/node_modules/urlMonitor.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 17 additions & 11 deletions infrastructure/lib/constructs/canarySns.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import {SnsMonitors} from "./snsMonitor";
import {SnsMonitorsProps} from "./snsMonitor";
import {Construct} from "constructs";
import {Alarm} from "aws-cdk-lib/aws-cloudwatch";
/**
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

import { SnsMonitors } from "./snsMonitor";
import { SnsMonitorsProps } from "./snsMonitor";
import { Construct } from "constructs";
import { Alarm, ComparisonOperator, TreatMissingData } from "aws-cdk-lib/aws-cloudwatch";
import { Canary } from 'aws-cdk-lib/aws-synthetics';
import * as cloudwatch from "aws-cdk-lib/aws-cloudwatch";
import {Duration} from "aws-cdk-lib";
import { Duration } from "aws-cdk-lib";

interface canarySnsProps extends SnsMonitorsProps {
readonly canaryAlarms: Array<{ alertName: string, canary: Canary }>;
Expand All @@ -15,24 +22,23 @@ export class canarySns extends SnsMonitors {
constructor(scope: Construct, id: string, props: canarySnsProps) {
super(scope, id, props);
this.canaryAlarms = props.canaryAlarms;
this.canaryAlarms.forEach(({ alertName, canary }) =>
{
this.canaryAlarms.forEach(({ alertName, canary }) => {
const alarm = this.canaryFailed(alertName, canary);
this.map[alarm[1]] = alarm[0];
});
this.createTopic();
}

private canaryFailed(alertName: string, canary: Canary): [Alarm, string] {
const alarmObject = new cloudwatch.Alarm(this, `error_alarm_${alertName}`, {
const alarmObject = new Alarm(this, `error_alarm_${alertName}`, {
metric: canary.metricSuccessPercent({
period: Duration.minutes(15)
}),
threshold: 0,
evaluationPeriods: 1,
comparisonOperator: cloudwatch.ComparisonOperator.LESS_THAN_OR_EQUAL_TO_THRESHOLD,
comparisonOperator: ComparisonOperator.LESS_THAN_OR_EQUAL_TO_THRESHOLD,
datapointsToAlarm: 1,
treatMissingData: cloudwatch.TreatMissingData.NOT_BREACHING,
treatMissingData: TreatMissingData.NOT_BREACHING,
alarmDescription: "Detect Canary failure",
alarmName: alertName,
});
Expand Down
21 changes: 15 additions & 6 deletions infrastructure/lib/constructs/lambda.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import {Code, Function, Runtime, Tracing} from 'aws-cdk-lib/aws-lambda';
import * as path from 'path';
import {Duration, Stack} from 'aws-cdk-lib';
import { Construct } from 'constructs';
import {ISecurityGroup, IVpc, SubnetType} from "aws-cdk-lib/aws-ec2";
/**
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

import { Duration } from 'aws-cdk-lib';
import { ISecurityGroup, IVpc, SubnetType } from "aws-cdk-lib/aws-ec2";
import { IRole } from "aws-cdk-lib/aws-iam";
import { Code, Function, Runtime, Tracing } from 'aws-cdk-lib/aws-lambda';
import { Construct } from 'constructs';
import * as path from 'path';

export class OpenSearchLambdaProps {
readonly lambdaNameBase: string;
Expand All @@ -27,7 +35,8 @@ export class OpenSearchLambda extends Construct {
this.lambda = new Function(scope, `${props.lambdaNameBase}Lambda`, {
vpc: props.vpc,
vpcSubnets: props.vpc?.selectSubnets({
subnetType: SubnetType.PRIVATE_WITH_EGRESS}),
subnetType: SubnetType.PRIVATE_WITH_EGRESS
}),
securityGroups: props.securityGroup ? [props.securityGroup] : undefined,
role: props.role ? props.role : undefined,
code: Code.fromAsset(path.join(__dirname, props.lambdaZipPath)),
Expand Down
30 changes: 19 additions & 11 deletions infrastructure/lib/constructs/opensearchCognito.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
/**
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

import { RemovalPolicy } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { Effect, FederatedPrincipal, ManagedPolicy, PolicyStatement, Role, ServicePrincipal } from "aws-cdk-lib/aws-iam";
import * as cognito from "aws-cdk-lib/aws-cognito";
import { CfnIdentityPool, CfnIdentityPoolRoleAttachment, CfnUserPool, CfnUserPoolClient, CfnUserPoolDomain, CfnUserPoolGroup } from 'aws-cdk-lib/aws-cognito';

export interface OpenSearchMetricsCognitoProps {
readonly openSearchDomainArn: string;
}

export class OpenSearchMetricsCognito extends Construct {
public readonly identityPool: cognito.CfnIdentityPool;
public readonly userPool: cognito.CfnUserPool;
public readonly userPoolDomain: cognito.CfnUserPoolDomain;
public readonly identityPool: CfnIdentityPool;
public readonly userPool: CfnUserPool;
public readonly userPoolDomain: CfnUserPoolDomain;
public readonly metricsCognitoAccessRole: Role;
public readonly identityPoolAuthRole: Role;
public readonly identityPoolAdminRole: Role;
Expand All @@ -20,7 +28,7 @@ export class OpenSearchMetricsCognito extends Construct {
super(scope, id);

const userPoolDomainName = "opensearch-health-user-pool"
this.userPool = new cognito.CfnUserPool(this, "OpenSearchHealthUserPool", {
this.userPool = new CfnUserPool(this, "OpenSearchHealthUserPool", {
userPoolName: userPoolDomainName,
adminCreateUserConfig: {
allowAdminCreateUserOnly: true
Expand All @@ -29,26 +37,26 @@ export class OpenSearchMetricsCognito extends Construct {
this.userPool.overrideLogicalId("UserPool");
this.userPool.applyRemovalPolicy(RemovalPolicy.RETAIN);

const adminGroup = new cognito.CfnUserPoolGroup(this, 'OpensearchAdminGroup', {
const adminGroup = new CfnUserPoolGroup(this, 'OpensearchAdminGroup', {
groupName: 'opensearch-admin-group',
userPoolId: this.userPool.ref,
});


const cognitoAppClient = new cognito.CfnUserPoolClient(this, "OpenSearchHealthUserPoolClient", {
const cognitoAppClient = new CfnUserPoolClient(this, "OpenSearchHealthUserPoolClient", {
userPoolId: this.userPool.ref,
clientName: 'Web',
generateSecret: false,
});


this.userPoolDomain = new cognito.CfnUserPoolDomain(this, "OpenSearchHealthUserPoolDomain", {
this.userPoolDomain = new CfnUserPoolDomain(this, "OpenSearchHealthUserPoolDomain", {
userPoolId: this.userPool.ref,
domain: `${userPoolDomainName}`
});

const identityPoolName = "opensearch-health-identity-pool";
this.identityPool = new cognito.CfnIdentityPool(this, "OpenSearchHealthIdentityPool", {
this.identityPool = new CfnIdentityPool(this, "OpenSearchHealthIdentityPool", {
identityPoolName: identityPoolName,
allowUnauthenticatedIdentities: false,
allowClassicFlow: false,
Expand Down Expand Up @@ -107,12 +115,12 @@ export class OpenSearchMetricsCognito extends Construct {
this.identityPoolAdminRole.addToPolicy(
new PolicyStatement({
effect: Effect.ALLOW,
actions: ["es:ESHttp*", ],
actions: ["es:ESHttp*",],
resources: [`${props.openSearchDomainArn}`],
}),
);

new cognito.CfnIdentityPoolRoleAttachment(this, "IdentityPoolRoleAttachment", {
new CfnIdentityPoolRoleAttachment(this, "IdentityPoolRoleAttachment", {
identityPoolId: this.identityPool.ref,
roles: {
authenticated: this.identityPoolAuthRole.roleArn,
Expand Down
30 changes: 19 additions & 11 deletions infrastructure/lib/constructs/opensearchNginxProxyCognito.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,40 @@
/**
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

import { Aspects, CfnOutput, Duration, Tag, Tags } from 'aws-cdk-lib';
import {
AutoScalingGroup,
BlockDeviceVolume,
CfnLaunchConfiguration,
HealthCheck,
UpdatePolicy,
AutoScalingGroup,
CfnLaunchConfiguration
UpdatePolicy
} from 'aws-cdk-lib/aws-autoscaling';
import {
InstanceClass,
InstanceSize,
InstanceType,
MachineImage,
Peer,
Port,
SecurityGroup,
SubnetType,
Vpc,
MachineImage
Vpc
} from 'aws-cdk-lib/aws-ec2';
import { ManagedPolicy, Role, ServicePrincipal } from 'aws-cdk-lib/aws-iam';
import {Aspects, CfnOutput, Duration, Tag, Tags} from 'aws-cdk-lib';
import { Construct } from 'constructs';
import {
ApplicationLoadBalancer, ApplicationProtocol,
ListenerCertificate, Protocol, SslPolicy,
} from "aws-cdk-lib/aws-elasticloadbalancingv2";
import { ManagedPolicy, Role, ServicePrincipal } from 'aws-cdk-lib/aws-iam';
import { ARecord, RecordTarget } from "aws-cdk-lib/aws-route53";
import { LoadBalancerTarget } from "aws-cdk-lib/aws-route53-targets";
import { Construct } from 'constructs';
import Project from "../enums/project";
import {ARecord, RecordTarget} from "aws-cdk-lib/aws-route53";
import {LoadBalancerTarget} from "aws-cdk-lib/aws-route53-targets";
import {OpenSearchHealthRoute53} from "../stacks/route53";
import { OpenSearchHealthRoute53 } from "../stacks/route53";


export interface NginxProps {
Expand Down
26 changes: 17 additions & 9 deletions infrastructure/lib/constructs/snsMonitor.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
/**
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

import { SnsAction } from 'aws-cdk-lib/aws-cloudwatch-actions';
import { Topic } from 'aws-cdk-lib/aws-sns';
import { EmailSubscription, LambdaSubscription } from 'aws-cdk-lib/aws-sns-subscriptions';
import { Construct } from 'constructs';
import * as sns from "aws-cdk-lib/aws-sns";
import * as subscriptions from "aws-cdk-lib/aws-sns-subscriptions";
import * as actions from "aws-cdk-lib/aws-cloudwatch-actions";
import {OpenSearchLambda} from "./lambda";
import Project from '../enums/project';
import { OpenSearchLambda } from "./lambda";


export interface SnsMonitorsProps {
Expand Down Expand Up @@ -42,24 +50,24 @@ export class SnsMonitors extends Construct {

}

protected createTopic(){
protected createTopic() {
// Create SNS topic for alarms to be sent to
const snsTopic = new sns.Topic(this, `OpenSearchMetrics-Alarm-${this.snsTopicName}`, {
const snsTopic = new Topic(this, `OpenSearchMetrics-Alarm-${this.snsTopicName}`, {
displayName: `OpenSearchMetrics-Alarm-${this.snsTopicName}`
});

// Iterate map to create SNS topic and add alarms on it
Object.keys(this.map).map(key => {
// Connect the alarm to the SNS
this.map[key].addAlarmAction(new actions.SnsAction(snsTopic));
this.map[key].addAlarmAction(new SnsAction(snsTopic));
})

// Send email notification to the recipients
for (const email of this.emailList) {
snsTopic.addSubscription(new subscriptions.EmailSubscription(email));
snsTopic.addSubscription(new EmailSubscription(email));
}

// Send slack notification
snsTopic.addSubscription(new subscriptions.LambdaSubscription(this.slackLambda.lambda));
snsTopic.addSubscription(new LambdaSubscription(this.slackLambda.lambda));
}
}
29 changes: 17 additions & 12 deletions infrastructure/lib/constructs/stepFunctionSns.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import {SnsMonitors} from "./snsMonitor";
import {SnsMonitorsProps} from "./snsMonitor";
import {Construct} from "constructs";
import {Alarm} from "aws-cdk-lib/aws-cloudwatch";
import * as cloudwatch from "aws-cdk-lib/aws-cloudwatch";
/**
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

import { Alarm, ComparisonOperator, Metric, TreatMissingData } from "aws-cdk-lib/aws-cloudwatch";
import { Construct } from "constructs";
import { SnsMonitors, SnsMonitorsProps } from "./snsMonitor";

interface stepFunctionSnsProps extends SnsMonitorsProps {
readonly stepFunctionSnsAlarms: Array<{ alertName: string, stateMachineName: string }>;
Expand All @@ -13,18 +19,17 @@ export class StepFunctionSns extends SnsMonitors {
constructor(scope: Construct, id: string, props: stepFunctionSnsProps) {
super(scope, id, props);
this.stepFunctionSnsAlarms = props.stepFunctionSnsAlarms;
this.stepFunctionSnsAlarms.forEach(({ alertName, stateMachineName }) =>
{
this.stepFunctionSnsAlarms.forEach(({ alertName, stateMachineName }) => {
const alarm = this.stepFunctionExecutionsFailed(alertName, stateMachineName);
this.map[alarm[1]] = alarm[0];
});
this.createTopic();
}

private stepFunctionExecutionsFailed(alertName: string, stateMachineName: string): [Alarm, string] {
const alarmObject = new cloudwatch.Alarm(this, `error_alarm_${alertName}`, {
metric: new cloudwatch.Metric({
namespace: this.alarmNameSpace,
const alarmObject = new Alarm(this, `error_alarm_${alertName}`, {
metric: new Metric({
namespace: this.alarmNameSpace,
metricName: "ExecutionsFailed",
statistic: "Sum",
dimensionsMap: {
Expand All @@ -33,9 +38,9 @@ export class StepFunctionSns extends SnsMonitors {
}),
threshold: 1,
evaluationPeriods: 1,
comparisonOperator: cloudwatch.ComparisonOperator.GREATER_THAN_OR_EQUAL_TO_THRESHOLD,
comparisonOperator: ComparisonOperator.GREATER_THAN_OR_EQUAL_TO_THRESHOLD,
datapointsToAlarm: 1,
treatMissingData: cloudwatch.TreatMissingData.NOT_BREACHING,
treatMissingData: TreatMissingData.NOT_BREACHING,
alarmDescription: "Detect SF execution failure",
alarmName: alertName,
});
Expand Down
10 changes: 9 additions & 1 deletion infrastructure/lib/enums/project.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
enum Project{
/**
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

enum Project {
AWS_ACCOUNT = '',
JENKINS_MASTER_ROLE = '',
JENKINS_AGENT_ROLE = '',
Expand Down
Loading
Loading