forked from SAP-archive/teched2020-developer-keynote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
localutils.sh
71 lines (55 loc) · 1.62 KB
/
localutils.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
#!/usr/bin/env bash
# Local utilities for use in various other scripts
output=${output:-silent}
log () {
echo "$(date '+%Y-%m-%d %H:%M:%S')" "$@"
}
err () {
echo "$@" 1>&2
}
getval () {
echo "${1}" | jq -r "${2}"
}
gettoken () {
# Produces an OAuth 2.0 access token from a JSON OAuth 2.0 info stanza
# that looks like this:
# {
# "clientid": "...",
# "clientsecret": "...",
# "granttype": "...",
# "tokenendpoint": "..."
# }
local stanza=${1}
local clientid clientsecret granttype tokenendpoint
clientid=$(getval "${stanza}" .clientid)
clientsecret=$(getval "${stanza}" .clientsecret)
granttype=$(getval "${stanza}" .granttype)
tokenendpoint=$(getval "${stanza}" .tokenendpoint)
# This is basically assuming the use of the OAuth 2.0
# Client Credentials grant type.
token=$(
curl \
--"${output}" \
--user "${clientid}:${clientsecret}" \
--data "grant_type=${granttype}" \
"${tokenendpoint}" \
| jq -r '.access_token'
)
echo "${token}"
}
# For a given service instance, returns the information
# in a service key, specified by name. Basic cacheing
# in that key data is regenerated from the cloud platform
# and cached for N mins in a local file.
getservicekey () {
local instance=${1}
local servicekey=${2}
local cachetime="+5" # mins
local file="sk-$instance-$servicekey.json"
# If the file doesn't exist, or is older than N mins,
# then we need to (re)refresh from the cloud.
if [ ! -f "$file" ] || test "$(find "$file" -mmin $cachetime)"; then
cf service-key "${instance}" "${servicekey}" | sed '1,2d' > "$file"
fi
cat "$file"
}