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(aws-ec2): add support for UDP to SecurityGroupRule #835

Merged
merged 5 commits into from
Oct 3, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
87 changes: 86 additions & 1 deletion packages/@aws-cdk/aws-ec2/lib/security-group-rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,92 @@ export class TcpAllPorts implements IPortRange {
}

/**
* All TCP Ports
* A single UDP port
*/
export class UdpPort implements IPortRange {
public readonly canInlineRule = true;

constructor(private readonly port: number) {
}

public toRuleJSON(): any {
return {
ipProtocol: Protocol.Udp,
fromPort: this.port,
toPort: this.port
};
}

public toString() {
return `${this.port}`;
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add the word UDP to this description string?

LIke UDP ${this.port}?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@rix0rrr Sure, while I prefix all the UDP-related toString() to use UDP , should I also do the same for the TCP ones? Just for consistency. We'd achieve uniqueness either way, but thought why should one be treated as a first-class citizen over the other?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also, is it worth it to do something like ${Protocol.Udp} ${this.port} as compared to UDP ${this.port}?

}
}

/**
* A single UDP port that is provided by a resource attribute
*/
export class UdpPortFromAttribute implements IPortRange {
public readonly canInlineRule = false;

constructor(private readonly port: string) {
}

public toRuleJSON(): any {
return {
ipProtocol: Protocol.Udp,
fromPort: this.port,
toPort: this.port
};
}

public toString() {
return '{IndirectPort}';
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@rix0rrr Will this conflict with TcpPortFromAttribute.toString()'s output? If yes, then I'll prefix UDP here as well else, I'll leave it as it is.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes it will.

}
}

/**
* A UDP port range
*/
export class UdpPortRange implements IPortRange {
public readonly canInlineRule = true;

constructor(private readonly startPort: number, private readonly endPort: number) {
}

public toRuleJSON(): any {
return {
ipProtocol: Protocol.Udp,
fromPort: this.startPort,
toPort: this.endPort
};
}

public toString() {
return `${this.startPort}-${this.endPort}`;
ChintanRaval marked this conversation as resolved.
Show resolved Hide resolved
}
}

/**
* All UDP Ports
*/
export class UdpAllPorts implements IPortRange {
public readonly canInlineRule = true;

public toRuleJSON(): any {
return {
ipProtocol: Protocol.Udp,
fromPort: 0,
toPort: 65535
};
}

public toString() {
return 'ALL PORTS';
ChintanRaval marked this conversation as resolved.
Show resolved Hide resolved
}
}

/**
* All Traffic
*/
export class AllConnections implements IPortRange {
public readonly canInlineRule = true;
Expand Down
8 changes: 6 additions & 2 deletions packages/@aws-cdk/aws-ec2/test/test.connections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { expect, haveResource } from '@aws-cdk/assert';
import { Stack } from '@aws-cdk/cdk';
import { Test } from 'nodeunit';
import { AllConnections, AnyIPv4, AnyIPv6, Connections, IConnectable, PrefixList, SecurityGroup, SecurityGroupRef,
TcpAllPorts, TcpPort, TcpPortFromAttribute, TcpPortRange, VpcNetwork } from '../lib';
TcpAllPorts, TcpPort, TcpPortFromAttribute, TcpPortRange, UdpAllPorts, UdpPort, UdpPortFromAttribute, UdpPortRange, VpcNetwork } from '../lib';

export = {
'peering between two security groups does not recursive infinitely'(test: Test) {
Expand Down Expand Up @@ -73,9 +73,13 @@ export = {

const ports = [
new TcpPort(1234),
new TcpPortFromAttribute("port!"),
new TcpPortFromAttribute("tcp-test-port!"),
new TcpAllPorts(),
new TcpPortRange(80, 90),
new UdpPort(2345),
new UdpPortFromAttribute("udp-test-port!"),
new UdpAllPorts(),
new UdpPortRange(85, 95),
new AllConnections()
];

Expand Down