-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpulli.go
More file actions
241 lines (194 loc) · 6.73 KB
/
pulli.go
File metadata and controls
241 lines (194 loc) · 6.73 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
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
237
238
239
240
241
package main
import (
"bufio"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
)
type GetTokenResponse struct {
Token string `json:"token"`
}
type GetImageManifestResponse struct {
Manifests []*Manifest `json:"manifests"`
MediaType string `json:"mediaType"`
SchemaVersion int `json:"schemaVersion"`
}
type Manifest struct {
Annotations map[string]string `json:"annotations"`
Digest string `json:"digest"`
MediaType string `json:"mediaType"`
Platform struct {
Architecture string `json:"architecture"`
OS string `json:"os"`
} `json:"platform"`
Size int `json:"size"`
}
type GetSpecificManifestResponse struct {
SchemaVersion int `json:"schemaVersion"`
MediaType string `json:"mediaType"`
Config *struct {
MediaType string `json:"mediaType"`
Digest string `json:"digest"`
Size int `json:"size"`
}
Layers []*struct {
MediaType string `json:"mediaType"`
Digest string `json:"digest"`
Size int `json:"size"`
Annotations map[string]string `json:"annotations"`
}
Annotations map[string]string `json:"annotations"`
}
func usage() {
prog := os.Args[0]
fmt.Fprintf(os.Stderr, "usage: %s <image-name> [architecture] [os]\n", prog)
fmt.Fprintf(os.Stderr, "architecture: default to amd64, os: default to linux\n")
fmt.Fprintf(os.Stderr, "currently only supports pulling from Dockerhub\n")
}
func main() {
if len(os.Args) < 2 {
usage()
os.Exit(-1)
}
image := os.Args[1]
arch := "amd64"
operatingSystem := "linux"
if len(os.Args) == 3 {
arch = os.Args[2]
}
if len(os.Args) == 4 {
arch = os.Args[2]
operatingSystem = os.Args[3]
}
token, err := getToken(image)
if err != nil {
fmt.Printf("failed to get bearer token: %s\n", err)
os.Exit(-1)
}
fmt.Fprintf(os.Stderr, "fetching image manifest for %s (arch: %s, os: %s)\n", image, arch, operatingSystem)
imageManifestResponse, err := getImageManifest(image, token)
if err != nil {
fmt.Printf("failed to get image manifests: %s\n", err)
os.Exit(-1)
}
platformSpecificManifests := make([]*Manifest, 0)
for _, manifest := range imageManifestResponse.Manifests {
if manifest.Platform.Architecture == arch && manifest.Platform.OS == operatingSystem {
platformSpecificManifests = append(platformSpecificManifests, manifest)
}
}
if len(platformSpecificManifests) == 0 {
fmt.Fprintf(os.Stderr, "No image manifest found for %s\n", image)
os.Exit(-1)
}
specificManifest := platformSpecificManifests[0]
fmt.Fprintf(os.Stderr, "getting image specific manifest for %s (arch=%s os=%s digest=%s)\n", image, arch, operatingSystem, specificManifest.Digest)
specificImageManifestResponse, err := getSpecificManifest(token, image, specificManifest.Digest)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to get manifest for %s (arch=%s os=%s digest=%s)\n", image, arch, operatingSystem, specificManifest.Digest)
os.Exit(-1)
}
if _, err := isLayerBlobExists(token, image, specificImageManifestResponse.Layers[0].Digest); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(-1)
}
if err := downloadLayerBlob(token, image, specificImageManifestResponse.Layers[0].Digest); err != nil {
fmt.Fprintf(os.Stderr, "error downloading image: %s\n", err)
}
}
func getToken(imageName string) (string, error) {
endpoint := fmt.Sprintf("https://auth.docker.io/token?service=registry.docker.io&scope=repository:library/%s:pull", imageName)
resp, err := http.Get(endpoint)
if err != nil {
return "", err
}
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("failed to read HTTP response body: %s", err)
}
var getTokenResp GetTokenResponse
if err := json.Unmarshal(bodyBytes, &getTokenResp); err != nil {
return "", fmt.Errorf("failed to deserialize JSON: %s", err)
}
return getTokenResp.Token, nil
}
func getImageManifest(imageName string, token string) (*GetImageManifestResponse, error) {
endpoint := fmt.Sprintf("https://registry-1.docker.io/v2/library/%s/manifests/latest", imageName)
req, err := http.NewRequest("GET", endpoint, nil)
if err != nil {
return nil, err
}
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to get image manifest: %s", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("unexpected HTTP status code %d when getting image manifest", resp.StatusCode)
}
respBytes, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %s", err)
}
var getManifestResp GetImageManifestResponse
if err := json.Unmarshal(respBytes, &getManifestResp); err != nil {
return nil, fmt.Errorf("failed to deserialize JSON: %s", err)
}
return &getManifestResp, nil
}
func getSpecificManifest(token string, imageName string, digest string) (*GetSpecificManifestResponse, error) {
endpoint := fmt.Sprintf("https://registry-1.docker.io/v2/library/%s/manifests/%s", imageName, digest)
req, _ := http.NewRequest("GET", endpoint, nil) // TODO: handle error
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
respBytes, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var specificManifestResp GetSpecificManifestResponse
if err := json.Unmarshal(respBytes, &specificManifestResp); err != nil {
return nil, err
}
return &specificManifestResp, nil
}
func isLayerBlobExists(token string, image string, digest string) (bool, error) {
endpoint := fmt.Sprintf("https://registry-1.docker.io/v2/library/%s/blobs/%s", image, digest)
req, err := http.NewRequest("HEAD", endpoint, nil)
if err != nil {
return false, fmt.Errorf("failed to create HTTP request: %s", err)
}
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
resp, err := http.DefaultClient.Do(req)
if err != nil {
return false, fmt.Errorf("failed to check layer blob: %s", err)
}
if resp.StatusCode != 200 {
return false, fmt.Errorf("layer blob possibly does not exist. status=%d\n", resp.StatusCode)
}
return true, nil
}
func downloadLayerBlob(token string, image string, digest string) error {
endpoint := fmt.Sprintf("https://registry-1.docker.io/v2/library/%s/blobs/%s", image, digest)
req, err := http.NewRequest("GET", endpoint, nil)
if err != nil {
return err
}
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
resp, err := http.DefaultClient.Do(req)
if err != nil {
return fmt.Errorf("failed to download blob: %s", err)
}
r := bufio.NewReader(resp.Body)
destFile := os.Stdout
wlen, err := r.WriteTo(destFile)
if err != nil {
return fmt.Errorf("write error: %s", err)
}
fmt.Fprintf(os.Stderr, "wrote %d bytes\n", wlen)
return nil
}