Skip to content

(ec2): addIngressRule and addEgressRule detect unresolved tokens as duplicates #17201

@peterwoodworth

Description

@peterwoodworth

What is the problem?

When passing in a token to the peer property of these functions, the renderPeer() function is called and will return a constant value '{IndirectPeer}'.

function determineRuleScope(
group: SecurityGroupBase,
peer: IPeer,
connection: Port,
fromTo: 'from' | 'to',
remoteRule?: boolean): [SecurityGroupBase, string] {
if (remoteRule && SecurityGroupBase.isSecurityGroup(peer) && differentStacks(group, peer)) {
// Reversed
const reversedFromTo = fromTo === 'from' ? 'to' : 'from';
return [peer, `${group.uniqueId}:${connection} ${reversedFromTo}`];
} else {
// Regular (do old ID escaping to in order to not disturb existing deployments)
return [group, `${fromTo} ${renderPeer(peer)}:${connection}`.replace('/', '_')];
}
}
function renderPeer(peer: IPeer) {
return Token.isUnresolved(peer.uniqueId) ? '{IndirectPeer}' : peer.uniqueId;
}

If the other properties remain constant, calling this multiple times will cause only one rule to be added to the security group due to the duplicate checker seen here

const [scope, id] = determineRuleScope(this, peer, connection, 'from', remoteRule);
// Skip duplicates
if (scope.node.tryFindChild(id) === undefined) {
new CfnSecurityGroupIngress(scope, id, {
groupId: this.securityGroupId,
...peer.toIngressRuleConfig(),
...connection.toRuleJson(),
description,
});
}

Reproduction Steps

Call addIngressRule() or addEgressRule() multiple times on a security group, while only changing the peer prop from one token to another token. Only one rule will be added

What did you expect to happen?

I was trying to add multiple ingress rules to a security group

What actually happened?

I only added one ingress rule to a security group

CDK CLI Version

latest

Framework Version

No response

Node.js Version

16

OS

mac

Language

Typescript

Language Version

No response

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    @aws-cdk/aws-ec2Related to Amazon Elastic Compute CloudbugThis issue is a bug.effort/smallSmall work item – less than a day of effortp1

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions