2021-05-26 01:30:49 +02:00
|
|
|
package solver
|
2021-04-27 02:39:19 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
2021-06-24 11:42:34 +02:00
|
|
|
"github.com/docker/distribution/reference"
|
2021-04-27 02:39:19 +02:00
|
|
|
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-07-01 14:08:49 +02:00
|
|
|
host := req.Host
|
|
|
|
if host == "registry-1.docker.io" {
|
|
|
|
host = "docker.io"
|
2021-04-27 02:39:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
a.m.RLock()
|
|
|
|
defer a.m.RUnlock()
|
|
|
|
|
|
|
|
for authHost, auth := range a.credentials {
|
|
|
|
u, err := parseAuthHost(authHost)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-07-01 14:08:49 +02:00
|
|
|
if u == host {
|
2021-04-27 02:39:19 +02:00
|
|
|
return auth, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &bkauth.CredentialsResponse{}, nil
|
|
|
|
}
|
|
|
|
|
2021-07-01 14:08:49 +02:00
|
|
|
func parseAuthHost(host string) (string, error) {
|
2021-06-30 18:27:26 +02:00
|
|
|
host = strings.TrimPrefix(host, "http://")
|
|
|
|
host = strings.TrimPrefix(host, "https://")
|
2021-06-18 22:01:16 +02:00
|
|
|
|
2021-06-30 18:27:26 +02:00
|
|
|
ref, err := reference.ParseNormalizedNamed(host)
|
2021-04-27 02:39:19 +02:00
|
|
|
|
2021-07-01 14:08:49 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2021-04-27 02:39:19 +02:00
|
|
|
}
|
2021-07-01 14:08:49 +02:00
|
|
|
return reference.Domain(ref), nil
|
2021-04-27 02:39:19 +02:00
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
}
|