From 6dcb8e5566a250f4c6b2850afb6cf9a2db640bd4 Mon Sep 17 00:00:00 2001 From: Lukasz Wojciechowski Date: Tue, 25 Nov 2014 18:15:09 +0100 Subject: [PATCH] Enhance PolicyKey and PolicyKeyFeature by ANY wildcard Enhance PolicyKey and PolicyKeyFeature classes, so they can hold new ANY wildcard as user, client or privilege. New wildcard can be used for listing and removal of many policies. It matches any PolicyKeyFeature. Minor cleanup in class: missing voids added, public section reordered Change-Id: Icad8e06b87a014fc2fbab32e0553ddd76e5bb06d --- src/common/types/PolicyKey.cpp | 11 +++-- src/common/types/PolicyKey.h | 95 +++++++++++++++++++++++++++--------------- test/types/policykey.cpp | 16 ++++--- 3 files changed, 80 insertions(+), 42 deletions(-) diff --git a/src/common/types/PolicyKey.cpp b/src/common/types/PolicyKey.cpp index 14b3bc6..ffb1400 100644 --- a/src/common/types/PolicyKey.cpp +++ b/src/common/types/PolicyKey.cpp @@ -25,15 +25,18 @@ #include +#include + namespace Cynara { -std::string PolicyKeyFeature::m_wildcardValue = "*"; +const std::string PolicyKeyFeature::m_wildcardValue = CYNARA_ADMIN_WILDCARD; +const std::string PolicyKeyFeature::m_anyValue = CYNARA_ADMIN_ANY; -const std::string &PolicyKeyFeature::toString() const { - return isWildcard() ? m_wildcardValue : value(); +const std::string &PolicyKeyFeature::toString(void) const { + return value(); } -std::string PolicyKey::toString() const { +std::string PolicyKey::toString(void) const { std::stringstream ss; ss << client().toString() << "\t" << user().toString() << "\t" diff --git a/src/common/types/PolicyKey.h b/src/common/types/PolicyKey.h index 3f65c58..4fed189 100644 --- a/src/common/types/PolicyKey.h +++ b/src/common/types/PolicyKey.h @@ -22,8 +22,8 @@ policy rule */ -#ifndef CYNARA_COMMON_TYPES_POLICYKEY_H -#define CYNARA_COMMON_TYPES_POLICYKEY_H +#ifndef CYNARA_COMMON_TYPES_POLICYKEY_H_ +#define CYNARA_COMMON_TYPES_POLICYKEY_H_ #include #include @@ -39,32 +39,59 @@ public: PolicyKeyFeature(const PolicyKeyFeature &) = default; PolicyKeyFeature(PolicyKeyFeature &&) = default; + PolicyKeyFeature& operator=(const PolicyKeyFeature &) = default; + PolicyKeyFeature& operator=(PolicyKeyFeature &&) = default; + typedef std::string ValueType; static PolicyKeyFeature create(ValueType value) { return PolicyKeyFeature(value); } - static PolicyKeyFeature createWildcard() { - return PolicyKeyFeature(); + static PolicyKeyFeature createWildcard(void) { + return PolicyKeyFeature(m_wildcardValue); + } + + static PolicyKeyFeature createAny(void) { + return PolicyKeyFeature(m_anyValue); } // TODO: Different features (client, user, privilege) // shouldn't be comparable bool operator==(const PolicyKeyFeature &other) const { - return anyWildcard(*this, other) || valuesMatch(*this, other); + return valuesMatch(*this, other); } bool operator==(const PolicyKeyFeature::ValueType &other) const { - return anyWildcard(*this, other) || valuesMatch(*this, other); + return valuesMatch(*this, PolicyKeyFeature(other)); } - const std::string &toString() const; + const std::string &toString(void) const; + + const ValueType &value(void) const { + return m_value; + } + + bool isWildcard(void) const { + return m_isWildcard; + } + + bool isAny(void) const { + return m_isAny; + } + + bool matchFilter(const PolicyKeyFeature &filter) const { + return filter.isAny() || valuesMatch(*this, filter); + } protected: - PolicyKeyFeature(const ValueType &value) : m_value(value), - m_isWildcard(value == PolicyKeyFeature::m_wildcardValue) {} - PolicyKeyFeature() : m_value(m_wildcardValue), m_isWildcard(true) {} + explicit PolicyKeyFeature(const ValueType &value) : m_value(value), + m_isWildcard(value == PolicyKeyFeature::m_wildcardValue), + m_isAny(value == PolicyKeyFeature::m_anyValue) {} + + static bool anyAny(const PolicyKeyFeature &pkf1, const PolicyKeyFeature &pkf2) { + return pkf1.isAny() || pkf2.isAny(); + } static bool anyWildcard(const PolicyKeyFeature &pkf1, const PolicyKeyFeature &pkf2) { return pkf1.isWildcard() || pkf2.isWildcard(); @@ -77,15 +104,10 @@ protected: private: ValueType m_value; bool m_isWildcard; - static std::string m_wildcardValue; + bool m_isAny; -public: - bool isWildcard() const { - return m_isWildcard; - } - const ValueType& value() const { - return m_value; - } + const static std::string m_wildcardValue; + const static std::string m_anyValue; }; class PolicyKey @@ -93,41 +115,48 @@ class PolicyKey public: PolicyKey(const PolicyKeyFeature &clientId, const PolicyKeyFeature &userId, - const PolicyKeyFeature &privilegeId) + const PolicyKeyFeature &privilegeId) : m_client(clientId), m_user(userId), m_privilege(privilegeId) {}; PolicyKey(const PolicyKeyFeature::ValueType &clientId, - const PolicyKeyFeature::ValueType &userId, - const PolicyKeyFeature::ValueType &privilegeId) + const PolicyKeyFeature::ValueType &userId, + const PolicyKeyFeature::ValueType &privilegeId) : m_client(clientId), m_user(userId), m_privilege(privilegeId) {}; PolicyKey(const PolicyKey &) = default; PolicyKey(PolicyKey &&) = default; + PolicyKey& operator=(const PolicyKey &) = default; + PolicyKey& operator=(PolicyKey &&) = default; + bool operator==(const PolicyKey &other) const { return std::tie(m_client, m_user, m_privilege) - == std::tie(other.m_client, other.m_user, other.m_privilege); + == std::tie(other.m_client, other.m_user, other.m_privilege); } - std::string toString() const; - -private: - PolicyKeyFeature m_client; - PolicyKeyFeature m_user; - PolicyKeyFeature m_privilege; + std::string toString(void) const; -public: - const PolicyKeyFeature &client() const { + const PolicyKeyFeature &client(void) const { return m_client; } - const PolicyKeyFeature &user() const { + const PolicyKeyFeature &user(void) const { return m_user; } - const PolicyKeyFeature &privilege() const { + const PolicyKeyFeature &privilege(void) const { return m_privilege; } + + bool matchFilter(const PolicyKey &filter) const { + return m_client.matchFilter(filter.m_client) && m_user.matchFilter(filter.m_user) + && m_privilege.matchFilter(filter.m_privilege); + } + +private: + PolicyKeyFeature m_client; + PolicyKeyFeature m_user; + PolicyKeyFeature m_privilege; }; bool operator ==(const PolicyKeyFeature::ValueType &pkf1, const PolicyKeyFeature &pkf2); @@ -135,4 +164,4 @@ bool operator ==(const PolicyKeyFeature::ValueType &pkf1, const PolicyKeyFeature } /* namespace Cynara */ -#endif /* CYNARA_COMMON_TYPES_POLICYKEY_H */ +#endif /* CYNARA_COMMON_TYPES_POLICYKEY_H_ */ diff --git a/test/types/policykey.cpp b/test/types/policykey.cpp index 228cb98..5556065 100644 --- a/test/types/policykey.cpp +++ b/test/types/policykey.cpp @@ -20,14 +20,13 @@ * @brief Tests for Cynara::PolicyKey */ - - #include #include #include "../helpers.h" -#include "types/PolicyKey.h" +#include +#include using namespace Cynara; @@ -38,8 +37,15 @@ TEST(PolicyKey, to_string) { ASSERT_EQ("c\tu\tp", pk1.toString()); PolicyKey pk2(PKF::createWildcard(), PKF::createWildcard(), PKF::createWildcard()); - ASSERT_EQ("*\t*\t*", pk2.toString()); + ASSERT_EQ(CYNARA_ADMIN_WILDCARD "\t" CYNARA_ADMIN_WILDCARD "\t" CYNARA_ADMIN_WILDCARD, + pk2.toString()); PolicyKey pk3(PKF::createWildcard(), PKF::create("u"), PKF::createWildcard()); - ASSERT_EQ("*\tu\t*", pk3.toString()); + ASSERT_EQ(CYNARA_ADMIN_WILDCARD "\tu\t" CYNARA_ADMIN_WILDCARD, pk3.toString()); + + PolicyKey pk4(PKF::createAny(), PKF::createAny(), PKF::createAny()); + ASSERT_EQ(CYNARA_ADMIN_ANY "\t" CYNARA_ADMIN_ANY "\t" CYNARA_ADMIN_ANY, pk4.toString()); + + PolicyKey pk5(PKF::createWildcard(), PKF::create("u"), PKF::createAny()); + ASSERT_EQ(CYNARA_ADMIN_WILDCARD "\tu\t" CYNARA_ADMIN_ANY, pk5.toString()); } -- 2.7.4