forked from section-io/aws-ecr-get-login
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.sh
106 lines (91 loc) · 2.15 KB
/
entrypoint.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
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
#!/bin/sh
test -z "${DEBUG}" || set -o xtrace
set -o errexit
if [ -z "${AWS_DEFAULT_REGION}" ] && [ -z "${AWS_REGIONS}" ]
then
echo AWS_DEFAULT_REGION or AWS_REGIONS environment variable required >&2
exit 1
fi
if [ -n "${AWS_DEFAULT_REGION}" ] && [ -z "${AWS_REGIONS}" ]
then
AWS_REGIONS="${AWS_DEFAULT_REGION}"
fi
unset AWS_DEFAULT_REGION
test -n "${AWS_ACCESS_KEY_ID}" || {
echo AWS_ACCESS_KEY_ID environment variable required >&2
exit 1
}
test -n "${AWS_SECRET_ACCESS_KEY}" || {
echo AWS_SECRET_ACCESS_KEY environment variable required >&2
exit 1
}
test -n "${AWS_ACCOUNT_ID}" || {
echo AWS_ACCOUNT_ID environment variable required >&2
exit 1
}
write_docker_credentials () {
user="${1}"
password="${2}"
url="${3}"
auth=$(printf '%s:%s' "${user}" "${password}" | base64 | tr -d "\n")
docker_file=/.docker/config.json
work_file=$(mktemp)
test -s "${docker_file}" || printf '{"auths":{}}' >"${docker_file}"
jq ".auths[\"${url}\"].auth = \"${auth}\"" <"${docker_file}" >"${work_file}"
mv -f "${work_file}" "${docker_file}"
}
parse_docker_login () {
user=
password=
url=
while test 0 -lt $#
do
case "${1}" in
-u)
user="${2}";
shift 2;;
-p)
password="${2}";
shift 2;;
-e)
shift 2;; # email ignored
*)
url="${1}"
shift;;
esac
done
test -n "${user}" || {
echo Expected -u argument >&2
return 1
}
test -n "${password}" || {
echo Expected -p argument >&2
return 1
}
test -n "${url}" || {
echo Expected a url argument >&2
return 1
}
write_docker_credentials "${user}" "${password}" "${url}"
}
refresh_credentials () {
for region in ${AWS_REGIONS}
do
login_command=$(aws ecr get-login --registry-ids "${AWS_ACCOUNT_ID}" --region "${region}")
login_args="${login_command#docker login }"
# shellcheck disable=SC2086
parse_docker_login ${login_args}
done
}
wait_for_credentials_to_approach_expiration () {
expire=$(( $(date +%s) + 60 * 60 * 11 ))
while [ "${expire}" -gt "$(date +%s)" ]
do
sleep 60
done
}
while true
do
refresh_credentials
wait_for_credentials_to_approach_expiration
done