-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgit-secure-fetch
executable file
·388 lines (327 loc) · 8.58 KB
/
git-secure-fetch
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#!/bin/bash
# Update this variable value with your GPG Key
GPG_KEY=`git config user.signingkey`
GPG_KEY_EXISTS=$(gpg2 --list-key $GPG_KEY)
if [[ $? -ne 0 ]]
then
echo "GPG Key $GPG_KEY is unavailable in the current GPG keychain"
exit 4
fi
#************** Functions ***********************************
function print_usage {
echo "Usage: git secure-fetch [branch]"
echo ""
echo "Fetches the current state of a branch and validates the RSL leading"
echo "up to that point."
echo ""
echo "NOTE: Only fetches from the 'origin' remote."
echo "origin: `git remote get-url origin`"
}
#This function creates the rsl entries related to the fetch operation
function update_rsl_fetch {
LAST_ENTRY=$(find_last_entry_number)
RSL=$(($LAST_ENTRY+1))
dd if=/dev/urandom bs=16 count=1 of=$RSL
# Commit the new RSL
git add $RSL
echo "Committing the fetch entry to the RSL branch."
echo "<---- GPG signature required."
git commit -S -m "Adding the fetch entry at RSL for $CURRENT_BRANCH"
}
#This function pushes the rsl branch to server
function push_rsl {
git push --set-upstream origin rsl 2>/dev/null
#Checking the status of RSL Push
status=$?
}
#This function initializes the rsl branch and creates rsl file initial entries
function rsl_init {
#Set init variable
init=1
git checkout -q --orphan rsl
git rm -qrf .
dd if=/dev/urandom bs=16 count=1 of=1
# Commit the new RSL
git add 1
echo "Committing inital entries to RSL branch."
echo "<---- GPG signature required."
git commit -S -qm "Commiting the changes at RSL"
}
function find_last_entry_number {
# the number of files in the RSL branch
echo $(find . -maxdepth 1 -type f | grep -v 'rsl.tmp' | wc -l)
}
# This function fetches the rsl and related branch
function rsl_fetch {
git fetch -q origin rsl
git checkout -q rsl
RSL=$(find_last_entry_number)
#finding the last verified push entry
while [ -f $RSL ]
do
if grep -q HEAD $RSL
then
#Update Last RSL push entry
LAST_VERIFIED_PUSH_ENTRY=$RSL
break
else
RSL=$(($RSL-1))
fi
done
git merge -q
#Fetch the current branch from server
git fetch -q --no-tags origin $CURRENT_BRANCH 2>/dev/null
RSL=$(find_last_entry_number)
#Finding out the last Push entry for the current branch
while [ -f $RSL ]
do
if grep -q "^Branch:$CURRENT_BRANCH$" $RSL
then
echo "RSL entry $RSL contains the latest push entry for branch $CURRENT_BRANCH"
#Get the branch head from the rsl
RSL_BRANCH_HEAD=`cat $RSL |grep HEAD |tail -1|cut -d':' -f2`
break
else
RSL=$(($RSL-1))
fi
done
#Get the FETCH_HEAD
FETCH_HEAD=`cat .git/FETCH_HEAD |grep $CURRENT_BRANCH |cut -f1`
#Verify the branch head in RSL matches with the FETCH_HEAD
if [ -z $RSL_BRANCH_HEAD ]
then
echo "No prior push entry in the rsl for branch $CURRENT_BRANCH"
fetch_status=0
elif [ "$RSL_BRANCH_HEAD" != "$FETCH_HEAD" ]
then
echo "ERROR: We fetched a commit that doesn't match the latest push entry"
echo ""
echo "This is indicative of someone pushing instead of secure-pushing"
echo ""
echo "FETCH_HEAD: $FETCH_HEAD"
echo "PUSH_ENTRY: $RSL_BRANCH_HEAD"
echo ""
git log --oneline origin/$CURRENT_BRANCH | head
restore_original_state
exit 99
fetch_status=1
else
fetch_status=0
fi
}
#This function verifies the rsl file
function rsl_verify {
git checkout -q rsl
if git diff $RSL_PREV_HEAD HEAD --stat|grep -q 'deletion'
then
echo "RSL file corrupted"
fi
#verify the signature on last enry
LAST_ENTRY=$(ls -lrt|wc -l)
#Computing Hash for previous entries
RSL=$(find_last_entry_number)
#Finding first push entry
for i in $(seq 1 $RSL)
do
if grep -q HEAD $i
then
#Update Last RSL push entry
FIRST_PUSH_ENTRY=$i
break
else
RSL=$(($i+1))
fi
done
RSL=$(find_last_entry_number)
#finding the last push entry
while [ -f $RSL ]
do
if grep -q HEAD $RSL
then
#Update Last RSL push entry
LAST_PUSH_ENTRY=$RSL
break
else
RSL=$(($RSL-1))
fi
done
echo "******* Verifying the sign on last rsl entry *******"
if [ -f $LAST_PUSH_ENTRY ]
then
gpg2 --default-key $GPG_KEY --verify $LAST_PUSH_ENTRY &> /dev/null
local status=$?
if [ $status -ne 0 ]
then
echo "Signature verification on last push entry is failed"
else
echo "Signature verification on last push entry is successful"
fi
else
echo "There is no RSL push entry exist before. No need of signature verification"
fi
echo "****************************************************"
echo "******* Verifying the hash on new rsl push entries *******"
#Hash Verification
VERIFY=1
while [ $VERIFY -eq 1 ]
do
RSL=$(($LAST_PUSH_ENTRY-1))
while [ -f $RSL ]
do
if grep -q HEAD $RSL
then
#Update Last RSL push entry
LAST_PUSH_ENTRY2=$RSL
break
else
RSL=$(($RSL-1))
fi
done
if [ -f $LAST_PUSH_ENTRY2 ]
then
for i in $(seq $LAST_PUSH_ENTRY2 $(($LAST_PUSH_ENTRY-1)))
do
cat $i >>hash_file.tmp
done
else
for i in $(seq 1 $(($LAST_PUSH_ENTRY-1)))
do
cat $i >>hash_file.tmp
done
fi
HASH_1=`cat $LAST_PUSH_ENTRY|grep 'PREV_HASH:'|cut -d':' -f2`
HASH_2=`cat hash_file.tmp|sha512sum|cut -d' ' -f1`
if [[ "$HASH_2" != "$HASH_1" && ! -z "$HASH_1" ]]
then
echo "Hash verfication on the following entry failed"
echo "HASH_1:"$HASH_1 " HASH_2":$HASH_2
echo `cat hash_file.tmp`
exit 1
else
echo "Hash verification successful "
fi
rm hash_file.tmp
if [[ $LAST_PUSH_ENTRY -eq $LAST_VERIFIED_PUSH_ENTRY || $LAST_PUSH_ENTRY -eq $FIRST_PUSH_ENTRY || -z $LAST_PUSH_ENTRY ]]
then
VERIFY=0
fi
LAST_PUSH_ENTRY=$LAST_PUSH_ENTRY2
LAST_PUSH_ENTRY2=0
done
}
function unstash_changes() {
if [ "$STASH_OUTPUT" != "No local changes to save" ]; then
POP_STASH="$(git stash pop)"
if [ $? != 0 ]; then
echo "ERROR:"
echo "$POP_STASH"
exit 9
fi
fi
cd $WORKING_DIRECTORY
}
function restore_original_state() {
CLEAR_INDEX="$(git reset --hard HEAD)"
if [ $? != 0 ]; then
echo "ERROR:"
echo "$CLEAR_INDEX"
fi
CHECKOUT_ORIGINAL_OUTPUT="$(git checkout -q $PRIOR_BRANCH)"
if [ $? != 0 ]; then
echo "ERROR:"
echo "$CHECKOUT_ORIGINAL_OUTPUT"
exit 8
fi
unstash_changes
}
#************************************************************************************
# Get Current Branch
PRIOR_BRANCH=$(git symbolic-ref --short HEAD)
# Get current working directory
WORKING_DIRECTORY="$(pwd)"
while [ $# -ne 0 ]
do
case "$1" in
-h | -help )
print_usage
exit 0
;;
-* )
print_usage
exit -1
;;
* )
CURRENT_BRANCH=$1
shift
;;
esac
done
if [[ -z $CURRENT_BRANCH ]]
then
CURRENT_BRANCH=$PRIOR_BRANCH
fi
#check if we are pushing rsl branch
if [ "$CURRENT_BRANCH" == "rsl" ]
then
exit 0
fi
echo "Stashing any local changes."
STASH_OUTPUT="$(git -c commit.gpgSign=false stash save -a)"
if [ $? != 0 ]; then
echo "ERROR:"
echo "$STASH_OUTPUT"
exit 1
fi
cd $(git rev-parse --show-toplevel)
status=1
# Repeat the following steps until rsl push is successful
while [ $status -ne 0 ]
do
#Fetch RSL
if [[ $(git branch -a |grep 'remotes/origin/rsl') ]]
then
fetch_status=1
while [ $fetch_status -ne 0 ]
do
#fetch the RSL branch and current branch
echo "Fetching the rsl ..."
rsl_fetch
done
# Update the RSL file
echo "Updating the rsl ..."
update_rsl_fetch
else
# RSL branch Intialization
echo "Intializing the rsl ..."
rsl_init
fi
#Pushing the RSL
echo "Pushing the rsl ... "
push_rsl
if [ $status -ne 0 ]
then
echo "rsl push failed !!"
git reset --hard origin/rsl
fi
done
RSL=$(find_last_entry_number)
#finding the last push entry
while [ -f $RSL ]
do
if grep -q HEAD $RSL
then
#Update Last RSL push entry
LAST_PUSH_ENTRY=$RSL
break
else
RSL=$(($RSL-1))
fi
done
if [[ $LAST_VERIFIED_PUSH_ENTRY -eq 1 || -z "$LAST_VERIFIED_PUSH_ENTRY" || $LAST_PUSH_ENTRY -eq $LAST_VERIFIED_PUSH_ENTRY ]]
then
echo "No verification required"
else
rsl_verify
fi
#Switch back to prior branch
restore_original_state