-
Notifications
You must be signed in to change notification settings - Fork 4.4k
feat(cloudfront): add PublicKey and KeyGroup L2 constructs #12743
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
Changes from 4 commits
9cbb524
4a7453b
14edeca
291fbd4
9157f47
6f13357
0f1fae2
ab7505b
688ff8a
8ea4a46
e51c103
ee435ef
080aa48
b6fb3ef
8f4a9f3
7c7e041
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| import { IResource, Lazy, Names, Resource } from '@aws-cdk/core'; | ||
| import { Construct } from 'constructs'; | ||
| import { CfnKeyGroup } from './cloudfront.generated'; | ||
| import { IPublicKey } from './public-key'; | ||
|
|
||
| /** | ||
| * Represents a Key Group | ||
| */ | ||
| export interface IKeyGroup extends IResource { | ||
| /** | ||
| * The ID of the key group. | ||
| * @attribute | ||
| */ | ||
| readonly keyGroupId: string; | ||
| } | ||
|
|
||
| /** | ||
| * Properties for creating a Public Key | ||
| */ | ||
| export interface KeyGroupProps { | ||
| /** | ||
| * A name to identify the key group. | ||
| * @default - generated from the `id` | ||
| */ | ||
| readonly keyGroupName?: string; | ||
|
|
||
| /** | ||
| * A comment to describe the key group. | ||
| * @default - no comment | ||
| */ | ||
| readonly comment?: string; | ||
|
|
||
| /** | ||
| * A list of the identifiers of the public keys in the key group. | ||
robertd marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| */ | ||
| readonly items: IPublicKey[]; | ||
| } | ||
|
|
||
| /** | ||
| * A Key Group configuration | ||
| * | ||
| * @resource AWS::CloudFront::KeyGroup | ||
| */ | ||
| export class KeyGroup extends Resource implements IKeyGroup { | ||
|
|
||
| /** Imports a Key Group from its id. */ | ||
| public static fromKeyGroupId(scope: Construct, id: string, keyGroupId: string): IKeyGroup { | ||
| return new class extends Resource implements IKeyGroup { | ||
| public readonly keyGroupId = keyGroupId; | ||
| }(scope, id); | ||
| } | ||
|
|
||
| public readonly keyGroupId: string; | ||
|
|
||
| constructor(scope: Construct, id: string, props: KeyGroupProps) { | ||
| super(scope, id, { | ||
| physicalName: props.keyGroupName ?? | ||
| Lazy.string({ produce: () => this.generateName() }), | ||
| }); | ||
robertd marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| const resource = new CfnKeyGroup(this, 'Resource', { | ||
| keyGroupConfig: { | ||
| name: this.physicalName, | ||
| comment: props.comment, | ||
| items: this.getKeyIdentifiers(props.items), | ||
robertd marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }, | ||
| }); | ||
| this.keyGroupId = resource.ref; | ||
| } | ||
|
|
||
| private getKeyIdentifiers(items: IPublicKey[]): string[] { | ||
| return items.map(key => key.publicKeyId); | ||
| } | ||
|
|
||
| private generateName(): string { | ||
| const name = Names.uniqueId(this); | ||
| if (name.length > 80) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you find this documented somewhere, or find it via trial & error, or is this just a conservative guess?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My main point was more around the length. If the key group can have a 255-character (or 1024-character) name, should we artificially restrict to 80? I suppose this is fine for a first take;
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should these helper functions (shorteners) be part of Names perhaps? |
||
| return name.substring(0, 40) + name.substring(name.length - 40); | ||
| } | ||
| return name; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| import { IResource, Lazy, Names, Resource } from '@aws-cdk/core'; | ||
| import { Construct } from 'constructs'; | ||
| import { CfnPublicKey } from './cloudfront.generated'; | ||
|
|
||
| /** | ||
| * Represents a Public Key | ||
| */ | ||
| export interface IPublicKey extends IResource { | ||
| /** | ||
| * The ID of the key group. | ||
| * @attribute | ||
| */ | ||
| readonly publicKeyId: string; | ||
| } | ||
|
|
||
| /** | ||
| * Properties for creating a Public Key | ||
| */ | ||
| export interface PublicKeyProps { | ||
| /** | ||
| * A name to identify the public key. | ||
| * @default - generated from the `id` | ||
| */ | ||
| readonly publicKeyName?: string; | ||
|
|
||
| /** | ||
| * A comment to describe the public key. | ||
| * @default - no comment | ||
| */ | ||
| readonly comment?: string; | ||
|
|
||
| /** | ||
| * The public key that you can use with signed URLs and signed cookies, or with field-level encryption. | ||
| * @see https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/PrivateContent.html | ||
| * @see https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/field-level-encryption.html | ||
| */ | ||
| readonly encodedKey: string; | ||
| } | ||
|
|
||
| /** | ||
| * A Public Key Configuration | ||
| * | ||
| * @resource AWS::CloudFront::PublicKey | ||
| */ | ||
| export class PublicKey extends Resource implements IPublicKey { | ||
|
|
||
| /** Imports a Public Key from its id. */ | ||
| public static fromPublicKeyId(scope: Construct, id: string, publicKeyId: string): IPublicKey { | ||
| return new class extends Resource implements IPublicKey { | ||
| public readonly publicKeyId = publicKeyId; | ||
| }(scope, id); | ||
| } | ||
|
|
||
| public readonly publicKeyId: string; | ||
|
|
||
| constructor(scope: Construct, id: string, props: PublicKeyProps) { | ||
| super(scope, id, { | ||
| physicalName: props.publicKeyName ?? | ||
| Lazy.string({ produce: () => this.generateName() }), | ||
| }); | ||
|
|
||
| const resource = new CfnPublicKey(this, 'Resource', { | ||
| publicKeyConfig: { | ||
| name: this.physicalName, | ||
| callerReference: this.node.addr, | ||
| encodedKey: props.encodedKey, | ||
| comment: props.comment, | ||
| }, | ||
| }); | ||
|
|
||
| this.publicKeyId = resource.ref; | ||
| } | ||
|
|
||
| private generateName(): string { | ||
| const name = Names.uniqueId(this); | ||
| if (name.length > 80) { | ||
| return name.substring(0, 40) + name.substring(name.length - 40); | ||
| } | ||
| return name; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| { | ||
| "Resources": { | ||
| "AwesomePublicKeyED3E7F55": { | ||
| "Type": "AWS::CloudFront::PublicKey", | ||
| "Properties": { | ||
| "PublicKeyConfig": { | ||
| "CallerReference": "c88e460888c5762c9c47ac0cdc669370d787fb2d9f", | ||
| "EncodedKey": "-----BEGIN PUBLIC KEY-----\n MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAudf8/iNkQgdvjEdm6xYS\n JAyxd/kGTbJfQNg9YhInb7TSm0dGu0yx8yZ3fnpmxuRPqJIlaVr+fT4YRl71gEYa\n dlhHmnVegyPNjP9dNqZ7zwNqMEPOPnS/NOHbJj1KYKpn1f8pPNycQ5MQCntKGnSj\n 6fc+nbcC0joDvGz80xuy1W4hLV9oC9c3GT26xfZb2jy9MVtA3cppNuTwqrFi3t6e\n 0iGpraxZlT5wewjZLpQkngqYr6s3aucPAZVsGTEYPo4nD5mswmtZOm+tgcOrivtD\n /3sD/qZLQ6c5siqyS8aTraD6y+VXugujfarTU65IeZ6QAUbLMsWuZOIi5Jn8zAwx\n NQIDAQAB\n -----END PUBLIC KEY-----\n ", | ||
| "Name": "awscdkcloudfrontcustomAwesomePublicKey0E83393B" | ||
| } | ||
| } | ||
| }, | ||
| "AwesomeKeyGroup3EF8348B": { | ||
| "Type": "AWS::CloudFront::KeyGroup", | ||
| "Properties": { | ||
| "KeyGroupConfig": { | ||
| "Items": [ | ||
| { | ||
| "Ref": "AwesomePublicKeyED3E7F55" | ||
| } | ||
| ], | ||
| "Name": "awscdkcloudfrontcustomAwesomeKeyGroup73FD4DCA" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import * as cdk from '@aws-cdk/core'; | ||
| import * as cloudfront from '../lib'; | ||
|
|
||
| const app = new cdk.App(); | ||
|
|
||
| const stack = new cdk.Stack(app, 'aws-cdk-cloudfront-custom'); | ||
|
|
||
| new cloudfront.KeyGroup(stack, 'AwesomeKeyGroup', { | ||
| items: [ | ||
| new cloudfront.PublicKey(stack, 'AwesomePublicKey', { | ||
| encodedKey: `-----BEGIN PUBLIC KEY----- | ||
| MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAudf8/iNkQgdvjEdm6xYS | ||
| JAyxd/kGTbJfQNg9YhInb7TSm0dGu0yx8yZ3fnpmxuRPqJIlaVr+fT4YRl71gEYa | ||
| dlhHmnVegyPNjP9dNqZ7zwNqMEPOPnS/NOHbJj1KYKpn1f8pPNycQ5MQCntKGnSj | ||
| 6fc+nbcC0joDvGz80xuy1W4hLV9oC9c3GT26xfZb2jy9MVtA3cppNuTwqrFi3t6e | ||
| 0iGpraxZlT5wewjZLpQkngqYr6s3aucPAZVsGTEYPo4nD5mswmtZOm+tgcOrivtD | ||
| /3sD/qZLQ6c5siqyS8aTraD6y+VXugujfarTU65IeZ6QAUbLMsWuZOIi5Jn8zAwx | ||
| NQIDAQAB | ||
| -----END PUBLIC KEY----- | ||
| `, | ||
| }), | ||
| ], | ||
| }); | ||
|
|
||
| app.synth(); |
Uh oh!
There was an error while loading. Please reload this page.