-
Notifications
You must be signed in to change notification settings - Fork 574
/
s3-restrict-bucket-policy.sh
executable file
·224 lines (190 loc) · 5.39 KB
/
s3-restrict-bucket-policy.sh
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/env bash
# This script sets an S3 bucket policy to only allow GetObject requests from an IP whitelist file named iplist
# Requires the AWS CLI and jq
# Set Variables
s3bucketname="YOUR-S3-BUCKET-NAME"
# Functions
# Check required commands
function check_command {
type -P $1 &>/dev/null || fail "Unable to find $1, please install it and run this script again."
}
# Fail
function fail(){
tput setaf 1; echo "Failure: $*" && tput sgr0
exit 1
}
# Horizontal Rule
function HorizontalRule(){
echo "============================================================"
}
# Verify AWS CLI Credentials are setup
# http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html
if ! grep -q aws_access_key_id ~/.aws/config; then
if ! grep -q aws_access_key_id ~/.aws/credentials; then
fail "AWS config not found or CLI not installed. Please run \"aws configure\"."
fi
fi
# Check required commands
check_command "aws"
check_command "jq"
# Validate Variables
if [ "$s3bucketname" = "YOUR-S3-BUCKET-NAME" ]; then
read -r -p "Enter S3 Bucket Name: " s3bucketname
fi
if [ -z "$s3bucketname" ]; then
fail "S3 Bucket Name must be set."
fi
# Validates CIDR notation
function validateCIDR {
while read iplist
do
# echo $iplist
if ! echo $iplist | egrep -q '/24$'; then
if ! echo $iplist | egrep -q '/32$'; then
echo $iplist/32 >> iplist3
else echo $iplist >> iplist3
fi
else echo $iplist >> iplist3
fi
done < iplist
mv iplist3 iplist
# Remove any empty lines
while read iplist
do
if echo $iplist | egrep -q '^/32$'; then
echo $iplist | sed -i '/\/32/d' | cat -s >> iplist4
else echo $iplist >> iplist4
fi
done < iplist
mv iplist4 iplist
if grep -qv '/[0-9]' iplist; then
echo "One or more lines contain invalid or missing CIDR notation. Please fix line:"
grep -vn '/[0-9]' iplist
failed
fi
}
# New Validate CIDR notation
function newValidateCIDR(){
while read iplist
do
if ! echo $iplist | egrep -q '^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\/([0-9]|[1-2][0-9]|3[0-2]))$'; then
echo $iplist/32 >> iplist3
else echo $iplist >> iplist3
fi
done < iplist
mv iplist3 iplist
}
# Remove any empty lines
function removeEmptyLines(){
while read iplist
do
if echo $iplist | egrep -q '^/32$'; then
echo $iplist | sed -i '/\/32/d' | cat -s >> iplist4
else echo $iplist >> iplist4
fi
done < iplist
mv iplist4 iplist
if grep -qv '/[0-9]' iplist; then
echo "One or more lines contain invalid or missing CIDR notation. Please fix line:"
grep -vn '/[0-9]' iplist
failed
fi
}
# Cleanup list
function cleanup {
sort -n -t . -k 1,1 -k 2,2 -k 3,3 -k 4,4 iplist | uniq > iplist2
mv iplist2 iplist
# echo "iplist cleanup completed."
}
# Output the list in JSON
function JSONizeiplist {
while read iplist
do
echo \"$iplist\",>> iplistjson2
done < iplist
cat iplistjson2 | sed '$ s/.$//' >> iplistjson3
rm iplistjson2 && mv iplistjson3 iplistjson
iplistjson=$(cat iplistjson)
# echo "$iplistjson"
}
# Create the JSON policy document
function JSONizePolicy {
echo '{"Version":"2012-10-17","Id":"'"$s3bucketname"'","Statement":[{"Sid":"'"$s3bucketname"'","Effect":"Allow","Principal":"*","Action":"s3:GetObject","Resource":"arn:aws:s3:::'"$s3bucketname"'/*","Condition":{"IpAddress":{"aws:SourceIp":['"$iplistjson"']}}}]}' > policy.json
}
# Set the S3 bucket policy
function setS3Policy {
setS3Policy=$(aws s3api put-bucket-policy --bucket $s3bucketname --policy file://policy.json 2>&1)
}
# Validate the new policy
function validateS3Policy {
bucketpolicy=$(aws s3api get-bucket-policy --bucket $s3bucketname --output text | jq '.Statement | .[] | .Condition | .IpAddress | ."aws:SourceIp" | .[]' | cut -d \" -f2 | sort -n -t . -k 1,1 -k 2,2 -k 3,3 -k 4,4)
jsonpolicy=$(cat policy.json | tr -d '\n' | jq '.Statement | .[] | .Condition | .IpAddress | ."aws:SourceIp" | .[]' | cut -d \" -f2)
# echo "$bucketpolicy" > bucketpolicy
# echo "$jsonpolicy" > jsonpolicy
if [ "$bucketpolicy" = "$jsonpolicy" ]; then
HorizontalRule
tput setaf 2; echo S3 Bucket: $s3bucketname Policy Set Successfully! && tput sgr0
tput setaf 2; echo Set Conditional IP Allow List && tput sgr0
HorizontalRule
# rm policy.json
else
fail $(echo "$setS3Policy")
fi
}
# Run functions
function run {
JSONizePolicy
setS3Policy
validateS3Policy
}
# Set S3 bucket name
function setBucketName (){
# Check for environment argument passed into the script
if [ $# -eq 0 ]; then
scriptname=`basename "$0"`
echo "Usage: ./$scriptname environment"
read -rp "S3 Bucket Environment? (dev/staging/prod/all): " s3bucketenv
if [ -z "$s3bucketenv" ]; then
fail "Invalid environment."
fi
if [ $s3bucketenv = "all" ]; then
s3bucketenv=all
else
s3bucketname="$s3bucketname"-"$s3bucketenv"
fi
fi
# Test for variable passed as argument
if [ -z "$1" ]; then
if [ $s3bucketenv = "all" ]; then
s3bucketname="$s3bucketname"-dev
run
s3bucketname="$s3bucketname"-staging
run
s3bucketname="$s3bucketname"-prod
run
else
s3bucketname="$s3bucketname"-"$s3bucketenv"
run
fi
else
if [ $1 = "all" ]; then
s3bucketname="$s3bucketname"-dev
run
s3bucketname="$s3bucketname"-staging
run
s3bucketname="$s3bucketname"-prod
run
else
s3bucketname="$s3bucketname"-"$1"
run
fi
fi
# echo $s3bucketname
}
# validateCIDR
newValidateCIDR
removeEmptyLines
cleanup
JSONizeiplist
setBucketName $1
rm iplistjson