-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
token.go
53 lines (45 loc) · 1.28 KB
/
token.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
package registry
import (
"context"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/aquasecurity/trivy/pkg/fanal/image/registry/azure"
"github.com/aquasecurity/trivy/pkg/fanal/image/registry/ecr"
"github.com/aquasecurity/trivy/pkg/fanal/image/registry/google"
"github.com/aquasecurity/trivy/pkg/fanal/log"
"github.com/aquasecurity/trivy/pkg/fanal/types"
)
var (
registries []Registry
)
func init() {
RegisterRegistry(&google.Registry{})
RegisterRegistry(&ecr.ECR{})
RegisterRegistry(&azure.Registry{})
}
type Registry interface {
CheckOptions(domain string, option types.RegistryOptions) error
GetCredential(ctx context.Context) (string, string, error)
}
func RegisterRegistry(registry Registry) {
registries = append(registries, registry)
}
func GetToken(ctx context.Context, domain string, opt types.RegistryOptions) (auth authn.Basic) {
// check registry which particular to get credential
for _, registry := range registries {
err := registry.CheckOptions(domain, opt)
if err != nil {
continue
}
username, password, err := registry.GetCredential(ctx)
if err != nil {
// only skip check registry if error occurred
log.Logger.Debug(err)
break
}
return authn.Basic{
Username: username,
Password: password,
}
}
return authn.Basic{}
}