-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrmS3
executable file
·122 lines (109 loc) · 2.76 KB
/
rmS3
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
#!/bin/bash
ENV_FILE=".env"
#
# loads the s3 env variables
#
load_s3_env_vars () {
if [ -f "${ENV_FILE}" ]
then
export $(grep -v '^#' ${ENV_FILE} | xargs)
fi
}
#
# write s3 variables to file
#
write_s3_env_vars () {
echo "ACCESS_KEY_ID=${ACCESS_KEY_ID}" > ${ENV_FILE}
echo "SECRET_ACCESS_KEY=${SECRET_ACCESS_KEY}" >> ${ENV_FILE}
}
#
# asks user to input s3 variables
#
ask_s3_env_vars () {
if [ -z "${ACCESS_KEY_ID}" ]
then
read -p 'Enter your Access Key ID: ' ACCESS_KEY_ID
export ACCESS_KEY_ID
fi
if [ -z "${SECRET_ACCESS_KEY}" ]
then
read -p 'Enter your Secret Access Key: ' SECRET_ACCESS_KEY
export SECRET_ACCESS_KEY
fi
}
#
# check to see if s3 variables exist. Ask use for them if they don't exist
#
check_s3_env_vars () {
while true
do
if [ -z "${ACCESS_KEY_ID}" ] || [ -z "${SECRET_ACCESS_KEY}" ]
then
ask_s3_env_vars
else
break
fi
done
}
#
# Main Program
#
if [ $# -gt 1 ] || [ $# -lt 1 ]
then
echo ""
echo "ERROR! Wrong # of command line arguements"
echo ""
echo "USAGE: $0 <S3 URL>"
echo ""
echo " <S3 URL> completed URL in the format of <access type>://<hostname>/<vault>/<object>"
echo ""
echo " <access type> either http or https"
echo " <hostname> either hostame or ip address of accesser (or load balancer)"
echo " <vault> name of the vault you wish to access"
echo " <object> name of object in ICOS to remove"
echo ""
echo " NOTE: The system will prompt you for your S3 credentials and store them in the file .env"
echo " This will allow you to share the S3 credentials between scripts and store them for future use"
echo ""
exit 1
else
# load the s3 keys if they exist
load_s3_env_vars
# check for the s3 keys and prompt if you don't have them
check_s3_env_vars
# write the s3 keys
write_s3_env_vars
URL=${1}
OBJECT=`basename ${URL}`
TEMP_URL=`dirname ${URL}`
HOST_URL=`dirname ${TEMP_URL}`
ACCESSER=`basename ${HOST_URL}`
VAULT=`basename ${TEMP_URL}`
resource="/${VAULT}/${OBJECT}"
contentType='application/x-compressed-tar'
dateValue=`date -R`
stringToSign="DELETE\n\n${contentType}\n${dateValue}\n${resource}"
s3Key=${ACCESS_KEY_ID}
s3Secret=${SECRET_ACCESS_KEY}
signature=`echo -en ${stringToSign} | openssl sha1 -hmac ${s3Secret} -binary | base64`
OUTPUT=`curl -k -X DELETE \
-H "Host: ${ACCESSER}" \
-H "Date: ${dateValue}" \
-H "Content-Type: ${contentType}" \
-H "Authorization: AWS ${s3Key}:${signature}" \
${URL} 2>&1`
IS_ERROR=`echo ${OUTPUT}| grep -i Error`
if [ ! -z "${IS_ERROR}" ]
then
echo ""
echo "THERE WAS AN ERROR"
echo ""
echo ${OUTPUT} | sed -e 's_>_&\
_g'
exit 1
else
echo ""
echo "OPERATION WAS SUCCESSFUL"
echo ""
fi
fi