[英]Parse AWS security group with jq and add new ingress rule
我要解決的問題是搜索AWS SG組並將新規則(例如:10.10.0.0/16)添加到模式為“ CidrIp”的同一個入口規則塊(端口,協議,cidr)中:匹配“ 10.219.0.0/16”。
找到匹配項:-https: //gist.github.com/mfang329/49575d6eb7ddb93e8f926648f9ba06e9
{
"PrefixListIds": [],
"FromPort": -1,
"IpRanges": [{
"CidrIp": "10.219.0.0/16"
},
{
"Description": "testing for vpc transit connectivity",
"CidrIp": "0.0.0.0/0"
}
],
"ToPort": -1,
"IpProtocol": "icmp",
"UserIdGroupPairs": [{
"UserId": "abcde80269151",
"Description": "default SG VPC - peering ",
"GroupId": "sg-33511e41"
}],
"Ipv6Ranges": []
}
更改為-https://gist.github.com/mfang329/b5e892cf2fee2da4b7e67106cd15b3b2
{
"PrefixListIds": [],
"FromPort": -1,
"IpRanges": [{
"CidrIp": "10.219.0.0/16"
},
{
"CidrIp": "10.10.0.0/16"
},
{
"Description": "testing for vpc transit connectivity",
"CidrIp": "0.0.0.0/0"
}
],
"ToPort": -1,
"IpProtocol": "icmp",
"UserIdGroupPairs": [{
"UserId": "abcde80269151",
"Description": "default SG VPC - peering ",
"GroupId": "sg-33511e41"
}],
"Ipv6Ranges": []
}
使用以下命令修改SG,但是如何表達jq來查詢這些信息並將其用作跟隨aws cli的輸入? 我知道JQ中有命令標志,但是我想攻擊這個問題的最簡單的解決方案是什么?
aws ec2 revoke-security-group-ingress \
— group-id sg-33511e41 \
— port -1 \
— protocol icmp \
— cidr 10.10.0.0/16;
完整的安全組JSON格式-https: //gist.github.com/mfang329/a1871731fe82e5255ccc571648ad4886 *
如果您打算創建一個新的CidrIp
規則,當該值為10.219.0.0/16
,請執行以下操作。 請注意,這不會保留數組中元素的順序, 而是將新IP添加到末尾
jq 'if .IpRanges[] | select(.CidrIp | contains("10.219.0.0/16") )
then .IpRanges += [ { "cidrIp" : "10.10.0.0/16" } ]
else . end' ingressJSON
要將輸出存儲在變量中並在以后的aws
命令中使用,請將上面的輸出存儲到shell變量中。 並將其傳遞給帶有正確引號的命令"$awsRule"
awsRule=$(jq 'if .IpRanges[] | select(.CidrIp | contains("10.219.0.0/16") )
then .IpRanges += [ { "cidrIp" : "10.10.0.0/16" } ]
else . end' ingressJSON)
我最終想出的答案是構造一個簡單的SG json格式,然后將其反饋給aws ec2語句。 這種方法花了我一段時間才能弄清楚,但是它非常優雅,我認為這就是我想要的。
# search for the matching pattern and write to the output file
aws ec2 describe-security-groups --region ${i} | \
jq -r --arg e_cidr "${existing_cidr}" \
--arg n_cidr "${new_cidr}" \
'.SecurityGroups[] | .GroupId as $gid | .IpPermissions[] | .FromPort as $port | .IpProtocol as $protocol |
.IpRanges[] | select(.CidrIp == $e_cidr) | .Description as $desc |
[{ GroupId:$gid, IpProtocol:$protocol, FromPort:$port, ToPort:$port, IpRanges:[{CidrIp:$n_cidr, Description:$desc}] }] ' -c \
| tee -a ${regional_file}
這將產生一個類似於此格式的json文件,
[{
"GroupId": "sg-60d78e12",
"IpProtocol": "tcp",
"FromPort": 443,
"ToPort": 443,
"IpRanges": [
{
"CidrIp": "12.179.53.18/32",
"Description": "Chicago Primary"
}
]
}]
然后,我要做的是從json語句中刪除“ GroupId”鍵值,然后饋入aws ec2命令行以使用與原始cidr塊相同的協議和端口添加新的入口規則。 這項工作很好,花了一些時間才得出結論。
while read -r sg_pattern
do
gid=$( echo $sg_pattern | jq '.[]|.GroupId' -r )
# remove the GroupId key to prepare the ip_permissions json
ip_permissions=$( echo $sg_pattern | jq '.[] | [ del(.GroupId) ]' -r )
echo " ---> adding new SG rule for ${ip_permissions}."
aws ec2 authorize-security-group-ingress --group-id $gid --ip-permissions "${ip_permissions}"
done < ${regional_file}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.