github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/pkg/client/docker/auth/auth.go (about) 1 // Copyright © 2022 Alibaba Group Holding Ltd. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package auth 16 17 import ( 18 "bytes" 19 "encoding/base64" 20 "encoding/json" 21 "fmt" 22 "os" 23 "path/filepath" 24 25 osi "github.com/sealerio/sealer/utils/os" 26 27 "github.com/docker/docker/api/types" 28 29 "github.com/sealerio/sealer/common" 30 ) 31 32 type Item struct { 33 Auth string `json:"auth"` 34 } 35 36 type DockerAuth struct { 37 Auths map[string]Item `json:"auths"` 38 } 39 40 func (d *DockerAuth) Get(domain string) (string, string, error) { 41 auth := d.Auths[domain].Auth 42 if auth == "" { 43 return "", "", fmt.Errorf("auth for %s doesn't exist", domain) 44 } 45 46 decode, err := base64.StdEncoding.DecodeString(auth) 47 if err != nil { 48 return "", "", err 49 } 50 i := bytes.IndexRune(decode, ':') 51 52 if i == -1 { 53 return "", "", fmt.Errorf("auth base64 has problem of format") 54 } 55 56 return string(decode[:i]), string(decode[i+1:]), nil 57 } 58 59 type DockerAuthService struct { 60 FilePath string 61 AuthContent DockerAuth `json:"auths"` 62 } 63 64 func (s *DockerAuthService) GetAuthByDomain(domain string) (types.AuthConfig, error) { 65 defaultAuthConfig := types.AuthConfig{ServerAddress: domain} 66 67 user, passwd, err := s.AuthContent.Get(domain) 68 if err != nil { 69 return defaultAuthConfig, err 70 } 71 72 return types.AuthConfig{ 73 Username: user, 74 Password: passwd, 75 ServerAddress: domain, 76 }, nil 77 } 78 79 func NewDockerAuthService() (DockerAuthService, error) { 80 var ( 81 authFile = common.DefaultRegistryAuthConfigDir() 82 ac = DockerAuth{Auths: map[string]Item{}} 83 das = DockerAuthService{FilePath: authFile, AuthContent: ac} 84 ) 85 86 if !osi.IsFileExist(authFile) { 87 return das, nil 88 } 89 90 content, err := os.ReadFile(filepath.Clean(authFile)) 91 if err != nil { 92 return das, err 93 } 94 95 err = json.Unmarshal(content, &ac) 96 if err != nil { 97 return das, err 98 } 99 das.AuthContent = ac 100 return das, nil 101 }