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 authentication #38

Merged
merged 10 commits into from
Apr 7, 2022
Merged

Add authentication #38

merged 10 commits into from
Apr 7, 2022

Conversation

carrieedwards
Copy link
Contributor

This PR replaces the fakeAuth middleware with middleware.Authenticate user, so that authentication can be done. The enableAuth flag is still available.

@grafanabot

This comment has been minimized.

ywwg
ywwg previously approved these changes Mar 31, 2022
Copy link
Contributor

@ywwg ywwg left a comment

Choose a reason for hiding this comment

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

lgtm, one suggestion

cmd/influx2cortex/main.go Outdated Show resolved Hide resolved
pkg/influx/api.go Outdated Show resolved Hide resolved
@grafanabot

This comment has been minimized.

@@ -27,8 +27,15 @@ type API struct {
recorder Recorder
}

func (a *API) Register(server *server.Server, authMiddleware middleware.Interface) {
server.HTTP.Handle("/api/v1/push/influx/write", authMiddleware.Wrap(http.HandlerFunc(a.handleSeriesPush)))
func (a *API) Register(server *server.Server, enableAuth bool) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you add tests to check enabling and disabling auth work as expected?

Copy link
Contributor

Choose a reason for hiding this comment

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

Since this is a small change that will unblock other work, we think it's ok to merge this now and add the test in a later PR (since it will require some refactoring to create the foundation necessary to do so)

Comment on lines 34 to 35
httpAuthMiddleware := middleware.AuthenticateUser
httpAuthMiddleware.Wrap(handler)
Copy link
Contributor

Choose a reason for hiding this comment

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

the wrap function returns a wrapped result, so this needs to change to work:

Suggested change
httpAuthMiddleware := middleware.AuthenticateUser
httpAuthMiddleware.Wrap(handler)
handler = middleware.AuthenticateUser.Wrap(handler)

Copy link
Contributor

Choose a reason for hiding this comment

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

(I think a small unit test could be written using InjectOrgIDIntoHTTPRequest -- the wrapper is here: https://github.com/grafana/mimir/blob/main/vendor/github.com/weaveworks/common/middleware/http_auth.go#L10 and makes a call to this code: https://github.com/grafana/mimir/blob/main/vendor/github.com/weaveworks/common/user/http.go#L23. A minimal artificial httprequest could be created to exercise this code)

Copy link
Contributor

Choose a reason for hiding this comment

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

(I think a small unit test could be written using InjectOrgIDIntoHTTPRequest -- the wrapper is here: https://github.com/grafana/mimir/blob/main/vendor/github.com/weaveworks/common/middleware/http_auth.go#L10 and makes a call to this code: https://github.com/grafana/mimir/blob/main/vendor/github.com/weaveworks/common/user/http.go#L23. A minimal artificial httprequest could be created to exercise this code)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you mean a unit test that creates a req with a userID injected into it, then verifies that its extracted properly (or errors as expected)?

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah that's what I mean -- again not worth totally blocking this if it's > 1/2 day of poking

fionaliao
fionaliao previously approved these changes Apr 1, 2022
Copy link
Contributor

@fionaliao fionaliao left a comment

Choose a reason for hiding this comment

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

I'm happy for the testing to be done in a later PR

@grafanabot

This comment has been minimized.

Copy link
Contributor

@ywwg ywwg left a comment

Choose a reason for hiding this comment

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

this is really coming together!

pkg/influx/api.go Outdated Show resolved Hide resolved
pkg/influx/auth_test.go Outdated Show resolved Hide resolved
@grafanabot

This comment has been minimized.

@grafanabot

This comment has been minimized.

ywwg
ywwg previously approved these changes Apr 7, 2022
Copy link
Contributor

@ywwg ywwg left a comment

Choose a reason for hiding this comment

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

looks great! thank you. One small note and then go for it

url: "/write",
data: "measurement,t1=v1 f1=2 1465839830100400200",
enableAuth: false,
orgID: "fake",
Copy link
Contributor

Choose a reason for hiding this comment

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

one more case to detect -- no orgid with auth off is also a success

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, it does test this because in auth is enabled, the test doesn't inject the org ID into the request, and relies on the middleware.HttpFakeAuth{} to inject the "fake" org ID into the request's context. This orgID for this test is just used to verify the "fake" orgID is added to the request context. But if there is a way to make this more clear, let me know!

@grafanabot
Copy link

Go coverage report:

Click to expand.
File %
github.com/grafana/influx2cortex/pkg/errorx/errors.go 67.3%
github.com/grafana/influx2cortex/pkg/errorx/http_translator.go 100.0%
github.com/grafana/influx2cortex/pkg/influx/api.go 82.2%
github.com/grafana/influx2cortex/pkg/influx/errors.go 100.0%
github.com/grafana/influx2cortex/pkg/influx/parser.go 84.1%
github.com/grafana/influx2cortex/pkg/influx/recorder.go 91.7%
github.com/grafana/influx2cortex/pkg/remotewrite/client.go 69.1%
github.com/grafana/influx2cortex/pkg/remotewrite/measured_client.go 88.9%
github.com/grafana/influx2cortex/pkg/remotewrite/recorder.go 100.0%
github.com/grafana/influx2cortex/pkg/route/registerer.go 100.0%
github.com/grafana/influx2cortex/pkg/server/middleware/http_auth.go 0.0%
github.com/grafana/influx2cortex/pkg/server/middleware/http_fake_auth.go 0.0%
github.com/grafana/influx2cortex/pkg/server/middleware/instrument.go 6.7%
github.com/grafana/influx2cortex/pkg/server/middleware/logging.go 69.7%
github.com/grafana/influx2cortex/pkg/server/middleware/middleware.go 0.0%
github.com/grafana/influx2cortex/pkg/server/middleware/request_limits.go 81.8%
github.com/grafana/influx2cortex/pkg/server/middleware/response.go 57.1%
github.com/grafana/influx2cortex/pkg/server/middleware/route_matcher.go 0.0%
github.com/grafana/influx2cortex/pkg/server/middleware/tracer.go 13.0%
github.com/grafana/influx2cortex/pkg/server/server.go 83.3%
github.com/grafana/influx2cortex/pkg/tenant/resolver.go 75.6%
github.com/grafana/influx2cortex/pkg/tenant/tenant.go 91.7%
github.com/grafana/influx2cortex/pkg/util/bytereplacer/byte_replacer.go 100.0%
github.com/grafana/influx2cortex/pkg/util/extract_forwarded.go 66.7%
github.com/grafana/influx2cortex/pkg/util/http.go 68.3%
github.com/grafana/influx2cortex/pkg/util/log/experimental.go 0.0%
github.com/grafana/influx2cortex/pkg/util/log/log.go 0.0%
github.com/grafana/influx2cortex/pkg/util/log/rate_limit.go 90.0%
github.com/grafana/influx2cortex/pkg/util/log/wrappers.go 0.0%
github.com/grafana/influx2cortex/pkg/util/push/push.go 60.0%
total 65.5%

Go lint report:

No issues found. 😎

Copy link
Contributor

@ywwg ywwg left a comment

Choose a reason for hiding this comment

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

thank you!

@carrieedwards carrieedwards merged commit fcd7901 into main Apr 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants