Skip to content

Commit

Permalink
Enable json format to be used as an authentication params string (apa…
Browse files Browse the repository at this point in the history
  • Loading branch information
yush1ga authored and merlimat committed Oct 24, 2017
1 parent 7ba3675 commit 21e09d6
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
30 changes: 29 additions & 1 deletion pulsar-client-cpp/lib/auth/AuthAthenz.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,29 @@ namespace pulsar {

AuthAthenz::~AuthAthenz() {
}


ParamMap parseAuthParamsString(const std::string& authParamsString) {
ParamMap params;
if(!authParamsString.empty()) {
Json::Value root;
Json::Reader reader;
if (reader.parse(authParamsString, root, false)) {
for (auto key: root.getMemberNames()) {
params[key] = root[key].asString();
}
} else {
LOG_ERROR("Invalid String Error: " << reader.getFormatedErrorMessages());
}
}
return params;
}

AuthenticationPtr AuthAthenz::create(const std::string& authParamsString) {
ParamMap params = parseAuthParamsString(authParamsString);
AuthenticationDataPtr authDataAthenz = AuthenticationDataPtr(new AuthDataAthenz(params));
return AuthenticationPtr(new AuthAthenz(authDataAthenz));
}

AuthenticationPtr AuthAthenz::create(ParamMap& params) {
AuthenticationDataPtr authDataAthenz = AuthenticationDataPtr(new AuthDataAthenz(params));
return AuthenticationPtr(new AuthAthenz(authDataAthenz));
Expand All @@ -84,6 +106,12 @@ namespace pulsar {
authDataContent = authDataAthenz_;
return ResultOk;
}

extern "C" Authentication* create(const std::string& authParamsString) {
ParamMap params = parseAuthParamsString(authParamsString);
AuthenticationDataPtr authDataAthenz = AuthenticationDataPtr(new AuthDataAthenz(params));
return new AuthAthenz(authDataAthenz);
}

extern "C" Authentication* createFromMap(ParamMap& params) {
AuthenticationDataPtr authDataAthenz = AuthenticationDataPtr(new AuthDataAthenz(params));
Expand Down
1 change: 1 addition & 0 deletions pulsar-client-cpp/lib/auth/AuthAthenz.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ namespace pulsar {
AuthAthenz(AuthenticationDataPtr&);
~AuthAthenz();
static AuthenticationPtr create(ParamMap& params);
static AuthenticationPtr create(const std::string& authParamsString);
const std::string getAuthMethodName() const;
Result getAuthData(AuthenticationDataPtr& authDataAthenz) const;
private:
Expand Down
15 changes: 8 additions & 7 deletions pulsar-client-cpp/tests/AuthPluginTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ namespace testAthenz {
if (kv[0]=="Athenz-Principal-Auth:") {
principalToken = kv[1];
}

if (headerLine == "\r" || headerLine == "\n" || headerLine == "\r\n") {
std::string mockToken = "{\"token\":\"mockToken\",\"expiryTime\":4133980800}";
stream << "HTTP/1.1 200 OK" << std::endl;
Expand All @@ -150,12 +150,13 @@ namespace testAthenz {
TEST(AuthPluginTest, testAthenz) {
boost::thread zts(&testAthenz::mockZTS);
pulsar::AuthenticationDataPtr data;
ParamMap params;
params["tenantDomain"] = "pulsar.test.tenant";
params["tenantService"] = "service";
params["providerDomain"] = "pulsar.test.provider";
params["privateKey"] = "file:../../pulsar-broker/src/test/resources/authentication/tls/client-key.pem";
params["ztsUrl"] = "http://localhost:9999";
std::string params = R"({
"tenantDomain": "pulsar.test.tenant",
"tenantService": "service",
"providerDomain": "pulsar.test.provider",
"privateKey": "file:../../pulsar-broker/src/test/resources/authentication/tls/client-key.pem",
"ztsUrl": "http://localhost:9999"
})";
pulsar::AuthenticationPtr auth = pulsar::AuthFactory::create("../lib/auth/libauthathenz.so", params);
ASSERT_EQ(auth->getAuthMethodName(), "athenz");
ASSERT_EQ(auth->getAuthData(data), pulsar::ResultOk);
Expand Down

0 comments on commit 21e09d6

Please sign in to comment.