use of software.amazon.awssdk.services.ec2.model.IpPermission in project photon-model by vmware.
the class AWSSecurityGroupClient method createRule.
private IpPermission createRule(int fromPort, int toPort, String subnet, String protocol) {
IpRange ipRange = new IpRange().withCidrIp(subnet);
protocol = protocol.equals(ALL_TRAFFIC) ? ALL_PROTOCOLS : protocol;
return new IpPermission().withIpProtocol(protocol).withFromPort(fromPort).withToPort(toPort).withIpv4Ranges(ipRange);
}
use of software.amazon.awssdk.services.ec2.model.IpPermission in project photon-model by vmware.
the class AWSSecurityGroupClient method buildRules.
/**
* Builds the white list rules for the firewall
*/
public List<IpPermission> buildRules(List<Rule> allowRules) {
ArrayList<IpPermission> awsRules = new ArrayList<>();
for (Rule rule : allowRules) {
int fromPort;
int toPort;
if (rule.ports.contains("-")) {
String[] ports = rule.ports.split("-");
fromPort = Integer.parseInt(ports[0]);
toPort = Integer.parseInt(ports[1]);
} else {
fromPort = Integer.parseInt(rule.ports);
toPort = fromPort;
}
awsRules.add(createRule(fromPort, toPort, rule.ipRangeCidr, rule.protocol));
}
return awsRules;
}
use of software.amazon.awssdk.services.ec2.model.IpPermission in project photon-model by vmware.
the class TestProvisionAWSSecurityGroup method isInternalRule.
private boolean isInternalRule(String sgId, List<IpPermission> ipPermissions) {
boolean isInternalRule = false;
assertNotNull(ipPermissions);
for (IpPermission ipPermission : ipPermissions) {
if (ipPermission.getUserIdGroupPairs() != null) {
assertEquals(1, ipPermission.getUserIdGroupPairs().size());
assertEquals(sgId, ipPermission.getUserIdGroupPairs().get(0).getGroupId());
isInternalRule = true;
break;
}
}
return isInternalRule;
}
use of software.amazon.awssdk.services.ec2.model.IpPermission in project photon-model by vmware.
the class TestAWSSecurityGroupService method testUpdateIngressRules.
/*
* Test updating ingress rules with the Security Group Service Allow
* object
*/
@Test
public void testUpdateIngressRules() throws Throwable {
String groupID = this.client.createDefaultSecurityGroup(null);
ArrayList<Rule> rules = TestUtils.getAllowIngressRules();
this.client.addIngressRules(groupID, this.client.buildRules(rules));
SecurityGroup awsSG = this.client.getSecurityGroupById(groupID);
List<IpPermission> ingress = awsSG.getIpPermissions();
for (IpPermission rule : ingress) {
assertDefaultRules(rule);
}
this.client.deleteSecurityGroup(groupID);
}
use of software.amazon.awssdk.services.ec2.model.IpPermission in project photon-model by vmware.
the class TestAWSSecurityGroupService method validateDefaultRules.
private void validateDefaultRules(List<IpPermission> rules) throws Throwable {
ArrayList<Integer> ports = new ArrayList<>();
for (int port : DEFAULT_ALLOWED_PORTS) {
ports.add(port);
}
for (IpPermission rule : rules) {
assertTrue(rule.getIpProtocol().equalsIgnoreCase(DEFAULT_PROTOCOL));
if (rule.getFromPort() == 1) {
assertTrue(rule.getIpv4Ranges().get(0).getCidrIp().equalsIgnoreCase(this.subnet));
assertTrue(rule.getToPort() == 65535);
} else {
assertTrue(rule.getIpv4Ranges().get(0).getCidrIp().equalsIgnoreCase(DEFAULT_ALLOWED_NETWORK));
assertEquals(rule.getFromPort(), rule.getToPort());
assertTrue(ports.contains(rule.getToPort()));
}
}
}
Aggregations