Skip to content

Commit

Permalink
feat(aws-ec2): add support for ICMP protocol's classification Types &…
Browse files Browse the repository at this point in the history
… Codes to SecurityGroupRule (#893)

Add classes to represent ICMP traffic in SecurityGroups.
  • Loading branch information
ChintanRaval authored and rix0rrr committed Oct 11, 2018
1 parent 9eebd05 commit 3de1726
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 2 deletions.
63 changes: 63 additions & 0 deletions packages/@aws-cdk/aws-ec2/lib/security-group-rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,69 @@ export class UdpAllPorts implements IPortRange {
}
}

/**
* A set of matching ICMP Type & Code
*/
export class IcmpTypeAndCode implements IPortRange {
public readonly canInlineRule = true;

constructor(private readonly type: number, private readonly code: number) {
}

public toRuleJSON(): any {
return {
ipProtocol: Protocol.Icmp,
fromPort: this.type,
toPort: this.code
};
}

public toString() {
return `ICMP Type ${this.type} Code ${this.code}`;
}
}

/**
* All ICMP Codes for a given ICMP Type
*/
export class IcmpAllTypeCodes implements IPortRange {
public readonly canInlineRule = true;

constructor(private readonly type: number) {
}

public toRuleJSON(): any {
return {
ipProtocol: Protocol.Icmp,
fromPort: this.type,
toPort: -1
};
}

public toString() {
return `ICMP Type ${this.type}`;
}
}

/**
* All ICMP Types & Codes
*/
export class IcmpAllTypesAndCodes implements IPortRange {
public readonly canInlineRule = true;

public toRuleJSON(): any {
return {
ipProtocol: Protocol.Icmp,
fromPort: -1,
toPort: -1
};
}

public toString() {
return 'ALL ICMP';
}
}

/**
* All Traffic
*/
Expand Down
27 changes: 25 additions & 2 deletions packages/@aws-cdk/aws-ec2/test/test.connections.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,28 @@
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, UdpAllPorts, UdpPort, UdpPortFromAttribute, UdpPortRange, VpcNetwork } from '../lib';
import {
AllConnections,
AnyIPv4,
AnyIPv6,
Connections,
IcmpAllTypeCodes,
IcmpAllTypesAndCodes,
IcmpTypeAndCode,
IConnectable,
PrefixList,
SecurityGroup,
SecurityGroupRef,
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 @@ -80,6 +100,9 @@ export = {
new UdpPortFromAttribute("udp-test-port!"),
new UdpAllPorts(),
new UdpPortRange(85, 95),
new IcmpTypeAndCode(5, 1),
new IcmpAllTypeCodes(8),
new IcmpAllTypesAndCodes(),
new AllConnections()
];

Expand Down

0 comments on commit 3de1726

Please sign in to comment.