Skip to content

Commit

Permalink
Merge pull request #5432 from tonistiigi/otel-auth-update
Browse files Browse the repository at this point in the history
Better OTEL tracing for authprovider
  • Loading branch information
tonistiigi authored Oct 17, 2024
2 parents 4b4a9aa + c4a9c39 commit 2be385c
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions session/auth/authprovider/authprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/moby/buildkit/session"
"github.com/moby/buildkit/session/auth"
"github.com/moby/buildkit/util/progress/progresswriter"
"github.com/moby/buildkit/util/tracing"
"github.com/pkg/errors"
"golang.org/x/crypto/nacl/sign"
"google.golang.org/grpc"
Expand Down Expand Up @@ -73,7 +74,7 @@ func (ap *authProvider) Register(server *grpc.Server) {
}

func (ap *authProvider) FetchToken(ctx context.Context, req *auth.FetchTokenRequest) (rr *auth.FetchTokenResponse, err error) {
ac, err := ap.getAuthConfig(req.Host)
ac, err := ap.getAuthConfig(ctx, req.Host)
if err != nil {
return nil, err
}
Expand All @@ -83,7 +84,7 @@ func (ap *authProvider) FetchToken(ctx context.Context, req *auth.FetchTokenRequ
return toTokenResponse(ac.RegistryToken, time.Time{}, 0), nil
}

creds, err := ap.credentials(req.Host)
creds, err := ap.credentials(ctx, req.Host)
if err != nil {
return nil, err
}
Expand All @@ -96,11 +97,11 @@ func (ap *authProvider) FetchToken(ctx context.Context, req *auth.FetchTokenRequ
Secret: creds.Secret,
}

httpClient := http.DefaultClient()
httpClient := tracing.DefaultClient
if tc, err := ap.tlsConfig(req.Host); err == nil && tc != nil {
transport := http.DefaultTransport()
transport.TLSClientConfig = tc
httpClient.Transport = transport
httpClient.Transport = tracing.NewTransport(transport)
}

if creds.Secret != "" {
Expand Down Expand Up @@ -186,8 +187,8 @@ func (ap *authProvider) tlsConfig(host string) (*tls.Config, error) {
return tc, nil
}

func (ap *authProvider) credentials(host string) (*auth.CredentialsResponse, error) {
ac, err := ap.getAuthConfig(host)
func (ap *authProvider) credentials(ctx context.Context, host string) (*auth.CredentialsResponse, error) {
ac, err := ap.getAuthConfig(ctx, host)
if err != nil {
return nil, err
}
Expand All @@ -202,7 +203,7 @@ func (ap *authProvider) credentials(host string) (*auth.CredentialsResponse, err
}

func (ap *authProvider) Credentials(ctx context.Context, req *auth.CredentialsRequest) (*auth.CredentialsResponse, error) {
resp, err := ap.credentials(req.Host)
resp, err := ap.credentials(ctx, req.Host)
if err != nil || resp.Secret != "" {
ap.mu.Lock()
defer ap.mu.Unlock()
Expand All @@ -218,7 +219,7 @@ func (ap *authProvider) Credentials(ctx context.Context, req *auth.CredentialsRe
}

func (ap *authProvider) GetTokenAuthority(ctx context.Context, req *auth.GetTokenAuthorityRequest) (*auth.GetTokenAuthorityResponse, error) {
key, err := ap.getAuthorityKey(req.Host, req.Salt)
key, err := ap.getAuthorityKey(ctx, req.Host, req.Salt)
if err != nil {
return nil, err
}
Expand All @@ -227,7 +228,7 @@ func (ap *authProvider) GetTokenAuthority(ctx context.Context, req *auth.GetToke
}

func (ap *authProvider) VerifyTokenAuthority(ctx context.Context, req *auth.VerifyTokenAuthorityRequest) (*auth.VerifyTokenAuthorityResponse, error) {
key, err := ap.getAuthorityKey(req.Host, req.Salt)
key, err := ap.getAuthorityKey(ctx, req.Host, req.Salt)
if err != nil {
return nil, err
}
Expand All @@ -238,7 +239,7 @@ func (ap *authProvider) VerifyTokenAuthority(ctx context.Context, req *auth.Veri
return &auth.VerifyTokenAuthorityResponse{Signed: sign.Sign(nil, req.Payload, priv)}, nil
}

func (ap *authProvider) getAuthConfig(host string) (*types.AuthConfig, error) {
func (ap *authProvider) getAuthConfig(ctx context.Context, host string) (*types.AuthConfig, error) {
ap.mu.Lock()
defer ap.mu.Unlock()

Expand All @@ -247,7 +248,9 @@ func (ap *authProvider) getAuthConfig(host string) (*types.AuthConfig, error) {
}

if _, exists := ap.authConfigCache[host]; !exists {
span, _ := tracing.StartSpan(ctx, fmt.Sprintf("load credentials for %s", host))
ac, err := ap.config.GetAuthConfig(host)
tracing.FinishWithError(span, err)
if err != nil {
return nil, err
}
Expand All @@ -257,12 +260,12 @@ func (ap *authProvider) getAuthConfig(host string) (*types.AuthConfig, error) {
return ap.authConfigCache[host], nil
}

func (ap *authProvider) getAuthorityKey(host string, salt []byte) (ed25519.PrivateKey, error) {
func (ap *authProvider) getAuthorityKey(ctx context.Context, host string, salt []byte) (ed25519.PrivateKey, error) {
if v, err := strconv.ParseBool(os.Getenv("BUILDKIT_NO_CLIENT_TOKEN")); err == nil && v {
return nil, status.Errorf(codes.Unavailable, "client side tokens disabled")
}

creds, err := ap.credentials(host)
creds, err := ap.credentials(ctx, host)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 2be385c

Please sign in to comment.