-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulti_auth.go
45 lines (38 loc) · 1.03 KB
/
multi_auth.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
package imageconfig
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ecr"
"github.com/aws/aws-sdk-go/service/ecr/ecriface"
"github.com/containers/image/docker/reference"
"net/http"
"regexp"
)
type MultiAuth struct {
Options []AuthOption
}
type AuthOption struct {
Authenticator
Regexp *regexp.Regexp
}
func (a *MultiAuth) Authenticate(req *http.Request, name reference.Named) error {
for _, opt := range a.Options {
if opt.Regexp.MatchString(req.Host) {
return opt.Authenticate(req, name)
}
}
return nil
}
var DefaultAuthenticator Authenticator
func init() {
DefaultAuthenticator = &MultiAuth{
Options: []AuthOption{
{NewDockerHubAuthenticator(), regexp.MustCompile("^registry-1.docker.io$")},
{&AwsEcrAuthenticator{Api: func(region string) ecriface.ECRAPI {
config := aws.NewConfig().WithRegion(region)
sess := session.Must(session.NewSession(config))
return ecr.New(sess)
}}, regexp.MustCompile("^\\.amazonaws.com(?:\\.cn)$")},
},
}
}