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

feat(synthetics): add artifactS3Encryption property to the Canary Construct. #30197

Open
wants to merge 15 commits into
base: main
Choose a base branch
from

Conversation

mazyu36
Copy link
Contributor

@mazyu36 mazyu36 commented May 15, 2024

Issue # (if applicable)

Closes #30190.

Reason for this change

To select encryption options.

Description of changes

Add artifactS3Encryption property to the Canary Construct.

Description of how you validated changes

Add unit tests and integ tests.

Checklist


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@aws-cdk-automation aws-cdk-automation requested a review from a team May 15, 2024 00:21
@github-actions github-actions bot added effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p2 repeat-contributor [Pilot] contributed between 3-5 PRs to the CDK labels May 15, 2024
@@ -296,6 +344,31 @@ export class Canary extends cdk.Resource implements ec2.IConnectable {
this._connections = new ec2.Connections({});
}

if (props.runtime.family !== RuntimeFamily.NODEJS && props.artifactS3Encryption) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Artifact encryption functionality is available only for canaries that use Synthetics runtime version syn-nodejs-puppeteer-3.3 or later.
However, versions prior to 3.3 are deprecated and can no longer be configured, which is why I implemented it this way.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense 👍 Can you please add a short comment about it before the check?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I've added a comment.

@aws-cdk-automation aws-cdk-automation added the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label May 15, 2024
Copy link
Contributor

@lpizzinidev lpizzinidev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks 👍 Left some comments for some initial adjustments

*
* @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Synthetics_artifact_encryption.html
*/
readonly artifactS3Encryption?: ArtifactS3Encryption;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
readonly artifactS3Encryption?: ArtifactS3Encryption;
readonly artifactS3EncryptionMode?: ArtifactsEncryptionMode;
/**
* The KMS key used to encrypt the data.
*
* @default - A KMS key is automatically created if `artifactS3EncryptionMode` is set to `SSE_KMS`. Otherwise, no key is generated.
*/
readonly artifactS3EncryptionKey?: kms.IKey;

What about keeping this flat? (guidelines)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. Canary Construct is implemented to flatten other properties as well, so I agree that it would be better to flatten it in this case as you suggested. I will modify it to make it flat.​​​​​​​​​​​​​​​​

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've changed it to flat.

packages/aws-cdk-lib/aws-synthetics/lib/canary.ts Outdated Show resolved Hide resolved
packages/aws-cdk-lib/aws-synthetics/lib/canary.ts Outdated Show resolved Hide resolved
packages/aws-cdk-lib/aws-synthetics/lib/canary.ts Outdated Show resolved Hide resolved
packages/aws-cdk-lib/aws-synthetics/lib/canary.ts Outdated Show resolved Hide resolved
packages/aws-cdk-lib/aws-synthetics/lib/canary.ts Outdated Show resolved Hide resolved
@@ -296,6 +344,31 @@ export class Canary extends cdk.Resource implements ec2.IConnectable {
this._connections = new ec2.Connections({});
}

if (props.runtime.family !== RuntimeFamily.NODEJS && props.artifactS3Encryption) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense 👍 Can you please add a short comment about it before the check?

@aws-cdk-automation aws-cdk-automation removed the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Jun 15, 2024
@@ -296,6 +335,18 @@ export class Canary extends cdk.Resource implements ec2.IConnectable {
this._connections = new ec2.Connections({});
}

if (!cdk.Token.isUnresolved(props.artifactS3EncryptionMode) &&
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I refactored the creation of artifactConfig into createArtifactConfig.
However, since kmsKey needs to be set as a property, I made it so that checking and setting are done here.

Please provide your opinion if there is a better way to do this.​​​​​​​​​​​​​​​​

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the approach, just a couple of adjustments:

  • Why not move all validations inside the method?
  • No need to declare the public readonly encryptionKey?: kms.IKey; variable
  • Watch out for the typo in the function name
    Suggestion:
  private createArtifactConfig(props: CanaryProps): CfnCanary.ArtifactConfigProperty | undefined {
    if (!props.artifactS3EncryptionMode) {
      return undefined;
    }

    const isNodeRuntime = !cdk.Token.isUnresolved(props.runtime) && props.runtime.family === RuntimeFamily.NODEJS;
    const isArtifactS3EncryptionModeDefined = !cdk.Token.isUnresolved(props.artifactS3EncryptionMode) && props.artifactS3EncryptionMode;
    const isArtifactS3KmsKeyDefined = !cdk.Token.isUnresolved(props.artifactS3KmsKey) && props.artifactS3KmsKey;

    if (isArtifactS3EncryptionModeDefined &&
      props.artifactS3EncryptionMode !== ArtifactsEncryptionMode.KMS &&
      isArtifactS3KmsKeyDefined
    ) {
      throw new Error('A customer-managed KMS key was provided, but the encryption mode is not set to SSE-KMS.');
    }

    // Only check runtime family is nodejs because versions prior to syn-nodejs-puppeteer-3.3 are deprecated and can no longer be configured.
    if (!isNodeRuntime && isArtifactS3EncryptionModeDefined) {
      throw new Error(`Artifact encryption is only supported for canaries that use Synthetics runtime version syn-nodejs-puppeteer-3.3 or later, got ${props.runtime.name}.`);
    }

    let encryptionKey: kms.IKey | undefined;
    if (props.artifactS3EncryptionMode === ArtifactsEncryptionMode.KMS) {
      // Kms Key is set or generated for using `createArtifactConfig`
      encryptionKey = props.artifactS3KmsKey ?? new kms.Key(this, 'Key', { description: `Created by ${this.node.path}` });
    }

    encryptionKey?.grantEncryptDecrypt(this.role);

    return {
      s3Encryption: {
        encryptionMode: props.artifactS3EncryptionMode,
        artifactS3KmsKeyArn: encryptionKey?.keyArn,
      },
    };
  }

@mazyu36
Copy link
Contributor Author

mazyu36 commented Jun 16, 2024

@lpizzinidev

Thank you for the review. I incorporated the comments and refactored.

Copy link
Contributor

@lpizzinidev lpizzinidev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks 👍 Left comments for some final adjustments

packages/aws-cdk-lib/aws-synthetics/lib/canary.ts Outdated Show resolved Hide resolved
@@ -296,6 +335,18 @@ export class Canary extends cdk.Resource implements ec2.IConnectable {
this._connections = new ec2.Connections({});
}

if (!cdk.Token.isUnresolved(props.artifactS3EncryptionMode) &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the approach, just a couple of adjustments:

  • Why not move all validations inside the method?
  • No need to declare the public readonly encryptionKey?: kms.IKey; variable
  • Watch out for the typo in the function name
    Suggestion:
  private createArtifactConfig(props: CanaryProps): CfnCanary.ArtifactConfigProperty | undefined {
    if (!props.artifactS3EncryptionMode) {
      return undefined;
    }

    const isNodeRuntime = !cdk.Token.isUnresolved(props.runtime) && props.runtime.family === RuntimeFamily.NODEJS;
    const isArtifactS3EncryptionModeDefined = !cdk.Token.isUnresolved(props.artifactS3EncryptionMode) && props.artifactS3EncryptionMode;
    const isArtifactS3KmsKeyDefined = !cdk.Token.isUnresolved(props.artifactS3KmsKey) && props.artifactS3KmsKey;

    if (isArtifactS3EncryptionModeDefined &&
      props.artifactS3EncryptionMode !== ArtifactsEncryptionMode.KMS &&
      isArtifactS3KmsKeyDefined
    ) {
      throw new Error('A customer-managed KMS key was provided, but the encryption mode is not set to SSE-KMS.');
    }

    // Only check runtime family is nodejs because versions prior to syn-nodejs-puppeteer-3.3 are deprecated and can no longer be configured.
    if (!isNodeRuntime && isArtifactS3EncryptionModeDefined) {
      throw new Error(`Artifact encryption is only supported for canaries that use Synthetics runtime version syn-nodejs-puppeteer-3.3 or later, got ${props.runtime.name}.`);
    }

    let encryptionKey: kms.IKey | undefined;
    if (props.artifactS3EncryptionMode === ArtifactsEncryptionMode.KMS) {
      // Kms Key is set or generated for using `createArtifactConfig`
      encryptionKey = props.artifactS3KmsKey ?? new kms.Key(this, 'Key', { description: `Created by ${this.node.path}` });
    }

    encryptionKey?.grantEncryptDecrypt(this.role);

    return {
      s3Encryption: {
        encryptionMode: props.artifactS3EncryptionMode,
        artifactS3KmsKeyArn: encryptionKey?.keyArn,
      },
    };
  }

@mazyu36
Copy link
Contributor Author

mazyu36 commented Jun 17, 2024

@lpizzinidev
Thank you for the review. I have addressed the comments.
(For some reason, the PR linter is showing an error...)

Copy link
Contributor

@lpizzinidev lpizzinidev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks 👍
Not sure about the failing build. It seems like an issue with automation scripts:

error /home/runner/work/aws-cdk/aws-cdk/node_modules/@lerna/create/node_modules/nx, /home/runner/work/aws-cdk/aws-cdk/node_modules/@nx/devkit/node_modules/nx, /home/runner/work/aws-cdk/aws-cdk/node_modules/lerna/node_modules/nx: Command failed.

packages/aws-cdk-lib/aws-synthetics/lib/canary.ts Outdated Show resolved Hide resolved
packages/aws-cdk-lib/aws-synthetics/lib/canary.ts Outdated Show resolved Hide resolved
packages/aws-cdk-lib/aws-synthetics/lib/canary.ts Outdated Show resolved Hide resolved
@aws-cdk-automation aws-cdk-automation added the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Jun 17, 2024
@mazyu36 mazyu36 force-pushed the synthetics-canary-artifact-config branch from ffa5818 to 4ca916c Compare June 18, 2024 00:43
Copy link
Contributor

@lpizzinidev lpizzinidev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks 👍

Copy link
Contributor

@comcalvi comcalvi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor changes only, looks great!

packages/aws-cdk-lib/aws-synthetics/README.md Outdated Show resolved Hide resolved
return undefined;
}

const isNodeRuntime = !cdk.Token.isUnresolved(props.runtime) && props.runtime.family === RuntimeFamily.NODEJS;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this value ever be a Token?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. This check is redundant.
I've removed it.

packages/aws-cdk-lib/aws-synthetics/test/canary.test.ts Outdated Show resolved Hide resolved
testCases: [stack],
});

app.synth();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need app.synth() at the end of new-style (eg, using IntegTest) integ tests. Old-style tests required it, but new-style ones should not use it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. removed.

@aws-cdk-automation aws-cdk-automation removed the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Jun 20, 2024
@mergify mergify bot dismissed comcalvi’s stale review June 20, 2024 22:26

Pull request has been modified.

@aws-cdk-automation aws-cdk-automation added the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Jun 20, 2024
mazyu36 and others added 3 commits June 21, 2024 08:44
Co-authored-by: Calvin Combs <66279577+comcalvi@users.noreply.github.com>
Co-authored-by: Calvin Combs <66279577+comcalvi@users.noreply.github.com>
@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: 47911d6
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@mazyu36
Copy link
Contributor Author

mazyu36 commented Jun 21, 2024

@comcalvi
Thank you for the review.
I've addressed all your comments.

@mazyu36 mazyu36 requested a review from comcalvi June 21, 2024 00:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p2 pr/needs-maintainer-review This PR needs a review from a Core Team Member repeat-contributor [Pilot] contributed between 3-5 PRs to the CDK
Projects
None yet
Development

Successfully merging this pull request may close these issues.

(synthetics): support SSE-S3 and SSE-KMS to encrypt canary artifacts
4 participants