【问题标题】:Updating Existing IPs from a Security Group in AWS using aws cli使用 aws cli 从 AWS 中的安全组更新现有 IP
【发布时间】:2020-10-16 22:05:59
【问题描述】:

我有一个 shell 脚本,它将我的公共 ip 添加到指定的 ec2-security-group。我浏览了一些 AWS 文档,但找不到用于更新现有 IP 地址而不是简单地添加一个的 API。

我经历了以下:

  1. update-security-group-rule-descriptions-ingress
  2. authorize-security-group-ingress

有没有可以用来简单更新安全组中现有IP地址的api?

我正在使用以下 bash 脚本向安全组添加新条目。

#!/bin/bash
curl https://checkip.amazonaws.com > ip.txt
awk '{ print $0 "/32" }' < ip.txt > ipnew.txt
export stuff=$(cat ipnew.txt)
aws ec2 authorize-security-group-ingress --group-name XXXXX --protocol tcp --port 22 --cidr $stuff --profile xxxxx

【问题讨论】:

    标签: amazon-web-services shell aws-cli aws-security-group


    【解决方案1】:

    此脚本将查找标记有密钥 ssh-from-my-ip 和不区分大小写的值 trueyes 的任何安全组。然后,它将撤销端口 22(如果有)的旧入口访问权限,并授权您的新 IP CIDR。它需要 aws cli 和 jq。

    #! /bin/bash
    
    # This script makes it easier to maintain security groups that allow SSH access
    # from a computer with a dynamic IP, such as a computer on a home network or ISP.
    #
    # Using the script will allow you to SSH to an EC2 without having to allow
    # access to the whole world (0.0.0.0/0). If you run this script whenever your IP
    # changes then the security groups in your account specified by your AWS profile
    # will be updated.
    #
    # The script will find any security groups for your current profile that are
    # tagged with a Tag with a Key of "ssh-from-my-ip" and a case insensitive value
    # of "true" or "yes".
    #
    # For each security group found it will revoke any existing tcp ingress on
    # port 22 and authorize ingress on port 22 for your current IP.
    #
    # Dependencies - AWS CLI and jq
    
    
    # need my current ip
    MY_IP=$(curl --silent https://checkip.amazonaws.com)
    echo "Your IP is ${MY_IP}"
    
    # need security group id(s) and existing CIDR for the SG
    pairs=$(aws ec2 describe-security-groups | aws ec2 describe-security-groups | jq -c '.SecurityGroups[]? | select( (.Tags[]? | select(.Key == "ssh-from-my-ip") | .Value | test("true|yes"; "i"))) | if .IpPermissions | length == 0 then {sg: .GroupId, cidr: null } else {sg: .GroupId, cidr: .IpPermissions[].IpRanges[].CidrIp} end')
    
    for p in $pairs
    do
      SG=$(echo "$p" | jq -r '.sg')
      OLD_CIDR=$(echo "$p" | jq -r '.cidr')
    
      echo "Updating security group ${SG}"
      if [[ $OLD_CIDR != 'null' ]]
      then
        echo "Revoking ingress permission for ${OLD_CIDR} in security group ${SG}"
        # remove the existing ingress permission
        aws ec2 revoke-security-group-ingress \
            --group-id "${SG}" \
            --protocol tcp \
            --port 22 \
            --cidr "${OLD_CIDR}"
      fi
    
      # authorize my new IP CIDR
      NEW_CIDR="${MY_IP}"/32
      echo "Authorizing ingress permission for ${NEW_CIDR} in security group ${SG}"
      aws ec2 authorize-security-group-ingress --group-id "${SG}" --ip-permissions '[{"IpProtocol": "tcp", "FromPort": 22, "ToPort": 22, "IpRanges": [{"CidrIp": "'"${NEW_CIDR}"'", "Description": "Rule0"}]}]'
    done
    

    【讨论】:

    • 这个脚本支持 IPv6 吗?
    • 我认为它会起作用,尽管 IPv6 将需要不同的 CIDR 掩码来限制单个地址。 NEW_CIDR="${MY_IP}"/128
    【解决方案2】:

    没有“更新”规则的命令。您将需要添加和删除规则。

    这是我使用的类似脚本:

    IP=`curl -s http://whatismyip.akamai.com/`
    aws ec2 authorize-security-group-ingress --group-name XXX --protocol tcp --port 22 --cidr $IP/32 --output text
    

    但是,这最终会添加太多规则,因此我需要删除现有规则。您可以在添加规则之前自动删除。

    【讨论】:

    • 我们如何确定要删除哪些规则?我们需要知道安全组之前保存的IP才能删除它
    • 我只是为此使用了一个特定的安全组,然后删除了 ALL 规则。一个实例可以有多个安全组。但是,可以在安全组规则上指定描述,这样可以帮助您确定要删除的内容。
    • 谢谢约翰。我已经能够创建一个 shell 脚本,它从安全组中删除所有规则并添加新 IP。
    【解决方案3】:

    我已经能够破解我的方法来完成这项工作。正如约翰建议的那样,我创建了另一个安全组,添加了需要访问的端口并通过 shell 脚本对其进行更新。更新的作用是删除安全组中提到的所有规则并使用所需的 IP 再次添加它们

    源代码已发布在Github

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-18
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 2017-11-05
      • 2021-11-21
      • 2021-02-26
      • 2022-01-03
      相关资源
      最近更新 更多