-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds wrapped secret approle authentiation (#21)
- Loading branch information
Showing
7 changed files
with
120 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#include <VaultClient.h> | ||
#include <nlohmann/json.hpp> | ||
|
||
std::string Unwrap::getUrl(const VaultClient& client, const std::string& path) { | ||
return client.getUrl("/v1/sys/wrapping", path); | ||
} | ||
|
||
optional<std::string> | ||
Unwrap::unwrap(const VaultClient& client, std::string token) { | ||
nlohmann::json j; | ||
j = nlohmann::json::object(); | ||
j["token"] = token; | ||
|
||
auto response = client | ||
.getHttpClient() | ||
.post(getUrl(client, "/unwrap"), | ||
client.getToken(), | ||
client.getNamespace(), | ||
j.dump() | ||
); | ||
|
||
if (HttpClient::is_success(response)) { | ||
return nlohmann::json::parse(response.value().body)["data"]["secret_id"]; | ||
} else { | ||
return std::experimental::nullopt; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,41 @@ | ||
#include <utility> | ||
#include "VaultClient.h" | ||
|
||
VaultClient::VaultClient(VaultConfig& config, | ||
AuthenticationStrategy& authStrategy) : | ||
host_(config.getHost()), | ||
port_(config.getPort()), | ||
namespace_(config.getNamespace()), | ||
tls_(config.getTls()), | ||
authStrategy_(authStrategy), | ||
httpClient_(HttpClient(config)) | ||
VaultClient::VaultClient( | ||
VaultConfig& config, | ||
AuthenticationStrategy& authStrategy) | ||
: host_(config.getHost()) | ||
, port_(config.getPort()) | ||
, namespace_(config.getNamespace()) | ||
, tls_(config.getTls()) | ||
, authStrategy_(authStrategy) | ||
, httpClient_(HttpClient(config)) | ||
, debug_(config.getDebug()) | ||
{ | ||
auto result = authStrategy_.authenticate(*this); | ||
if (result) { | ||
token_ = result.value(); | ||
} | ||
} | ||
|
||
VaultClient::VaultClient(VaultConfig& config, | ||
AuthenticationStrategy& authStrategy, | ||
HttpErrorCallback httpErrorCallback) : | ||
host_(config.getHost()), | ||
port_(config.getPort()), | ||
namespace_(config.getNamespace()), | ||
tls_(config.getTls()), | ||
authStrategy_(authStrategy), | ||
httpClient_(HttpClient(config, httpErrorCallback)) | ||
VaultClient::VaultClient( | ||
VaultConfig& config, | ||
AuthenticationStrategy& authStrategy, | ||
HttpErrorCallback httpErrorCallback) | ||
: host_(config.getHost()) | ||
, port_(config.getPort()) | ||
, namespace_(config.getNamespace()) | ||
, tls_(config.getTls()) | ||
, authStrategy_(authStrategy) | ||
, httpClient_(HttpClient(config, std::move(httpErrorCallback))) | ||
, debug_(config.getDebug()) | ||
{ | ||
auto result = authStrategy_.authenticate(*this); | ||
if (result) { | ||
token_ = result.value(); | ||
} | ||
} | ||
|
||
std::string VaultClient::getUrl(std::string base, std::string path) const { | ||
std::string VaultClient::getUrl(const std::string& base, const std::string& path) const { | ||
return (tls_ ? "https://" : "http://") + host_ + ":" + port_ + base + path; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#include <utility> | ||
#include "VaultClient.h" | ||
|
||
WrappedSecretAppRole::WrappedSecretAppRole(std::string role_id, std::string token) | ||
: role_id_(std::move(role_id)) | ||
, token_(std::move(token)) | ||
{} | ||
|
||
optional<std::string> WrappedSecretAppRole::authenticate(const VaultClient &vaultClient) { | ||
auto secret_id = Unwrap::unwrap(vaultClient, token_); | ||
|
||
return secret_id | ||
? AppRole(role_id_, secret_id.value()).authenticate(vaultClient) | ||
: std::experimental::nullopt; | ||
} |