-
Notifications
You must be signed in to change notification settings - Fork 741
sr/context: implement /contexts/{context}/... prefixed handlers #30189
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
Changes from all commits
cceffc0
c1506c2
5551d43
913cedb
0acf252
a93647d
c44d2c3
8e2b44b
7c2d3d8
18720e2
cb5aff6
955d7be
e314131
bc778c4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ | |
| #include "container/chunked_hash_map.h" | ||
| #include "pandaproxy/api/api-doc/schema_registry.json.hh" | ||
| #include "pandaproxy/parsing/httpd.h" | ||
| #include "pandaproxy/schema_registry/context_router.h" | ||
| #include "pandaproxy/schema_registry/service.h" | ||
| #include "pandaproxy/schema_registry/sharded_store.h" | ||
| #include "pandaproxy/schema_registry/types.h" | ||
|
|
@@ -54,15 +55,21 @@ namespace { | |
|
|
||
| auth::resource | ||
| extract_resource_from_request(const server::request_t& rq, const auth& auth) { | ||
| auto resource = auth.get_resource(); | ||
| ss::visit( | ||
| resource, | ||
| [&rq](context_subject& ctx_sub) { | ||
| ctx_sub = context_subject::from_string( | ||
| return ss::visit( | ||
| auth.get_resource(), | ||
| [&rq](const context_subject&) -> auth::resource { | ||
| return context_subject::from_string( | ||
| parse::request_param<ss::sstring>(*rq.req, "subject")); | ||
| }, | ||
| [](const auto&) {}); | ||
| return resource; | ||
| [&rq](const auth::context_prefix_subject&) -> auth::resource { | ||
| auto ctx = parse_normalized_context(*rq.req); | ||
| auto sub = parse::request_param<ss::sstring>(*rq.req, "subject"); | ||
| if (!starts_with_context(sub)) { | ||
| sub = fmt::format(":{}:{}", ctx, sub); | ||
| } | ||
| return context_subject::from_string(sub); | ||
| }, | ||
| [](const auto& res) -> auth::resource { return res; }); | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens to the authorization of deferred handler like Can you add a few tests around the context path + ACLs + audit logging integration as well please?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch, pushed some changes to address this. Updated Added |
||
|
|
||
| void throw_unauthorized() { | ||
|
|
@@ -157,10 +164,9 @@ void handle_authz( | |
|
|
||
| void handle_get_schemas_ids_id_authz( | ||
| const server::request_t& rq, | ||
| std::string_view operation_name, | ||
| std::optional<request_auth_result>& auth_result, | ||
|
Comment on lines
+167
to
168
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: we could wrap the |
||
| const chunked_vector<context_subject>& subjects) { | ||
| const auto& operation_name | ||
| = ss::httpd::schema_registry_json::get_schemas_ids_id.operations.nickname; | ||
| constexpr auto op = security::acl_operation::read; | ||
| if (!auth_result.has_value()) { | ||
| // ACLs or authentication is disabled | ||
|
|
@@ -222,10 +228,9 @@ void handle_get_schemas_ids_id_authz( | |
|
|
||
| void handle_get_subjects_authz( | ||
| const server::request_t& rq, | ||
| std::string_view operation_name, | ||
| std::optional<request_auth_result>& auth_result, | ||
| chunked_vector<context_subject>& subjects) { | ||
| const auto& operation_name | ||
| = ss::httpd::schema_registry_json::get_subjects.operations.nickname; | ||
| constexpr auto op = security::acl_operation::describe; | ||
|
|
||
| if (!auth_result.has_value()) { | ||
|
|
@@ -286,11 +291,10 @@ void handle_get_subjects_authz( | |
|
|
||
| ss::future<> handle_get_contexts_authz( | ||
| const server::request_t& rq, | ||
| std::string_view operation_name, | ||
| sharded_store& store, | ||
| std::optional<request_auth_result>& auth_result, | ||
| chunked_vector<context>& contexts) { | ||
| const auto& operation_name | ||
| = ss::httpd::schema_registry_json::get_contexts.operations.nickname; | ||
| constexpr auto op = security::acl_operation::describe; | ||
|
|
||
| if (!auth_result.has_value()) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| // Copyright 2026 Redpanda Data, Inc. | ||
| // | ||
| // Use of this software is governed by the Business Source License | ||
| // included in the file licenses/BSL.md | ||
| // | ||
| // As of the Change Date specified in that file, in accordance with | ||
| // the Business Source License, use of this software will be governed | ||
| // by the Apache License, Version 2.0 | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "base/seastarx.h" | ||
| #include "pandaproxy/parsing/httpd.h" | ||
| #include "pandaproxy/schema_registry/errors.h" | ||
|
|
||
| #include <seastar/core/sstring.hh> | ||
| #include <seastar/http/request.hh> | ||
|
|
||
| #include <fmt/format.h> | ||
|
|
||
| #include <string_view> | ||
|
|
||
| namespace pandaproxy::schema_registry { | ||
|
|
||
| /// \brief Normalize a context name from a URL path parameter. | ||
| inline ss::sstring normalize_context(std::string_view ctx) { | ||
| if (ctx.starts_with(':')) { | ||
| ctx.remove_prefix(1); | ||
| } | ||
|
|
||
| if (ctx.ends_with(':')) { | ||
| ctx.remove_suffix(1); | ||
| } | ||
|
|
||
| if (ctx.find(':') != std::string_view::npos) { | ||
| throw as_exception(context_invalid(ctx)); | ||
| } | ||
|
|
||
| if (!ctx.starts_with('.')) { | ||
| return {fmt::format(".{}", ctx)}; | ||
| } | ||
| return ss::sstring(ctx); | ||
| } | ||
|
|
||
| /// \brief Parse the "context" path parameter and normalize it. | ||
| inline ss::sstring parse_normalized_context(const ss::http::request& req) { | ||
| return normalize_context(parse::request_param<ss::sstring>(req, "context")); | ||
| } | ||
|
|
||
| /// \brief Check if a string already has a context prefix. | ||
| inline bool starts_with_context(std::string_view s) { | ||
| return s.starts_with(":.") || s.starts_with(":*:"); | ||
| } | ||
|
|
||
| /// \brief Scope the "subject" path parameter by prepending the context. | ||
| /// | ||
| /// ctx must already be normalized (in the form ".name"). The resulting | ||
| /// subject is ":.ctx:subject". | ||
| inline void scope_subject_param(ss::http::request& req, std::string_view ctx) { | ||
| auto sub = req.get_path_param("subject"); | ||
| if (!starts_with_context(sub)) { | ||
| req.param.set( | ||
| ss::sstring("subject"), | ||
| ss::sstring(fmt::format("/:{0}:{1}", ctx, sub))); | ||
| } | ||
| } | ||
|
|
||
| /// \brief Inject or prepend context into the "subject" query parameter. | ||
| /// | ||
| /// ctx must already be normalized. | ||
| inline void scope_subject_query(ss::http::request& req, std::string_view ctx) { | ||
| auto existing = req.get_query_param("subject"); | ||
| if (existing.empty()) { | ||
| req.set_query_param("subject", fmt::format(":{0}:", ctx)); | ||
| } else if (!starts_with_context(existing)) { | ||
| req.set_query_param("subject", fmt::format(":{0}:{1}", ctx, existing)); | ||
| } | ||
| } | ||
|
|
||
| /// \brief Inject or prepend context into the "subjectPrefix" query parameter. | ||
| /// | ||
| /// ctx must already be normalized. | ||
| inline void | ||
| scope_subject_prefix_query(ss::http::request& req, std::string_view ctx) { | ||
| auto existing = req.get_query_param("subjectPrefix"); | ||
| if (existing.empty()) { | ||
| req.set_query_param("subjectPrefix", fmt::format(":{0}:", ctx)); | ||
| } else if (!starts_with_context(existing)) { | ||
| req.set_query_param( | ||
| "subjectPrefix", fmt::format(":{0}:{1}", ctx, existing)); | ||
| } | ||
| } | ||
|
|
||
| /// \brief Inject the context as a context-only qualified subject path | ||
| /// parameter. | ||
| /// | ||
| /// ctx must already be normalized. | ||
| inline void | ||
| inject_context_as_subject(ss::http::request& req, std::string_view ctx) { | ||
| req.param.set( | ||
| ss::sstring("subject"), ss::sstring(fmt::format("/:{0}:", ctx))); | ||
| } | ||
|
|
||
| } // namespace pandaproxy::schema_registry |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering if it would be simpler to just merge
resourceandroute_resource. The reason being thatresourceis already a "route-behaviour-variant", not really a resource, so I thinkcontext_prefix_subjectwould fit in well there.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've split them because narrowing from
route_resourcetoresourceinextract_resource_from_requestgives us a compile-time guarantee thatcontext_prefix_subjectis gone by the time we hit the authorizer (get_resource_type<T>insrc/v/security/acl.ccwould static_assert otherwise).If we merge,
requires_authmatchescontext_prefix_subjectand the static_assert fires, so we'd have to either lump it intono_auth(which probably wouldn't be right) or put avassert(false)arm in the visit, which would be replacing a compile-time check with a runtime one.