Commit 72395351 authored by ale's avatar ale

Upgrade id/auth (tracing)

parent a1bb2db9
Pipeline #5935 passed with stages
in 3 minutes and 12 seconds
......@@ -6,6 +6,7 @@ import (
"net/textproto"
"github.com/cenkalti/backoff"
"go.opencensus.io/trace"
"git.autistici.org/id/auth"
)
......@@ -29,19 +30,31 @@ func New(socketPath string) Client {
}
func (c *socketClient) Authenticate(ctx context.Context, req *auth.Request) (*auth.Response, error) {
// Create a tracing span for the authentication request.
sctx, span := trace.StartSpan(ctx, "auth-server.Authenticate",
trace.WithSpanKind(trace.SpanKindClient))
defer span.End()
span.AddAttributes(
trace.StringAttribute("auth.service", req.Service),
trace.StringAttribute("auth.username", req.Username),
)
// Retry the request, with backoff, if we get a temporary
// network error.
var resp *auth.Response
err := backoff.Retry(func() error {
var err error
resp, err = c.doAuthenticate(ctx, req)
resp, err = c.doAuthenticate(sctx, req)
if err == nil {
return nil
} else if netErr, ok := err.(net.Error); ok && netErr.Temporary() {
return netErr
}
return backoff.Permanent(err)
}, backoff.WithContext(backoff.NewExponentialBackOff(), ctx))
}, backoff.WithContext(backoff.NewExponentialBackOff(), sctx))
span.SetStatus(responseToTraceStatus(resp, err))
return resp, err
}
......@@ -93,3 +106,23 @@ func (c *socketClient) doAuthenticate(ctx context.Context, req *auth.Request) (*
return nil, ctx.Err()
}
}
func responseToTraceStatus(resp *auth.Response, err error) trace.Status {
switch err {
case nil:
switch resp.Status {
case auth.StatusOK:
return trace.Status{Code: trace.StatusCodeOK, Message: "OK"}
case auth.StatusInsufficientCredentials:
return trace.Status{Code: trace.StatusCodePermissionDenied, Message: "Insufficient Credentials"}
default:
return trace.Status{Code: trace.StatusCodePermissionDenied, Message: "Authentication Failure"}
}
case context.Canceled:
return trace.Status{Code: trace.StatusCodeCancelled, Message: "CANCELED"}
case context.DeadlineExceeded:
return trace.Status{Code: trace.StatusCodeDeadlineExceeded, Message: "DEADLINE_EXCEEDED"}
default:
return trace.Status{Code: trace.StatusCodeUnknown, Message: err.Error()}
}
}
......@@ -33,10 +33,10 @@
"revisionTime": "2019-10-24T15:02:31Z"
},
{
"checksumSHA1": "3xM1BQ7kVyqn74GQz07uCBSNh2E=",
"checksumSHA1": "bcwQ+zvfpEIZtOKPVXeaG1XyhfI=",
"path": "git.autistici.org/id/auth/client",
"revision": "ffc5d8791fd81d28fb2b0bce4540a10426a25124",
"revisionTime": "2019-10-24T15:02:31Z"
"revision": "3d44524ae2e5a232f4620329a68fb00d3048953a",
"revisionTime": "2020-02-12T08:17:28Z"
},
{
"checksumSHA1": "MlpsZgRytv/c9IX9YawRJDN/ibQ=",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment