Skip to content

Commit

Permalink
support encoding for boolean values
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardomourar committed Jun 10, 2020
1 parent 1e78a68 commit e38402e
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 20 deletions.
20 changes: 20 additions & 0 deletions packages/@aws-cdk/aws-eks/lib/cluster-resource-handler/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,22 @@ export interface EksUpdateId {

export type ResourceEvent = AWSLambda.CloudFormationCustomResourceEvent & EksUpdateId;

/**
* Decodes encoded true/false values
*/
function decodeBooleans(object: object) {
return JSON.parse(JSON.stringify(object), (_k, v) => {
switch (v) {
case 'TRUE:BOOLEAN':
return true;
case 'FALSE:BOOLEAN':
return false;
default:
return v;
}
});
}

export abstract class ResourceHandler {
protected readonly requestId: string;
protected readonly logicalResourceId: string;
Expand All @@ -33,6 +49,10 @@ export abstract class ResourceHandler {
throw new Error('AssumeRoleArn must be provided');
}

if (event.ResourceProperties.Config) {
this.event.ResourceProperties.Config = decodeBooleans(event.ResourceProperties.Config);
}

eks.configureAssumeRole({
RoleArn: roleToAssume,
RoleSessionName: `AWSCDK.EKSCluster.${this.requestType}.${this.requestId}`,
Expand Down
29 changes: 28 additions & 1 deletion packages/@aws-cdk/core/lib/custom-resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ export interface CustomResourceProps {
* @default false
*/
readonly pascalCaseProperties?: boolean;

/**
* Encode property values from boolean to special strings.
*
* @default false
*/
readonly encodeValues?: boolean;
}

/**
Expand All @@ -110,13 +117,14 @@ export class CustomResource extends Resource {

const type = renderResourceType(props.resourceType);
const pascalCaseProperties = props.pascalCaseProperties ?? false;
const shouldEncode = props.encodeValues ?? false;
const properties = pascalCaseProperties ? uppercaseProperties(props.properties || {}) : (props.properties || {});

this.resource = new CfnResource(this, 'Default', {
type,
properties: {
ServiceToken: props.serviceToken,
...properties,
...(shouldEncode ? encodeValues(properties) : properties),
},
});

Expand Down Expand Up @@ -174,6 +182,25 @@ function uppercaseProperties(props: { [key: string]: any }) {
return ret;
}

/**
* Encodes values (specially boolean) as special strings
*
* Because CloudFormation converts every input as string for custom resources,
* we will use these encoded values to cast them into proper types.
*/
function encodeValues(object: object) {
return JSON.parse(JSON.stringify(object), (_k, v) => {
switch (v) {
case true:
return 'TRUE:BOOLEAN';
case false:
return 'FALSE:BOOLEAN';
default:
return v;
}
});
}

function renderResourceType(resourceType?: string) {
if (!resourceType) {
return 'AWS::CloudFormation::CustomResource';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,10 +328,11 @@ export class AwsCustomResource extends cdk.Construct implements iam.IGrantable {
resourceType: props.resourceType || 'Custom::AWS',
serviceToken: provider.functionArn,
pascalCaseProperties: true,
encodeValues: true,
properties: {
create: create && encodeBooleans(create),
update: props.onUpdate && encodeBooleans(props.onUpdate),
delete: props.onDelete && encodeBooleans(props.onDelete),
create: create,
update: props.onUpdate,
delete: props.onDelete,
},
});
}
Expand Down Expand Up @@ -385,19 +386,3 @@ function awsSdkToIamAction(service: string, action: string): string {
const iamAction = action.charAt(0).toUpperCase() + action.slice(1);
return `${iamService}:${iamAction}`;
}

/**
* Encodes booleans as special strings
*/
function encodeBooleans(object: object) {
return JSON.parse(JSON.stringify(object), (_k, v) => {
switch (v) {
case true:
return 'TRUE:BOOLEAN';
case false:
return 'FALSE:BOOLEAN';
default:
return v;
}
});
}

0 comments on commit e38402e

Please sign in to comment.