This repository has been archived by the owner on Aug 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from dweomer/lift-auth-from-cri
lift auth parsing from containerd/cri
- Loading branch information
Showing
2 changed files
with
52 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package auth | ||
|
||
import ( | ||
"encoding/base64" | ||
"net/url" | ||
"strings" | ||
|
||
"github.com/pkg/errors" | ||
criv1 "k8s.io/cri-api/pkg/apis/runtime/v1alpha2" | ||
) | ||
|
||
// Parse parses AuthConfig and returns username and password/secret required by containerd. | ||
func Parse(auth *criv1.AuthConfig, host string) (string, string, error) { | ||
if auth == nil { | ||
return "", "", nil | ||
} | ||
if auth.ServerAddress != "" { | ||
// Do not return the auth info when server address doesn't match. | ||
u, err := url.Parse(auth.ServerAddress) | ||
if err != nil { | ||
return "", "", errors.Wrap(err, "parse server address") | ||
} | ||
if host != u.Host { | ||
return "", "", nil | ||
} | ||
} | ||
if auth.Username != "" { | ||
return auth.Username, auth.Password, nil | ||
} | ||
if auth.IdentityToken != "" { | ||
return "", auth.IdentityToken, nil | ||
} | ||
if auth.Auth != "" { | ||
decLen := base64.StdEncoding.DecodedLen(len(auth.Auth)) | ||
decoded := make([]byte, decLen) | ||
_, err := base64.StdEncoding.Decode(decoded, []byte(auth.Auth)) | ||
if err != nil { | ||
return "", "", err | ||
} | ||
fields := strings.SplitN(string(decoded), ":", 2) | ||
if len(fields) != 2 { | ||
return "", "", errors.Errorf("invalid decoded auth: %q", decoded) | ||
} | ||
user, passwd := fields[0], fields[1] | ||
return user, strings.Trim(passwd, "\x00"), nil | ||
} | ||
// TODO(random-liu): Support RegistryToken. | ||
// An empty auth config is valid for anonymous registry | ||
return "", "", nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters