2018-04-18 19:36:26 -04:00
|
|
|
package authprovider
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-05-13 21:24:27 -04:00
|
|
|
"io"
|
2019-05-09 18:06:10 -04:00
|
|
|
"sync"
|
2018-04-18 19:36:26 -04:00
|
|
|
|
|
|
|
"github.com/docker/cli/cli/config"
|
|
|
|
"github.com/docker/cli/cli/config/configfile"
|
|
|
|
"github.com/moby/buildkit/session"
|
|
|
|
"github.com/moby/buildkit/session/auth"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
)
|
|
|
|
|
2019-05-13 21:24:27 -04:00
|
|
|
func NewDockerAuthProvider(stderr io.Writer) session.Attachable {
|
2018-04-18 19:36:26 -04:00
|
|
|
return &authProvider{
|
2019-05-13 21:24:27 -04:00
|
|
|
config: config.LoadDefaultConfigFile(stderr),
|
2018-04-18 19:36:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type authProvider struct {
|
|
|
|
config *configfile.ConfigFile
|
2019-05-09 18:06:10 -04:00
|
|
|
|
|
|
|
// The need for this mutex is not well understood.
|
|
|
|
// Without it, the docker cli on OS X hangs when
|
|
|
|
// reading credentials from docker-credential-osxkeychain.
|
|
|
|
// See issue https://github.com/docker/cli/issues/1862
|
|
|
|
mu sync.Mutex
|
2018-04-18 19:36:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ap *authProvider) Register(server *grpc.Server) {
|
|
|
|
auth.RegisterAuthServer(server, ap)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ap *authProvider) Credentials(ctx context.Context, req *auth.CredentialsRequest) (*auth.CredentialsResponse, error) {
|
2019-05-09 18:06:10 -04:00
|
|
|
ap.mu.Lock()
|
|
|
|
defer ap.mu.Unlock()
|
2018-04-18 19:36:26 -04:00
|
|
|
if req.Host == "registry-1.docker.io" {
|
|
|
|
req.Host = "https://index.docker.io/v1/"
|
|
|
|
}
|
|
|
|
ac, err := ap.config.GetAuthConfig(req.Host)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res := &auth.CredentialsResponse{}
|
|
|
|
if ac.IdentityToken != "" {
|
|
|
|
res.Secret = ac.IdentityToken
|
|
|
|
} else {
|
|
|
|
res.Username = ac.Username
|
|
|
|
res.Secret = ac.Password
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|