-
Notifications
You must be signed in to change notification settings - Fork 0
/
geoip_update.sh
executable file
·273 lines (248 loc) · 9.17 KB
/
geoip_update.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#!/bin/sh
## Set environment variables
source ./config.conf
## Check options, set variables for flow control
while getopts ":dac" opt; do
case $opt in
d ) DEBUG=1 ;;
a ) ASN=1 ;;
c ) CITY=1 ;;
esac
done
if [ -n "$DEBUG" ]; then echo "DEBUG is ON, BABY!" && echo; fi
if [ -n "$ASN" ]; then echo "Called to upload new ASN lookup file" && echo; fi
if [ -n "$CITY" ]; then echo "Called to upload new City lookup file" && echo; fi
## Define functions to be called by flow control portion below
function GET_AUTH_TOKEN {
## Create JSON body to send via curl to retrieve auth token
echo "Creating a JSON data object for auth token request"
echo
DATA=$(jq -n \
--arg grant_type grant_type \
--arg type client_credentials \
--arg client_id client_id \
--arg id "$CLIENT_ID" \
--arg client_secret client_secret \
--arg secret "$CLIENT_SECRET" \
--arg audience audience \
--arg api_audience "https://api.cribl.cloud" \
'{($grant_type):$type,($client_id):$id,($client_secret):$secret,($audience):$api_audience}')
if [ $DEBUG ]; then echo "DATA is $DATA" && echo; fi
## Make curl POST for a Bearer token and capture it
echo "Making a curl POST with JSON data object for Bearer toekn"
echo
TOKEN_RESPONSE=$(curl -s --request POST \
--url https://login.cribl.cloud/oauth/token \
--header "content-type: application/json" \
--data "$DATA")
if [ $DEBUG ]; then echo "TOKEN_RESPONSE is " $TOKEN_RESPONSE; fi
## Parse out the actual token from the response
TOKEN=$(echo $TOKEN_RESPONSE | jq -r .access_token)
if [ $DEBUG ]; then echo "TOKEN is " $TOKEN && echo; fi
}
function UPLOAD_ASN_MMDB {
## Make API call to upload new mmdb file and capture temp filename
echo "Uploading new ASN mmbdb file and capturing temp filename"
echo
TEMP_FILENAME=$(curl -s -X PUT \
https://main-$INSTANCE_ID.cribl.cloud/api/v1/m/defaultHybrid/system/lookups?filename=GeoLite2-ASN.mmdb \
-H "Authorization: Bearer $TOKEN" \
-H 'accept: application/json' \
-H "Content-Type: text/csv" \
--data-binary @$ASN_PATH)
if [ $DEBUG ]; then echo "TEMP_FILENAME IS "$TEMP_FILENAME && echo; fi
## Parse out the fileinfo
FILENAME=$(echo $TEMP_FILENAME | jq -r .filename)
if [ $DEBUG ]; then echo "FILENAME IS " $FILENAME && echo; fi
## Create nested JSON body to send via curl PATCH to update lookup file
echo "Creating nested JSON to send via curl PATCH to update lookup file"
echo
INFO=$(jq -n \
--arg filename filename \
--arg mmdb "$FILENAME" \
'{($filename):$mmdb}' | jq -c )
if [ $DEBUG ]; then echo "INFO is " $INFO && echo; fi
PATCH_DATA=$(jq -n \
--arg id id \
--arg file_id 'GeoLite2-ASN.mmdb' \
--arg fileInfo fileInfo \
--argjson info $INFO \
'{($id):$file_id,($fileInfo):$info}' | jq -c )
if [ $DEBUG ]; then echo "PATCH_DATA IS " $PATCH_DATA && echo; fi
## Make curl PATCH to update the existing file with the new temp file
echo "Updating lookup file with curl PATCH"
echo
FILE_UPDATE=$(curl -s -X PATCH \
https://main-$INSTANCE_ID.cribl.cloud/api/v1/m/defaultHybrid/system/lookups/GeoLite2-ASN.mmdb \
-H "Authorization: Bearer $TOKEN" \
-H 'accept: application/json' \
-H "Content-Type: application/json" \
-d "$PATCH_DATA")
if [ $DEBUG ]; then echo "FILE_UPDATE output is " $FILE_UPDATE && echo; fi
## Make a nested JSON body to send via curl PATCH for commit
echo "Creating nested JSON to send for commit"
echo
FILES=$(jq -n \
--arg files files \
--arg file1 'groups/defaultHybrid/data/lookups/GeoLite2-ASN.mmdb' \
--arg file2 'groups/defaultHybrid/data/lookups/GeoLite2-ASN.yml' \
'{($files):[$file1,$file2]}' | jq -c .files)
if [ $DEBUG ]; then echo "FILES IS " $FILES && echo; fi
COMMIT_DATA=$(jq -n \
--arg message message \
--arg whodat 'automation@cribl:commit' \
--arg group group \
--arg groupname defaultHybrid \
--arg files files \
--argjson filelist $FILES \
'{($message):$whodat,($group):$groupname,($files):$filelist}' | jq -c)
if [ $DEBUG ]; then echo "COMMIT_DATA is " $COMMIT_DATA && echo; fi
## Make a commit
echo "Making a commit with curl POST and nested JSON"
echo
COMMIT_RESPONSE=$(curl -s -X POST \
https://main-$INSTANCE_ID.cribl.cloud/api/v1/version/commit \
-H "Authorization: Bearer $TOKEN" \
-H "accept: application/json" \
-H "Content-Type: application/json" \
-d "$COMMIT_DATA")
if [ $DEBUG ]; then echo "COMMIT_RESPONSE IS "$COMMIT_RESPONSE && echo; fi
## Parse a string ID from the response above
COMMIT_ID=$(echo $COMMIT_RESPONSE | jq -r .items[0].commit)
if [ $DEBUG ]; then echo "COMMIT_ID is " $COMMIT_ID; fi
if [ -n "$COMMIT_ID" ]; then
echo "We have a commit ID! Making a commit"
echo "Making json data object"
DEPLOY_DATA=$(jq -n \
--arg version version \
--arg c_id $COMMIT_ID \
'{($version):$c_id}' | jq -c)
if [ $DEBUG ]; then echo "DEPLOY_DATA is " $DEPLOY_DATA && echo; fi
echo "curl PATCH to deply"
curl -s -X PATCH \
https://main-$INSTANCE_ID.cribl.cloud/api/v1/master/groups/defaultHybrid/deploy \
-H "Authorization: Bearer $TOKEN" \
-H "accept: application/json" \
-H "Content-Type: application/json" \
-d "$DEPLOY_DATA"
else
echo "No commit to deploy"
fi
}
function UPLOAD_CITY_MMDB {
## Make API call to upload new mmdb file and capture temp filename
echo "Uploading new mmbdb file and capturing temp filename"
echo
TEMP_FILENAME=$(curl -s -X PUT \
https://main-$INSTANCE_ID.cribl.cloud/api/v1/m/defaultHybrid/system/lookups?filename=GeoLite2-City.mmdb \
-H "Authorization: Bearer $TOKEN" \
-H 'accept: application/json' \
-H "Content-Type: text/csv" \
--data-binary @$CITY_PATH)
if [ $DEBUG ]; then echo "TEMP_FILENAME IS "$TEMP_FILENAME && echo; fi
## Parse out the fileinfo
FILENAME=$(echo $TEMP_FILENAME | jq -r .filename)
if [ $DEBUG ]; then echo "FILENAME IS " $FILENAME && echo; fi
## Create nested JSON body to send via curl PATCH to update lookup file
echo "Creating nested JSON to send via curl PATCH to update lookup file"
echo
INFO=$(jq -n \
--arg filename filename \
--arg mmdb "$FILENAME" \
'{($filename):$mmdb}' | jq -c )
if [ $DEBUG ]; then echo "INFO is " $INFO && echo; fi
PATCH_DATA=$(jq -n \
--arg id id \
--arg file_id 'GeoLite2-City.mmdb' \
--arg fileInfo fileInfo \
--argjson info $INFO \
'{($id):$file_id,($fileInfo):$info}' | jq -c )
if [ $DEBUG ]; then echo "PATCH_DATA IS " $PATCH_DATA && echo; fi
## Make curl PATCH to update the existing file with the new temp file
echo "Updating lookup file with curl PATCH"
echo
FILE_UPDATE=$(curl -s -X PATCH \
https://main-$INSTANCE_ID.cribl.cloud/api/v1/m/defaultHybrid/system/lookups/GeoLite2-City.mmdb \
-H "Authorization: Bearer $TOKEN" \
-H 'accept: application/json' \
-H "Content-Type: application/json" \
-d "$PATCH_DATA")
if [ $DEBUG ]; then echo "FILE_UPDATE output is " $FILE_UPDATE && echo; fi
## Make a nested JSON body to send via curl PATCH for commit
echo "Creating nested JSON to send for commit"
echo
FILES=$(jq -n \
--arg files files \
--arg file1 'groups/defaultHybrid/data/lookups/GeoLite2-City.mmdb' \
--arg file2 'groups/defaultHybrid/data/lookups/GeoLite2-City.yml' \
'{($files):[$file1,$file2]}' | jq -c .files)
if [ $DEBUG ]; then echo "FILES IS " $FILES && echo; fi
COMMIT_DATA=$(jq -n \
--arg message message \
--arg whodat 'automation@cribl:commit' \
--arg group group \
--arg groupname defaultHybrid \
--arg files files \
--argjson filelist $FILES \
'{($message):$whodat,($group):$groupname,($files):$filelist}' | jq -c)
if [ $DEBUG ]; then echo "COMMIT_DATA is " $COMMIT_DATA && echo; fi
## Make a commit
echo "Making a commit with curl POST and nested JSON"
echo
COMMIT_RESPONSE=$(curl -s -X POST \
https://main-$INSTANCE_ID.cribl.cloud/api/v1/version/commit \
-H "Authorization: Bearer $TOKEN" \
-H "accept: application/json" \
-H "Content-Type: application/json" \
-d "$COMMIT_DATA")
if [ $DEBUG ]; then echo "COMMIT_RESPONSE IS "$COMMIT_RESPONSE && echo; fi
## Parse a string ID from the response above
COMMIT_ID=$(echo $COMMIT_RESPONSE | jq -r .items[0].commit)
if [ $DEBUG ]; then echo "COMMIT_ID is " $COMMIT_ID; fi
if [ -n "$COMMIT_ID" ]; then
echo "We have a commit ID! Making a commit"
echo "Making json data object"
DEPLOY_DATA=$(jq -n \
--arg version version \
--arg c_id $COMMIT_ID \
'{($version):$c_id}' | jq -c)
if [ $DEBUG ]; then echo "DEPLOY_DATA is " $DEPLOY_DATA && echo; fi
echo "curl PATCH to deply"
curl -s -X PATCH \
https://main-$INSTANCE_ID.cribl.cloud/api/v1/master/groups/defaultHybrid/deploy \
-H "Authorization: Bearer $TOKEN" \
-H "accept: application/json" \
-H "Content-Type: application/json" \
-d "$DEPLOY_DATA"
else
echo "No commit to deploy"
fi
}
function VERSION_CONTROL_COMMIT {
## Make a commit to the master branch in version control
echo "Making a JSON data objet"
PUSH_DATA=$(jq -n \
--arg message message \
--arg text "Making a commit here" \
'{($message):$text}' | jq -c)
echo $PUSH_DATA
echo "Making a commit to master branch in version control"
echo
VS_COMMIT=$(curl -s -X POST \
https://main-$INSTANCE_ID.cribl.cloud/api/v1/version/commit \
-H "Authorization: Bearer $TOKEN" \
-H "accept: application/json" \
-H "Content-Type: application/json" \
-d "$PUSH_DATA"
)
echo $VS_COMMIT
}
## Flow Control - don't let it all happen at once
GET_AUTH_TOKEN
if [ $ASN ]; then
UPLOAD_ASN_MMDB
fi
if [ $CITY ]; then
UPLOAD_CITY_MMDB
fi
VERSION_CONTROL_COMMIT