Debian Codegraph @

service/keychain/dockerconfig/dockerconfig.go

raw
1 /*
2 Copyright The containerd Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package dockerconfig
18
19 import (
20 "context"
21
22 "github.com/containerd/containerd/log"
23 "github.com/containerd/containerd/reference"
24 "github.com/containerd/stargz-snapshotter/service/resolver"
25 "github.com/docker/cli/cli/config"
26 )
27
28 func NewDockerconfigKeychain(ctx context.Context) resolver.Credential {
29 return func(host string, refspec reference.Spec) (string, string, error) {
30 cf, err := config.Load("")
31 if err != nil {
32 log.G(ctx).WithError(err).Warnf("failed to load docker config file")
33 return "", "", nil
34 }
35
36 if host == "docker.io" || host == "registry-1.docker.io" {
37 // Creds of docker.io is stored keyed by "https://index.docker.io/v1/".
38 host = "https://index.docker.io/v1/"
39 }
40 ac, err := cf.GetAuthConfig(host)
41 if err != nil {
42 return "", "", err
43 }
44 if ac.IdentityToken != "" {
45 return "", ac.IdentityToken, nil
46 }
47 return ac.Username, ac.Password, nil
48 }
49 }