github.com/devseccon/trivy@v0.47.1-0.20231123133102-bd902a0bd996/pkg/fanal/image/registry/google/google.go (about) 1 package google 2 3 import ( 4 "context" 5 "strings" 6 7 "github.com/GoogleCloudPlatform/docker-credential-gcr/config" 8 "github.com/GoogleCloudPlatform/docker-credential-gcr/credhelper" 9 "github.com/GoogleCloudPlatform/docker-credential-gcr/store" 10 "golang.org/x/xerrors" 11 12 "github.com/devseccon/trivy/pkg/fanal/types" 13 ) 14 15 type Registry struct { 16 Store store.GCRCredStore 17 domain string 18 } 19 20 // Google container registry 21 const gcrURL = "gcr.io" 22 23 // Google artifact registry 24 const garURL = "docker.pkg.dev" 25 26 func (g *Registry) CheckOptions(domain string, option types.RegistryOptions) error { 27 if !strings.HasSuffix(domain, gcrURL) && !strings.HasSuffix(domain, garURL) { 28 return xerrors.Errorf("Google registry: %w", types.InvalidURLPattern) 29 } 30 g.domain = domain 31 if option.GCPCredPath != "" { 32 g.Store = store.NewGCRCredStore(option.GCPCredPath) 33 } 34 return nil 35 } 36 37 func (g *Registry) GetCredential(_ context.Context) (username, password string, err error) { 38 var credStore store.GCRCredStore 39 if g.Store == nil { 40 credStore, err = store.DefaultGCRCredStore() 41 if err != nil { 42 return "", "", xerrors.Errorf("failed to get GCRCredStore: %w", err) 43 } 44 } else { 45 credStore = g.Store 46 } 47 userCfg, err := config.LoadUserConfig() 48 if err != nil { 49 return "", "", xerrors.Errorf("failed to load user config: %w", err) 50 } 51 helper := credhelper.NewGCRCredentialHelper(credStore, userCfg) 52 return helper.Get(g.domain) 53 }