Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/scripts/credential-helper.sh
2864 views
1
#!/usr/bin/env bash
2
3
# We try not to rely on there being anything installed on the
4
# local machine so although it would be nice to use `jq` here
5
# we don't do so. This means that a selenium build needs very
6
# little installed by the user
7
8
function emit_headers() {
9
echo "{\"headers\":{\"Authorization\":[\"Bearer ${1}\"]}}"
10
exit 0
11
}
12
13
function netrc_password() {
14
if [ ! -e "$HOME/.netrc" ]; then
15
return
16
fi
17
18
cat $HOME/.netrc | sed -e 's/#.*//' | awk -v host="$1" -v field=password '{
19
if ($1 == "machine") {
20
found = ($2 == host);
21
} else if (found && ($1 == field)) {
22
print $2;
23
exit
24
} else if (found && $1 == "machine") {
25
found = false
26
}
27
}'
28
}
29
30
function get() {
31
INPUT=$1
32
33
if [ -z "$(echo "$INPUT" | grep "gypsum.cluster")" ]; then
34
exit 0
35
fi
36
37
if [ -n "$GITHUB_TOKEN" ]; then
38
emit_headers "$GITHUB_TOKEN"
39
fi
40
41
if [ -n "$ENGFLOW_GITHUB_TOKEN" ]; then
42
emit_headers "${ENGFLOW_GITHUB_TOKEN}"
43
fi
44
45
NETRC="$(netrc_password github.com)"
46
if [ -n "${NETRC}" ]; then
47
emit_headers "${NETRC}"
48
fi
49
50
KEYCHAIN="$(security find-generic-password -a selenium -s 'Selenium EngFlow' -w )" 2>/dev/null
51
if [ -n "$KEYCHAIN" ]; then
52
emit_headers "${KEYCHAIN}"
53
fi
54
55
echo "{\"headers\":{}}"
56
exit 0
57
}
58
59
function set() {
60
security add-generic-password -a selenium -s 'Selenium EngFlow' -w "$1"
61
exit 0
62
}
63
64
cmd=$1
65
66
case $cmd in
67
"get")
68
get "$(</dev/stdin)"
69
;;
70
71
"set")
72
set "$(</dev/stdin)"
73
;;
74
75
"test")
76
get '{"uri":"https://gypsum.cluster.engflow.com/google.devtools.build.v1.PublishBuildEvent"}'
77
;;
78
79
*)
80
exit 1
81
;;
82
esac
83
84