remote-access 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #
  2. # remote-access - Setup remote access to the name ${1} security group for the ${2} port
  3. #
  4. # usage:
  5. # grantAccess name port
  6. # revokeAccess name port
  7. #
  8. grantAccess() {
  9. local data ip prior sgroup
  10. local GROUP_NAME="$1"
  11. local PORT="$2"
  12. ip=$(curl -s ipecho.net/plain)
  13. data=$(aws ec2 describe-security-groups --filter "Name=group-name,Values=${GROUP_NAME}")
  14. [ $? != 0 ] && exit 1
  15. sgroup=$(echo $data | jq -r ".SecurityGroups[].GroupId")
  16. existing=$(echo $data | jq -r ".SecurityGroups[].IpPermissions[] | select( (.FromPort == ${PORT})).IpRanges[].CidrIp")
  17. if [[ "${existing}" != *"${ip}"* ]] ; then
  18. aws ec2 revoke-security-group-ingress --group-id ${sgroup} --protocol tcp --port ${PORT} --cidr ${ip}/32 >/dev/null 2>&1
  19. aws ec2 authorize-security-group-ingress --group-id ${sgroup} --protocol tcp --port ${PORT} --cidr ${ip}/32
  20. [ $? != 0 ] && exit 1
  21. fi
  22. }
  23. revokeAccess() {
  24. local data ip prior sgroup
  25. local GROUP_NAME="$1"
  26. local PORT="$2"
  27. ip=$(curl -s ipecho.net/plain)
  28. data=$(aws ec2 describe-security-groups --filter "Name=group-name,Values=${GROUP_NAME}")
  29. [ $? != 0 ] && exit 1
  30. sgroup=$(echo $data | jq -r ".SecurityGroups[].GroupId")
  31. existing=$(echo $data | jq -r ".SecurityGroups[].IpPermissions[] | select( (.FromPort == ${PORT})).IpRanges[].CidrIp")
  32. if [[ "${existing}" != *"${ip}"* ]] ; then
  33. aws ec2 revoke-security-group-ingress --group-id ${sgroup} --protocol tcp --port 22 --cidr ${ip}/32
  34. [ $? != 0 ] && exit 1
  35. fi
  36. }