diff --git a/src/v/security/license.cc b/src/v/security/license.cc index eb6ae618cfa3e..64ec0f7138414 100644 --- a/src/v/security/license.cc +++ b/src/v/security/license.cc @@ -18,6 +18,11 @@ #include "json/validator.h" #include "utils/base64.h" +#include +#include + +using namespace std::chrono_literals; + namespace security { namespace crypto { @@ -178,9 +183,11 @@ license make_license(const ss::sstring& raw_license) { } bool license::is_expired() const noexcept { - const auto now = std::chrono::duration_cast( - std::chrono::system_clock::now().time_since_epoch()); - return now > expiry; + return clock::now() > expiration(); +} + +license::clock::time_point license::expiration() const noexcept { + return clock::time_point{expiry}; } } // namespace security diff --git a/src/v/security/license.h b/src/v/security/license.h index 474a6b84c503c..43975b3cb5d38 100644 --- a/src/v/security/license.h +++ b/src/v/security/license.h @@ -21,6 +21,7 @@ #include +#include #include #include #include @@ -67,6 +68,8 @@ inline std::ostream& operator<<(std::ostream& os, license_type lt) { struct license : serde::envelope, serde::compat_version<0>> { + using clock = std::chrono::system_clock; + /// Expected encoded contents uint8_t format_version; license_type type; @@ -84,6 +87,9 @@ struct license /// Seconds since epoch until license expiration std::chrono::seconds expires() const noexcept; + /// Expiration timepoint + clock::time_point expiration() const noexcept; + auto operator<=>(const license&) const = delete; private: diff --git a/src/v/security/tests/license_test.cc b/src/v/security/tests/license_test.cc index 2e726ba7dd8af..2a9dc64511cc3 100644 --- a/src/v/security/tests/license_test.cc +++ b/src/v/security/tests/license_test.cc @@ -13,6 +13,11 @@ #include #include #include + +#include + +using namespace std::chrono_literals; + namespace security { BOOST_AUTO_TEST_CASE(test_license_invalid_signature) { @@ -76,7 +81,10 @@ BOOST_AUTO_TEST_CASE(test_license_valid_content) { BOOST_CHECK_EQUAL(license.format_version, 0); BOOST_CHECK_EQUAL(license.type, license_type::enterprise); BOOST_CHECK_EQUAL(license.organization, "redpanda-testing"); + BOOST_CHECK(!license.is_expired()); BOOST_CHECK_EQUAL(license.expiry.count(), 4813252273); + BOOST_CHECK( + license.expiration() == license::clock::time_point{4813252273s}); BOOST_CHECK_EQUAL( license.checksum, "2730125070a934ca1067ed073d7159acc9975dc61015892308aae186f7455daf");