2021-05-26 01:30:49 +02:00
|
|
|
package solver
|
2021-04-27 02:39:19 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
bkauth "github.com/moby/buildkit/session/auth"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
)
|
|
|
|
|
2021-05-26 01:30:49 +02:00
|
|
|
// RegistryAuthProvider is a buildkit provider for registry authentication
|
2021-04-27 02:39:19 +02:00
|
|
|
// Adapted from: https://github.com/moby/buildkit/blob/master/session/auth/authprovider/authprovider.go
|
2021-05-26 01:30:49 +02:00
|
|
|
type RegistryAuthProvider struct {
|
2021-04-27 02:39:19 +02:00
|
|
|
credentials map[string]*bkauth.CredentialsResponse
|
|
|
|
m sync.RWMutex
|
|
|
|
}
|
|
|
|
|
2021-05-26 01:30:49 +02:00
|
|
|
func NewRegistryAuthProvider() *RegistryAuthProvider {
|
|
|
|
return &RegistryAuthProvider{
|
2021-04-27 02:39:19 +02:00
|
|
|
credentials: map[string]*bkauth.CredentialsResponse{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-26 01:30:49 +02:00
|
|
|
func (a *RegistryAuthProvider) AddCredentials(target, username, secret string) {
|
2021-04-27 02:39:19 +02:00
|
|
|
a.m.Lock()
|
|
|
|
defer a.m.Unlock()
|
|
|
|
|
|
|
|
a.credentials[target] = &bkauth.CredentialsResponse{
|
|
|
|
Username: username,
|
|
|
|
Secret: secret,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-26 01:30:49 +02:00
|
|
|
func (a *RegistryAuthProvider) Register(server *grpc.Server) {
|
2021-04-27 02:39:19 +02:00
|
|
|
bkauth.RegisterAuthServer(server, a)
|
|
|
|
}
|
|
|
|
|
2021-05-26 01:30:49 +02:00
|
|
|
func (a *RegistryAuthProvider) Credentials(ctx context.Context, req *bkauth.CredentialsRequest) (*bkauth.CredentialsResponse, error) {
|
2021-04-27 02:39:19 +02:00
|
|
|
reqURL, err := parseAuthHost(req.Host)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
a.m.RLock()
|
|
|
|
defer a.m.RUnlock()
|
|
|
|
|
|
|
|
for authHost, auth := range a.credentials {
|
|
|
|
u, err := parseAuthHost(authHost)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.Host == reqURL.Host {
|
|
|
|
return auth, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &bkauth.CredentialsResponse{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseAuthHost(host string) (*url.URL, error) {
|
|
|
|
if host == "registry-1.docker.io" {
|
|
|
|
host = "https://index.docker.io/v1/"
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasPrefix(host, "http://") && !strings.HasPrefix(host, "https://") {
|
|
|
|
host = "https://" + host
|
|
|
|
}
|
|
|
|
return url.Parse(host)
|
|
|
|
}
|
|
|
|
|
2021-05-26 01:30:49 +02:00
|
|
|
func (a *RegistryAuthProvider) FetchToken(ctx context.Context, req *bkauth.FetchTokenRequest) (rr *bkauth.FetchTokenResponse, err error) {
|
2021-04-27 02:39:19 +02:00
|
|
|
return nil, status.Errorf(codes.Unavailable, "client side tokens not implemented")
|
|
|
|
}
|
|
|
|
|
2021-05-26 01:30:49 +02:00
|
|
|
func (a *RegistryAuthProvider) GetTokenAuthority(ctx context.Context, req *bkauth.GetTokenAuthorityRequest) (*bkauth.GetTokenAuthorityResponse, error) {
|
2021-04-27 02:39:19 +02:00
|
|
|
return nil, status.Errorf(codes.Unavailable, "client side tokens not implemented")
|
|
|
|
}
|
|
|
|
|
2021-05-26 01:30:49 +02:00
|
|
|
func (a *RegistryAuthProvider) VerifyTokenAuthority(ctx context.Context, req *bkauth.VerifyTokenAuthorityRequest) (*bkauth.VerifyTokenAuthorityResponse, error) {
|
2021-04-27 02:39:19 +02:00
|
|
|
return nil, status.Errorf(codes.Unavailable, "client side tokens not implemented")
|
|
|
|
}
|