2018-04-18 19:36:26 -04:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-11-17 08:19:27 -05:00
|
|
|
"crypto/subtle"
|
|
|
|
"math/rand"
|
|
|
|
"sync"
|
2018-04-18 19:36:26 -04:00
|
|
|
|
|
|
|
"github.com/moby/buildkit/session"
|
2020-07-20 09:49:37 -04:00
|
|
|
"github.com/moby/buildkit/util/grpcerrors"
|
2020-11-17 08:19:27 -05:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"golang.org/x/crypto/nacl/sign"
|
2018-04-18 19:36:26 -04:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
)
|
|
|
|
|
2020-11-17 08:19:27 -05:00
|
|
|
var salt []byte
|
|
|
|
var saltOnce sync.Once
|
|
|
|
|
|
|
|
// getSalt returns unique component per daemon restart to avoid persistent keys
|
|
|
|
func getSalt() []byte {
|
|
|
|
saltOnce.Do(func() {
|
|
|
|
salt = make([]byte, 32)
|
|
|
|
rand.Read(salt)
|
|
|
|
})
|
|
|
|
return salt
|
|
|
|
}
|
|
|
|
|
|
|
|
func CredentialsFunc(sm *session.Manager, g session.Group) func(string) (session, username, secret string, err error) {
|
|
|
|
return func(host string) (string, string, string, error) {
|
|
|
|
var sessionID, user, secret string
|
|
|
|
err := sm.Any(context.TODO(), g, func(ctx context.Context, id string, c session.Caller) error {
|
2020-07-28 11:08:10 -04:00
|
|
|
client := NewAuthClient(c.Conn())
|
2018-04-18 19:36:26 -04:00
|
|
|
|
2020-07-28 11:08:10 -04:00
|
|
|
resp, err := client.Credentials(ctx, &CredentialsRequest{
|
|
|
|
Host: host,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
if grpcerrors.Code(err) == codes.Unimplemented {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2020-11-17 08:19:27 -05:00
|
|
|
sessionID = id
|
2020-07-28 11:08:10 -04:00
|
|
|
user = resp.Username
|
|
|
|
secret = resp.Secret
|
|
|
|
return nil
|
2018-04-18 19:36:26 -04:00
|
|
|
})
|
|
|
|
if err != nil {
|
2020-11-17 08:19:27 -05:00
|
|
|
return "", "", "", err
|
|
|
|
}
|
|
|
|
return sessionID, user, secret, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-22 04:17:41 -04:00
|
|
|
func FetchToken(ctx context.Context, req *FetchTokenRequest, sm *session.Manager, g session.Group) (resp *FetchTokenResponse, err error) {
|
|
|
|
err = sm.Any(ctx, g, func(ctx context.Context, id string, c session.Caller) error {
|
2020-11-17 08:19:27 -05:00
|
|
|
client := NewAuthClient(c.Conn())
|
|
|
|
|
|
|
|
resp, err = client.FetchToken(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2021-06-22 04:17:41 -04:00
|
|
|
func VerifyTokenAuthority(ctx context.Context, host string, pubKey *[32]byte, sm *session.Manager, g session.Group) (sessionID string, ok bool, err error) {
|
2020-11-17 08:19:27 -05:00
|
|
|
var verified bool
|
2021-06-22 04:17:41 -04:00
|
|
|
err = sm.Any(ctx, g, func(ctx context.Context, id string, c session.Caller) error {
|
2020-11-17 08:19:27 -05:00
|
|
|
client := NewAuthClient(c.Conn())
|
|
|
|
|
|
|
|
payload := make([]byte, 32)
|
|
|
|
rand.Read(payload)
|
|
|
|
resp, err := client.VerifyTokenAuthority(ctx, &VerifyTokenAuthorityRequest{
|
|
|
|
Host: host,
|
|
|
|
Salt: getSalt(),
|
|
|
|
Payload: payload,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
if grpcerrors.Code(err) == codes.Unimplemented {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var dt []byte
|
|
|
|
dt, ok = sign.Open(nil, resp.Signed, pubKey)
|
|
|
|
if ok && subtle.ConstantTimeCompare(dt, payload) == 1 {
|
|
|
|
verified = true
|
|
|
|
}
|
|
|
|
sessionID = id
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "", false, err
|
|
|
|
}
|
|
|
|
return sessionID, verified, nil
|
|
|
|
}
|
|
|
|
|
2021-06-22 04:17:41 -04:00
|
|
|
func GetTokenAuthority(ctx context.Context, host string, sm *session.Manager, g session.Group) (sessionID string, pubKey *[32]byte, err error) {
|
|
|
|
err = sm.Any(ctx, g, func(ctx context.Context, id string, c session.Caller) error {
|
2020-11-17 08:19:27 -05:00
|
|
|
client := NewAuthClient(c.Conn())
|
|
|
|
|
|
|
|
resp, err := client.GetTokenAuthority(ctx, &GetTokenAuthorityRequest{
|
|
|
|
Host: host,
|
|
|
|
Salt: getSalt(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
if grpcerrors.Code(err) == codes.Unimplemented || grpcerrors.Code(err) == codes.Unavailable {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(resp.PublicKey) != 32 {
|
|
|
|
return errors.Errorf("invalid pubkey length %d", len(pubKey))
|
2018-04-18 19:36:26 -04:00
|
|
|
}
|
2020-11-17 08:19:27 -05:00
|
|
|
|
|
|
|
sessionID = id
|
|
|
|
pubKey = new([32]byte)
|
|
|
|
copy((*pubKey)[:], resp.PublicKey)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
2018-04-18 19:36:26 -04:00
|
|
|
}
|
2020-11-17 08:19:27 -05:00
|
|
|
return sessionID, pubKey, nil
|
2018-04-18 19:36:26 -04:00
|
|
|
}
|