This repository has been archived by the owner on Jul 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ee-token
executable file
·236 lines (209 loc) · 8.77 KB
/
ee-token
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
#!/usr/bin/env bash
#
# Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# The Universal Permissive License (UPL), Version 1.0
#
# Subject to the condition set forth below, permission is hereby granted to any
# person obtaining a copy of this software, associated documentation and/or
# data (collectively the "Software"), free of charge and under any and all
# copyright rights in the Software, and any and all patent rights owned or
# freely licensable by each licensor hereunder covering either (i) the
# unmodified Software as contributed to or provided by such licensor, or (ii)
# the Larger Works (as defined below), to deal in both
#
# (a) the Software, and
#
# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
# one is included with the Software each a "Larger Work" to which the Software
# is contributed by such licensors),
#
# without restriction, including without limitation the rights to copy, create
# derivative works of, display, perform, and distribute the Software and make,
# use, sell, offer for sale, import, export, have made, and have sold the
# Software and the Larger Work(s), and to sublicense the foregoing rights on
# either these or other terms.
#
# This license is subject to the following condition:
#
# The above copyright notice and either this complete permission notice or at a
# minimum a reference to the UPL must be included in all copies or substantial
# portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
set -o errexit # fail on error
set -o nounset # fail if a variable is undefined
GDS_BASE="https://gds.oracle.com/api/20220101"
LICENSE_ID="D53FA58D12817B3CE0530F15000A74CA"
PROBE_ARTIFACT_ID="EADE25DCAF6B2655E0531618000AAB08"
GU_CONFIG="${HOME}/.gu/config"
GVMD_VERSION="1.0.1"
GVMD_NAME="GraalVM Download Token Generator"
GVMD_SCRIPT="ee-token"
GVMD_USER_AGENT="${GVMD_NAME}/${GVMD_VERSION} (arch:$(uname -m); os:$(uname -s)"
readonly GDS_BASE LICENSE_ID GU_CONFIG GVMD_VERSION GVMD_NAME GVMD_SCRIPT GVMD_USER_AGENT
###########
# Helpers #
###########
function fail() {
echo "$1"
exit 1
}
function print_help() {
cat <<EOF
${GVMD_NAME} v${GVMD_VERSION}
Usage:
bash <(curl -sL https://get.graalvm.org/${GVMD_SCRIPT}) [opts]
Options:
-d | --debug Enable debug mode.
-h | --help Show this help text.
--revoke [<token>/<email>]
Revoke download tokens. If no argument is provided, the
scripts attempts to detect a persisted token and revoke
it. Provide an specific token as an argument to revoke it.
Provide an email address to revoke all associated tokens.
Visit https://github.com/graalvm/graalvm-jdk-downloader for more information.
EOF
}
function ensure_command() {
local cmd=$1
if ! command -v "${cmd}" > /dev/null; then
echo "${cmd} not found."
echo ""
echo "======================================================================================================"
echo " Please install ${cmd} on your system using your favourite package manager."
echo ""
echo " Restart after installing ${cmd}."
echo "======================================================================================================"
fail ""
fi
}
ensure_command "curl"
function is_valid_token() {
response_code=$(curl --silent --head --write-out '%{http_code}' \
-o /dev/null --user-agent "${GVMD_USER_AGENT}" \
-H "x-download-token: $1" \
-X GET "${GDS_BASE}/artifacts/${PROBE_ARTIFACT_ID}/content" || "400")
[[ "${response_code}" == "302" ]]
}
function revoke() {
local token_or_email=$1
local key="downloadToken"
local question="Do you want to revoke the token '${token_or_email}'? (y/N): "
if [[ -z "${token_or_email}" ]]; then
if [[ -n "${GRAAL_EE_DOWNLOAD_TOKEN:-}" ]]; then
token_or_email="${GRAAL_EE_DOWNLOAD_TOKEN}"
else
# shellcheck source=/dev/null
source "${GU_CONFIG}"
if [[ -z "${GRAAL_EE_DOWNLOAD_TOKEN:-}" ]]; then
fail "Unable to detect \$GRAAL_EE_DOWNLOAD_TOKEN in '${GU_CONFIG}'. Please remove the file and try again."
fi
token_or_email="${GRAAL_EE_DOWNLOAD_TOKEN}"
fi
question="Found download token '${token_or_email}'.
Do you want to revoke it? (y/N): "
elif [[ "${token_or_email}" == *"@"* ]]; then
key="email"
question="Do you want to revoke all download tokens for '${token_or_email}'? (y/N): "
fi
read -r -p "${question}" user_input
if [[ "${user_input}" != [yY] ]]; then
exit
fi
response=$(curl -sS -X POST "${GDS_BASE}/tokenRequests" --user-agent "${GVMD_USER_AGENT}" -H "Content-Type: application/json" \
-d "{ \"type\": \"REVOKE\", \"${key}\": \"${token_or_email}\" }")
if [[ -n "${response}" ]]; then
fail "Revocation request failed with response: ${response}."
fi
echo "Successfully requested revocation.
Please check your email for a message asking you to confirm that you want to revoke your download token(s)."
}
#############################
# Check and parse arguments #
#############################
while [[ $# -gt 0 ]]; do
case "$1" in
-d | --debug)
set -x
shift
;;
-h | --help)
print_help
exit 0
;;
--revoke)
revoke "${2:-}"
exit 0
;;
*)
fail "Unknown option: $1"
;;
esac
done
#########################
# Detect existing token #
#########################
if [[ -z "${GRAAL_EE_DOWNLOAD_TOKEN:-}" ]]; then
if [[ -f "${GU_CONFIG}" ]]; then
# shellcheck source=/dev/null
source "${GU_CONFIG}"
if [[ -z "${GRAAL_EE_DOWNLOAD_TOKEN:-}" ]]; then
fail "Unable to detect \$GRAAL_EE_DOWNLOAD_TOKEN in '${GU_CONFIG}'. Please remove the file and try again."
else
if is_valid_token "${GRAAL_EE_DOWNLOAD_TOKEN}"; then
echo "A valid download token is already stored in '${GU_CONFIG}'."
echo "Your download token is: '${GRAAL_EE_DOWNLOAD_TOKEN}'"
exit
fi
echo "The token stored in '${GU_CONFIG}' is not yet valid, because you have not yet accepted the license emailed to you, or no longer valid, because you have generated another token for the same email address."
read -r -p "Would you like to generate a new token? (y/N): " user_input
if [[ "${user_input}" != [yY] ]]; then
exit
fi
fi
fi
else
if is_valid_token "${GRAAL_EE_DOWNLOAD_TOKEN}"; then
echo "You already provide a valid download token via \$GRAAL_EE_DOWNLOAD_TOKEN: '${GRAAL_EE_DOWNLOAD_TOKEN}'."
exit
else
fail "The download token you provided via \$GRAAL_EE_DOWNLOAD_TOKEN is invalid: '${GRAAL_EE_DOWNLOAD_TOKEN}'."
fi
fi
#####################
# Request new token #
#####################
echo "About to request a download token for GraalVM Enterprise Edition..."
echo "Please provide an email address and review Oracle's Privacy Policy at https://www.oracle.com/legal/privacy/privacy-policy.html."
read -r -p "Enter a valid email address: " email
if [[ "${email}" != *"@"* ]]; then
fail "'${email}' is not a valid email address"
fi
response=$(curl -sS -X POST "${GDS_BASE}/licenseAcceptance" --user-agent "${GVMD_USER_AGENT}" -H "Content-Type: application/json" \
-d "{ \"email\": \"${email}\", \"licenseId\": \"${LICENSE_ID}\", \"type\": \"GENERATE_TOKEN_AND_ACCEPT_LICENSE\"}")
token=$(echo "${response}" | grep -o '"token":"[^"]*' | grep -o '[^"]*$') || fail "Unable to find token (${response})."
echo "Your new download token is: '${token}'"
echo "Please check your email inbox and accept the license to activate your download token."
#####################
# Persist new token #
#####################
read -r -p "Would you like to persist this token in '${GU_CONFIG}' on this machine? (y/N): " user_input
if [[ "${user_input}" == [yY] ]]; then
if [[ ! -d "$(dirname "${GU_CONFIG}")" ]]; then
mkdir -p "$(dirname "${GU_CONFIG}")"
fi
echo "#$(date)" > "${GU_CONFIG}"
echo "GRAAL_EE_DOWNLOAD_TOKEN=${token}" >> "${GU_CONFIG}"
echo "Download token persisted."
else
echo "Download token not persisted."
fi