From aa244baa45d8db84e98e6dca9944a3f679da70d1 Mon Sep 17 00:00:00 2001 From: Quinn Leng Date: Tue, 18 Jul 2017 17:06:56 -0700 Subject: [PATCH] Added class definition for the 'IDAcceptor'. This commit contains the class definition for 'IDAcceptor', which is used to filter IDs in the '/master/frameworks', '/master/slaves', '/master/tasks', and '/slave/containers' endpoints. Review: https://reviews.apache.org/r/60820/ --- src/common/http.cpp | 41 ----------------------------------------- src/common/http.hpp | 42 +++++++++++++++++++++--------------------- src/master/http.cpp | 27 +++++++++++++-------------- 3 files changed, 34 insertions(+), 76 deletions(-) diff --git a/src/common/http.cpp b/src/common/http.cpp index 3825a13fde9..dfd5f335e8a 100644 --- a/src/common/http.cpp +++ b/src/common/http.cpp @@ -1165,45 +1165,4 @@ Future> AuthorizationAcceptor::create( } -FrameworkIDAcceptor::FrameworkIDAcceptor( - const Option& _frameworkId) -{ - if (_frameworkId.isSome()) { - FrameworkID frameworkId_; - frameworkId_.set_value(_frameworkId.get()); - frameworkId = frameworkId_; - } -} - - -TaskIDAcceptor::TaskIDAcceptor(const Option& _taskId) -{ - if (_taskId.isSome()) { - TaskID taskId_; - taskId_.set_value(_taskId.get()); - taskId = taskId_; - } -} - - -bool FrameworkIDAcceptor::accept(const FrameworkID& _frameworkId) -{ - if (frameworkId.isSome()) { - return frameworkId.get() == _frameworkId; - } - - return true; -} - - -bool TaskIDAcceptor::accept(const TaskID& _taskId) -{ - if (taskId.isSome()) { - return taskId.get() == _taskId; - } - - return true; -} - - } // namespace mesos { diff --git a/src/common/http.hpp b/src/common/http.hpp index 4822a23bd70..ba8dda18a02 100644 --- a/src/common/http.hpp +++ b/src/common/http.hpp @@ -195,34 +195,34 @@ class AuthorizationAcceptor /** - * Filtering results based on framework ID. When no framework ID is specified - * it will accept all inputs. + * Used to filter results for API handlers. Provides the 'accept()' method to + * test whether the supplied ID is equal to a stored target ID. If no target + * ID is provided when the acceptor is constructed, it will accept all inputs. */ -class FrameworkIDAcceptor +template +class IDAcceptor { public: - FrameworkIDAcceptor(const Option& _frameworkId); - - bool accept(const FrameworkID& frameworkId); - -protected: - Option frameworkId; -}; - + IDAcceptor(const Option& id = None()) + { + if (id.isSome()) { + T targetId_; + targetId_.set_value(id.get()); + targetId = targetId_; + } + } -/** - * Filtering results based on task ID. When no task ID is specified - * it will accept all inputs. - */ -class TaskIDAcceptor -{ -public: - TaskIDAcceptor(const Option& _taskId); + bool accept(const T& candidateId) const + { + if (targetId.isNone()) { + return true; + } - bool accept(const TaskID& taskId); + return candidateId.value() == targetId->value(); + } protected: - Option taskId; + Option targetId; }; diff --git a/src/master/http.cpp b/src/master/http.cpp index 3ddb54b318c..cbe6d96a0ef 100644 --- a/src/master/http.cpp +++ b/src/master/http.cpp @@ -3900,11 +3900,10 @@ Future Master::Http::tasks( principal, master->authorizer, authorization::VIEW_TASK); - Future> selectFrameworkId = - Owned( - new FrameworkIDAcceptor(request.url.query.get("framework_id"))); - Future> selectTaskId = - Owned(new TaskIDAcceptor(request.url.query.get("task_id"))); + Future> selectFrameworkId = + IDAcceptor(request.url.query.get("framework_id")); + Future> selectTaskId = + IDAcceptor(request.url.query.get("task_id")); return collect( authorizeFrameworkInfo, @@ -3915,12 +3914,12 @@ Future Master::Http::tasks( master->self(), [=](const tuple, Owned, - Owned, - Owned>& acceptors)-> Future { + IDAcceptor, + IDAcceptor>& acceptors)-> Future { Owned authorizeFrameworkInfo; Owned authorizeTask; - Owned selectFrameworkId; - Owned selectTaskId; + IDAcceptor selectFrameworkId; + IDAcceptor selectTaskId; tie(authorizeFrameworkInfo, authorizeTask, selectFrameworkId, @@ -3931,7 +3930,7 @@ Future Master::Http::tasks( foreachvalue (Framework* framework, master->frameworks.registered) { // Skip unauthorized frameworks or frameworks without matching // framework ID. - if (!selectFrameworkId->accept(framework->id()) || + if (!selectFrameworkId.accept(framework->id()) || !authorizeFrameworkInfo->accept(framework->info)) { continue; } @@ -3943,7 +3942,7 @@ Future Master::Http::tasks( master->frameworks.completed) { // Skip unauthorized frameworks or frameworks without matching // framework ID. - if (!selectFrameworkId->accept(framework->id()) || + if (!selectFrameworkId.accept(framework->id()) || !authorizeFrameworkInfo->accept(framework->info)) { continue; } @@ -3958,7 +3957,7 @@ Future Master::Http::tasks( foreachvalue (Task* task, framework->tasks) { CHECK_NOTNULL(task); // Skip unauthorized tasks or tasks without matching task ID. - if (!selectTaskId->accept(task->task_id()) || + if (!selectTaskId.accept(task->task_id()) || !authorizeTask->accept(*task, framework->info)) { continue; } @@ -3970,7 +3969,7 @@ Future Master::Http::tasks( const Owned& task, framework->unreachableTasks) { // Skip unauthorized tasks or tasks without matching task ID. - if (!selectTaskId->accept(task.get()->task_id()) || + if (!selectTaskId.accept(task.get()->task_id()) || !authorizeTask->accept(*task.get(), framework->info)) { continue; } @@ -3980,7 +3979,7 @@ Future Master::Http::tasks( foreach (const Owned& task, framework->completedTasks) { // Skip unauthorized tasks or tasks without matching task ID. - if (!selectTaskId->accept(task.get()->task_id()) || + if (!selectTaskId.accept(task.get()->task_id()) || !authorizeTask->accept(*task.get(), framework->info)) { continue; }