-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostgresAuthIp.sh
More file actions
executable file
·52 lines (46 loc) · 1.58 KB
/
Copy pathpostgresAuthIp.sh
File metadata and controls
executable file
·52 lines (46 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/bin/bash
MY_NEW_IP=$(curl -s https://api.ipify.org)
echo "[ip] Current IP: $MY_NEW_IP"
SG_ID=$(aws rds describe-db-instances \
--db-instance-identifier mcq-app-db \
--region eu-west-2 \
--query "DBInstances[0].VpcSecurityGroups[0].VpcSecurityGroupId" \
--output text)
echo "[sg] Security Group ID: $SG_ID"
echo "[sg] Currently authorized IPs:"
aws ec2 describe-security-groups \
--group-ids $SG_ID \
--region eu-west-2 \
--query "SecurityGroups[0].IpPermissions[?FromPort==\`5432\`].IpRanges[].CidrIp" \
--output text | tr '\t' '\n' | while read ip; do
echo " - $ip"
done
EXISTING=$(aws ec2 describe-security-groups \
--group-ids $SG_ID \
--region eu-west-2 \
--query "SecurityGroups[0].IpPermissions[?FromPort==\`5432\`].IpRanges[?CidrIp==\`${MY_NEW_IP}/32\`].CidrIp" \
--output text)
if [ -n "$EXISTING" ]; then
echo "[sg] IP $MY_NEW_IP is already authorized for port 5432"
else
echo "[sg] Adding new IP to authorized list..."
if aws ec2 authorize-security-group-ingress \
--group-id $SG_ID \
--protocol tcp \
--port 5432 \
--cidr ${MY_NEW_IP}/32 \
--region eu-west-2 2>/dev/null; then
echo "[sg] Added $MY_NEW_IP to authorized IPs"
else
echo "[sg] Failed to add IP (it may already exist or there was an error)"
fi
fi
echo ""
echo "[sg] Updated authorized IPs:"
aws ec2 describe-security-groups \
--group-ids $SG_ID \
--region eu-west-2 \
--query "SecurityGroups[0].IpPermissions[?FromPort==\`5432\`].IpRanges[].CidrIp" \
--output text | tr '\t' '\n' | while read ip; do
echo " - $ip"
done