Skip to content

Commit

Permalink
Adds a GitHub Action to verify the aws-testing-cdk project. It runs t…
Browse files Browse the repository at this point in the history
…he linter and tests. Adds a unit test for this project. Correct the formatting to pass the linter. (#4263)

Signed-off-by: David Venable <[email protected]>
  • Loading branch information
dlvenable authored Mar 12, 2024
1 parent 20b0dd9 commit aa40256
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 24 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/testing-resources-cdk-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Testing Resources CDK App Check

on:
push:
branches: [ main ]
paths:
- 'testing/aws-testing-cdk/**'
pull_request:
paths:
- 'testing/aws-testing-cdk/**'
workflow_dispatch:

jobs:
verify:
runs-on: ubuntu-latest

defaults:
run:
working-directory: ./testing/aws-testing-cdk
steps:
- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '18'

- name: Checkout Data Prepper
uses: actions/checkout@v2

- name: Install NPM Dependencies
run: npm install
- name: Test
run: npm run test
- name: Lint
run: npm run lint
2 changes: 1 addition & 1 deletion testing/aws-testing-cdk/bin/aws-testing-cdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'source-map-support/register';
import * as cdk from 'aws-cdk-lib';
import {GitHubAccessStack} from '../lib/common/GitHubAccessStack';
import {SecretsManagerStack} from '../lib/aws-secrets-manager/SecretsManagerStack';
import {KmsStack} from "../lib/common/KmsStack";
import {KmsStack} from '../lib/common/KmsStack';

const app = new cdk.App();

Expand Down
22 changes: 11 additions & 11 deletions testing/aws-testing-cdk/lib/common/GitHubAccessStack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ export class GitHubAccessStack extends Stack {
const oidcProviderExists: boolean = scope.node.tryGetContext('gitHubOidcProviderExists');

const gitHubOidcProvider =
oidcProviderExists ?
OpenIdConnectProvider.fromOpenIdConnectProviderArn(this, 'GitHubOidcProvider', `arn:aws:iam::${this.account}:oidc-provider/${GITHUB_TOKEN_URL}`) :
new OpenIdConnectProvider(this, 'GitHubOidcProvider', {
url: `https://${GITHUB_TOKEN_URL}`,
thumbprints: [
'6938fd4d98bab03faadb97b34396831e3780aea1'
],
clientIds: ['sts.amazonaws.com']
});
oidcProviderExists ?
OpenIdConnectProvider.fromOpenIdConnectProviderArn(this, 'GitHubOidcProvider', `arn:aws:iam::${this.account}:oidc-provider/${GITHUB_TOKEN_URL}`) :
new OpenIdConnectProvider(this, 'GitHubOidcProvider', {
url: `https://${GITHUB_TOKEN_URL}`,
thumbprints: [
'6938fd4d98bab03faadb97b34396831e3780aea1'
],
clientIds: ['sts.amazonaws.com']
});

const dataPrepperOrganization: string = scope.node.tryGetContext('dataPrepperOrganization') || DEFAULT_ORGANIZATION;

Expand All @@ -53,8 +53,8 @@ export class GitHubAccessStack extends Stack {
this.gitHubActionsTestingRole = new Role(this, 'GitHubActionsTestingRole', {
roleName: 'GitHubActionsTesting',
assumedBy: new CompositePrincipal(
gitHubPrincipal,
currentAccountPrincipal
gitHubPrincipal,
currentAccountPrincipal
)
});
}
Expand Down
24 changes: 12 additions & 12 deletions testing/aws-testing-cdk/lib/common/KmsStack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,28 @@

import {Stack, StackProps} from 'aws-cdk-lib';
import {Construct} from 'constructs';
import {Role} from "aws-cdk-lib/aws-iam";
import {Key} from "aws-cdk-lib/aws-kms";
import {Role} from 'aws-cdk-lib/aws-iam';
import {Key} from 'aws-cdk-lib/aws-kms';

export interface KmsStackProps extends StackProps {
readonly testingRole: Role;
readonly testingRole: Role;
}

/**
* CDK stack that creates a common KMS key.
*/
export class KmsStack extends Stack {
readonly kmsKey: Key;
readonly kmsKey: Key;

constructor(scope: Construct, id: string, props: KmsStackProps) {
super(scope, id, props);
constructor(scope: Construct, id: string, props: KmsStackProps) {
super(scope, id, props);

this.kmsKey = new Key(this, 'DataPrepperTestingKey', {
alias: 'DataPrepperTesting',
description: 'Shared KMS key for testing any Data Prepper features that use KMS.'
});
this.kmsKey = new Key(this, 'DataPrepperTestingKey', {
alias: 'DataPrepperTesting',
description: 'Shared KMS key for testing any Data Prepper features that use KMS.'
});

this.kmsKey.grantEncryptDecrypt(props.testingRole)
}
this.kmsKey.grantEncryptDecrypt(props.testingRole)
}
}

36 changes: 36 additions & 0 deletions testing/aws-testing-cdk/lib/s3/S3SinkStack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import {Duration, RemovalPolicy, Stack, StackProps} from 'aws-cdk-lib';
import {Construct} from 'constructs';
import {Role} from 'aws-cdk-lib/aws-iam';
import {Bucket} from 'aws-cdk-lib/aws-s3';

export interface S3SinkStackProps extends StackProps {
readonly testingRole: Role;
}

/**
* CDK stack that creates a common KMS key.
*/
export class S3SinkStack extends Stack {
readonly bucket: Bucket;

constructor(scope: Construct, id: string, props: S3SinkStackProps) {
super(scope, id, props);

new Bucket(this, 'MyBucket', {
removalPolicy: RemovalPolicy.DESTROY,
lifecycleRules: [
{
expiration: Duration.days(10)
}
]
});

this.bucket.grantWrite(props.testingRole)
}
}

40 changes: 40 additions & 0 deletions testing/aws-testing-cdk/test/common/GitHubAccessStack.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import {App} from 'aws-cdk-lib';
import {Template} from 'aws-cdk-lib/assertions';
import {GitHubAccessStack} from '../../lib/common/GitHubAccessStack';

test('Creates OpenId Connect provider for GitHub', () => {
const app = new App();

const stackUnderTest = new GitHubAccessStack(app, 'TestStack');

const template = Template.fromStack(stackUnderTest);

template.hasResourceProperties('Custom::AWSCDKOpenIdConnectProvider', {
Url: 'https://token.actions.githubusercontent.com',
ClientIDList: ['sts.amazonaws.com']
});

template.hasResourceProperties('AWS::IAM::Role', {
RoleName: 'GitHubActionsTesting',
});
});

test('Uses existing OpenId Connect provider for GitHub when gitHubOidcProviderExists', () => {
const app = new App();
app.node.setContext('gitHubOidcProviderExists', true)

const stackUnderTest = new GitHubAccessStack(app, 'TestStack');

const template = Template.fromStack(stackUnderTest);

template.resourceCountIs('Custom::AWSCDKOpenIdConnectProvider', 0)

template.hasResourceProperties('AWS::IAM::Role', {
RoleName: 'GitHubActionsTesting',
});
});

0 comments on commit aa40256

Please sign in to comment.