You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by ti...@apache.org on 2017/06/16 19:31:08 UTC

[5/6] mesos git commit: Enabled authorization when accesing maintenance status in v0 and v1 API.

Enabled authorization when accesing maintenance status in v0 and v1 API.

Enables the use of authorization for the 'GET_MAINTENANCE_STATUS' v1
API call, as well as to the 'maintenance/status' endpoint, using the
ACL 'GetMaintenanceStatus' and the action of the same name as the API
call.

It also updates the ApiTests to take into account the authorization
case.

Review: https://reviews.apache.org/r/59101/


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/3f838a15
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/3f838a15
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/3f838a15

Branch: refs/heads/master
Commit: 3f838a15f0183bc97ffe040e3baa4942e8df90e7
Parents: 17817ef
Author: Alexander Rojas <al...@mesosphere.io>
Authored: Fri Jun 16 20:30:20 2017 +0200
Committer: Till Toenshoff <to...@me.com>
Committed: Fri Jun 16 20:30:20 2017 +0200

----------------------------------------------------------------------
 src/master/http.cpp                    | 84 +++++++++++++++++++++++------
 src/master/master.hpp                  |  4 +-
 src/tests/api_tests.cpp                | 60 ++++++++++++++++-----
 src/tests/master_maintenance_tests.cpp | 38 +++++++++++--
 4 files changed, 149 insertions(+), 37 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/3f838a15/src/master/http.cpp
----------------------------------------------------------------------
diff --git a/src/master/http.cpp b/src/master/http.cpp
index 3c4583a..0567d54 100644
--- a/src/master/http.cpp
+++ b/src/master/http.cpp
@@ -4623,14 +4623,18 @@ string Master::Http::MAINTENANCE_STATUS_HELP()
         "**NOTE**:",
         "Inverse offer responses are cleared if the master fails over.",
         "However, new inverse offers will be sent once the master recovers."),
-    AUTHENTICATION(true));
+    AUTHENTICATION(true),
+    AUTHORIZATION(
+        "The response will contain only the maintenance status for those",
+        "machines the current principal is allowed to see. If none, an empty",
+        "response will be returned."));
 }
 
 
 // /master/maintenance/status endpoint handler.
 Future<Response> Master::Http::maintenanceStatus(
     const Request& request,
-    const Option<Principal>&) const
+    const Option<Principal>& principal) const
 {
   // When current master is not the leader, redirect to the leading master.
   if (!master->elected()) {
@@ -4641,16 +4645,33 @@ Future<Response> Master::Http::maintenanceStatus(
     return MethodNotAllowed({"GET"}, request.method);
   }
 
-  return _getMaintenanceStatus()
-    .then([request](const mesos::maintenance::ClusterStatus& status)
-      -> Response {
-      return OK(JSON::protobuf(status), request.url.query.get("jsonp"));
+  Future<Owned<ObjectApprover>> approver;
+
+  if (master->authorizer.isSome()) {
+    Option<authorization::Subject> subject = createSubject(principal);
+
+    approver = master->authorizer.get()->getObjectApprover(
+        subject, authorization::GET_MAINTENANCE_STATUS);
+  } else {
+    approver = Owned<ObjectApprover>(new AcceptingObjectApprover());
+  }
+
+  Option<string> jsonp = request.url.query.get("jsonp");
+
+  return approver
+    .then(defer(
+      master->self(),
+      [this](const Owned<ObjectApprover>& approver) {
+        return _getMaintenanceStatus(approver);
+      }))
+    .then([jsonp](const mesos::maintenance::ClusterStatus& status) -> Response {
+      return OK(JSON::protobuf(status), jsonp);
     });
 }
 
 
-Future<mesos::maintenance::ClusterStatus>
-  Master::Http::_getMaintenanceStatus() const
+Future<mesos::maintenance::ClusterStatus> Master::Http::_getMaintenanceStatus(
+    const Owned<ObjectApprover>& approver) const
 {
   return master->allocator->getInverseOfferStatuses()
     .then(defer(
@@ -4667,6 +4688,21 @@ Future<mesos::maintenance::ClusterStatus>
         const MachineID& id,
         const Machine& machine,
         master->machines) {
+      ObjectApprover::Object object;
+      object.machine_id = &id;
+      Try<bool> approved = approver->approved(object);
+
+      if (approved.isError()) {
+        LOG(WARNING) << "Error during MachineID authorization: "
+                     << approved.error();
+        // TODO(arojas): Consider exposing these errors to the caller.
+        continue;
+      }
+
+      if (!approved.get()) {
+        continue;
+      }
+
       switch (machine.info.mode()) {
         case MachineInfo::DRAINING: {
           mesos::maintenance::ClusterStatus::DrainingMachine* drainingMachine =
@@ -4712,17 +4748,33 @@ Future<Response> Master::Http::getMaintenanceStatus(
 {
   CHECK_EQ(mesos::master::Call::GET_MAINTENANCE_STATUS, call.type());
 
-  return _getMaintenanceStatus()
+  Future<Owned<ObjectApprover>> approver;
+
+  if (master->authorizer.isSome()) {
+    Option<authorization::Subject> subject = createSubject(principal);
+
+    approver = master->authorizer.get()->getObjectApprover(
+        subject, authorization::GET_MAINTENANCE_STATUS);
+  } else {
+    approver = Owned<ObjectApprover>(new AcceptingObjectApprover());
+  }
+
+  return approver
+    .then(defer(
+      master->self(),
+      [this](const Owned<ObjectApprover>& approver) {
+        return _getMaintenanceStatus(approver);
+      }))
     .then([contentType](const mesos::maintenance::ClusterStatus& status)
           -> Response {
-      mesos::master::Response response;
-      response.set_type(mesos::master::Response::GET_MAINTENANCE_STATUS);
-      response.mutable_get_maintenance_status()->mutable_status()
-        ->CopyFrom(status);
+        mesos::master::Response response;
+        response.set_type(mesos::master::Response::GET_MAINTENANCE_STATUS);
+        response.mutable_get_maintenance_status()->mutable_status()
+            ->CopyFrom(status);
 
-      return OK(serialize(contentType, evolve(response)),
-                stringify(contentType));
-    });
+        return OK(serialize(contentType, evolve(response)),
+                  stringify(contentType));
+      });
 }
 
 

http://git-wip-us.apache.org/repos/asf/mesos/blob/3f838a15/src/master/master.hpp
----------------------------------------------------------------------
diff --git a/src/master/master.hpp b/src/master/master.hpp
index f81beac..cc5b151 100644
--- a/src/master/master.hpp
+++ b/src/master/master.hpp
@@ -1384,8 +1384,8 @@ private:
     mesos::maintenance::Schedule _getMaintenanceSchedule(
         const process::Owned<ObjectApprover>& approver) const;
 
-    process::Future<mesos::maintenance::ClusterStatus>
-      _getMaintenanceStatus() const;
+    process::Future<mesos::maintenance::ClusterStatus> _getMaintenanceStatus(
+        const process::Owned<ObjectApprover>& approver) const;
 
     process::Future<process::http::Response> _startMaintenance(
         const google::protobuf::RepeatedPtrField<MachineID>& machineIds) const;

http://git-wip-us.apache.org/repos/asf/mesos/blob/3f838a15/src/tests/api_tests.cpp
----------------------------------------------------------------------
diff --git a/src/tests/api_tests.cpp b/src/tests/api_tests.cpp
index 679d7b6..0bde547 100644
--- a/src/tests/api_tests.cpp
+++ b/src/tests/api_tests.cpp
@@ -1276,7 +1276,17 @@ TEST_P(MasterAPITest, UpdateAndGetMaintenanceSchedule)
 TEST_P(MasterAPITest, GetMaintenanceStatus)
 {
   // Set up a master.
-  Try<Owned<cluster::Master>> master = this->StartMaster();
+  master::Flags flags = CreateMasterFlags();
+
+  {
+    // Default principal 2 is not allowed to view any maintenance status.
+    mesos::ACL::GetMaintenanceStatus* acl =
+      flags.acls.get().add_get_maintenance_statuses();
+    acl->mutable_principals()->add_values(DEFAULT_CREDENTIAL_2.principal());
+    acl->mutable_machines()->set_type(mesos::ACL::Entity::NONE);
+  }
+
+  Try<Owned<cluster::Master>> master = StartMaster(flags);
   ASSERT_SOME(master);
 
   ContentType contentType = GetParam();
@@ -1319,20 +1329,42 @@ TEST_P(MasterAPITest, GetMaintenanceStatus)
   v1::master::Call v1GetStatusCall;
   v1GetStatusCall.set_type(v1::master::Call::GET_MAINTENANCE_STATUS);
 
-  Future<v1::master::Response> v1GetStatusResponse =
-    post(master.get()->pid, v1GetStatusCall, contentType);
+  {
+    http::Headers headers = createBasicAuthHeaders(DEFAULT_CREDENTIAL_2);
+    headers["Accept"] = stringify(contentType);
 
-  AWAIT_READY(v1GetStatusResponse);
-  ASSERT_TRUE(v1GetStatusResponse->IsInitialized());
-  ASSERT_EQ(
-      v1::master::Response::GET_MAINTENANCE_STATUS,
-      v1GetStatusResponse->type());
-
-  // Verify maintenance status matches the expectation.
-  v1::maintenance::ClusterStatus status =
-    v1GetStatusResponse->get_maintenance_status().status();
-  ASSERT_EQ(2, status.draining_machines().size());
-  ASSERT_EQ(0, status.down_machines().size());
+    Future<http::Response> response = http::post(
+        master.get()->pid,
+        "api/v1",
+        headers,
+        serialize(contentType, v1GetStatusCall),
+        stringify(contentType));
+
+    AWAIT_EXPECT_RESPONSE_STATUS_EQ(http::OK().status, response);
+
+    Try<v1::master::Response> v1Response =
+      deserialize<v1::master::Response>(contentType, response->body);
+
+    v1::maintenance::ClusterStatus status =
+      v1Response->get_maintenance_status().status();
+    ASSERT_EQ(0, status.draining_machines().size());
+    ASSERT_EQ(0, status.down_machines().size());
+  }
+
+  {
+    Future<v1::master::Response> v1Response =
+      post(master.get()->pid, v1GetStatusCall, contentType);
+
+    AWAIT_READY(v1Response);
+    ASSERT_TRUE(v1Response->IsInitialized());
+    ASSERT_EQ(v1::master::Response::GET_MAINTENANCE_STATUS, v1Response->type());
+
+    // Verify maintenance status matches the expectation.
+    v1::maintenance::ClusterStatus status =
+      v1Response->get_maintenance_status().status();
+    ASSERT_EQ(2, status.draining_machines().size());
+    ASSERT_EQ(0, status.down_machines().size());
+  }
 }
 
 

http://git-wip-us.apache.org/repos/asf/mesos/blob/3f838a15/src/tests/master_maintenance_tests.cpp
----------------------------------------------------------------------
diff --git a/src/tests/master_maintenance_tests.cpp b/src/tests/master_maintenance_tests.cpp
index 9edd74a..139c42b 100644
--- a/src/tests/master_maintenance_tests.cpp
+++ b/src/tests/master_maintenance_tests.cpp
@@ -943,7 +943,18 @@ TEST_F(MasterMaintenanceTest, BringUpMachines)
 TEST_F(MasterMaintenanceTest, MachineStatus)
 {
   // Set up a master.
-  Try<Owned<cluster::Master>> master = StartMaster();
+  master::Flags flags = CreateMasterFlags();
+
+  {
+    // Default principal 2 is not allowed to view any maintenance status.
+    mesos::ACL::GetMaintenanceStatus* acl =
+      flags.acls.get().add_get_maintenance_statuses();
+    acl->mutable_principals()->add_values(DEFAULT_CREDENTIAL_2.principal());
+    acl->mutable_machines()->set_type(mesos::ACL::Entity::NONE);
+  }
+
+  // Set up a master.
+  Try<Owned<cluster::Master>> master = StartMaster(flags);
   ASSERT_SOME(master);
 
   // Try to stop maintenance on an unscheduled machine.
@@ -958,6 +969,25 @@ TEST_F(MasterMaintenanceTest, MachineStatus)
 
   AWAIT_EXPECT_RESPONSE_STATUS_EQ(OK().status, response);
 
+  // Get the maintenance statuses for unauthorized principal.
+  response = process::http::get(
+      master.get()->pid,
+      "maintenance/status",
+      None(),
+      createBasicAuthHeaders(DEFAULT_CREDENTIAL_2));
+
+  AWAIT_EXPECT_RESPONSE_STATUS_EQ(OK().status, response);
+
+  Try<JSON::Object> statuses_ = JSON::parse<JSON::Object>(response->body);
+
+  ASSERT_SOME(statuses_);
+  Try<maintenance::ClusterStatus> statuses =
+      ::protobuf::parse<maintenance::ClusterStatus>(statuses_.get());
+
+  ASSERT_SOME(statuses);
+  ASSERT_EQ(0, statuses->draining_machines().size());
+  ASSERT_EQ(0, statuses->down_machines().size());
+
   // Get the maintenance statuses.
   response = process::http::get(
       master.get()->pid,
@@ -968,12 +998,10 @@ TEST_F(MasterMaintenanceTest, MachineStatus)
   AWAIT_EXPECT_RESPONSE_STATUS_EQ(OK().status, response);
 
   // Check that both machines are draining.
-  Try<JSON::Object> statuses_ =
-    JSON::parse<JSON::Object>(response->body);
+  statuses_ = JSON::parse<JSON::Object>(response->body);
 
   ASSERT_SOME(statuses_);
-  Try<maintenance::ClusterStatus> statuses =
-    ::protobuf::parse<maintenance::ClusterStatus>(statuses_.get());
+  statuses = ::protobuf::parse<maintenance::ClusterStatus>(statuses_.get());
 
   ASSERT_SOME(statuses);
   ASSERT_EQ(2, statuses->draining_machines().size());