|
| 1 | +// Copyright 2025 Redpanda Data, Inc. |
| 2 | +// |
| 3 | +// Use of this software is governed by the Business Source License |
| 4 | +// included in the file licenses/BSL.md |
| 5 | +// |
| 6 | +// As of the Change Date specified in that file, in accordance with |
| 7 | +// the Business Source License, use of this software will be governed |
| 8 | +// by the Apache License, Version 2.0 |
| 9 | + |
| 10 | +#include "cluster/security_frontend.h" |
| 11 | +#include "kafka/protocol/describe_acls.h" |
| 12 | +#include "redpanda/tests/fixture.h" |
| 13 | +#include "security/acl.h" |
| 14 | + |
| 15 | +using namespace std::chrono_literals; |
| 16 | + |
| 17 | +class describe_acls_fixture : public redpanda_thread_fixture { |
| 18 | +protected: |
| 19 | + void create_acl(security::acl_binding binding) { |
| 20 | + app.controller->get_security_frontend() |
| 21 | + .local() |
| 22 | + .create_acls({std::move(binding)}, 5s) |
| 23 | + .get(); |
| 24 | + } |
| 25 | +}; |
| 26 | + |
| 27 | +FIXTURE_TEST(describe_acls_any_kafka, describe_acls_fixture) { |
| 28 | + // This test verifies that when requesting any ACL and |
| 29 | + // "describe_registry_acls" is false, only receive back Kafka resource ACLs |
| 30 | + wait_for_controller_leadership().get(); |
| 31 | + |
| 32 | + create_acl(security::acl_binding{ |
| 33 | + security::resource_pattern{ |
| 34 | + security::resource_type::topic, |
| 35 | + "test-topic", |
| 36 | + security::pattern_type::literal}, |
| 37 | + security::acl_entry{ |
| 38 | + security::acl_principal{ |
| 39 | + security::principal_type::user, "User:test-user"}, |
| 40 | + security::acl_host::wildcard_host(), |
| 41 | + security::acl_operation::read, |
| 42 | + security::acl_permission::allow}}); |
| 43 | + |
| 44 | + create_acl(security::acl_binding{ |
| 45 | + security::resource_pattern{ |
| 46 | + security::resource_type::sr_subject, |
| 47 | + "test-topic-value", |
| 48 | + security::pattern_type::literal}, |
| 49 | + security::acl_entry{ |
| 50 | + security::acl_principal{ |
| 51 | + security::principal_type::user, "User:test-user"}, |
| 52 | + security::acl_host::wildcard_host(), |
| 53 | + security::acl_operation::read, |
| 54 | + security::acl_permission::allow}}); |
| 55 | + |
| 56 | + auto client = make_kafka_client().get(); |
| 57 | + auto deferred_close = ss::defer([&client] { client.stop().get(); }); |
| 58 | + client.connect().get(); |
| 59 | + |
| 60 | + kafka::describe_acls_request req; |
| 61 | + req.data.describe_registry_acls = false; |
| 62 | + req.data.resource_type = 1; |
| 63 | + req.data.resource_pattern_type = 1; |
| 64 | + req.data.operation = 1; |
| 65 | + req.data.permission_type = 1; |
| 66 | + |
| 67 | + auto resp = client.dispatch(std::move(req), kafka::api_version(2)).get(); |
| 68 | + BOOST_REQUIRE(!resp.data.errored()); |
| 69 | + BOOST_REQUIRE_EQUAL(resp.data.error_code, kafka::error_code::none); |
| 70 | + BOOST_REQUIRE_EQUAL(resp.data.resources.size(), 1); |
| 71 | + BOOST_REQUIRE_EQUAL(resp.data.resources[0].type, 2); |
| 72 | + BOOST_REQUIRE_EQUAL(resp.data.resources[0].name, "test-topic"); |
| 73 | + BOOST_REQUIRE(!resp.data.resources[0].registry_resource); |
| 74 | +} |
| 75 | + |
| 76 | +FIXTURE_TEST(describe_acls_any_sr, describe_acls_fixture) { |
| 77 | + // This test verifies that when requesting any ACL and |
| 78 | + // "describe_registry_acls" is true, only receive back Schema Registry |
| 79 | + // resource ACLs |
| 80 | + wait_for_controller_leadership().get(); |
| 81 | + |
| 82 | + create_acl(security::acl_binding{ |
| 83 | + security::resource_pattern{ |
| 84 | + security::resource_type::topic, |
| 85 | + "test-topic", |
| 86 | + security::pattern_type::literal}, |
| 87 | + security::acl_entry{ |
| 88 | + security::acl_principal{ |
| 89 | + security::principal_type::user, "User:test-user"}, |
| 90 | + security::acl_host::wildcard_host(), |
| 91 | + security::acl_operation::read, |
| 92 | + security::acl_permission::allow}}); |
| 93 | + |
| 94 | + create_acl(security::acl_binding{ |
| 95 | + security::resource_pattern{ |
| 96 | + security::resource_type::sr_subject, |
| 97 | + "test-topic-value", |
| 98 | + security::pattern_type::literal}, |
| 99 | + security::acl_entry{ |
| 100 | + security::acl_principal{ |
| 101 | + security::principal_type::user, "User:test-user"}, |
| 102 | + security::acl_host::wildcard_host(), |
| 103 | + security::acl_operation::read, |
| 104 | + security::acl_permission::allow}}); |
| 105 | + |
| 106 | + auto client = make_kafka_client().get(); |
| 107 | + auto deferred_close = ss::defer([&client] { client.stop().get(); }); |
| 108 | + client.connect().get(); |
| 109 | + |
| 110 | + kafka::describe_acls_request req; |
| 111 | + req.data.describe_registry_acls = true; |
| 112 | + req.data.resource_type = 1; |
| 113 | + req.data.resource_pattern_type = 1; |
| 114 | + req.data.operation = 1; |
| 115 | + req.data.permission_type = 1; |
| 116 | + |
| 117 | + auto resp = client.dispatch(std::move(req), kafka::api_version(2)).get(); |
| 118 | + BOOST_REQUIRE(!resp.data.errored()); |
| 119 | + BOOST_REQUIRE_EQUAL(resp.data.error_code, kafka::error_code::none); |
| 120 | + BOOST_REQUIRE_EQUAL(resp.data.resources.size(), 1); |
| 121 | + BOOST_REQUIRE_EQUAL(resp.data.resources[0].type, 100); // 100 is SR_SUBJECT |
| 122 | + BOOST_REQUIRE_EQUAL(resp.data.resources[0].name, "test-topic-value"); |
| 123 | + BOOST_REQUIRE(resp.data.resources[0].registry_resource); |
| 124 | +} |
| 125 | + |
| 126 | +FIXTURE_TEST(describe_acls_invalid_for_kafka, describe_acls_fixture) { |
| 127 | + auto client = make_kafka_client().get(); |
| 128 | + auto deferred_close = ss::defer([&client] { client.stop().get(); }); |
| 129 | + client.connect().get(); |
| 130 | + |
| 131 | + kafka::describe_acls_request req; |
| 132 | + req.data.describe_registry_acls = false; |
| 133 | + // Request SR_SUBJECT resource type, which is invalid if |
| 134 | + // describe_registry_acls is false |
| 135 | + req.data.resource_type = 100; |
| 136 | + req.data.resource_pattern_type = 1; |
| 137 | + req.data.operation = 1; |
| 138 | + req.data.permission_type = 1; |
| 139 | + |
| 140 | + auto resp = client.dispatch(std::move(req), kafka::api_version(2)).get(); |
| 141 | + BOOST_REQUIRE(resp.data.errored()); |
| 142 | + BOOST_REQUIRE_EQUAL( |
| 143 | + resp.data.error_code, kafka::error_code::invalid_request); |
| 144 | +} |
| 145 | + |
| 146 | +FIXTURE_TEST(describe_acls_invalid_for_sr, describe_acls_fixture) { |
| 147 | + auto client = make_kafka_client().get(); |
| 148 | + auto deferred_close = ss::defer([&client] { client.stop().get(); }); |
| 149 | + client.connect().get(); |
| 150 | + |
| 151 | + kafka::describe_acls_request req; |
| 152 | + req.data.describe_registry_acls = true; |
| 153 | + // Request TOPIC resource type, which is invalid if describe_registry_acls |
| 154 | + // is true |
| 155 | + req.data.resource_type = 2; |
| 156 | + req.data.resource_pattern_type = 1; |
| 157 | + req.data.operation = 1; |
| 158 | + req.data.permission_type = 1; |
| 159 | + |
| 160 | + auto resp = client.dispatch(std::move(req), kafka::api_version(2)).get(); |
| 161 | + BOOST_REQUIRE(resp.data.errored()); |
| 162 | + BOOST_REQUIRE_EQUAL( |
| 163 | + resp.data.error_code, kafka::error_code::invalid_request); |
| 164 | +} |
0 commit comments