#include "hashicorp.h"
#include "zbxkvs.h"
#include "zbxjson.h"
#include "zbxhttp.h"
#include "zbxstr.h"
int zbx_hashicorp_kvs_get(const char *vault_url, const char *token, const char *ssl_cert_file,
const char *ssl_key_file, const char *config_source_ip, const char *path, long timeout,zbx_kvs_t *kvs,
char **error)
{
#ifndef HAVE_LIBCURL
ZBX_UNUSED(vault_url);
ZBX_UNUSED(token);
ZBX_UNUSED(ssl_cert_file);
ZBX_UNUSED(ssl_key_file);
ZBX_UNUSED(path);
ZBX_UNUSED(timeout);
ZBX_UNUSED(config_source_ip);
ZBX_UNUSED(kvs);
*error = zbx_dsprintf(*error, "missing cURL library");
return FAIL;
#else
char *out = NULL, *url, header[MAX_STRING_LEN], *left, *right;
struct zbx_json_parse jp, jp_data, jp_data_data;
int ret = FAIL;
long response_code;
if (NULL == token)
{
*error = zbx_dsprintf(*error, "\"VaultToken\" configuration parameter or \"VAULT_TOKEN\" environment"
" variable should be defined");
return FAIL;
}
zbx_strsplit_first(path, '/', &left, &right);
if (NULL == right)
{
*error = zbx_dsprintf(*error, "cannot find separator \"\\\" in path");
free(left);
return FAIL;
}
url = zbx_dsprintf(NULL, "%s/v1/%s/data/%s", vault_url, left, right);
zbx_free(right);
zbx_free(left);
zbx_snprintf(header, sizeof(header), "X-Vault-Token: %s", token);
if (SUCCEED != zbx_http_get(url, header, timeout, ssl_cert_file, ssl_key_file, config_source_ip, &out,
&response_code, error))
{
goto fail;
}
if (200 != response_code && 204 != response_code)
{
*error = zbx_dsprintf(*error, "unsuccessful response code \"%ld\"", response_code);
goto fail;
}
if (SUCCEED != zbx_json_open(out, &jp))
{
*error = zbx_dsprintf(*error, "cannot parse secrets from vault: %s", zbx_json_strerror());
goto fail;
}
if (SUCCEED != zbx_json_brackets_by_name(&jp, "data", &jp_data))
{
*error = zbx_dsprintf(*error, "cannot find the \"%s\" object in the received JSON object.",
ZBX_PROTO_TAG_DATA);
goto fail;
}