|
16 | 16 |
|
17 | 17 | #include "testapp_client_test.h" |
18 | 18 | #include <auditd/couchbase_audit_events.h> |
| 19 | +#include <folly/ScopeGuard.h> |
19 | 20 | #include <mcbp/codec/frameinfo.h> |
20 | 21 | #include <nlohmann/json.hpp> |
21 | 22 | #include <platform/dirutils.h> |
@@ -654,38 +655,66 @@ TEST_P(AuditTest, MB51863) { |
654 | 655 | ASSERT_EQ("0x0", document["collection_id"].get<std::string>()); |
655 | 656 | } |
656 | 657 |
|
657 | | -#ifdef WIN32 |
658 | | -#define AuditDroppedTest DISABLED_AuditDroppedTest |
659 | | -#endif |
660 | 658 | TEST_P(AuditTest, AuditDroppedTest) { |
| 659 | + using namespace std::filesystem; |
661 | 660 | auto orgLogDir = mcd_env->getAuditLogDir(); |
662 | 661 | setEnabled(true); |
663 | 662 |
|
664 | 663 | auto stats = getAdminConnection().stats("audit"); |
665 | 664 | // Get the current count for dropped events: |
666 | | - const auto org_dropped = stats["dropped_events"].get<size_t>(); |
| 665 | + auto org_dropped = stats["dropped_events"].get<size_t>(); |
667 | 666 |
|
668 | 667 | auto& json = mcd_env->getAuditConfig(); |
669 | | - // Set the audit log to a path which cannot be created |
670 | | - // due to access permissions (not just the file but |
671 | | - // missing path elements in the path which needs to be |
672 | | - // created which we won't have access to create). |
673 | | - json["log_path"] = "/AuditTest/auditlog/myaudit"; |
674 | | - try { |
675 | | - mcd_env->rewriteAuditConfig(); |
676 | | - } catch (std::exception& e) { |
677 | | - FAIL() << "Failed to toggle audit state: " << e.what(); |
678 | | - } |
679 | 668 |
|
680 | | - getAdminConnection().reloadAuditConfiguration(); |
| 669 | + auto writeProtectedDir = orgLogDir / "writeprotected"; |
| 670 | + auto filesystem_permission_guard = folly::makeGuard([&writeProtectedDir]() { |
| 671 | + std::error_code ec; |
| 672 | + permissions( |
| 673 | + writeProtectedDir, perms::owner_all, perm_options::replace, ec); |
| 674 | + if (ec) { |
| 675 | + std::cerr << "Failed to restore permissions on '" |
| 676 | + << writeProtectedDir.string() << "': " << ec.message() |
| 677 | + << std::endl; |
| 678 | + } |
| 679 | + }); |
681 | 680 |
|
682 | | - stats = getAdminConnection().stats("audit"); |
683 | | - while (!stats["enabled"].get<bool>()) { |
684 | | - std::this_thread::sleep_for(std::chrono::milliseconds{10}); |
685 | | - stats = getAdminConnection().stats("audit"); |
| 681 | + create_directories(writeProtectedDir); |
| 682 | +#ifdef WIN32 |
| 683 | + // Seems like we can't remove the ability for a user to create a directory |
| 684 | + // in a directory by removing the write permission on the parent directory. |
| 685 | + // Let's just create a file with the name of the audit directory. That |
| 686 | + // should cause creation of the file to fail ;) |
| 687 | + { |
| 688 | + auto* fp = fopen((writeProtectedDir / "myaudit").string().c_str(), "w"); |
| 689 | + ASSERT_NE(nullptr, fp); |
| 690 | + fclose(fp); |
| 691 | + std::error_code ec; |
| 692 | + create_directories(writeProtectedDir / "myaudit", ec); |
| 693 | + ASSERT_TRUE(ec) << ec.message(); |
686 | 694 | } |
| 695 | +#endif |
| 696 | + // Verify that it work with both read-exec and read-only permissions |
| 697 | + // set for the directory |
| 698 | + for (const auto mode : |
| 699 | + {perms::owner_read | perms::owner_exec, perms::owner_read}) { |
| 700 | + permissions(writeProtectedDir, mode, perm_options::replace); |
| 701 | + json["log_path"] = writeProtectedDir / "myaudit"; |
| 702 | + try { |
| 703 | + mcd_env->rewriteAuditConfig(); |
| 704 | + } catch (std::exception& e) { |
| 705 | + FAIL() << "Failed to toggle audit state: " << e.what(); |
| 706 | + } |
687 | 707 |
|
688 | | - EXPECT_LT(org_dropped, stats["dropped_events"].get<size_t>()); |
| 708 | + getAdminConnection().reloadAuditConfiguration(); |
| 709 | + stats = getAdminConnection().stats("audit"); |
| 710 | + while (!stats["enabled"].get<bool>()) { |
| 711 | + std::this_thread::sleep_for(std::chrono::milliseconds{10}); |
| 712 | + stats = getAdminConnection().stats("audit"); |
| 713 | + } |
| 714 | + |
| 715 | + EXPECT_LT(org_dropped, stats["dropped_events"].get<size_t>()); |
| 716 | + org_dropped = stats["dropped_events"].get<size_t>(); |
| 717 | + } |
689 | 718 |
|
690 | 719 | // Rewrite the config back to the original one |
691 | 720 | json["log_path"] = orgLogDir; |
|
0 commit comments