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

fix(backup): validation for vault name fails when parameters are referred in the name #25943

Merged
merged 6 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"version": "32.0.0",
"files": {
"cf2d89a4eac2c90005b9a1e142f1cac80cf9cd29d1b8f98f002b9f3a1849454c": {
"fe2763e4001ed2b0a9a062ed2067e8fbccd32a00736b2a2ffa50382e10ec7b0d": {
"source": {
"path": "cdk-backup.template.json",
"packaging": "file"
},
"destinations": {
"current_account-current_region": {
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
"objectKey": "cf2d89a4eac2c90005b9a1e142f1cac80cf9cd29d1b8f98f002b9f3a1849454c.json",
"objectKey": "fe2763e4001ed2b0a9a062ed2067e8fbccd32a00736b2a2ffa50382e10ec7b0d.json",
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,27 @@
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete"
},
"ThirdVault3441C01E": {
"Type": "AWS::Backup::BackupVault",
"Properties": {
"BackupVaultName": {
"Fn::Join": [
"",
[
"backupVault-",
{
"Ref": "Env"
}
]
]
},
"LockConfiguration": {
"MinRetentionDays": 5
}
},
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete"
},
"PlanDAF4E53A": {
"Type": "AWS::Backup::BackupPlan",
"Properties": {
Expand Down Expand Up @@ -111,9 +132,6 @@
}
}
],
"RecoveryPointTags": {
"stage": "prod"
},
"RuleName": "PlanRule3",
"TargetBackupVault": {
"Fn::GetAtt": [
Expand Down Expand Up @@ -234,6 +252,11 @@
}
},
"Parameters": {
"Env": {
"Type": "String",
"Default": "test",
"Description": "Env"
},
"BootstrapVersion": {
"Type": "AWS::SSM::Parameter::Value<String>",
"Default": "/cdk-bootstrap/hnb659fds/version",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"validateOnSynth": false,
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}",
"cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}",
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/cf2d89a4eac2c90005b9a1e142f1cac80cf9cd29d1b8f98f002b9f3a1849454c.json",
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/fe2763e4001ed2b0a9a062ed2067e8fbccd32a00736b2a2ffa50382e10ec7b0d.json",
"requiresBootstrapStackVersion": 6,
"bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version",
"additionalDependencies": [
Expand Down Expand Up @@ -57,6 +57,18 @@
"data": "SecondaryVault67665B5E"
}
],
"/cdk-backup/Env": [
{
"type": "aws:cdk:logicalId",
"data": "Env"
}
],
"/cdk-backup/ThirdVault/Resource": [
{
"type": "aws:cdk:logicalId",
"data": "ThirdVault3441C01E"
}
],
"/cdk-backup/Plan/Resource": [
{
"type": "aws:cdk:logicalId",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,51 @@
"version": "0.0.0"
}
},
"Env": {
"id": "Env",
"path": "cdk-backup/Env",
"constructInfo": {
"fqn": "aws-cdk-lib.CfnParameter",
"version": "0.0.0"
}
},
"ThirdVault": {
"id": "ThirdVault",
"path": "cdk-backup/ThirdVault",
"children": {
"Resource": {
"id": "Resource",
"path": "cdk-backup/ThirdVault/Resource",
"attributes": {
"aws:cdk:cloudformation:type": "AWS::Backup::BackupVault",
"aws:cdk:cloudformation:props": {
"backupVaultName": {
"Fn::Join": [
"",
[
"backupVault-",
{
"Ref": "Env"
}
]
]
},
"lockConfiguration": {
"minRetentionDays": 5
}
}
},
"constructInfo": {
"fqn": "aws-cdk-lib.aws_backup.CfnBackupVault",
"version": "0.0.0"
}
}
},
"constructInfo": {
"fqn": "aws-cdk-lib.aws_backup.BackupVault",
"version": "0.0.0"
}
},
"Plan": {
"id": "Plan",
"path": "cdk-backup/Plan",
Expand Down Expand Up @@ -195,10 +240,7 @@
"moveToColdStorageAfterDays": 30
}
}
],
"recoveryPointTags": {
"stage": "prod"
}
]
}
]
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as dynamodb from 'aws-cdk-lib/aws-dynamodb';
import * as efs from 'aws-cdk-lib/aws-efs';
import { App, Duration, RemovalPolicy, Stack, StackProps } from 'aws-cdk-lib';
import { App, Duration, RemovalPolicy, Stack, StackProps, CfnParameter } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as backup from 'aws-cdk-lib/aws-backup';

Expand Down Expand Up @@ -31,6 +31,17 @@ class TestStack extends Stack {
minRetention: Duration.days(5),
},
});

const env = new CfnParameter(this, 'Env', { type: 'String', description: 'Env', default: 'test' });

new backup.BackupVault(this, 'ThirdVault', {
removalPolicy: RemovalPolicy.DESTROY,
backupVaultName: `backupVault-${env.valueAsString}`,
lockConfiguration: {
minRetention: Duration.days(5),
},
});

const plan = backup.BackupPlan.dailyWeeklyMonthly5YearRetention(this, 'Plan', vault);

plan.addSelection('Selection', {
Expand All @@ -46,9 +57,6 @@ class TestStack extends Stack {
moveToColdStorageAfter: Duration.days(30),
deleteAfter: Duration.days(120),
}],
recoveryPointTags: {
stage: 'prod',
},
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this removed from the existing integ test?

Copy link
Contributor Author

@tam0ri tam0ri Aug 2, 2023

Choose a reason for hiding this comment

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

I don't remember the intention at the time, but it might be by my mistake. These lines were introduced few days before I submitted this PR, so I guess that I took some miss operation in my local environment at the time. Anyway, thank you for your nice catching! I'll revert this change and regenerate snapshots.

}));
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/aws-cdk-lib/aws-backup/lib/vault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { CfnBackupVault } from './backup.generated';
import * as iam from '../../aws-iam';
import * as kms from '../../aws-kms';
import * as sns from '../../aws-sns';
import { ArnFormat, Duration, IResource, Lazy, Names, RemovalPolicy, Resource, Stack } from '../../core';
import { ArnFormat, Duration, IResource, Lazy, Names, RemovalPolicy, Resource, Stack, Token } from '../../core';

/**
* A backup vault
Expand Down Expand Up @@ -270,7 +270,7 @@ export class BackupVault extends BackupVaultBase {
constructor(scope: Construct, id: string, props: BackupVaultProps = {}) {
super(scope, id);

if (props.backupVaultName && !/^[a-zA-Z0-9\-_]{2,50}$/.test(props.backupVaultName)) {
if (props.backupVaultName && !Token.isUnresolved(props.backupVaultName) && !/^[a-zA-Z0-9\-_]{2,50}$/.test(props.backupVaultName)) {
throw new Error('Expected vault name to match pattern `^[a-zA-Z0-9\-_]{2,50}$`');
}

Expand Down
17 changes: 16 additions & 1 deletion packages/aws-cdk-lib/aws-backup/test/vault.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Template } from '../../assertions';
import * as iam from '../../aws-iam';
import * as kms from '../../aws-kms';
import * as sns from '../../aws-sns';
import { ArnFormat, Duration, Stack } from '../../core';
import { ArnFormat, Duration, Stack, Fn } from '../../core';
import { BackupVault, BackupVaultEvents } from '../lib';

let stack: Stack;
Expand Down Expand Up @@ -303,6 +303,21 @@ test('import from name', () => {
}));
});

test('specify imported value as vault name', () => {
// WHEN
const vaultName = Fn.importValue('VaultName');
new BackupVault(stack, 'Vault', {
backupVaultName: vaultName,
});

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::Backup::BackupVault', {
BackupVaultName: {
'Fn::ImportValue': 'VaultName',
},
});
});

test('grant action', () => {
// GIVEN
const vaultName = 'myVaultName';
Expand Down