-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathgoogle.go
54 lines (45 loc) · 1.37 KB
/
google.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package google
import (
"context"
"strings"
"github.com/aquasecurity/trivy/pkg/fanal/types"
"golang.org/x/xerrors"
"github.com/GoogleCloudPlatform/docker-credential-gcr/config"
"github.com/GoogleCloudPlatform/docker-credential-gcr/credhelper"
"github.com/GoogleCloudPlatform/docker-credential-gcr/store"
)
type Registry struct {
Store store.GCRCredStore
domain string
}
// Google container registry
const gcrURL = "gcr.io"
// Google artifact registry
const garURL = "docker.pkg.dev"
func (g *Registry) CheckOptions(domain string, d types.DockerOption) error {
if !strings.HasSuffix(domain, gcrURL) && !strings.HasSuffix(domain, garURL) {
return xerrors.Errorf("Google registry: %w", types.InvalidURLPattern)
}
g.domain = domain
if d.GcpCredPath != "" {
g.Store = store.NewGCRCredStore(d.GcpCredPath)
}
return nil
}
func (g *Registry) GetCredential(ctx context.Context) (username, password string, err error) {
var credStore store.GCRCredStore
if g.Store == nil {
credStore, err = store.DefaultGCRCredStore()
if err != nil {
return "", "", xerrors.Errorf("failed to get GCRCredStore: %w", err)
}
} else {
credStore = g.Store
}
userCfg, err := config.LoadUserConfig()
if err != nil {
return "", "", xerrors.Errorf("failed to load user config: %w", err)
}
helper := credhelper.NewGCRCredentialHelper(credStore, userCfg)
return helper.Get(g.domain)
}