mirror of
https://github.com/aquasecurity/trivy.git
synced 2025-12-20 22:33:53 -08:00
45 lines
1.0 KiB
Go
45 lines
1.0 KiB
Go
package docker
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/docker/docker/api/types"
|
|
"github.com/genuinetools/reg/repoutils"
|
|
)
|
|
|
|
var (
|
|
registries []Registry
|
|
)
|
|
|
|
type Registry interface {
|
|
CheckOptions(domain string, option DockerOption) error
|
|
GetCredential(ctx context.Context) (string, string, error)
|
|
}
|
|
|
|
func RegisterRegistry(registry Registry) {
|
|
registries = append(registries, registry)
|
|
}
|
|
|
|
func GetToken(ctx context.Context, domain string, opt DockerOption) (auth types.AuthConfig, err error) {
|
|
authDomain := opt.AuthURL
|
|
if authDomain == "" {
|
|
authDomain = domain
|
|
}
|
|
auth.ServerAddress = authDomain
|
|
// check registry which particular to get credential
|
|
for _, registry := range registries {
|
|
err := registry.CheckOptions(authDomain, opt)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
auth.Username, auth.Password, err = registry.GetCredential(ctx)
|
|
if err != nil {
|
|
// only skip check registry if error occured
|
|
break
|
|
} else {
|
|
return auth, nil
|
|
}
|
|
}
|
|
return repoutils.GetAuthConfig(opt.UserName, opt.Password, authDomain)
|
|
}
|