26#include <unordered_set>
27#include <boost/thread/locks.hpp>
29#include <drizzled/item.h>
30#include <drizzled/plugin/authorization.h>
37namespace po= boost::program_options;
45uint64_t max_cache_buckets= DEFAULT_MAX_CACHE_BUCKETS;
46uint64_t max_lru_length= DEFAULT_MAX_LRU_LENGTH;
47bool updatePolicyFile(Session *, set_var *);
48bool parsePolicyFile(std::string, PolicyItemList&, PolicyItemList&, PolicyItemList&);
51bool updatePolicyFile(Session *, set_var* var)
55 std::string newPolicyFile(var->value->
str_value.data());
56 if (policy->setPolicyFile(newPolicyFile))
61 errmsg_printf(error::ERROR, _(
"regex_policy file cannot be NULL"));
65bool parsePolicyFile(std::string new_policy_file, PolicyItemList& table_policies_dummy, PolicyItemList& schema_policies_dummy, PolicyItemList& process_policies_dummy)
67 ifstream file(new_policy_file.c_str());
68 boost::regex comment_re;
69 boost::regex empty_re;
70 boost::regex table_matches_re;
71 boost::regex process_matches_re;
72 boost::regex schema_matches_re;
73 table_policies_dummy.clear();
74 schema_policies_dummy.clear();
75 process_policies_dummy.clear();
79 comment_re= comment_regex;
80 empty_re= empty_regex;
81 table_matches_re= table_match_regex;
82 process_matches_re= process_match_regex;
83 schema_matches_re= schema_match_regex;
85 catch (
const std::exception &e)
87 errmsg_printf(error::ERROR, _(
"parsePolicyFile() %s"), e.what());
93 errmsg_printf(error::ERROR, _(
"Unable to open regex policy file: %s"), new_policy_file.c_str());
99 for (
string line; getline(file, line); )
101 if (boost::regex_match(line, comment_re))
105 if (boost::regex_match(line, empty_re))
109 boost::smatch matches;
110 PolicyItemList *policies;
111 if (boost::regex_match(line, matches, table_matches_re, boost::match_extra))
113 policies= &table_policies_dummy;
115 else if (boost::regex_match(line, matches, process_matches_re, boost::match_extra))
117 policies= &process_policies_dummy;
119 else if (boost::regex_match(line, matches, schema_matches_re, boost::match_extra))
121 policies= &schema_policies_dummy;
125 throw std::exception();
127 string user_regex= matches[MATCH_REGEX_USER_POS];
128 string object_regex= matches[MATCH_REGEX_OBJECT_POS];
129 string action= matches[MATCH_REGEX_ACTION_POS];
132 policies->push_back(
new PolicyItem(user_regex, object_regex, action));
134 catch (
const std::exception &e)
136 errmsg_printf(error::ERROR, _(
"Bad policy item: user= %s object= %s action %s"), user_regex.c_str(), object_regex.c_str(), action.c_str());
137 throw std::exception();
142 catch (
const std::exception &e)
145 errmsg_printf(error::ERROR, _(
"Unable to parse policy file %s:%s"), new_policy_file.c_str(), e.what());
151static int init(module::Context &context)
153 const module::option_map &vm= context.getOptions();
155 max_cache_buckets= vm[
"max-cache-buckets"].as<uint64_t>();
156 if (max_cache_buckets < 1)
158 errmsg_printf(error::ERROR, _(
"max-cache-buckets is too low, must be greater than 0"));
161 max_lru_length= vm[
"max-lru-length"].as<uint64_t>();
162 if (max_lru_length < 1)
164 errmsg_printf(error::ERROR, _(
"max-lru-length is too low, must be greater than 0"));
167 policy=
new Policy(vm[
"policy"].as<string>());
168 if (!policy->setPolicyFile(policy->getPolicyFile()))
170 errmsg_printf(error::ERROR, _(
"Could not load regex policy file: %s\n"),
171 (policy ? policy->getError().str().c_str() : _(
"Unknown")));
176 context.registerVariable(
new sys_var_std_string(
"policy", policy->getPolicyFile(), NULL, &updatePolicyFile));
181static void init_options(drizzled::module::option_context &context)
184 po::value<string>()->default_value(DEFAULT_POLICY_FILE.string()),
185 N_(
"File to load for regex authorization policies"));
186 context(
"max-cache-buckets",
187 po::value<uint64_t>()->default_value(DEFAULT_MAX_CACHE_BUCKETS),
188 N_(
"Maximum buckets for authorization cache"));
189 context(
"max-lru-length",
190 po::value<uint64_t>()->default_value(DEFAULT_MAX_LRU_LENGTH),
191 N_(
"Maximum number of LRU entries to track at once"));
194void Policy::setPolicies(PolicyItemList new_table_policies, PolicyItemList new_schema_policies, PolicyItemList new_process_policies)
196 policy->clearPolicies();
198 for (PolicyItemList::iterator it= new_table_policies.begin(); it!= new_table_policies.end(); it++)
199 table_policies.push_back(*it);
201 for (PolicyItemList::iterator it= new_schema_policies.begin(); it!= new_schema_policies.end(); it++)
202 schema_policies.push_back(*it);
204 for (PolicyItemList::iterator it= new_process_policies.begin(); it!= new_process_policies.end(); it++)
205 process_policies.push_back(*it);
208std::string& Policy::getPolicyFile()
210 return sysvar_policy_file;
213bool Policy::setPolicyFile(std::string &new_policy_file)
215 if (new_policy_file.empty())
217 errmsg_printf(error::ERROR, _(
"regex_policy file cannot be an empty string"));
221 PolicyItemList new_table_policies;
222 PolicyItemList new_schema_policies;
223 PolicyItemList new_process_policies;
224 if(parsePolicyFile(new_policy_file, new_table_policies, new_schema_policies, new_process_policies))
226 policy->setPolicies(new_table_policies, new_schema_policies, new_process_policies);
227 sysvar_policy_file= new_policy_file;
228 fs::path newPolicyFile(getPolicyFile());
229 policy_file= newPolicyFile;
235static void clearPolicyItemList(PolicyItemList& policies)
245 clearPolicyItemList(table_policies);
246 clearPolicyItemList(process_policies);
247 clearPolicyItemList(schema_policies);
254void Policy::clearPolicies()
256 table_policies.clear();
257 process_policies.clear();
258 schema_policies.clear();
259 table_check_cache.clear();
260 process_check_cache.clear();
261 schema_check_cache.clear();
265bool Policy::restrictObject(
const drizzled::identifier::User &user_ctx,
266 const string &obj,
const PolicyItemList &policies,
269 CheckItem c(user_ctx.username(), obj, check_cache);
270 if (!c.hasCachedResult())
272 PolicyItemList::const_iterator m= find_if(policies.begin(), policies.end(), c);
273 if (m != policies.end())
275 c.setCachedResult((*m)->isRestricted());
280 c.setCachedResult(
false);
283 return c.getCachedResult();
289 return restrictObject(user_ctx, schema.getSchemaName(), schema_policies, schema_check_cache);
295 return restrictObject(user_ctx, session_ctx.username(), process_policies, process_check_cache);
301 return restrictObject(user_ctx, table.getTableName(), table_policies, table_check_cache);
306 if (p->userMatches(user))
308 errmsg_printf(error::INSPECT, _(
"User %s matches regex\n"), user.c_str());
309 if (p->objectMatches(
object))
311 errmsg_printf(error::INSPECT, _(
"Object %s matches regex %s (%s)\n"),
313 p->getObject().c_str(),
317 errmsg_printf(error::INSPECT, _(
"Object %s NOT restricted by regex %s (%s)\n"),
319 p->getObject().c_str(),
325CheckItem::CheckItem(
const std::string &user_in,
const std::string &obj_in,
CheckMap &check_cache_in)
326 : user(user_in), object(obj_in), has_cached_result(false), check_cache(check_cache_in)
328 key= user +
"_" + object;
330 if (
bool* check_val= check_cache.find(key))
333 cached_result= *check_val;
334 has_cached_result= true;
338bool* CheckMap::find(std::string
const &k)
341 boost::mutex::scoped_lock lock(lru_mutex);
343 if (lru.size() > max_lru_length)
346 uint64_t size_halfway= lru.size() / 2;
347 LruList::iterator halfway= lru.begin();
348 halfway += size_halfway;
349 std::unordered_set<std::string> uniqs;
350 uniqs.insert(lru.begin(), halfway);
353 if (size_halfway < uniqs.size())
355 lru.erase(lru.begin(), halfway);
358 lru.insert(lru.begin(), uniqs.begin(), uniqs.end());
362 boost::shared_lock<boost::shared_mutex> map_lock(map_mutex);
363 return find_ptr(map, k);
366void CheckMap::insert(std::string
const &k,
bool v)
368 boost::unique_lock<boost::shared_mutex> map_lock(map_mutex);
372 if (map.bucket_count() > max_cache_buckets)
375 std::unordered_set<std::string> found;
378 boost::mutex::scoped_lock lock(lru_mutex);
379 for (LruList::reverse_iterator x= lru.rbegin(); x < lru.rend(); ++x)
381 if (found.find(*x) == found.end())
384 if (found.size() >= max_cache_buckets)
396 if (map.bucket_count() > max_cache_buckets)
402 map.erase(*(lru.begin()));
407 errmsg_printf(error::WARN,
408 _(
"Unable to reduce size of cache below max buckets (current buckets=%" PRIu64
")"),
409 static_cast<uint64_t
>(map.bucket_count()));
417void CheckItem::setCachedResult(
bool result)
419 check_cache.insert(key, result);
420 has_cached_result=
true;
421 cached_result= result;
426DRIZZLE_DECLARE_PLUGIN
432 N_(
"Authorization using a regex-matched policy file"),
436 regex_policy::init_options
438DRIZZLE_DECLARE_PLUGIN_END;
A set of Session members describing the current authenticated user.
virtual bool restrictSchema(const drizzled::identifier::User &user_ctx, const drizzled::identifier::Schema &schema)
virtual bool restrictTable(const drizzled::identifier::User &user_ctx, const drizzled::identifier::Table &table)
virtual bool restrictProcess(const drizzled::identifier::User &user_ctx, const drizzled::identifier::User &session_ctx)
TODO: Rename this file - func.h is stupid.
An Proxy Wrapper around boost::program_options::variables_map.