Skip to content

Commit 66331f4

Browse files
authored
Merge pull request ceph#46093 from pdvian/wip-admin-curl
examples: Add rgw script to access admin APIs using curl Reviewed-by: Daniel Gryniewicz <[email protected]> Reviewed-by: Casey Bodley <[email protected]>
2 parents c959ae6 + 9b40fda commit 66331f4

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

examples/rgw_admin_curl.sh

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#!/usr/bin/env bash
2+
3+
show_help()
4+
{
5+
echo "Usage: `basename $0` -a <access-key> -s <secret-key>" \
6+
"-e <rgw-endpoint> -r <http-request>" \
7+
"-p <admin-resource> -q \"<http-query-string>\""
8+
echo " -a Access key of rgw user"
9+
echo " -s Secret key of rgw user"
10+
echo " -e RGW endpoint in <ipaddr:port> format"
11+
echo " -r HTTP request type GET/PUT/DELETE"
12+
echo " -p RGW admin resource e.g user, bucket etc"
13+
echo " -q HTTP query string"
14+
echo " -j (Optional) Print output in pretty JSON format"
15+
echo " Examples :"
16+
echo " - To create rgw user"
17+
echo " # `basename $0` -a ABCD1234EFGH5678IJ90" \
18+
"-s klmnopqrstuvwxyz12345ABCD987654321efghij" \
19+
"-e 10.0.0.1:8080 -r PUT -p user" \
20+
"-q \"uid=admin&display-name=Administrator\""
21+
echo " - To get rgw user info"
22+
echo " # `basename $0` -a ABCD1234EFGH5678IJ90" \
23+
"-s klmnopqrstuvwxyz12345ABCD987654321efghij" \
24+
"-e 10.0.0.1:8080 -r GET -p user -q \"uid=admin\""
25+
echo " - To list buckets"
26+
echo " (List all buckets)"
27+
echo " # `basename $0` -a ABCD1234EFGH5678IJ90" \
28+
"-s klmnopqrstuvwxyz12345ABCD987654321efghij" \
29+
"-e 10.0.0.1:8080 -r GET -p bucket"
30+
echo " (For specific rgw user)"
31+
echo " # `basename $0` -a ABCD1234EFGH5678IJ90" \
32+
"-s klmnopqrstuvwxyz12345ABCD987654321efghij" \
33+
"-e 10.0.0.1:8080 -r GET -p bucket -q \"uid=admin\""
34+
echo " - To delete bucket"
35+
echo " # `basename $0` -a ABCD1234EFGH5678IJ90" \
36+
"-s klmnopqrstuvwxyz12345ABCD987654321efghij" \
37+
"-e 10.0.0.1:8080 -r DELETE -p bucket -q \"bucket=foo\""
38+
echo " - To delete rgw user"
39+
echo " # `basename $0` -a ABCD1234EFGH5678IJ90" \
40+
"-s klmnopqrstuvwxyz12345ABCD987654321efghij" \
41+
"-e 10.0.0.1:8080 -r DELETE -p user -q \"uid=admin\""
42+
exit 1
43+
}
44+
45+
access_key=""
46+
secret_key=""
47+
rgw_endpoint=""
48+
http_request=""
49+
admin_resource=""
50+
http_query=""
51+
use_jq=false
52+
53+
while getopts "a:s:e:r:p:q:j" opt; do
54+
case "$opt" in
55+
a)
56+
access_key=${OPTARG}
57+
;;
58+
s) secret_key=${OPTARG}
59+
;;
60+
e) rgw_endpoint=${OPTARG}
61+
;;
62+
r) http_request=${OPTARG}
63+
;;
64+
p) admin_resource=${OPTARG}
65+
;;
66+
q) http_query=${OPTARG}
67+
;;
68+
j) use_jq=true
69+
;;
70+
*)
71+
show_help
72+
exit 1
73+
;;
74+
esac
75+
done
76+
shift $((OPTIND-1))
77+
78+
if [ -z "${access_key}" ] || [ -z "${secret_key}" ] || \
79+
[ -z "${rgw_endpoint}" ] || [ -z "${http_request}" ] || \
80+
[ -z "${admin_resource}" ] || [ -z "${http_query}" ]; then
81+
if [ "${http_request}" = "GET" ] && [ "${admin_resource}" = "bucket" ] && \
82+
[ -z "${http_query}" ]; then
83+
:
84+
else
85+
show_help
86+
fi
87+
fi
88+
89+
resource="/admin/${admin_resource}"
90+
contentType="application/x-compressed-tar"
91+
dateTime=`date -R -u`
92+
93+
headerToSign="${http_request}
94+
95+
${contentType}
96+
${dateTime}
97+
${resource}"
98+
99+
signature=`echo -en "$headerToSign" | \
100+
openssl sha1 -hmac ${secret_key} -binary | base64`
101+
102+
if "$use_jq";
103+
then
104+
curl -X ${http_request} -H "Content-Type: ${contentType}" -H "Date: ${dateTime}" \
105+
-H "Authorization: AWS ${access_key}:${signature}" -H "Host: ${rgw_endpoint}" \
106+
"http://${rgw_endpoint}${resource}?${http_query}" 2> /dev/null|jq "."
107+
else
108+
curl -X ${http_request} -H "Content-Type: ${contentType}" -H "Date: ${dateTime}" \
109+
-H "Authorization: AWS ${access_key}:${signature}" -H "Host: ${rgw_endpoint}" \
110+
"http://${rgw_endpoint}${resource}?${http_query}"
111+
fi
112+
echo ""

0 commit comments

Comments
 (0)