internal RevokeSecurityGroupIngressResponse RevokeSecurityGroupIngress(RevokeSecurityGroupIngressRequest request) { var marshaller = new RevokeSecurityGroupIngressRequestMarshaller(); var unmarshaller = RevokeSecurityGroupIngressResponseUnmarshaller.Instance; return Invoke<RevokeSecurityGroupIngressRequest,RevokeSecurityGroupIngressResponse>(request, marshaller, unmarshaller); }
private static void ManageSecurityGroups() { IAmazonEC2 ec2 = new Amazon.EC2.AmazonEC2Client(); var sgResponse = ec2.DescribeSecurityGroups(); string ipRange = "22.22.22.22/0"; List <string> ranges = new List <string>() { ipRange }; var ipPermission = new IpPermission(); ipPermission.IpProtocol = "tcp"; ipPermission.FromPort = 3333; ipPermission.ToPort = 3333; ipPermission.IpRanges = ranges; var ingressRequest = new AuthorizeSecurityGroupIngressRequest(); ingressRequest.IpPermissions.Add(ipPermission); var revokeRequest = new RevokeSecurityGroupIngressRequest(); revokeRequest.IpPermissions.Add(ipPermission); foreach (var sg in sgResponse.SecurityGroups) { try { if (new Random().Next(2) == 1) { ingressRequest.GroupId = sg.GroupId; var ingressResponse = ec2.AuthorizeSecurityGroupIngress(ingressRequest); } else { revokeRequest.GroupId = sg.GroupId; ec2.RevokeSecurityGroupIngress(revokeRequest); } //Console.WriteLine("New RDP rule for: " + ipRange); } catch (AmazonEC2Exception ex) { // Check the ErrorCode to see if the rule already exists. if ("InvalidPermission.Duplicate" == ex.ErrorCode) { //Console.WriteLine("An RDP rule for: {0} already exists.", ipRange); } else { // The exception was thrown for another reason, so re-throw the exception. //throw; } } } }