-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdocker-create-tag.sh
executable file
·186 lines (147 loc) · 3.88 KB
/
docker-create-tag.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
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
#!/bin/sh
# Create Docker image tag using Registry v2 API.
#
# Author: Elan Ruusamäe <glen@pld-linux.org>
# URL: https://github.com/glensc/docker-create-tag
#
# Requires:
# - curl
# - jq
# - base64
#
# This is tested against GitLab Registry
set -eu
PROGRAM=${0##*/}
# defaults
: ${USERNAME=''}
: ${PASSWORD=''}
: ${SOURCE_IMAGE=''}
: ${TARGET_IMAGE=''}
die() {
echo >&2 "$PROGRAM: ERROR: $*"
exit 1
}
usage() {
cat <<-EOF
$PROGRAM - Create Docker image tag using Registry v2 API
Usage: $PROGRAM <source_image> <target_image>
Flags:
-u, --username username for the registry (default: ${USERNAME:-<none>})
-p, --password password for the registry (default: ${PASSWORD:-<none>})
Examples:
$PROGRAM registry.example.net/alpine:latest registry.example.net/alpine:recent
EOF
}
get_param() {
local header="$1" param="$2"
echo "$header" | sed -r -ne 's,.*'"$param"'="([^"]+)".*,\1,p'
}
discover_auth() {
local url="$1" headers header
shift
headers=$(curl -sI "$url" "$@")
header=$(echo "$headers" | grep -i '^Www-Authenticate:')
# Need to extract realm, service and scope from "Www-Authenticate" header:
# Www-Authenticate: Bearer realm="https://gitlab.example.net/jwt/auth",service="container_registry",scope="repository:ed/php:pull"
realm=$(get_param "$header" realm)
service=$(get_param "$header" service)
scope=$(get_param "$header" scope)
}
request_url() {
local method="$1" url="$2" rc
shift 2
# try unauthenticated
curl -sf -X "$method" "${url}" "$@" && rc=$? || rc=$?
if [ "$rc" -eq "0" ]; then
return 0
fi
# discover auth and retry
local realm service scope token
discover_auth "$url" -X "$method"
token=$(get_token "$realm" "$service" "$scope")
curl -sSf -X "$method" -H "Authorization: Bearer ${token}" "${url}" "$@"
}
load_docker_credentials() {
local registry="$1" config="${HOME}/.docker/config.json" token decoded
# reset to globally provided values
username="$USERNAME" password="$PASSWORD"
test -f "$config" || return 0
token=$(jq -er ".auths.\"${registry}\".auth" "$config") || return 0
decoded=$(echo "$token" | base64 -d)
# updates username, password which should be locally scoped from parent
username=${decoded%%:*}
password=${decoded#*:}
}
get_token() {
local realm="$1" service="$2" scope="$3" response
shift 3
if [ -n "$username" ]; then
set -- --user "$username:$password" "$@"
fi
response=$(curl -sSf "$@" "$realm?client_id=docker-create-tag&offline_token=true&service=$service&scope=$scope")
echo "$response" | jq -r .token
}
# parses registry.example.net/alpine:latest into
# - registry
# - image
# - tag
parse_image() {
local origin="$1"
registry=${origin%%/*}
image=${origin#$registry/}
tag=${image##*:}
image=${image%:$tag}
}
parse_options() {
local t
t=$(getopt -o u:p:h --long user:,password:,help -n "$PROGRAM" -- "$@")
[ $? != 0 ] && exit $?
eval set -- "$t"
while :; do
case "$1" in
-h|--help)
usage
exit 0
;;
-u|--user)
shift
USERNAME="$1"
;;
-p|--password)
shift
PASSWORD="$1"
;;
--)
shift
break
;;
*)
die "Internal error: [$1] not recognized!"
;;
esac
shift
done
test "$#" -eq 2 || die "Images not specified or excess arguments"
SOURCE_IMAGE="$1"
TARGET_IMAGE="$2"
}
main() {
local source_image="${1}" target_image="${2}"
local username password
local registry image tag manifest
manifest=$(mktemp)
parse_image "$source_image"
load_docker_credentials "$registry"
request_url GET "https://$registry/v2/$image/manifests/$tag" \
-H 'accept: application/vnd.docker.distribution.manifest.v2+json' \
> $manifest
parse_image "$target_image"
load_docker_credentials "$registry"
request_url PUT "https://$registry/v2/$image/manifests/$tag" \
-H 'content-type: application/vnd.docker.distribution.manifest.v2+json' \
-d "@$manifest"
rm $manifest
echo "Created tag: $source_image -> $target_image"
}
parse_options "$@"
main "$SOURCE_IMAGE" "$TARGET_IMAGE"