Skip to content

Commit 10618d4

Browse files
committed
imagetools: move auth function to separate file
Signed-off-by: Tonis Tiigi <[email protected]>
1 parent 52b5d08 commit 10618d4

File tree

2 files changed

+47
-41
lines changed

2 files changed

+47
-41
lines changed

util/imagetools/auth.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package imagetools
2+
3+
import (
4+
"encoding/base64"
5+
"encoding/json"
6+
7+
"github.com/distribution/reference"
8+
)
9+
10+
func toCredentialsFunc(a Auth) func(string) (string, string, error) {
11+
return func(host string) (string, string, error) {
12+
if host == "registry-1.docker.io" {
13+
host = "https://index.docker.io/v1/"
14+
}
15+
ac, err := a.GetAuthConfig(host)
16+
if err != nil {
17+
return "", "", err
18+
}
19+
if ac.IdentityToken != "" {
20+
return "", ac.IdentityToken, nil
21+
}
22+
return ac.Username, ac.Password, nil
23+
}
24+
}
25+
26+
func RegistryAuthForRef(ref string, a Auth) (string, error) {
27+
if a == nil {
28+
return "", nil
29+
}
30+
r, err := parseRef(ref)
31+
if err != nil {
32+
return "", err
33+
}
34+
host := reference.Domain(r)
35+
if host == "docker.io" {
36+
host = "https://index.docker.io/v1/"
37+
}
38+
ac, err := a.GetAuthConfig(host)
39+
if err != nil {
40+
return "", err
41+
}
42+
buf, err := json.Marshal(ac)
43+
if err != nil {
44+
return "", err
45+
}
46+
return base64.URLEncoding.EncodeToString(buf), nil
47+
}

util/imagetools/inspect.go

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package imagetools
33
import (
44
"bytes"
55
"context"
6-
"encoding/base64"
7-
"encoding/json"
86
"io"
97
"net/http"
108

@@ -121,42 +119,3 @@ func parseRef(s string) (reference.Named, error) {
121119
ref = reference.TagNameOnly(ref)
122120
return ref, nil
123121
}
124-
125-
func toCredentialsFunc(a Auth) func(string) (string, string, error) {
126-
return func(host string) (string, string, error) {
127-
if host == "registry-1.docker.io" {
128-
host = "https://index.docker.io/v1/"
129-
}
130-
ac, err := a.GetAuthConfig(host)
131-
if err != nil {
132-
return "", "", err
133-
}
134-
if ac.IdentityToken != "" {
135-
return "", ac.IdentityToken, nil
136-
}
137-
return ac.Username, ac.Password, nil
138-
}
139-
}
140-
141-
func RegistryAuthForRef(ref string, a Auth) (string, error) {
142-
if a == nil {
143-
return "", nil
144-
}
145-
r, err := parseRef(ref)
146-
if err != nil {
147-
return "", err
148-
}
149-
host := reference.Domain(r)
150-
if host == "docker.io" {
151-
host = "https://index.docker.io/v1/"
152-
}
153-
ac, err := a.GetAuthConfig(host)
154-
if err != nil {
155-
return "", err
156-
}
157-
buf, err := json.Marshal(ac)
158-
if err != nil {
159-
return "", err
160-
}
161-
return base64.URLEncoding.EncodeToString(buf), nil
162-
}

0 commit comments

Comments
 (0)