imagetools: move auth function to separate file
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
This commit is contained in:
@@ -0,0 +1,47 @@
|
|||||||
|
package imagetools
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/base64"
|
||||||
|
"encoding/json"
|
||||||
|
|
||||||
|
"github.com/distribution/reference"
|
||||||
|
)
|
||||||
|
|
||||||
|
func toCredentialsFunc(a Auth) func(string) (string, string, error) {
|
||||||
|
return func(host string) (string, string, error) {
|
||||||
|
if host == "registry-1.docker.io" {
|
||||||
|
host = "https://index.docker.io/v1/"
|
||||||
|
}
|
||||||
|
ac, err := a.GetAuthConfig(host)
|
||||||
|
if err != nil {
|
||||||
|
return "", "", err
|
||||||
|
}
|
||||||
|
if ac.IdentityToken != "" {
|
||||||
|
return "", ac.IdentityToken, nil
|
||||||
|
}
|
||||||
|
return ac.Username, ac.Password, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func RegistryAuthForRef(ref string, a Auth) (string, error) {
|
||||||
|
if a == nil {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
r, err := parseRef(ref)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
host := reference.Domain(r)
|
||||||
|
if host == "docker.io" {
|
||||||
|
host = "https://index.docker.io/v1/"
|
||||||
|
}
|
||||||
|
ac, err := a.GetAuthConfig(host)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
buf, err := json.Marshal(ac)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return base64.URLEncoding.EncodeToString(buf), nil
|
||||||
|
}
|
||||||
@@ -3,8 +3,6 @@ package imagetools
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"encoding/base64"
|
|
||||||
"encoding/json"
|
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
@@ -121,42 +119,3 @@ func parseRef(s string) (reference.Named, error) {
|
|||||||
ref = reference.TagNameOnly(ref)
|
ref = reference.TagNameOnly(ref)
|
||||||
return ref, nil
|
return ref, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func toCredentialsFunc(a Auth) func(string) (string, string, error) {
|
|
||||||
return func(host string) (string, string, error) {
|
|
||||||
if host == "registry-1.docker.io" {
|
|
||||||
host = "https://index.docker.io/v1/"
|
|
||||||
}
|
|
||||||
ac, err := a.GetAuthConfig(host)
|
|
||||||
if err != nil {
|
|
||||||
return "", "", err
|
|
||||||
}
|
|
||||||
if ac.IdentityToken != "" {
|
|
||||||
return "", ac.IdentityToken, nil
|
|
||||||
}
|
|
||||||
return ac.Username, ac.Password, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func RegistryAuthForRef(ref string, a Auth) (string, error) {
|
|
||||||
if a == nil {
|
|
||||||
return "", nil
|
|
||||||
}
|
|
||||||
r, err := parseRef(ref)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
host := reference.Domain(r)
|
|
||||||
if host == "docker.io" {
|
|
||||||
host = "https://index.docker.io/v1/"
|
|
||||||
}
|
|
||||||
ac, err := a.GetAuthConfig(host)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
buf, err := json.Marshal(ac)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
return base64.URLEncoding.EncodeToString(buf), nil
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user