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

Merged
merged 29 commits into from
Nov 6, 2024

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
@Leo10Gama
Copy link
Member

Because of the nature of the encryption key and permission granting changes, I'm adding the security review label to this as well. We'll review the PR internally with security and let you know if there's any extra changes needed.

@Leo10Gama Leo10Gama removed the needs-security-review Related to feature or issues that needs security review label Nov 4, 2024
@mazyu36 mazyu36 force-pushed the synthetics-canary-artifact-config branch from 4aaf084 to f914da9 Compare November 5, 2024 06:35
@mazyu36 mazyu36 force-pushed the synthetics-canary-artifact-config branch from f914da9 to fa72ee2 Compare November 5, 2024 09:07
Copy link
Member

@Leo10Gama Leo10Gama left a comment

Choose a reason for hiding this comment

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

Thanks for addressing the changes! Just a few more comments about some of the recent changes.

packages/aws-cdk-lib/aws-synthetics/lib/canary.ts Outdated Show resolved Hide resolved
Comment on lines 681 to 680
isArtifactS3EncryptionModeDefined &&
props.artifactS3EncryptionMode !== ArtifactsEncryptionMode.KMS &&
isArtifactS3KmsKeyDefined
props.artifactS3EncryptionMode === ArtifactsEncryptionMode.S3_MANAGED &&
props.artifactS3KmsKey
Copy link
Member

Choose a reason for hiding this comment

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

I can see we're no longer checking if these props are tokens. Is it possible for users to provide them as tokens? I don't think it would change much of the logic here, but if they can be tokens, we ought to have unit tests for them.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As I understand it, these are not expected to become tokens. Therefore, the token check is unnecessary, so I removed it.

packages/aws-cdk-lib/aws-synthetics/lib/canary.ts Outdated Show resolved Hide resolved
@aws-cdk-automation aws-cdk-automation removed the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Nov 5, 2024
@mergify mergify bot dismissed Leo10Gama’s stale review November 6, 2024 10:45

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 Nov 6, 2024
Leo10Gama
Leo10Gama previously approved these changes Nov 6, 2024
Copy link
Member

@Leo10Gama Leo10Gama left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for the contribution!

@Leo10Gama
Copy link
Member

@Mergifyio update

Copy link
Contributor

mergify bot commented Nov 6, 2024

update

❌ Mergify doesn't have permission to update

For security reasons, Mergify can't update this pull request. Try updating locally.
GitHub response: refusing to allow a GitHub App to create or update workflow .github/workflows/github-merit-badger.yml without workflows permission

Copy link
Contributor

mergify bot commented Nov 6, 2024

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@aws-cdk-automation aws-cdk-automation removed the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Nov 6, 2024
Copy link
Contributor

mergify bot commented Nov 6, 2024

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@mergify mergify bot dismissed Leo10Gama’s stale review November 6, 2024 19:16

Pull request has been modified.

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

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

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

Copy link
Contributor

mergify bot commented Nov 6, 2024

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@mergify mergify bot merged commit 1f39cb9 into aws:main Nov 6, 2024
14 checks passed
Copy link

github-actions bot commented Nov 6, 2024

Comments on closed issues and PRs are hard for our team to see.
If you need help, please open a new issue that references this one.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 6, 2024
@mazyu36 mazyu36 deleted the synthetics-canary-artifact-config branch November 6, 2024 22:29
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

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