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

[Audit Logging] End2end test for audit logging in authorization policy #33196

Merged
merged 19 commits into from May 24, 2023

Conversation

rockspore
Copy link
Contributor

@rockspore rockspore commented May 19, 2023

I generated a new client key and cert where a Spiffe ID is added as the URI SAN. As such, we are able to test the audit log contains the principal correctly.

As for the test strategy, a test logger is registered to check the log count only and the log content is verified in details via the built-in stdout logger.

Update: I switched to use the test logger to verify the log content and removed stdout logger here because one the failure of RBE Windows Debug C/C++.

Update again: Refactored the test logger in a util such that the authz engine test also uses the same logger. Subsequently, xDS e2e test will also use it.

@rockspore rockspore requested a review from erm-g May 19, 2023 16:55
@rockspore rockspore requested a review from gtcooke94 May 19, 2023 16:55
@rockspore rockspore added the release notes: no Indicates if PR should not be in release notes label May 19, 2023
Copy link
Contributor

@gtcooke94 gtcooke94 left a comment

Choose a reason for hiding this comment

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

A few initial comments

src/core/tsi/test_creds/README Outdated Show resolved Hide resolved
test/cpp/end2end/BUILD Outdated Show resolved Hide resolved
test/cpp/end2end/grpc_authz_end2end_test.cc Outdated Show resolved Hide resolved
test/cpp/end2end/grpc_authz_end2end_test.cc Outdated Show resolved Hide resolved
test/cpp/end2end/grpc_authz_end2end_test.cc Outdated Show resolved Hide resolved
test/cpp/end2end/grpc_authz_end2end_test.cc Outdated Show resolved Hide resolved
@rockspore
Copy link
Contributor Author

Bazel RBE Windows Debug C/C++ has been consistently failing and it seems like the captured stdout failed to be unmarshaled into a valid json. This is difficult to troubleshoot as I don't have a windows machine (and running RBE Windows locally needs it as well).

I think I will switch to use the test logger to verify the log content and have to ditch stdout logger here.

@rockspore rockspore marked this pull request as draft May 19, 2023 18:34
@rockspore rockspore marked this pull request as ready for review May 19, 2023 19:38
@rockspore rockspore requested a review from gtcooke94 May 19, 2023 19:38
test/cpp/end2end/grpc_authz_end2end_test.cc Show resolved Hide resolved
test/cpp/end2end/grpc_authz_end2end_test.cc Outdated Show resolved Hide resolved
test/cpp/end2end/grpc_authz_end2end_test.cc Outdated Show resolved Hide resolved
test/cpp/end2end/grpc_authz_end2end_test.cc Outdated Show resolved Hide resolved
@rockspore rockspore requested a review from gtcooke94 May 19, 2023 20:51
Copy link
Contributor

@gtcooke94 gtcooke94 left a comment

Choose a reason for hiding this comment

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

LGTM (assuming all tests are good, etc)

@rockspore rockspore requested a review from markdroth May 19, 2023 21:06
@rockspore rockspore requested a review from erm-g May 22, 2023 21:39
Copy link
Contributor

@erm-g erm-g left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@erm-g erm-g left a comment

Choose a reason for hiding this comment

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

Would it make sense to name it authz_audit_logging_utils?

@rockspore
Copy link
Contributor Author

Would it make sense to name it authz_audit_logging_utils?

We have public headers simply named grpc_audit_logging.h or audit_logging.h so I don' think it's strictly necessary. But let me know if you feel strongly about making this clear and I am open to changing it accordingly. @erm-g

@erm-g
Copy link
Contributor

erm-g commented May 24, 2023

Would it make sense to name it authz_audit_logging_utils?

We have public headers simply named grpc_audit_logging.h or audit_logging.h so I don' think it's strictly necessary. But let me know if you feel strongly about making this clear and I am open to changing it accordingly. @erm-g

Yeah in this case I think I'm fine

Copy link
Member

@markdroth markdroth left a comment

Choose a reason for hiding this comment

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

This looks good! My comments are all fairly minor, so feel free to merge after addressing.

@@ -82,9 +93,15 @@ class GrpcAuthzEnd2EndTest : public ::testing::Test {
channel_options.watch_identity_key_cert_pairs();
channel_options.watch_root_certs();
channel_creds_ = grpc::experimental::TlsCredentials(channel_options);

Copy link
Member

Choose a reason for hiding this comment

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

Nit: Please remove unnecessary blank lines within functions.

Same thing throughout.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

grpc::Status status;

ClientContext context1;
// Matches the allow rule.
Copy link
Member

Choose a reason for hiding this comment

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

Please split each of these cases into their own TEST_F() blocks, as per go/tott/649.

Same for all of these tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. Thanks for the link!

@@ -798,6 +1120,11 @@ TEST_F(GrpcAuthzEnd2EndTest, FileWatcherRecoversFromFailure) {
grpc::Status status = SendRpc(channel, &context1, &resp1);
EXPECT_TRUE(status.ok());
EXPECT_EQ(resp1.message(), kMessage);
ASSERT_EQ(audit_logs_.size(), 1);
Copy link
Member

Choose a reason for hiding this comment

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

Suggest using EXPECT_THAT(audit_logs_, ::testing::ElementsAre(...)) here. That way, if the check fails, we won't bail out early.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@@ -823,6 +1150,11 @@ TEST_F(GrpcAuthzEnd2EndTest, FileWatcherRecoversFromFailure) {
status = SendRpc(channel, &context2, &resp2);
EXPECT_TRUE(status.ok());
EXPECT_EQ(resp2.message(), kMessage);
EXPECT_EQ(audit_logs_.size(), 2);
Copy link
Member

Choose a reason for hiding this comment

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

Suggest using ElementsAre() here as well. Note that you can say audit_logs_.clear() after the previous pass to clear out the entry you've already checked.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@@ -871,6 +1212,11 @@ TEST_F(GrpcAuthzEnd2EndTest, FileWatcherRecoversFromFailure) {
EXPECT_EQ(status.error_code(), grpc::StatusCode::PERMISSION_DENIED);
EXPECT_EQ(status.error_message(), "Unauthorized RPC request rejected.");
EXPECT_TRUE(resp3.message().empty());
EXPECT_EQ(audit_logs_.size(), 3);
Copy link
Member

Choose a reason for hiding this comment

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

Same here.

Copy link
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
Contributor Author

@rockspore rockspore left a comment

Choose a reason for hiding this comment

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

Thanks for the review, Mark!

@@ -82,9 +93,15 @@ class GrpcAuthzEnd2EndTest : public ::testing::Test {
channel_options.watch_identity_key_cert_pairs();
channel_options.watch_root_certs();
channel_creds_ = grpc::experimental::TlsCredentials(channel_options);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

grpc::Status status;

ClientContext context1;
// Matches the allow rule.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. Thanks for the link!

@@ -798,6 +1120,11 @@ TEST_F(GrpcAuthzEnd2EndTest, FileWatcherRecoversFromFailure) {
grpc::Status status = SendRpc(channel, &context1, &resp1);
EXPECT_TRUE(status.ok());
EXPECT_EQ(resp1.message(), kMessage);
ASSERT_EQ(audit_logs_.size(), 1);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@@ -823,6 +1150,11 @@ TEST_F(GrpcAuthzEnd2EndTest, FileWatcherRecoversFromFailure) {
status = SendRpc(channel, &context2, &resp2);
EXPECT_TRUE(status.ok());
EXPECT_EQ(resp2.message(), kMessage);
EXPECT_EQ(audit_logs_.size(), 2);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@@ -871,6 +1212,11 @@ TEST_F(GrpcAuthzEnd2EndTest, FileWatcherRecoversFromFailure) {
EXPECT_EQ(status.error_code(), grpc::StatusCode::PERMISSION_DENIED);
EXPECT_EQ(status.error_message(), "Unauthorized RPC request rejected.");
EXPECT_TRUE(resp3.message().empty());
EXPECT_EQ(audit_logs_.size(), 3);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@rockspore rockspore merged commit de9d398 into grpc:master May 24, 2023
64 of 65 checks passed
@copybara-service copybara-service bot added the imported Specifies if the PR has been imported to the internal repository label May 25, 2023
@yijiem yijiem added release notes: yes Indicates if PR needs to be in release notes and removed release notes: no Indicates if PR should not be in release notes labels May 31, 2023
eugeneo pushed a commit to eugeneo/grpc that referenced this pull request Jun 1, 2023
grpc#33196)

I generated a new client key and cert where a Spiffe ID is added as the
URI SAN. As such, we are able to test the audit log contains the
principal correctly.

Update: I switched to use the test logger to verify the log content and
removed stdout logger here because one the failure of [RBE Windows Debug
C/C++](https://source.cloud.google.com/results/invocations/c3187f41-bb1f-44b3-b2b1-23f38e47386d).

Update again: Refactored the test logger in a util such that the authz
engine test also uses the same logger. Subsequently, xDS e2e test will
also use it.

---------

Co-authored-by: rockspore <rockspore@users.noreply.github.com>
@erm-g erm-g removed the release notes: yes Indicates if PR needs to be in release notes label Jun 12, 2023
@yijiem yijiem added the release notes: no Indicates if PR should not be in release notes label Jun 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bloat/none imported Specifies if the PR has been imported to the internal repository lang/c++ lang/core per-call-memory/neutral per-channel-memory/neutral release notes: no Indicates if PR should not be in release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants