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

Fix the peerIsOptional and originIsOptional for authn filter. #1959

Merged
merged 1 commit into from
Sep 5, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 4 additions & 4 deletions src/envoy/http/authn/http_filter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ FilterHeadersStatus AuthenticationFilter::decodeHeaders(HeaderMap& headers,

Payload payload;

if (!filter_config_.policy().peer_is_optional() &&
!createPeerAuthenticator(filter_context_.get())->run(&payload)) {
if (!createPeerAuthenticator(filter_context_.get())->run(&payload) &&
!filter_config_.policy().peer_is_optional()) {
rejectRequest("Peer authentication failed.");
return FilterHeadersStatus::StopIteration;
}

bool success =
filter_config_.policy().origin_is_optional() ||
createOriginAuthenticator(filter_context_.get())->run(&payload);
createOriginAuthenticator(filter_context_.get())->run(&payload) ||
filter_config_.policy().origin_is_optional();

if (!success) {
rejectRequest("Origin authentication failed.");
Expand Down
60 changes: 59 additions & 1 deletion src/envoy/http/authn/http_filter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ TEST_F(AuthenticationFilterTest, PeerFail) {
request_info.dynamicMetadata()));
}

TEST_F(AuthenticationFilterTest, PeerPassOrginFail) {
TEST_F(AuthenticationFilterTest, PeerPassOriginFail) {
// Peer pass thus origin authentication must be called. Final result should
// fail as origin authn fails.
EXPECT_CALL(filter_, createPeerAuthenticator(_))
Expand Down Expand Up @@ -210,10 +210,68 @@ TEST_F(AuthenticationFilterTest, IgnoreBothFail) {
*filter_config_.mutable_policy() = policy_;
StrictMock<MockAuthenticationFilter> filter(filter_config_);
filter.setDecoderFilterCallbacks(decoder_callbacks_);

EXPECT_CALL(filter, createPeerAuthenticator(_))
.Times(1)
.WillOnce(Invoke(createAlwaysFailAuthenticator));
EXPECT_CALL(filter, createOriginAuthenticator(_))
.Times(1)
.WillOnce(Invoke(createAlwaysFailAuthenticator));
EXPECT_EQ(Http::FilterHeadersStatus::Continue,
filter.decodeHeaders(request_headers_, true));
}

TEST_F(AuthenticationFilterTest, IgnoreBothPass) {
iaapi::Policy policy_;
ASSERT_TRUE(
Protobuf::TextFormat::ParseFromString(ingoreBothPolicy, &policy_));
*filter_config_.mutable_policy() = policy_;
StrictMock<MockAuthenticationFilter> filter(filter_config_);
filter.setDecoderFilterCallbacks(decoder_callbacks_);

EXPECT_CALL(filter, createPeerAuthenticator(_))
.Times(1)
.WillOnce(Invoke(createAlwaysPassAuthenticator));
EXPECT_CALL(filter, createOriginAuthenticator(_))
.Times(1)
.WillOnce(Invoke(createAlwaysPassAuthenticator));
RequestInfo::RequestInfoImpl request_info(Http::Protocol::Http2);
EXPECT_CALL(decoder_callbacks_, requestInfo())
.Times(AtLeast(1))
.WillRepeatedly(ReturnRef(request_info));

EXPECT_EQ(Http::FilterHeadersStatus::Continue,
filter.decodeHeaders(request_headers_, true));

EXPECT_EQ(1, request_info.dynamicMetadata().filter_metadata_size());
const auto *data = Utils::Authentication::GetResultFromMetadata(
request_info.dynamicMetadata());
ASSERT_TRUE(data);

ProtobufWkt::Struct expected_data;
ASSERT_TRUE(Protobuf::TextFormat::ParseFromString(R"(
fields {
key: "source.namespace"
value {
string_value: "test_ns"
}
}
fields {
key: "source.principal"
value {
string_value: "cluster.local/sa/test_user/ns/test_ns/"
}
}
fields {
key: "source.user"
value {
string_value: "cluster.local/sa/test_user/ns/test_ns/"
}
})",
&expected_data));
EXPECT_TRUE(TestUtility::protoEqual(expected_data, *data));
}

} // namespace
} // namespace AuthN
} // namespace Istio
Expand Down