Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions source/extensions/filters/http/rbac/rbac_filter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,30 @@ RoleBasedAccessControlRouteSpecificFilterConfig::RoleBasedAccessControlRouteSpec

Http::FilterHeadersStatus RoleBasedAccessControlFilter::decodeHeaders(Http::HeaderMap& headers,
bool) {
ENVOY_LOG(
debug,
"checking request:\n"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Debug lines in general should be single line. Please remove the newlines.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

"checking request" isn't a very descriptive message. Either make a new "rbac" log type, or prefix all of the messages you added with "rbac: " (or something similar).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the suggestion, I added a new "rbac" log type.

"remoteAddress: {}, localAddress: {}"
"ssl: {}\n"
"headers:\n{}\n"
"dynamicMetadata:\n{}\n",
callbacks_->connection()->remoteAddress()->asString(),
callbacks_->connection()->localAddress()->asString(),
callbacks_->connection()->ssl()
? "uriSanPeerCertificate: " + callbacks_->connection()->ssl()->uriSanPeerCertificate() +
", subjectPeerCertificate: " +
callbacks_->connection()->ssl()->subjectPeerCertificate()
: "none",
headers, callbacks_->requestInfo().dynamicMetadata().DebugString());
const absl::optional<Filters::Common::RBAC::RoleBasedAccessControlEngineImpl>& shadow_engine =
config_->engine(callbacks_->route(), EnforcementMode::Shadow);
if (shadow_engine.has_value()) {
if (shadow_engine->allowed(*callbacks_->connection(), headers,
callbacks_->requestInfo().dynamicMetadata())) {
ENVOY_LOG(debug, "shadow allowed");
config_->stats().shadow_allowed_.inc();
} else {
ENVOY_LOG(debug, "shadow denied");
config_->stats().shadow_denied_.inc();
}
}
Expand All @@ -81,15 +98,18 @@ Http::FilterHeadersStatus RoleBasedAccessControlFilter::decodeHeaders(Http::Head
if (engine.has_value()) {
if (engine->allowed(*callbacks_->connection(), headers,
callbacks_->requestInfo().dynamicMetadata())) {
ENVOY_LOG(debug, "enforced allowed");
config_->stats().allowed_.inc();
return Http::FilterHeadersStatus::Continue;
} else {
ENVOY_LOG(debug, "enforced denied");
callbacks_->sendLocalReply(Http::Code::Forbidden, "RBAC: access denied", nullptr);
config_->stats().denied_.inc();
return Http::FilterHeadersStatus::StopIteration;
}
}

ENVOY_LOG(debug, "no engine, allowed by default");
return Http::FilterHeadersStatus::Continue;
}

Expand Down
5 changes: 4 additions & 1 deletion source/extensions/filters/http/rbac/rbac_filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include "envoy/http/filter.h"
#include "envoy/stats/stats_macros.h"

#include "common/common/logger.h"

#include "extensions/filters/common/rbac/engine_impl.h"

namespace Envoy {
Expand Down Expand Up @@ -80,7 +82,8 @@ typedef std::shared_ptr<RoleBasedAccessControlFilterConfig>
/**
* A filter that provides role-based access control authorization for HTTP requests.
*/
class RoleBasedAccessControlFilter : public Http::StreamDecoderFilter {
class RoleBasedAccessControlFilter : public Http::StreamDecoderFilter,
public Logger::Loggable<Logger::Id::filter> {
public:
RoleBasedAccessControlFilter(RoleBasedAccessControlFilterConfigSharedPtr config)
: config_(config) {}
Expand Down
6 changes: 3 additions & 3 deletions test/extensions/filters/http/rbac/rbac_filter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ class RoleBasedAccessControlFilterTest : public testing::Test {
filter_.setDecoderFilterCallbacks(callbacks_);
}

void setDestinationPort(uint16_t port, int times = 2) {
void setDestinationPort(uint16_t port, int at_most_times = 3) {
address_ = Envoy::Network::Utility::parseInternetAddress("1.2.3.4", port, false);
auto& expect = EXPECT_CALL(connection_, localAddress());
if (times > 0) {
expect.Times(times);
if (at_most_times > 0) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This isn't a useful expectation. Please just make this an ON_CALL(...).WillByDefault(...)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

expect.Times(testing::AtMost(at_most_times));
}
expect.WillRepeatedly(ReturnRef(address_));
}
Expand Down