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

Shard: Add support for Authorization header #518

Merged
merged 3 commits into from
Jul 3, 2024

Conversation

vincent-olivert-riera
Copy link
Contributor

The API end point of some shards are protected with HTTP Basic Auth. This will make the promql-query component to fail receiving a '401 Unauthorized' error response.

To address that, we have added the possibility to specify HTTP Basic Auth credentials on shards.

Apart from that, the query is performed in the back end for security issues. Since the request's "Authorization" header contains the base64-encoded user and password, if we performed the query in the front end that information would be easy to obtain by inspecting the HTTP requests.

@vincent-olivert-riera vincent-olivert-riera requested a review from a team as a code owner June 4, 2024 10:12
Copy link
Collaborator

@kfdm kfdm left a comment

Choose a reason for hiding this comment

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

If we need to add Auth for this, instead of limiting this to basic_auth, I'm wondering if it would be better to generalize it, and implement it as an optional Authorization header that can be added. Then, this would support not only Basic Auth, but also if an upstream server was using some kind of Token or other authentication scheme 🤔

promgen/views.py Outdated Show resolved Hide resolved
promgen/views.py Show resolved Hide resolved
@vincent-olivert-riera
Copy link
Contributor Author

vincent-olivert-riera commented Jun 28, 2024

If we need to add Auth for this, instead of limiting this to basic_auth, I'm wondering if it would be better to generalize it, and implement it as an optional Authorization header that can be added. Then, this would support not only Basic Auth, but also if an upstream server was using some kind of Token or other authentication scheme 🤔

Uhm..., yeah, that would be useful. I will change this PR in that direction 👍

@vincent-olivert-riera vincent-olivert-riera changed the title Shard: Add support for HTTP Basic Auth Shard: Add support for Authorization header Jul 1, 2024
@vincent-olivert-riera
Copy link
Contributor Author

@kfdm , I have reworked the PR from scratch because I think is easier to review.

It is now ready for review. Thanks.

kfdm
kfdm previously approved these changes Jul 2, 2024
Copy link
Collaborator

@kfdm kfdm left a comment

Choose a reason for hiding this comment

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

All seems reasonable. I can probably merge as-is without any complaints, but two questions for consideration first. 😄

Comment on lines +129 to +130
# PromQL Query
path("promql-query", views.PromqlQuery.as_view(), name="promql-query"),
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is more of a 'thinking out loud' comment, but we have a few internal APIs, like this, exporter test, and so on, I wonder if there would be value in making some kind of internal/ prefix for them, just to help organize them. There's also some older API cleanup I would like to so, so picking some kind of internal prefix might help us see what have been migrated so far.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

but we have a few internal APIs, like this, exporter test, and so on, I wonder if there would be value in making some kind of internal/ prefix for them, just to help organize them

We could. But since the others are not already organized under the same internal/ prefix, I guess we could leave this one as it is, and organize all of them in the future.

Or, if you tell me which paths on the urls.py file you want organized under an internal/ prefix, I can do that on this PR.

promgen/views.py Outdated
if not all(x in request.GET for x in ["shard", "query"]):
return HttpResponse("BAD REQUEST", status=400)

shard = models.Shard.objects.get(pk=request.GET["shard"])
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is mostly a style thing,

Suggested change
shard = models.Shard.objects.get(pk=request.GET["shard"])
shard = get_object_or_404(models.Shard, pk=request.GET["shard"])

but since we already have get__object_or_404 included, this means if for whatever reason, an invalid shard ID was called, this would result in a 404 instead of a 500.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense. Done in 04b06cc. Thanks 👍

Copy link
Collaborator

@kfdm kfdm left a comment

Choose a reason for hiding this comment

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

LGTM

This new field in the Shard model will hold the value for the HTTP Authorization
header. Although unused at the moment, this will give us the possibility to
support Shards that require authentication.
The API end point of some shards are protected with HTTP Basic Auth. This is
making the promql-query component to fail receiving a '401 Unauthorized' error
response.

Now that the Shard model supports an "authorization" field to specify the value
of the "Authorization" HTTP header, we can address this issue.

However, if we perform the request in the front end, that information would be
visible by a simple inspection of the HTTP headers. Because of that we have
moved it to the back end.
Now that our Shard model supports an "authorization" field to specify the value
of the HTTP "Authorization" header, we can use it in our PrometheusProxy class.

Note that we have replace "host" with "shard" since the objects obtained by the
'models.Shard.objects.filter(proxy=True)' instruction are shards, not hosts.
@vincent-olivert-riera
Copy link
Contributor Author

LGTM

Thanks. Rebased on master with fixup commits squashed.

@vincent-olivert-riera vincent-olivert-riera merged commit 32f1bbd into master Jul 3, 2024
5 checks passed
@vincent-olivert-riera vincent-olivert-riera deleted the shard-http-basic-auth branch July 3, 2024 03:02
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.

2 participants