forked from joshdk/docker-retag
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
174 lines (143 loc) · 3.99 KB
/
main.go
File metadata and controls
174 lines (143 loc) · 3.99 KB
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
// Copyright 2018 Josh Komoroske. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE.txt file.
package main
import (
"bytes"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"os"
"strings"
"docker-retag/arguments"
)
const (
dockerRegistryEnv = "DOCKER_REGISTRY"
dockerUsernameEnv = "DOCKER_USER"
dockerPasswordEnv = "DOCKER_PASS"
defaultRegistry = "https://index.docker.io"
dockerManifestV2MIME = "application/vnd.docker.distribution.manifest.v2+json"
dockerManifestListV2MIME = "application/vnd.docker.distribution.manifest.list.v2+json"
ociManifestV1MIME = "application/vnd.oci.image.manifest.v1+json"
ociIndexV1MIME = "application/vnd.oci.image.index.v1+json"
)
func main() {
if err := mainCmd(os.Args); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "docker-retag: %s\n", err.Error())
os.Exit(1)
}
}
func mainCmd(args []string) error {
// allow empty username/passowrd
username, _ := os.LookupEnv(dockerUsernameEnv)
password, _ := os.LookupEnv(dockerPasswordEnv)
registryUrl, found := os.LookupEnv(dockerRegistryEnv)
if !found || registryUrl == "" {
registryUrl = defaultRegistry
}
if !strings.HasPrefix(registryUrl, "http://") && !strings.HasPrefix(registryUrl, "https://") {
// automatically add https scheme if no scheme is present
registryUrl = "https://" + registryUrl
}
regUrl, err := url.Parse(registryUrl)
if err != nil {
return err
}
prog_args := args[1:]
if len(prog_args) < 2 {
return errors.New("Not enough arguments provided, 2 or 3 arguments are required")
}
prog_args[0] = strings.TrimPrefix(prog_args[0], regUrl.Host+"/")
repository, oldTag, newTag, err := arguments.Parse(prog_args)
if err != nil {
return err
}
reg := NewRegistry(registryUrl, username, password)
err = reg.ReTag(repository, oldTag, newTag)
if err != nil {
return err
}
separator := ":"
if strings.HasPrefix(oldTag, "sha256:") {
separator = "@"
}
fmt.Printf("Retagged %s%s%s as %s:%s\n", repository, separator, oldTag, repository, newTag)
return nil
}
type HttpError struct {
Status string
URL string
}
func (h HttpError) Error() string {
return fmt.Sprintf("HTTP %s when accessing %q", h.Status, h.URL)
}
type Registry struct {
URL string
Client *http.Client
}
func NewRegistry(url, username, password string) *Registry {
authTransport := &basicAuthTransport{
Wrapped: &tokenAuthTransport{
Wrapped: http.DefaultTransport,
Username: username,
Password: password,
},
URL: url,
Username: username,
Password: password,
}
r := Registry{
url,
&http.Client{
Transport: authTransport,
},
}
return &r
}
func (r *Registry) url(pathTemplate string, args ...any) string {
pathSuffix := fmt.Sprintf(pathTemplate, args...)
url := fmt.Sprintf("%s%s", r.URL, pathSuffix)
return url
}
func (r *Registry) ReTag(repo, oldTag, newTag string) error {
sourceUrl := r.url("/v2/%s/manifests/%s", repo, oldTag)
sourceReq, err := http.NewRequest("GET", sourceUrl, nil)
if err != nil {
return err
}
sourceReq.Header.Set("Accept", strings.Join([]string{ociManifestV1MIME, ociIndexV1MIME, dockerManifestListV2MIME, dockerManifestV2MIME}, ", "))
sourceResp, err := r.Client.Do(sourceReq)
if err != nil {
return err
}
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(sourceResp.Body)
if sourceResp.StatusCode != http.StatusOK {
return HttpError{sourceResp.Status, sourceUrl}
}
receivedMIME := sourceResp.Header.Get("Content-Type")
manifest, err := io.ReadAll(sourceResp.Body)
if err != nil {
return err
}
destUrl := r.url("/v2/%s/manifests/%s", repo, newTag)
destReq, err := http.NewRequest("PUT", destUrl, bytes.NewBuffer(manifest))
if err != nil {
return err
}
destReq.Header.Set("Content-Type", receivedMIME)
destResp, err := r.Client.Do(destReq)
if err != nil {
return err
}
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(destResp.Body)
if destResp.StatusCode != http.StatusCreated {
return HttpError{destResp.Status, destUrl}
}
return nil
}