You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by me...@apache.org on 2016/03/24 05:07:43 UTC
[4/9] mesos git commit: Modified basic HTTP authenticator creator to
accept realm.
Modified basic HTTP authenticator creator to accept realm.
To accommodate different authentication realms for the master and agent,
the default basic HTTP authenticator needs to accept its authentication
realm as a parameter. This patch adds this parameter and modifies the
HTTP authentication tests to validate it appropriately. A new test was
also added: `HttpAuthenticationTest.BasicWithoutRealm`.
Review: https://reviews.apache.org/r/44678/
Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/8e51b37f
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/8e51b37f
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/8e51b37f
Branch: refs/heads/master
Commit: 8e51b37fed466b3cc2d1a5f73d47d4f919044c31
Parents: 4f073a0
Author: Greg Mann <gr...@mesosphere.io>
Authored: Wed Mar 23 02:26:57 2016 -0700
Committer: Adam B <ad...@mesosphere.io>
Committed: Wed Mar 23 21:07:23 2016 -0700
----------------------------------------------------------------------
.../http/basic_authenticator_factory.hpp | 2 +
.../http/basic_authenticator_factory.cpp | 45 ++++++++++++--
src/examples/test_http_authenticator_module.cpp | 14 ++++-
src/master/master.cpp | 4 +-
src/tests/http_authentication_tests.cpp | 63 +++++++++++++++++---
5 files changed, 112 insertions(+), 16 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/mesos/blob/8e51b37f/include/mesos/authentication/http/basic_authenticator_factory.hpp
----------------------------------------------------------------------
diff --git a/include/mesos/authentication/http/basic_authenticator_factory.hpp b/include/mesos/authentication/http/basic_authenticator_factory.hpp
index c11bb47..95c653b 100644
--- a/include/mesos/authentication/http/basic_authenticator_factory.hpp
+++ b/include/mesos/authentication/http/basic_authenticator_factory.hpp
@@ -38,9 +38,11 @@ public:
const Parameters& parameters);
static Try<process::http::authentication::Authenticator*> create(
+ const std::string& realm,
const Credentials& credentials);
static Try<process::http::authentication::Authenticator*> create(
+ const std::string& realm,
const hashmap<std::string, std::string>& credentials);
protected:
http://git-wip-us.apache.org/repos/asf/mesos/blob/8e51b37f/src/authentication/http/basic_authenticator_factory.cpp
----------------------------------------------------------------------
diff --git a/src/authentication/http/basic_authenticator_factory.cpp b/src/authentication/http/basic_authenticator_factory.cpp
index 62f8516..bfc0835 100644
--- a/src/authentication/http/basic_authenticator_factory.cpp
+++ b/src/authentication/http/basic_authenticator_factory.cpp
@@ -19,6 +19,10 @@
#include <mesos/module/http_authenticator.hpp>
#include <stout/foreach.hpp>
+#include <stout/json.hpp>
+#include <stout/protobuf.hpp>
+
+#include "credentials/credentials.hpp"
#include "master/constants.hpp"
@@ -30,11 +34,14 @@ namespace authentication {
using std::string;
+using google::protobuf::RepeatedPtrField;
+
using process::http::authentication::Authenticator;
using process::http::authentication::BasicAuthenticator;
Try<Authenticator*> BasicAuthenticatorFactory::create(
+ const string& realm,
const Credentials& credentials)
{
hashmap<string, string> credentialMap;
@@ -43,27 +50,55 @@ Try<Authenticator*> BasicAuthenticatorFactory::create(
credentialMap.put(credential.principal(), credential.secret());
}
- return create(credentialMap);
+ return create(realm, credentialMap);
}
Try<Authenticator*> BasicAuthenticatorFactory::create(
const Parameters& parameters)
{
- hashmap<string, string> credentials;
+ Credentials credentials;
+ Option<string> realm;
foreach (const Parameter& parameter, parameters.parameter()) {
- credentials.put(parameter.key(), parameter.value());
+ if (parameter.key() == "credentials") {
+ Try<JSON::Value> json = JSON::parse(parameter.value());
+ if (json.isError()) {
+ return Error(
+ "Unable to parse HTTP credentials as JSON: " +
+ json.error());
+ }
+
+ Try<RepeatedPtrField<Credential>> credentials_ =
+ protobuf::parse<RepeatedPtrField<Credential>>(json.get());
+ if (credentials_.isError()) {
+ return Error(
+ "Unable to parse credentials for basic HTTP authenticator: " +
+ credentials_.error());
+ }
+
+ credentials.mutable_credentials()->CopyFrom(credentials_.get());
+ } else if (parameter.key() == "authentication_realm") {
+ realm = parameter.value();
+ } else {
+ return Error(
+ "Unknown basic authenticator parameter: " + parameter.key());
+ }
+ }
+
+ if (realm.isNone()) {
+ return Error("Must specify a realm for the basic HTTP authenticator");
}
- return create(credentials);
+ return create(realm.get(), credentials);
}
Try<Authenticator*> BasicAuthenticatorFactory::create(
+ const string& realm,
const hashmap<string, string>& credentials)
{
- Authenticator* authenticator = new BasicAuthenticator("mesos", credentials);
+ Authenticator* authenticator = new BasicAuthenticator(realm, credentials);
return authenticator;
}
http://git-wip-us.apache.org/repos/asf/mesos/blob/8e51b37f/src/examples/test_http_authenticator_module.cpp
----------------------------------------------------------------------
diff --git a/src/examples/test_http_authenticator_module.cpp b/src/examples/test_http_authenticator_module.cpp
index 459b704..8affdb6 100644
--- a/src/examples/test_http_authenticator_module.cpp
+++ b/src/examples/test_http_authenticator_module.cpp
@@ -25,6 +25,8 @@
#include <stout/hashmap.hpp>
+#include "logging/logging.hpp"
+
using namespace mesos;
using mesos::http::authentication::BasicAuthenticatorFactory;
@@ -34,7 +36,17 @@ using process::http::authentication::Authenticator;
static Authenticator* createHttpAuthenticator(const Parameters& parameters)
{
- return BasicAuthenticatorFactory::create(parameters).get();
+ Try<Authenticator*> authenticator =
+ BasicAuthenticatorFactory::create(parameters);
+
+ if (authenticator.isError()) {
+ LOG(ERROR) << "Failed to create basic HTTP authenticator: "
+ << authenticator.error();
+
+ return NULL;
+ }
+
+ return authenticator.get();
}
http://git-wip-us.apache.org/repos/asf/mesos/blob/8e51b37f/src/master/master.cpp
----------------------------------------------------------------------
diff --git a/src/master/master.cpp b/src/master/master.cpp
index e6290ea..32914a2 100644
--- a/src/master/master.cpp
+++ b/src/master/master.cpp
@@ -538,7 +538,9 @@ void Master::initialize()
<< "' HTTP authenticator";
Try<authentication::Authenticator*> authenticator =
- BasicAuthenticatorFactory::create(credentials.get());
+ BasicAuthenticatorFactory::create(
+ DEFAULT_HTTP_AUTHENTICATION_REALM,
+ credentials.get());
if (authenticator.isError()) {
EXIT(1) << "Could not create HTTP authenticator module '"
<< httpAuthenticatorNames[0] << "': " << authenticator.error();
http://git-wip-us.apache.org/repos/asf/mesos/blob/8e51b37f/src/tests/http_authentication_tests.cpp
----------------------------------------------------------------------
diff --git a/src/tests/http_authentication_tests.cpp b/src/tests/http_authentication_tests.cpp
index cf2bb76..6140819 100644
--- a/src/tests/http_authentication_tests.cpp
+++ b/src/tests/http_authentication_tests.cpp
@@ -83,6 +83,30 @@ using process::http::authentication::Authenticator;
using process::http::authentication::AuthenticationResult;
+static const std::string REALM = "tatooine";
+
+static Parameters createBasicAuthenticatorParameters(
+ const Option<std::string>& realm,
+ const Option<Credentials>& credentials)
+{
+ Parameters parameters;
+
+ if (realm.isSome()) {
+ Parameter* parameter = parameters.add_parameter();
+ parameter->set_key("authentication_realm");
+ parameter->set_value(realm.get());
+ }
+
+ if (credentials.isSome()) {
+ Parameter* parameter = parameters.add_parameter();
+ parameter->set_key("credentials");
+ parameter->set_value(
+ stringify(JSON::protobuf(credentials.get().credentials())));
+ }
+
+ return parameters;
+}
+
template <typename T>
class HttpAuthenticationTest : public MesosTest {};
@@ -98,7 +122,7 @@ TYPED_TEST_CASE(HttpAuthenticationTest, HttpAuthenticatorTypes);
// Full HTTP stack tests are located in libprocess-tests.
TYPED_TEST(HttpAuthenticationTest, BasicWithoutCredentialsTest)
{
- Parameters parameters;
+ Parameters parameters = createBasicAuthenticatorParameters(REALM, None());
Try<Authenticator*> create = TypeParam::create(parameters);
ASSERT_SOME(create);
@@ -110,7 +134,7 @@ TYPED_TEST(HttpAuthenticationTest, BasicWithoutCredentialsTest)
{
AuthenticationResult unauthorized;
unauthorized.unauthorized =
- Unauthorized({"Basic realm=\"mesos\""});
+ Unauthorized({"Basic realm=\"" + REALM + "\""});
Request request;
@@ -126,7 +150,7 @@ TYPED_TEST(HttpAuthenticationTest, BasicWithoutCredentialsTest)
AuthenticationResult unauthorized;
unauthorized.unauthorized =
- Unauthorized({"Basic realm=\"mesos\""});
+ Unauthorized({"Basic realm=\"" + REALM + "\""});
AWAIT_EXPECT_EQ(unauthorized, authenticator->authenticate(request));
}
@@ -137,10 +161,13 @@ TYPED_TEST(HttpAuthenticationTest, BasicWithoutCredentialsTest)
// Full HTTP stack tests are located in libprocess-tests.
TYPED_TEST(HttpAuthenticationTest, BasicWithCredentialsTest)
{
- Parameters parameters;
- auto entry = parameters.add_parameter();
- entry->set_key("user");
- entry->set_value("password");
+ Credentials credentials;
+ Credential* credential = credentials.add_credentials();
+ credential->set_principal("user");
+ credential->set_secret("password");
+
+ Parameters parameters =
+ createBasicAuthenticatorParameters(REALM, credentials);
Try<Authenticator*> create = TypeParam::create(parameters);
ASSERT_SOME(create);
@@ -154,7 +181,7 @@ TYPED_TEST(HttpAuthenticationTest, BasicWithCredentialsTest)
AuthenticationResult unauthorized;
unauthorized.unauthorized =
- Unauthorized({"Basic realm=\"mesos\""});
+ Unauthorized({"Basic realm=\"" + REALM + "\""});
AWAIT_EXPECT_EQ(unauthorized, authenticator->authenticate(request));
}
@@ -169,7 +196,7 @@ TYPED_TEST(HttpAuthenticationTest, BasicWithCredentialsTest)
AuthenticationResult unauthorized;
unauthorized.unauthorized =
- Unauthorized({"Basic realm=\"mesos\""});
+ Unauthorized({"Basic realm=\"" + REALM + "\""});
AWAIT_EXPECT_EQ(unauthorized, authenticator->authenticate(request));
}
@@ -189,6 +216,24 @@ TYPED_TEST(HttpAuthenticationTest, BasicWithCredentialsTest)
}
}
+
+// Tests that the HTTP basic authenticator will return an error if it is given
+// module parameters that don't contain an authentication realm.
+TYPED_TEST(HttpAuthenticationTest, BasicWithoutRealm)
+{
+ Credentials credentials;
+ Credential* credential = credentials.add_credentials();
+ credential->set_principal("user");
+ credential->set_secret("password");
+
+ Parameters parameters =
+ createBasicAuthenticatorParameters(None(), credentials);
+
+ Try<Authenticator*> create = TypeParam::create(parameters);
+
+ ASSERT_ERROR(create);
+}
+
} // namespace tests {
} // namespace internal {
} // namespace mesos {