You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by vi...@apache.org on 2014/04/25 21:56:45 UTC
git commit: Added authentication support for slaves.
Repository: mesos
Updated Branches:
refs/heads/master f41c8242f -> 55739f3a9
Added authentication support for slaves.
Review: https://reviews.apache.org/r/18381
Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/55739f3a
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/55739f3a
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/55739f3a
Branch: refs/heads/master
Commit: 55739f3a98be8484d4c52d06208e46a0381ed6c6
Parents: f41c824
Author: Adam B <ad...@mesosphere.io>
Authored: Fri Apr 25 12:56:31 2014 -0700
Committer: Vinod Kone <vi...@twitter.com>
Committed: Fri Apr 25 12:56:31 2014 -0700
----------------------------------------------------------------------
include/mesos/mesos.proto | 4 +-
src/master/flags.hpp | 12 +-
src/master/master.cpp | 100 +++++++++--
src/master/master.hpp | 7 +-
src/sasl/authenticatee.hpp | 7 +-
src/slave/flags.hpp | 7 +
src/slave/slave.cpp | 147 ++++++++++++++++-
src/slave/slave.hpp | 22 +++
src/tests/authentication_tests.cpp | 283 +++++++++++++++++++++++++++++++-
src/tests/mesos.cpp | 33 +++-
src/tests/slave_recovery_tests.cpp | 7 +-
11 files changed, 591 insertions(+), 38 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/mesos/blob/55739f3a/include/mesos/mesos.proto
----------------------------------------------------------------------
diff --git a/include/mesos/mesos.proto b/include/mesos/mesos.proto
index de02253..52257ec 100644
--- a/include/mesos/mesos.proto
+++ b/include/mesos/mesos.proto
@@ -487,8 +487,8 @@ message Parameters {
/**
* Credential used for authentication.
*
- * NOTE: The 'principal' is used for authenticating the framework with
- * the master. This is different from 'FrameworkInfo.user'
+ * NOTE: The 'principal' is used for authenticating the framework or slave
+ * with the master. This is different from 'FrameworkInfo.user'
* which is used to determine the user under which the framework's
* executors/tasks are run.
*/
http://git-wip-us.apache.org/repos/asf/mesos/blob/55739f3a/src/master/flags.hpp
----------------------------------------------------------------------
diff --git a/src/master/flags.hpp b/src/master/flags.hpp
index 26a0492..56e2459 100644
--- a/src/master/flags.hpp
+++ b/src/master/flags.hpp
@@ -183,13 +183,20 @@ public:
"of the form 'role=weight,role=weight'. Weights\n"
"are used to indicate forms of priority.");
- add(&Flags::authenticate,
+ // TODO(adam-mesos): Deprecate --authenticate for --authenticate_frameworks.
+ add(&Flags::authenticate_frameworks,
"authenticate",
"If authenticate is 'true' only authenticated frameworks are allowed\n"
"to register. If 'false' unauthenticated frameworks are also\n"
"allowed to register.",
false);
+ add(&Flags::authenticate_slaves,
+ "authenticate_slaves",
+ "If 'true' only authenticated slaves are allowed to register.\n"
+ "If 'false' unauthenticated slaves are also allowed to register.",
+ false);
+
add(&Flags::credentials,
"credentials",
"Path to a file with a list of credentials.\n"
@@ -218,7 +225,8 @@ public:
Option<std::string> cluster;
Option<std::string> roles;
Option<std::string> weights;
- bool authenticate;
+ bool authenticate_frameworks;
+ bool authenticate_slaves;
Option<std::string> credentials;
};
http://git-wip-us.apache.org/repos/asf/mesos/blob/55739f3a/src/master/master.cpp
----------------------------------------------------------------------
diff --git a/src/master/master.cpp b/src/master/master.cpp
index 890873b..f205dca 100644
--- a/src/master/master.cpp
+++ b/src/master/master.cpp
@@ -290,7 +290,6 @@ void Master::initialize()
if (limit.isError()) {
EXIT(1) << "Invalid value '" << flags.recovery_slave_removal_limit << "' "
<< "for --recovery_slave_removal_percent_limit: " << limit.error();
-
}
if (limit.get() < 0.0 || limit.get() > 100.0) {
@@ -300,11 +299,16 @@ void Master::initialize()
}
// Log authentication state.
- if (flags.authenticate) {
+ if (flags.authenticate_frameworks) {
LOG(INFO) << "Master only allowing authenticated frameworks to register";
} else {
LOG(INFO) << "Master allowing unauthenticated frameworks to register";
}
+ if (flags.authenticate_slaves) {
+ LOG(INFO) << "Master only allowing authenticated slaves to register";
+ } else {
+ LOG(INFO) << "Master allowing unauthenticated slaves to register";
+ }
// Load credentials.
if (flags.credentials.isSome()) {
@@ -319,7 +323,7 @@ void Master::initialize()
// Give Authenticator access to credentials.
sasl::secrets::load(credentials.get());
- } else if (flags.authenticate) {
+ } else if (flags.authenticate_frameworks || flags.authenticate_slaves) {
EXIT(1) << "Authentication requires a credentials file"
<< " (see --credentials flag)";
}
@@ -954,7 +958,7 @@ void Master::registerFramework(
return;
}
- if (flags.authenticate && !authenticated.contains(from)) {
+ if (flags.authenticate_frameworks && !authenticated.contains(from)) {
// This could happen if another authentication request came
// through before we are here or if a framework tried to register
// without authentication.
@@ -1038,12 +1042,12 @@ void Master::reregisterFramework(
return;
}
- if (flags.authenticate && !authenticated.contains(from)) {
+ if (flags.authenticate_frameworks && !authenticated.contains(from)) {
// This could happen if another authentication request came
// through before we are here or if a framework tried to
// re-register without authentication.
- LOG(WARNING) << "Refusing registration of framework at " << from
- << " because it is not authenticated";
+ LOG(WARNING) << "Refusing re-registration of framework at " << from
+ << " because it is not authenticated";
FrameworkErrorMessage message;
message.set_message("Framework '" + frameworkInfo.id().value() + "' at " +
stringify(from) + " is not authenticated.");
@@ -1262,6 +1266,10 @@ void Master::disconnect(Slave* slave)
slave->disconnected = true;
allocator->slaveDisconnected(slave->id);
+ // Remove the slave from authenticated. This is safe because
+ // a slave will always reauthenticate before (re-)registering.
+ authenticated.erase(slave->pid);
+
// If a slave is checkpointing, remove all non-checkpointing
// frameworks from the slave.
// First, collect all the frameworks running on this slave.
@@ -1494,9 +1502,10 @@ struct ExecutorInfoChecker : TaskInfoVisitor
}
if (task.has_executor()) {
- if (slave.hasExecutor(framework.id, task.executor().executor_id())) {
+ const ExecutorID& executorId = task.executor().executor_id();
+ if (slave.hasExecutor(framework.id, executorId)) {
const Option<ExecutorInfo> executorInfo =
- slave.executors.get(framework.id).get().get(task.executor().executor_id());
+ slave.executors.get(framework.id).get().get(executorId);
if (!(task.executor() == executorInfo.get())) {
return "Task has invalid ExecutorInfo (existing ExecutorInfo"
@@ -2061,6 +2070,27 @@ void Master::registerSlave(const UPID& from, const SlaveInfo& slaveInfo)
{
++metrics.slave_registration_messages;
+ if (authenticating.contains(from)) {
+ LOG(INFO) << "Queuing up registration request from " << from
+ << " because authentication is still in progress";
+
+ authenticating[from]
+ .onReady(defer(self(), &Self::registerSlave, from, slaveInfo));
+ return;
+ }
+
+ if (flags.authenticate_slaves && !authenticated.contains(from)) {
+ // This could happen if another authentication request came
+ // through before we are here or if a slave tried to register
+ // without authentication.
+ LOG(WARNING) << "Refusing registration of slave at " << from
+ << " because it is not authenticated";
+ ShutdownMessage message;
+ message.set_message("Slave is not authenticated");
+ send(from, message);
+ return;
+ }
+
// Check if this slave is already registered (because it retries).
foreachvalue (Slave* slave, slaves.activated) {
if (slave->pid == from) {
@@ -2154,6 +2184,35 @@ void Master::reregisterSlave(
{
++metrics.slave_reregistration_messages;
+ if (authenticating.contains(from)) {
+ LOG(INFO) << "Queuing up re-registration request from " << from
+ << " because authentication is still in progress";
+
+ authenticating[from]
+ .onReady(defer(self(),
+ &Self::reregisterSlave,
+ from,
+ slaveId,
+ slaveInfo,
+ executorInfos,
+ tasks,
+ completedFrameworks));
+ return;
+ }
+
+ if (flags.authenticate_slaves && !authenticated.contains(from)) {
+ // This could happen if another authentication request came
+ // through before we are here or if a slave tried to
+ // re-register without authentication.
+ LOG(WARNING) << "Refusing re-registration of slave at " << from
+ << " because it is not authenticated";
+ ShutdownMessage message;
+ message.set_message("Slave is not authenticated");
+ send(from, message);
+ return;
+ }
+
+
if (slaves.deactivated.get(slaveInfo.id()).isSome()) {
// To compensate for the case where a non-strict registrar is
// being used, we explicitly deny deactivated slaves from
@@ -2696,13 +2755,25 @@ void Master::offer(const FrameworkID& frameworkId,
// 'authenticate' message doesn't contain the 'FrameworkID'.
void Master::authenticate(const UPID& from, const UPID& pid)
{
- // Deactivate the framework if it's already registered.
+ // Deactivate the framework/slave if it's already registered.
+ // TODO(adam-mesos): MESOS-1081: Do not deactivate the current
+ // framework/slave before we find out if the new one is legit.
+ bool found = false;
foreachvalue (Framework* framework, frameworks.activated) {
if (framework->pid == pid) {
deactivate(framework);
+ found = true;
break;
}
}
+ if (!found) {
+ foreachvalue (Slave* slave, slaves.activated) {
+ if (slave->pid == pid) {
+ disconnect(slave);
+ break;
+ }
+ }
+ }
authenticated.erase(pid);
@@ -2721,7 +2792,7 @@ void Master::authenticate(const UPID& from, const UPID& pid)
return;
}
- LOG(INFO) << "Authenticating framework at " << pid;
+ LOG(INFO) << "Authenticating " << pid;
// Create a promise to capture the entire "authenticating"
// procedure. We'll set this _after_ we finish _authenticate.
@@ -2756,12 +2827,12 @@ void Master::_authenticate(
? "Refused authentication"
: (future.isFailed() ? future.failure() : "future discarded");
- LOG(WARNING) << "Failed to authenticate framework at " << pid
+ LOG(WARNING) << "Failed to authenticate " << pid
<< ": " << error;
promise->fail(error);
} else {
- LOG(INFO) << "Successfully authenticated framework at " << pid;
+ LOG(INFO) << "Successfully authenticated " << pid;
promise->set(Nothing());
authenticated.insert(pid);
@@ -3147,7 +3218,6 @@ void Master::removeFramework(Framework* framework)
roles[framework->info.role()]->removeFramework(framework);
-
// Remove the framework from authenticated.
authenticated.erase(framework->pid);
@@ -3408,6 +3478,7 @@ void Master::removeSlave(Slave* slave)
slaves.removing.insert(slave->id);
slaves.activated.erase(slave->id);
slaves.deactivated.put(slave->id, Nothing());
+ authenticated.erase(slave->pid);
// Kill the slave observer.
terminate(slave->observer);
@@ -3416,7 +3487,6 @@ void Master::removeSlave(Slave* slave)
// TODO(benh): unlink(slave->pid);
-
// Remove this slave from the registrar. Once this is completed, we
// can forward the LOST task updates to the frameworks and notify
// all frameworks that this slave was lost.
http://git-wip-us.apache.org/repos/asf/mesos/blob/55739f3a/src/master/master.hpp
----------------------------------------------------------------------
diff --git a/src/master/master.hpp b/src/master/master.hpp
index 9808378..333e37c 100644
--- a/src/master/master.hpp
+++ b/src/master/master.hpp
@@ -271,6 +271,7 @@ protected:
// remove its offers and reallocate its resources.
void removeFramework(Slave* slave, Framework* framework);
+ // TODO(adam-mesos): Rename deactivate to disconnect, or v.v.
void deactivate(Framework* framework);
void disconnect(Slave* slave);
@@ -439,14 +440,14 @@ private:
hashmap<std::string, Role*> roles;
- // Frameworks that are currently in the process of authentication.
- // 'authenticating' future for a framework is ready when it is
+ // Frameworks/slaves that are currently in the process of authentication.
+ // 'authenticating' future for an authenticatee is ready when it is
// authenticated.
hashmap<process::UPID, process::Future<Nothing> > authenticating;
hashmap<process::UPID, process::Owned<sasl::Authenticator> > authenticators;
- // Authenticated frameworks keyed by framework's PID.
+ // Authenticated frameworks/slaves keyed by PID.
hashset<process::UPID> authenticated;
int64_t nextFrameworkId; // Used to give each framework a unique ID.
http://git-wip-us.apache.org/repos/asf/mesos/blob/55739f3a/src/sasl/authenticatee.hpp
----------------------------------------------------------------------
diff --git a/src/sasl/authenticatee.hpp b/src/sasl/authenticatee.hpp
index 42a4eba..dd68704 100644
--- a/src/sasl/authenticatee.hpp
+++ b/src/sasl/authenticatee.hpp
@@ -377,7 +377,7 @@ private:
};
-Authenticatee::Authenticatee(
+inline Authenticatee::Authenticatee(
const Credential& credential,
const process::UPID& client)
{
@@ -386,7 +386,7 @@ Authenticatee::Authenticatee(
}
-Authenticatee::~Authenticatee()
+inline Authenticatee::~Authenticatee()
{
process::terminate(process);
process::wait(process);
@@ -394,7 +394,8 @@ Authenticatee::~Authenticatee()
}
-process::Future<bool> Authenticatee::authenticate(const process::UPID& pid)
+inline process::Future<bool> Authenticatee::authenticate(
+ const process::UPID& pid)
{
return process::dispatch(process, &AuthenticateeProcess::authenticate, pid);
}
http://git-wip-us.apache.org/repos/asf/mesos/blob/55739f3a/src/slave/flags.hpp
----------------------------------------------------------------------
diff --git a/src/slave/flags.hpp b/src/slave/flags.hpp
index 55cf671..f38de51 100644
--- a/src/slave/flags.hpp
+++ b/src/slave/flags.hpp
@@ -204,6 +204,12 @@ public:
"no cgroup limits are set, they are inherited from the root mesos\n"
"cgroup.");
#endif
+
+ add(&Flags::credential,
+ "credential",
+ "Path to a file containing a single line with\n"
+ "the 'principal' and 'secret' separated by whitespace.\n"
+ "Path could be of the form 'file:///path/to/file' or '/path/to/file'");
}
bool version;
@@ -234,6 +240,7 @@ public:
bool cgroups_enable_cfs;
Option<std::string> slave_subsystems;
#endif
+ Option<std::string> credential;
};
} // namespace mesos {
http://git-wip-us.apache.org/repos/asf/mesos/blob/55739f3a/src/slave/slave.cpp
----------------------------------------------------------------------
diff --git a/src/slave/slave.cpp b/src/slave/slave.cpp
index 6bcf64f..cb80609 100644
--- a/src/slave/slave.cpp
+++ b/src/slave/slave.cpp
@@ -63,8 +63,12 @@
#include "common/protobuf_utils.hpp"
#include "common/type_utils.hpp"
+#include "credentials/credentials.hpp"
+
#include "logging/logging.hpp"
+#include "sasl/authenticatee.hpp"
+
#include "slave/constants.hpp"
#include "slave/flags.hpp"
#include "slave/paths.hpp"
@@ -107,7 +111,12 @@ Slave::Slave(const slave::Flags& _flags,
monitor(containerizer),
statusUpdateManager(new StatusUpdateManager()),
metaDir(paths::getMetaRootDir(flags.work_dir)),
- recoveryErrors(0) {}
+ recoveryErrors(0),
+ credential(None()),
+ authenticatee(NULL),
+ authenticating(None()),
+ authenticated(false),
+ reauthenticate(false) {}
Slave::~Slave()
@@ -121,6 +130,7 @@ Slave::~Slave()
delete framework;
}
+ delete authenticatee;
delete statusUpdateManager;
}
@@ -207,6 +217,22 @@ void Slave::initialize()
}
#endif // __linux__
+ if (flags.credential.isSome()) {
+ const string& path = flags.credential.get();
+ Result<vector<Credential> > credentials = credentials::read(path);
+ if (credentials.isError()) {
+ EXIT(1) << credentials.error() << " (see --credential flag)";
+ } else if (credentials.isNone()) {
+ EXIT(1) << "Empty credential file " << path << " (see --credential flag)";
+ } else if (credentials.get().size() != 1) {
+ EXIT(1) << "Not expecting multiple credentials (see --credential flag)";
+ } else {
+ credential = credentials.get()[0];
+ LOG(INFO) << "Slave using credential for: "
+ << credential.get().principal();
+ }
+ }
+
// Ensure slave work directory exists.
CHECK_SOME(os::mkdir(flags.work_dir))
<< "Failed to create slave work directory '" << flags.work_dir << "'";
@@ -515,7 +541,15 @@ void Slave::detected(const Future<Option<MasterInfo> >& _master)
return;
}
- doReliableRegistration();
+ if (credential.isSome()) {
+ // Authenticate with the master.
+ authenticate();
+ } else {
+ // Proceed with registration without authentication.
+ LOG(INFO) << "No credentials provided."
+ << " Attempting to register without authentication";
+ doReliableRegistration();
+ }
} else {
LOG(INFO) << "Lost leading master";
}
@@ -527,6 +561,105 @@ void Slave::detected(const Future<Option<MasterInfo> >& _master)
}
+void Slave::authenticate()
+{
+ authenticated = false;
+
+ if (master.isNone()) {
+ return;
+ }
+
+ if (authenticating.isSome()) {
+ // Authentication is in progress. Try to cancel it.
+ // Note that it is possible that 'authenticating' is ready
+ // and the dispatch to '_authenticate' is enqueued when we
+ // are here, making the 'discard' here a no-op. This is ok
+ // because we set 'reauthenticate' here which enforces a retry
+ // in '_authenticate'.
+ Future<bool> authenticating_ = authenticating.get();
+ authenticating_.discard();
+ reauthenticate = true;
+ return;
+ }
+
+ LOG(INFO) << "Authenticating with master " << master.get();
+
+ CHECK_SOME(credential);
+
+ CHECK(authenticatee == NULL);
+ authenticatee = new sasl::Authenticatee(credential.get(), self());
+
+ authenticating = authenticatee->authenticate(master.get())
+ .onAny(defer(self(), &Self::_authenticate));
+
+ delay(Seconds(5),
+ self(),
+ &Self::authenticationTimeout,
+ authenticating.get());
+}
+
+
+void Slave::_authenticate()
+{
+ delete CHECK_NOTNULL(authenticatee);
+ authenticatee = NULL;
+
+ CHECK_SOME(authenticating);
+ const Future<bool>& future = authenticating.get();
+
+ if (master.isNone()) {
+ LOG(INFO) << "Ignoring _authenticate because the master is lost";
+ authenticating = None();
+ // Set it to false because we do not want further retries until
+ // a new master is detected.
+ // We obviously do not need to reauthenticate either even if
+ // 'reauthenticate' is currently true because the master is
+ // lost.
+ reauthenticate = false;
+ return;
+ }
+
+ if (reauthenticate || !future.isReady()) {
+ LOG(WARNING)
+ << "Failed to authenticate with master " << master.get() << ": "
+ << (reauthenticate ? "master changed" :
+ (future.isFailed() ? future.failure() : "future discarded"));
+
+ authenticating = None();
+ reauthenticate = false;
+
+ // TODO(vinod): Add a limit on number of retries.
+ dispatch(self(), &Self::authenticate); // Retry.
+ return;
+ }
+
+ if (!future.get()) {
+ LOG(ERROR) << "Master " << master.get() << " refused authentication";
+ shutdown(UPID(), "Master refused authentication");
+ return;
+ }
+
+ LOG(INFO) << "Successfully authenticated with master " << master.get();
+
+ authenticated = true;
+ authenticating = None();
+
+ doReliableRegistration(); // Proceed with registration.
+}
+
+
+void Slave::authenticationTimeout(Future<bool> future)
+{
+ // NOTE: Discarded future results in a retry in '_authenticate()'.
+ // Also note that a 'discard' here is safe even if another
+ // authenticator is in progress because this copy of the future
+ // corresponds to the original authenticator that started the timer.
+ if (future.discard()) { // This is a no-op if the future is already ready.
+ LOG(WARNING) << "Authentication timed out";
+ }
+}
+
+
void Slave::registered(const UPID& from, const SlaveID& slaveId)
{
if (master != from) {
@@ -630,6 +763,11 @@ void Slave::doReliableRegistration(const Duration& duration)
return;
}
+ if (credential.isSome() && !authenticated) {
+ LOG(INFO) << "Skipping registration because not authenticated";
+ return;
+ }
+
if (state == RUNNING) { // Slave (re-)registered with the master.
return;
}
@@ -768,8 +906,9 @@ void Slave::runTask(
<< " for framework " << frameworkId;
if (!(task.slave_id() == info.id())) {
- LOG(WARNING) << "Slave " << info.id() << " ignoring task " << task.task_id()
- << " because it was intended for old slave " << task.slave_id();
+ LOG(WARNING)
+ << "Slave " << info.id() << " ignoring task " << task.task_id()
+ << " because it was intended for old slave " << task.slave_id();
return;
}
http://git-wip-us.apache.org/repos/asf/mesos/blob/55739f3a/src/slave/slave.hpp
----------------------------------------------------------------------
diff --git a/src/slave/slave.hpp b/src/slave/slave.hpp
index 90b2c83..ed20dca 100644
--- a/src/slave/slave.hpp
+++ b/src/slave/slave.hpp
@@ -68,6 +68,10 @@ namespace internal {
class MasterDetector; // Forward declaration.
+namespace sasl {
+class Authenticatee;
+} // namespace sasl {
+
namespace slave {
using namespace process;
@@ -229,6 +233,8 @@ public:
Nothing detachFile(const std::string& path);
+ void authenticate();
+
// Helper routine to lookup a framework.
Framework* getFramework(const FrameworkID& frameworkId);
@@ -291,6 +297,9 @@ public:
Future<Nothing> garbageCollect(const std::string& path);
private:
+ void _authenticate();
+ void authenticationTimeout(process::Future<bool> future);
+
// Inner class used to namespace HTTP route handlers (see
// slave/http.cpp for implementations).
class Http
@@ -366,6 +375,19 @@ private:
// Indicates the number of errors ignored in "--no-strict" recovery mode.
unsigned int recoveryErrors;
+
+ Option<Credential> credential;
+
+ sasl::Authenticatee* authenticatee;
+
+ // Indicates if an authentication attempt is in progress.
+ Option<Future<bool> > authenticating;
+
+ // Indicates if the authentication is successful.
+ bool authenticated;
+
+ // Indicates if a new authentication attempt should be enforced.
+ bool reauthenticate;
};
http://git-wip-us.apache.org/repos/asf/mesos/blob/55739f3a/src/tests/authentication_tests.cpp
----------------------------------------------------------------------
diff --git a/src/tests/authentication_tests.cpp b/src/tests/authentication_tests.cpp
index 127c5e6..b60e595 100644
--- a/src/tests/authentication_tests.cpp
+++ b/src/tests/authentication_tests.cpp
@@ -74,12 +74,37 @@ TEST_F(AuthenticationTest, UnauthenticatedFramework)
}
-// This test verifies that when the master is started with
+// This test verifies that an unauthenticated slave is
+// denied registration by the master.
+TEST_F(AuthenticationTest, UnauthenticatedSlave)
+{
+ Try<PID<Master> > master = StartMaster();
+ ASSERT_SOME(master);
+
+ Future<ShutdownMessage> shutdownMessage =
+ FUTURE_PROTOBUF(ShutdownMessage(), _, _);
+
+ // Start the slave without credentials.
+ slave::Flags flags = CreateSlaveFlags();
+ flags.credential = None();
+
+ Try<PID<Slave> > slave = StartSlave(flags);
+ ASSERT_SOME(slave);
+
+ // Slave should get error message from the master.
+ AWAIT_READY(shutdownMessage);
+ ASSERT_NE("", shutdownMessage.get().message());
+
+ Shutdown();
+}
+
+
+// This test verifies that when the master is started with framework
// authentication disabled, it registers unauthenticated frameworks.
-TEST_F(AuthenticationTest, DisableAuthentication)
+TEST_F(AuthenticationTest, DisableFrameworkAuthentication)
{
master::Flags flags = CreateMasterFlags();
- flags.authenticate = false; // Disable authentcation.
+ flags.authenticate_frameworks = false; // Disable authentication.
Try<PID<Master> > master = StartMaster(flags);
ASSERT_SOME(master);
@@ -104,12 +129,40 @@ TEST_F(AuthenticationTest, DisableAuthentication)
}
+// This test verifies that when the master is started with slave
+// authentication disabled, it registers unauthenticated slaves.
+TEST_F(AuthenticationTest, DisableSlaveAuthentication)
+{
+ master::Flags flags = CreateMasterFlags();
+ flags.authenticate_slaves = false; // Disable authentication.
+
+ Try<PID<Master> > master = StartMaster(flags);
+ ASSERT_SOME(master);
+
+ Future<SlaveRegisteredMessage> slaveRegisteredMessage =
+ FUTURE_PROTOBUF(SlaveRegisteredMessage(), _, _);
+
+ // Start the slave without credentials.
+ slave::Flags slaveFlags = CreateSlaveFlags();
+ slaveFlags.credential = None();
+
+ Try<PID<Slave> > slave = StartSlave(slaveFlags);
+ ASSERT_SOME(slave);
+
+ // Slave should be able to get registered.
+ AWAIT_READY(slaveRegisteredMessage);
+ ASSERT_NE("", slaveRegisteredMessage.get().slave_id().value());
+
+ Shutdown();
+}
+
+
// This test verifies that when the master is started with
// authentication disabled, it registers authenticated frameworks.
TEST_F(AuthenticationTest, AuthenticatedFramework)
{
master::Flags flags = CreateMasterFlags();
- flags.authenticate = false; // Disable authentcation.
+ flags.authenticate_frameworks = false; // Disable authentication.
Try<PID<Master> > master = StartMaster(flags);
ASSERT_SOME(master);
@@ -135,9 +188,35 @@ TEST_F(AuthenticationTest, AuthenticatedFramework)
}
+// This test verifies that when the master is started with slave
+// authentication disabled, it registers authenticated slaves.
+TEST_F(AuthenticationTest, AuthenticatedSlave)
+{
+ master::Flags flags = CreateMasterFlags();
+ flags.authenticate_slaves = false; // Disable authentication.
+
+ Try<PID<Master> > master = StartMaster(flags);
+ ASSERT_SOME(master);
+
+ Future<SlaveRegisteredMessage> slaveRegisteredMessage =
+ FUTURE_PROTOBUF(SlaveRegisteredMessage(), _, _);
+
+ // Start the slave with credentials.
+ slave::Flags slaveFlags = CreateSlaveFlags();
+ Try<PID<Slave> > slave = StartSlave(slaveFlags);
+ ASSERT_SOME(slave);
+
+ // Slave should be able to get registered.
+ AWAIT_READY(slaveRegisteredMessage);
+ ASSERT_NE("", slaveRegisteredMessage.get().slave_id().value());
+
+ Shutdown();
+}
+
+
// This test verifies that the framework properly retries
// authentication when authenticate message is lost.
-TEST_F(AuthenticationTest, RetryAuthentication)
+TEST_F(AuthenticationTest, RetryFrameworkAuthentication)
{
Try<PID<Master> > master = StartMaster();
ASSERT_SOME(master);
@@ -174,6 +253,40 @@ TEST_F(AuthenticationTest, RetryAuthentication)
}
+// This test verifies that the slave properly retries
+// authentication when authenticate message is lost.
+TEST_F(AuthenticationTest, RetrySlaveAuthentication)
+{
+ Try<PID<Master> > master = StartMaster();
+ ASSERT_SOME(master);
+
+ // Drop the first authenticate message from the slave.
+ Future<AuthenticateMessage> authenticateMessage =
+ DROP_PROTOBUF(AuthenticateMessage(), _, _);
+
+ slave::Flags slaveFlags = CreateSlaveFlags();
+ Try<PID<Slave> > slave = StartSlave(slaveFlags);
+ ASSERT_SOME(slave);
+
+ AWAIT_READY(authenticateMessage);
+
+ Future<SlaveRegisteredMessage> slaveRegisteredMessage =
+ FUTURE_PROTOBUF(SlaveRegisteredMessage(), _, _);
+
+ // Advance the clock for the slave to retry.
+ Clock::pause();
+ Clock::advance(Seconds(5));
+ Clock::settle();
+ Clock::resume();
+
+ // Slave should be able to get registered.
+ AWAIT_READY(slaveRegisteredMessage);
+ ASSERT_NE("", slaveRegisteredMessage.get().slave_id().value());
+
+ Shutdown();
+}
+
+
// This test verifies that the framework properly retries
// authentication when an intermediate message in SASL protocol
// is lost.
@@ -220,6 +333,47 @@ TEST_F(AuthenticationTest, DropIntermediateSASLMessage)
}
+// This test verifies that the slave properly retries
+// authentication when an intermediate message in SASL protocol
+// is lost.
+TEST_F(AuthenticationTest, DropIntermediateSASLMessageForSlave)
+{
+ Try<PID<Master> > master = StartMaster();
+ ASSERT_SOME(master);
+
+ // Drop the AuthenticationStepMessage from authenticator.
+ Future<AuthenticationStepMessage> authenticationStepMessage =
+ DROP_PROTOBUF(AuthenticationStepMessage(), _, _);
+
+ slave::Flags slaveFlags = CreateSlaveFlags();
+ Try<PID<Slave> > slave = StartSlave(slaveFlags);
+ ASSERT_SOME(slave);
+
+ AWAIT_READY(authenticationStepMessage);
+
+ Future<AuthenticationCompletedMessage> authenticationCompletedMessage =
+ FUTURE_PROTOBUF(AuthenticationCompletedMessage(), _, _);
+
+ Future<SlaveRegisteredMessage> slaveRegisteredMessage =
+ FUTURE_PROTOBUF(SlaveRegisteredMessage(), _, _);
+
+ // Advance the clock for the slave to retry.
+ Clock::pause();
+ Clock::advance(Seconds(5));
+ Clock::settle();
+ Clock::resume();
+
+ // Ensure another authentication attempt was made.
+ AWAIT_READY(authenticationCompletedMessage);
+
+ // Slave should be able to get registered.
+ AWAIT_READY(slaveRegisteredMessage);
+ ASSERT_NE("", slaveRegisteredMessage.get().slave_id().value());
+
+ Shutdown();
+}
+
+
// This test verifies that the framework properly retries
// authentication when the final message in SASL protocol
// is lost. The dropped message causes the master to think
@@ -269,7 +423,51 @@ TEST_F(AuthenticationTest, DropFinalSASLMessage)
}
-// This test verifies that when a master fails over while an
+// This test verifies that the slave properly retries
+// authentication when the final message in SASL protocol
+// is lost. The dropped message causes the master to think
+// the slave is authenticated but the slave to think
+// otherwise. The slave should retry authentication and
+// eventually register.
+TEST_F(AuthenticationTest, DropFinalSASLMessageForSlave)
+{
+ Try<PID<Master> > master = StartMaster();
+ ASSERT_SOME(master);
+
+ // Drop the AuthenticationCompletedMessage from authenticator.
+ Future<AuthenticationCompletedMessage> authenticationCompletedMessage =
+ DROP_PROTOBUF(AuthenticationCompletedMessage(), _, _);
+
+ slave::Flags slaveFlags = CreateSlaveFlags();
+ Try<PID<Slave> > slave = StartSlave(slaveFlags);
+ ASSERT_SOME(slave);
+
+ AWAIT_READY(authenticationCompletedMessage);
+
+ authenticationCompletedMessage =
+ FUTURE_PROTOBUF(AuthenticationCompletedMessage(), _, _);
+
+ Future<SlaveRegisteredMessage> slaveRegisteredMessage =
+ FUTURE_PROTOBUF(SlaveRegisteredMessage(), _, _);
+
+ // Advance the clock for the scheduler to retry.
+ Clock::pause();
+ Clock::advance(Seconds(5));
+ Clock::settle();
+ Clock::resume();
+
+ // Ensure another authentication attempt was made.
+ AWAIT_READY(authenticationCompletedMessage);
+
+ // Slave should be able to get registered.
+ AWAIT_READY(slaveRegisteredMessage);
+ ASSERT_NE("", slaveRegisteredMessage.get().slave_id().value());
+
+ Shutdown();
+}
+
+
+// This test verifies that when a master fails over while a framework
// authentication attempt is in progress the framework properly
// authenticates.
TEST_F(AuthenticationTest, MasterFailover)
@@ -313,6 +511,45 @@ TEST_F(AuthenticationTest, MasterFailover)
}
+// This test verifies that when a master fails over while a slave
+// authentication attempt is in progress the slave properly
+// authenticates.
+TEST_F(AuthenticationTest, MasterFailoverDuringSlaveAuthentication)
+{
+ Try<PID<Master> > master = StartMaster();
+ ASSERT_SOME(master);
+
+ // Drop the authenticate message from the slave.
+ Future<AuthenticateMessage> authenticateMessage =
+ DROP_PROTOBUF(AuthenticateMessage(), _, _);
+
+ StandaloneMasterDetector detector(master.get());
+ slave::Flags slaveFlags = CreateSlaveFlags();
+ Try<PID<Slave> > slave = StartSlave(&detector, slaveFlags);
+ ASSERT_SOME(slave);
+
+ AWAIT_READY(authenticateMessage);
+
+ // While the authentication is in progress simulate a failed over
+ // master by restarting the master.
+ Stop(master.get());
+ master = StartMaster();
+ ASSERT_SOME(master);
+
+ Future<SlaveRegisteredMessage> slaveRegisteredMessage =
+ FUTURE_PROTOBUF(SlaveRegisteredMessage(), _, _);
+
+ // Appoint a new master and inform the slave about it.
+ detector.appoint(master.get());
+
+ // Slave should be able to get registered.
+ AWAIT_READY(slaveRegisteredMessage);
+ ASSERT_NE("", slaveRegisteredMessage.get().slave_id().value());
+
+ Shutdown();
+}
+
+
// This test verifies that if the scheduler retries authentication
// before the original authentication finishes (e.g., new master
// detected due to leader election), it is handled properly.
@@ -352,6 +589,40 @@ TEST_F(AuthenticationTest, LeaderElection)
}
+// This test verifies that if the slave retries authentication
+// before the original authentication finishes (e.g., new master
+// detected due to leader election), it is handled properly.
+TEST_F(AuthenticationTest, LeaderElectionDuringSlaveAuthentication)
+{
+ Try<PID<Master> > master = StartMaster();
+ ASSERT_SOME(master);
+
+ // Drop the AuthenticationStepMessage from authenticator.
+ Future<AuthenticationStepMessage> authenticationStepMessage =
+ DROP_PROTOBUF(AuthenticationStepMessage(), _, _);
+
+ StandaloneMasterDetector detector(master.get());
+ slave::Flags slaveFlags = CreateSlaveFlags();
+ Try<PID<Slave> > slave = StartSlave(&detector, slaveFlags);
+ ASSERT_SOME(slave);
+
+ // Drop the intermediate SASL message so that authentication fails.
+ AWAIT_READY(authenticationStepMessage);
+
+ Future<SlaveRegisteredMessage> slaveRegisteredMessage =
+ FUTURE_PROTOBUF(SlaveRegisteredMessage(), _, _);
+
+ // Appoint a new master and inform the slave about it.
+ detector.appoint(master.get());
+
+ // Slave should be able to get registered.
+ AWAIT_READY(slaveRegisteredMessage);
+ ASSERT_NE("", slaveRegisteredMessage.get().slave_id().value());
+
+ Shutdown();
+}
+
+
// This test verifies that if a scheduler fails over in the midst of
// authentication it successfully re-authenticates and re-registers
// with the master when it comes back up.
http://git-wip-us.apache.org/repos/asf/mesos/blob/55739f3a/src/tests/mesos.cpp
----------------------------------------------------------------------
diff --git a/src/tests/mesos.cpp b/src/tests/mesos.cpp
index ae926c9..29a968d 100644
--- a/src/tests/mesos.cpp
+++ b/src/tests/mesos.cpp
@@ -1,3 +1,21 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
#include <stout/check.hpp>
#include <stout/foreach.hpp>
#include <stout/os.hpp>
@@ -43,8 +61,8 @@ master::Flags MesosTest::CreateMasterFlags()
flags.work_dir = directory.get();
- // Enable authentication.
- flags.authenticate = true;
+ flags.authenticate_frameworks = true;
+ flags.authenticate_slaves = true;
// Create a default credentials file.
const std::string& path = path::join(directory.get(), "credentials");
@@ -76,6 +94,17 @@ slave::Flags MesosTest::CreateSlaveFlags()
flags.launcher_dir = path::join(tests::flags.build_dir, "src");
+ // Create a default credential file.
+ const std::string& path = path::join(directory.get(), "credential");
+
+ const std::string& credential =
+ DEFAULT_CREDENTIAL.principal() + " " + DEFAULT_CREDENTIAL.secret();
+
+ CHECK_SOME(os::write(path, credential))
+ << "Failed to write slave credential to '" << path << "'";
+
+ flags.credential = "file://" + path;
+
// TODO(vinod): Consider making this true and fixing the tests.
flags.checkpoint = false;
http://git-wip-us.apache.org/repos/asf/mesos/blob/55739f3a/src/tests/slave_recovery_tests.cpp
----------------------------------------------------------------------
diff --git a/src/tests/slave_recovery_tests.cpp b/src/tests/slave_recovery_tests.cpp
index 84b7796..f4793e2 100644
--- a/src/tests/slave_recovery_tests.cpp
+++ b/src/tests/slave_recovery_tests.cpp
@@ -1737,7 +1737,12 @@ TYPED_TEST(SlaveRecoveryTest, ShutdownSlave)
// all the tasks of the old slave to LOST.
TYPED_TEST(SlaveRecoveryTest, RegisterDisconnectedSlave)
{
- Try<PID<Master> > master = this->StartMaster();
+ master::Flags masterFlags = this->CreateMasterFlags();
+
+ // Disable authentication so the spoofed re-registration below works.
+ masterFlags.authenticate_slaves = false;
+
+ Try<PID<Master> > master = this->StartMaster(masterFlags);
ASSERT_SOME(master);
Future<RegisterSlaveMessage> registerSlaveMessage =