Skip to content

Commit

Permalink
debug checkServiceUpstreamAuth span
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeynabRezaei committed Mar 17, 2024
1 parent 0dd4d61 commit a6fcf37
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
17 changes: 14 additions & 3 deletions pkg/auth/authenticator.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,10 +336,21 @@ func (a *Authenticator) checkServiceUpstreamAuth(service WebservicesCacheEntry,
attribute.String("upstream-http-request-start", reqStart.Format(tracing.TimeFormat)),
attribute.String("upstream-http-request-end", time.Now().Format(tracing.TimeFormat)),
attribute.Float64("upstream-http-request-rtt-seconds", time.Since(reqStart).Seconds()),
attribute.Int("upstream-auth-status-code", resp.StatusCode),
)
labels := AddWithDownstreamDeadlineLabel(AddStatusLabel(nil, resp.StatusCode), hasDownstreamDeadline)
upstreamAuthRequestDuration.With(labels).Observe(reqDuration.Seconds())

if err != nil {
span.RecordError(err)
span.SetStatus(otelcodes.Error, err.Error())
return CerberusReasonUpstreamAuthFailed
}

if resp != nil {
span.SetAttributes(attribute.Int("upstream-auth-status-code", resp.StatusCode))
labels := AddWithDownstreamDeadlineLabel(AddStatusLabel(nil, resp.StatusCode), hasDownstreamDeadline)
upstreamAuthRequestDuration.With(labels).Observe(reqDuration.Seconds())
} else {
span.SetStatus(otelcodes.Error, "Unexpected nil response")
}

if reason := processResponseError(err); reason != "" {
span.RecordError(err)
Expand Down
11 changes: 7 additions & 4 deletions pkg/auth/authenticator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1230,8 +1230,11 @@ func TestCheck_UpstreamAuthTimeout(t *testing.T) {
mockHTTPClient := &http.Client{
Transport: &MockTransport{
DoFunc: func(req *http.Request) (*http.Response, error) {
// Return a simulated timeout error
return nil, &url.Error{
return &http.Response{
StatusCode: http.StatusInternalServerError,
Body: io.NopCloser(strings.NewReader("Internal Server Error")),
Header: make(http.Header),
}, &url.Error{
Op: "Get",
URL: "http://fake-upstream-service/authenticate",
Err: errors.New("timeout"),
Expand Down Expand Up @@ -1277,6 +1280,6 @@ func TestCheck_UpstreamAuthTimeout(t *testing.T) {

assert.NoError(t, err, "Expected no error from Check function itself")
assert.NotNil(t, finalResponse, "Expected a non-nil response")
assert.False(t, finalResponse.Allow, "Expected the request to be denied due to upstream authentication timeout")
assert.Equal(t, CerberusReasonUpstreamAuthTimeout, finalResponse.Response.Header.Get("X-Cerberus-Reason"), "Expected reason to indicate upstream authentication timeout")
assert.False(t, finalResponse.Allow, "Expected the request to be denied due to upstream authentication failed")
assert.Equal(t, "upstream-auth-failed", finalResponse.Response.Header.Get("X-Cerberus-Reason"), "Expected reason to indicate upstream authentication failed")
}

0 comments on commit a6fcf37

Please sign in to comment.