Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add auth token propagation for metrics reader #3341

Merged
merged 17 commits into from
Nov 2, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion plugin/metrics/prometheus/metricsstore/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/jaegertracing/jaeger/plugin/metrics/prometheus/metricsstore/dbmodel"
"github.com/jaegertracing/jaeger/proto-gen/api_v2/metrics"
"github.com/jaegertracing/jaeger/storage/metricsstore"
"github.com/jaegertracing/jaeger/storage/spanstore"
)

const (
Expand Down Expand Up @@ -253,13 +254,30 @@ func getHTTPRoundTripper(c *config.Configuration, logger *zap.Logger) (rt http.R

// KeepAlive and TLSHandshake timeouts are kept to existing Prometheus client's
// DefaultRoundTripper to simplify user configuration and may be made configurable when required.
return &http.Transport{
httpTransport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: c.ConnectTimeout,
KeepAlive: 30 * time.Second,
}).DialContext,
TLSHandshakeTimeout: 10 * time.Second,
TLSClientConfig: ctlsConfig,
}
return &tokenAuthTransport{
wrapped: httpTransport,
}, nil
}

// tokenAuthTransport wraps an instance of http.Transport for the purpose of
// propagating an Authorization token from inbound to outbound HTTP requests.
type tokenAuthTransport struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: could we take this opportunity and refactor/cleanup bearer token functionality into a standalone package? We have ./cmd/query/app/token_propagation_handler.go and ./storage/spanstore/token_propagation.go, which should really be in a single package like ./pkg/bearertoken.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in d1c96ad.

wrapped *http.Transport
}

// RoundTrip implements the http.RoundTripper interface, injecting the outbound
// Authorization header with the token provided in the inbound request.
func (tr *tokenAuthTransport) RoundTrip(r *http.Request) (*http.Response, error) {
headerToken, _ := spanstore.GetBearerToken(r.Context())
r.Header.Set("Authorization", "Bearer "+headerToken)
return tr.wrapped.RoundTrip(r)
}
28 changes: 25 additions & 3 deletions plugin/metrics/prometheus/metricsstore/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/jaegertracing/jaeger/pkg/prometheus/config"
"github.com/jaegertracing/jaeger/proto-gen/api_v2/metrics"
"github.com/jaegertracing/jaeger/storage/metricsstore"
"github.com/jaegertracing/jaeger/storage/spanstore"
)

type (
Expand Down Expand Up @@ -331,12 +332,33 @@ func TestGetRoundTripper(t *testing.T) {
},
}, logger)
require.NoError(t, err)
assert.IsType(t, &http.Transport{}, rt)
assert.IsType(t, &tokenAuthTransport{}, rt)
if tc.tlsEnabled {
assert.NotNil(t, rt.(*http.Transport).TLSClientConfig)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving the common transport into the bearertoken package meant that we can't inspect the wrapped http.Transport.
The original test didn't feel quite right in the first place when it had to make type assertions.

Suggestions also welcome.

assert.NotNil(t, rt.(*tokenAuthTransport).wrapped.TLSClientConfig)
} else {
assert.Nil(t, rt.(*http.Transport).TLSClientConfig)
assert.Nil(t, rt.(*tokenAuthTransport).wrapped.TLSClientConfig)
}

server := httptest.NewServer(
http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "Bearer foo", r.Header.Get("Authorization"))
},
),
)
defer server.Close()

req, err := http.NewRequestWithContext(
spanstore.ContextWithBearerToken(context.Background(), "foo"),
http.MethodGet,
server.URL,
nil,
)
require.NoError(t, err)

resp, err := rt.RoundTrip(req)
require.NoError(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
})
}
}
Expand Down