Skip to content

Commit eb7e8d7

Browse files
authored
rbac: refactor matchers from shared-ptr to unique-ptr (#40230)
Commit Message: rbac: refactor matchers from shared-ptr to unique-ptr Additional Description: This PR replaces the creation interface of the RBAC matchers. Instead of returning a shared-ptr it now returns a unique-ptr. There is no reason to keep a shared points, because the matchers are owned by the filter-config which is already shared. Risk Level: low Testing: N/A Docs Changes: N/A Release Notes: N/A Platform Specific Features: N/A Signed-off-by: Adi Suissa-Peleg <[email protected]>
1 parent 6f258de commit eb7e8d7

File tree

5 files changed

+50
-50
lines changed

5 files changed

+50
-50
lines changed

source/extensions/filters/common/rbac/matcher_extension.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ class MatcherExtensionFactory : public Envoy::Config::TypedFactory {
2121
* @param config supplies the matcher configuration
2222
* @return a new MatcherExtension
2323
*/
24-
virtual MatcherConstSharedPtr create(const Protobuf::Message& config,
25-
ProtobufMessage::ValidationVisitor& validation_visitor) PURE;
24+
virtual MatcherConstPtr create(const Protobuf::Message& config,
25+
ProtobufMessage::ValidationVisitor& validation_visitor) PURE;
2626

2727
// @brief the category of the matcher extension type for factory registration.
2828
std::string category() const override { return "envoy.rbac.matchers"; }
@@ -35,7 +35,7 @@ class MatcherExtensionFactory : public Envoy::Config::TypedFactory {
3535
template <typename M, typename P>
3636
class BaseMatcherExtensionFactory : public Filters::Common::RBAC::MatcherExtensionFactory {
3737
public:
38-
Filters::Common::RBAC::MatcherConstSharedPtr
38+
Filters::Common::RBAC::MatcherConstPtr
3939
create(const Protobuf::Message& config,
4040
ProtobufMessage::ValidationVisitor& validation_visitor) override {
4141
const auto& matcher_typed_config =
@@ -44,7 +44,7 @@ class BaseMatcherExtensionFactory : public Filters::Common::RBAC::MatcherExtensi
4444

4545
const auto proto_message = MessageUtil::anyConvert<P>(matcher_typed_config.typed_config());
4646

47-
return std::make_shared<M>(proto_message);
47+
return std::make_unique<M>(proto_message);
4848
}
4949

5050
ProtobufTypes::MessagePtr createEmptyConfigProto() override { return std::make_unique<P>(); }

source/extensions/filters/common/rbac/matcher_interface.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Common {
1212
namespace RBAC {
1313

1414
class Matcher;
15-
using MatcherConstSharedPtr = std::shared_ptr<const Matcher>;
15+
using MatcherConstPtr = std::unique_ptr<const Matcher>;
1616

1717
/**
1818
* Matchers describe the rules for matching either a permission action or principal.
@@ -34,19 +34,19 @@ class Matcher {
3434
const StreamInfo::StreamInfo& info) const PURE;
3535

3636
/**
37-
* Creates a shared instance of a matcher based off the rules defined in the Permission config
37+
* Creates an instance of a matcher based off the rules defined in the Permission config
3838
* proto message.
3939
*/
40-
static MatcherConstSharedPtr create(const envoy::config::rbac::v3::Permission& permission,
41-
ProtobufMessage::ValidationVisitor& validation_visitor,
42-
Server::Configuration::CommonFactoryContext& context);
40+
static MatcherConstPtr create(const envoy::config::rbac::v3::Permission& permission,
41+
ProtobufMessage::ValidationVisitor& validation_visitor,
42+
Server::Configuration::CommonFactoryContext& context);
4343

4444
/**
45-
* Creates a shared instance of a matcher based off the rules defined in the Principal config
45+
* Creates an instance of a matcher based off the rules defined in the Principal config
4646
* proto message.
4747
*/
48-
static MatcherConstSharedPtr create(const envoy::config::rbac::v3::Principal& principal,
49-
Server::Configuration::CommonFactoryContext& context);
48+
static MatcherConstPtr create(const envoy::config::rbac::v3::Principal& principal,
49+
Server::Configuration::CommonFactoryContext& context);
5050
};
5151

5252
} // namespace RBAC

source/extensions/filters/common/rbac/matchers.cc

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,46 +13,46 @@ namespace Filters {
1313
namespace Common {
1414
namespace RBAC {
1515

16-
MatcherConstSharedPtr Matcher::create(const envoy::config::rbac::v3::Permission& permission,
17-
ProtobufMessage::ValidationVisitor& validation_visitor,
18-
Server::Configuration::CommonFactoryContext& context) {
16+
MatcherConstPtr Matcher::create(const envoy::config::rbac::v3::Permission& permission,
17+
ProtobufMessage::ValidationVisitor& validation_visitor,
18+
Server::Configuration::CommonFactoryContext& context) {
1919
switch (permission.rule_case()) {
2020
case envoy::config::rbac::v3::Permission::RuleCase::kAndRules:
21-
return std::make_shared<const AndMatcher>(permission.and_rules(), validation_visitor, context);
21+
return std::make_unique<const AndMatcher>(permission.and_rules(), validation_visitor, context);
2222
case envoy::config::rbac::v3::Permission::RuleCase::kOrRules:
23-
return std::make_shared<const OrMatcher>(permission.or_rules(), validation_visitor, context);
23+
return std::make_unique<const OrMatcher>(permission.or_rules(), validation_visitor, context);
2424
case envoy::config::rbac::v3::Permission::RuleCase::kHeader:
25-
return std::make_shared<const HeaderMatcher>(permission.header(), context);
25+
return std::make_unique<const HeaderMatcher>(permission.header(), context);
2626
case envoy::config::rbac::v3::Permission::RuleCase::kDestinationIp:
27-
return std::make_shared<const IPMatcher>(permission.destination_ip(),
27+
return std::make_unique<const IPMatcher>(permission.destination_ip(),
2828
IPMatcher::Type::DownstreamLocal);
2929
case envoy::config::rbac::v3::Permission::RuleCase::kDestinationPort:
30-
return std::make_shared<const PortMatcher>(permission.destination_port());
30+
return std::make_unique<const PortMatcher>(permission.destination_port());
3131
case envoy::config::rbac::v3::Permission::RuleCase::kDestinationPortRange:
32-
return std::make_shared<const PortRangeMatcher>(permission.destination_port_range());
32+
return std::make_unique<const PortRangeMatcher>(permission.destination_port_range());
3333
case envoy::config::rbac::v3::Permission::RuleCase::kAny:
34-
return std::make_shared<const AlwaysMatcher>();
34+
return std::make_unique<const AlwaysMatcher>();
3535
case envoy::config::rbac::v3::Permission::RuleCase::kMetadata:
36-
return std::make_shared<const MetadataMatcher>(
36+
return std::make_unique<const MetadataMatcher>(
3737
Matchers::MetadataMatcher(permission.metadata(), context),
3838
envoy::config::rbac::v3::MetadataSource::DYNAMIC);
3939
case envoy::config::rbac::v3::Permission::RuleCase::kSourcedMetadata:
40-
return std::make_shared<const MetadataMatcher>(
40+
return std::make_unique<const MetadataMatcher>(
4141
Matchers::MetadataMatcher(permission.sourced_metadata().metadata_matcher(), context),
4242
permission.sourced_metadata().metadata_source());
4343
case envoy::config::rbac::v3::Permission::RuleCase::kNotRule:
44-
return std::make_shared<const NotMatcher>(permission.not_rule(), validation_visitor, context);
44+
return std::make_unique<const NotMatcher>(permission.not_rule(), validation_visitor, context);
4545
case envoy::config::rbac::v3::Permission::RuleCase::kRequestedServerName:
46-
return std::make_shared<const RequestedServerNameMatcher>(permission.requested_server_name(),
46+
return std::make_unique<const RequestedServerNameMatcher>(permission.requested_server_name(),
4747
context);
4848
case envoy::config::rbac::v3::Permission::RuleCase::kUrlPath:
49-
return std::make_shared<const PathMatcher>(permission.url_path(), context);
49+
return std::make_unique<const PathMatcher>(permission.url_path(), context);
5050
case envoy::config::rbac::v3::Permission::RuleCase::kUriTemplate: {
5151
auto& factory =
5252
Config::Utility::getAndCheckFactory<Router::PathMatcherFactory>(permission.uri_template());
5353
ProtobufTypes::MessagePtr config = Envoy::Config::Utility::translateAnyToFactoryConfig(
5454
permission.uri_template().typed_config(), validation_visitor, factory);
55-
return std::make_shared<const UriTemplateMatcher>(factory.createPathMatcher(*config));
55+
return std::make_unique<const UriTemplateMatcher>(factory.createPathMatcher(*config));
5656
}
5757
case envoy::config::rbac::v3::Permission::RuleCase::kMatcher: {
5858
auto& factory =
@@ -65,42 +65,42 @@ MatcherConstSharedPtr Matcher::create(const envoy::config::rbac::v3::Permission&
6565
PANIC_DUE_TO_CORRUPT_ENUM;
6666
}
6767

68-
MatcherConstSharedPtr Matcher::create(const envoy::config::rbac::v3::Principal& principal,
69-
Server::Configuration::CommonFactoryContext& context) {
68+
MatcherConstPtr Matcher::create(const envoy::config::rbac::v3::Principal& principal,
69+
Server::Configuration::CommonFactoryContext& context) {
7070
switch (principal.identifier_case()) {
7171
case envoy::config::rbac::v3::Principal::IdentifierCase::kAndIds:
72-
return std::make_shared<const AndMatcher>(principal.and_ids(), context);
72+
return std::make_unique<const AndMatcher>(principal.and_ids(), context);
7373
case envoy::config::rbac::v3::Principal::IdentifierCase::kOrIds:
74-
return std::make_shared<const OrMatcher>(principal.or_ids(), context);
74+
return std::make_unique<const OrMatcher>(principal.or_ids(), context);
7575
case envoy::config::rbac::v3::Principal::IdentifierCase::kAuthenticated:
76-
return std::make_shared<const AuthenticatedMatcher>(principal.authenticated(), context);
76+
return std::make_unique<const AuthenticatedMatcher>(principal.authenticated(), context);
7777
case envoy::config::rbac::v3::Principal::IdentifierCase::kSourceIp:
78-
return std::make_shared<const IPMatcher>(principal.source_ip(),
78+
return std::make_unique<const IPMatcher>(principal.source_ip(),
7979
IPMatcher::Type::ConnectionRemote);
8080
case envoy::config::rbac::v3::Principal::IdentifierCase::kDirectRemoteIp:
81-
return std::make_shared<const IPMatcher>(principal.direct_remote_ip(),
81+
return std::make_unique<const IPMatcher>(principal.direct_remote_ip(),
8282
IPMatcher::Type::DownstreamDirectRemote);
8383
case envoy::config::rbac::v3::Principal::IdentifierCase::kRemoteIp:
84-
return std::make_shared<const IPMatcher>(principal.remote_ip(),
84+
return std::make_unique<const IPMatcher>(principal.remote_ip(),
8585
IPMatcher::Type::DownstreamRemote);
8686
case envoy::config::rbac::v3::Principal::IdentifierCase::kHeader:
87-
return std::make_shared<const HeaderMatcher>(principal.header(), context);
87+
return std::make_unique<const HeaderMatcher>(principal.header(), context);
8888
case envoy::config::rbac::v3::Principal::IdentifierCase::kAny:
89-
return std::make_shared<const AlwaysMatcher>();
89+
return std::make_unique<const AlwaysMatcher>();
9090
case envoy::config::rbac::v3::Principal::IdentifierCase::kMetadata:
91-
return std::make_shared<const MetadataMatcher>(
91+
return std::make_unique<const MetadataMatcher>(
9292
Matchers::MetadataMatcher(principal.metadata(), context),
9393
envoy::config::rbac::v3::MetadataSource::DYNAMIC);
9494
case envoy::config::rbac::v3::Principal::IdentifierCase::kSourcedMetadata:
95-
return std::make_shared<const MetadataMatcher>(
95+
return std::make_unique<const MetadataMatcher>(
9696
Matchers::MetadataMatcher(principal.sourced_metadata().metadata_matcher(), context),
9797
principal.sourced_metadata().metadata_source());
9898
case envoy::config::rbac::v3::Principal::IdentifierCase::kNotId:
99-
return std::make_shared<const NotMatcher>(principal.not_id(), context);
99+
return std::make_unique<const NotMatcher>(principal.not_id(), context);
100100
case envoy::config::rbac::v3::Principal::IdentifierCase::kUrlPath:
101-
return std::make_shared<const PathMatcher>(principal.url_path(), context);
101+
return std::make_unique<const PathMatcher>(principal.url_path(), context);
102102
case envoy::config::rbac::v3::Principal::IdentifierCase::kFilterState:
103-
return std::make_shared<const FilterStateMatcher>(principal.filter_state(), context);
103+
return std::make_unique<const FilterStateMatcher>(principal.filter_state(), context);
104104
case envoy::config::rbac::v3::Principal::IdentifierCase::kCustom:
105105
return Config::Utility::getAndCheckFactory<PrincipalExtensionFactory>(principal.custom())
106106
.create(principal.custom(), context);

source/extensions/filters/common/rbac/matchers.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class AndMatcher : public Matcher {
4747
const StreamInfo::StreamInfo&) const override;
4848

4949
private:
50-
std::vector<MatcherConstSharedPtr> matchers_;
50+
std::vector<MatcherConstPtr> matchers_;
5151
};
5252

5353
/**
@@ -73,7 +73,7 @@ class OrMatcher : public Matcher {
7373
const StreamInfo::StreamInfo&) const override;
7474

7575
private:
76-
std::vector<MatcherConstSharedPtr> matchers_;
76+
std::vector<MatcherConstPtr> matchers_;
7777
};
7878

7979
class NotMatcher : public Matcher {
@@ -90,7 +90,7 @@ class NotMatcher : public Matcher {
9090
const StreamInfo::StreamInfo&) const override;
9191

9292
private:
93-
MatcherConstSharedPtr matcher_;
93+
MatcherConstPtr matcher_;
9494
};
9595

9696
/**

source/extensions/filters/common/rbac/principal_extension.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ class PrincipalExtensionFactory : public Envoy::Config::TypedFactory {
2020
* @param config supplies the matcher configuration
2121
* @return a new MatcherExtension
2222
*/
23-
virtual MatcherConstSharedPtr create(const envoy::config::core::v3::TypedExtensionConfig& config,
24-
Server::Configuration::CommonFactoryContext& context) PURE;
23+
virtual MatcherConstPtr create(const envoy::config::core::v3::TypedExtensionConfig& config,
24+
Server::Configuration::CommonFactoryContext& context) PURE;
2525

2626
// @brief the category of the matcher extension type for factory registration.
2727
std::string category() const override { return "envoy.rbac.principals"; }
@@ -34,13 +34,13 @@ class PrincipalExtensionFactory : public Envoy::Config::TypedFactory {
3434
template <typename PrincipalType, typename ConfigProto>
3535
class BasePrincipalExtensionFactory : public PrincipalExtensionFactory {
3636
public:
37-
Filters::Common::RBAC::MatcherConstSharedPtr
37+
Filters::Common::RBAC::MatcherConstPtr
3838
create(const envoy::config::core::v3::TypedExtensionConfig& config,
3939
Server::Configuration::CommonFactoryContext& context) override {
4040
ConfigProto typed_config;
4141
MessageUtil::anyConvertAndValidate(config.typed_config(), typed_config,
4242
context.messageValidationVisitor());
43-
return std::make_shared<PrincipalType>(typed_config, context);
43+
return std::make_unique<PrincipalType>(typed_config, context);
4444
}
4545

4646
ProtobufTypes::MessagePtr createEmptyConfigProto() override {

0 commit comments

Comments
 (0)