简体   繁体   English

从Java中的另一个安全组创建AWS安全组入站规则

[英]Create Aws Security Group Inbound Rule from another security group in java

I have multiple AWS Security Groups and i want to create an inbound traffic rule in 1 security group from another security group. 我有多个AWS安全组 ,我想在另一个安全组的1个安全组中创建入站流量规则。 I can do that from the AWS Console but i want to automate it using java api. 我可以从AWS控制台执行此操作,但是我想使用java api将其自动化。 How can i do it? 我该怎么做?

For simple Rules with Cidr Blocks, I have used AuthorizeSecurityGroupIngressRequest , But in this i can't find a way to achieve this. 对于带有Cidr块的简单规则,我使用了AuthorizeSecurityGroupIngressRequest ,但是在这种情况下,我找不到实现此目的的方法。

Sample Code: 样例代码:

AuthorizeSecurityGroupIngressRequest authorizeSecurityGroupIngressRequest =new AuthorizeSecurityGroupIngressRequest();
authorizeSecurityGroupIngressRequest.withFromPort(securityGroupIngressRequestParam.getFromPort());
authorizeSecurityGroupIngressRequest.withIpProtocol(securityGroupIngressRequestParam.getIpProtocols().getName());
authorizeSecurityGroupIngressRequest.withToPort(securityGroupIngressRequestParam.getToPort());
authorizeSecurityGroupIngressRequest.withCidrIp(securityGroupIngressRequestParam.getCidrBlock());
authorizeSecurityGroupIngressRequest.setGroupId(securityGroupIngressRequestParam.getSecurityGroupId());
amazonEc2Client.authorizeSecurityGroupIngress(authorizeSecurityGroupIngressRequest);

After some try and error with Aws Apis, I have found the solution with works for me. 经过与Aws Apis的反复尝试,我找到了适合我的解决方案。

We can use the model IpPermission instead of setting the rule details with provides the api to add sourceSecurityGroupId. 我们可以使用模型IpPermission来代替使用提供用于添加sourceSecurityGroupId的api来设置规则详细信息。

   AuthorizeSecurityGroupIngressRequest authorizeSecurityGroupIngressRequest = new AuthorizeSecurityGroupIngressRequest();

    IpPermission ipPermission = new IpPermission();
    ipPermission.withFromPort(securityGroupIngressRequestParam.getFromPort()).withToPort(securityGroupIngressRequestParam.getToPort())
        .withIpProtocol(securityGroupIngressRequestParam.getIpProtocols().getName());

    if (!StringUtil.isEmpty(securityGroupIngressRequestParam.getCidrBlock())) {
      ipPermission.withIpRanges(securityGroupIngressRequestParam.getCidrBlock());
    } else if (!StringUtil.isEmpty(securityGroupIngressRequestParam.getSourceSecurityGroupId())) {
      UserIdGroupPair userIdGroupPairs = new UserIdGroupPair();
      userIdGroupPairs.setGroupId(securityGroupIngressRequestParam.getSourceSecurityGroupId());
      ipPermission.withUserIdGroupPairs(userIdGroupPairs);
    } else {
      // TODO throw exception
    }

    authorizeSecurityGroupIngressRequest.withIpPermissions(ipPermission);
    authorizeSecurityGroupIngressRequest.setGroupId(securityGroupIngressRequestParam.getSecurityGroupId());

    amazonEc2Client.authorizeSecurityGroupIngress(authorizeSecurityGroupIngressRequest);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用AWS Java SDK列出安全组的规则 - How to list the rules for a security group using AWS java SDK 如何创建安全组以允许使用Java代码中的所有端口 - how to create security group to allow all ports in java code java.lang.NoClassDefFoundError:来自 Keycloak 的 java/security/acl/Group - java.lang.NoClassDefFoundError: java/security/acl/Group from Keycloak 从安全组Azure SDK获取实例 - get instances from security group azure sdk 如何使用 java sdk 自动修改 aws ec2 中的安全组入站规则? - How to automate modify security groups inbound rules in aws ec2 using java sdk? 安全组“ gettingstartedgroup”不存在 - The security group 'gettingstartedgroup' does not exist JackRabbit 服务器引起:java.lang.ClassNotFoundException: java.security.acl.Group on Java 1 - JackRabbit Server Caused by: java.lang.ClassNotFoundException: java.security.acl.Group on Java 17 Java Spring 引导:KeyCloak 服务器:java.lang.NoClassDefFoundError:java/security/acl/Group - Java Spring Boot : KeyCloak Server : java.lang.NoClassDefFoundError: java/security/acl/Group Spring 安全和 Azure:是否有 Active Directory 组通配符? - Spring Security and Azure: is there a Active Directory group wildcard? 使用Spring Security检索记录的用户组 - Retrieving logged user group(s) with Spring Security
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM