Files
trivy/extractor/docker/token/gcr/gcr.go
Teppei Fukuda d5d2729237 Add error wrap (fanal#35)
* Add error wrap

* Fix bug

* Fix typo
2019-09-25 12:57:09 +03:00

51 lines
1.2 KiB
Go

package gcr
import (
"context"
"strings"
"github.com/aquasecurity/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 GCR struct {
Store store.GCRCredStore
domain string
}
const gcrURL = "gcr.io"
func (g *GCR) CheckOptions(domain string, d types.DockerOption) error {
if !strings.HasSuffix(domain, gcrURL) {
return xerrors.Errorf("GCR : %w", types.InvalidURLPattern)
}
g.domain = domain
if d.GcpCredPath != "" {
g.Store = store.NewGCRCredStore(d.GcpCredPath)
}
return nil
}
func (g *GCR) 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)
}