Skip to content

Commit

Permalink
Allow HTTP Basic auth.
Browse files Browse the repository at this point in the history
  • Loading branch information
vktr committed Oct 3, 2018
1 parent 58f21b7 commit 750a9eb
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 9 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ cat dump.json | parallel --pipe -l 50000 curl -s -H "Content-Type: application/x
Defaults to *5000*.
- `--dump-mappings` - specify this flag to dump the index mappings instead of the source.

#### Authentication

To use HTTP Basic authentication you need to pass the following options. *Note*
that passing a password on the command line will put it in your terminal
history, so please use with care.

- `--auth=basic` - enable HTTP Basic authentication.
- `--basic-username=foo` - the username.
- `--basic-password=bar` - the password.


## Building from source

Expand Down
79 changes: 79 additions & 0 deletions src/base64.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#ifndef _MACARON_BASE64_H_
#define _MACARON_BASE64_H_

/**
* The MIT License (MIT)
* Copyright (c) 2016 tomykaira
* 2018 vktr
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include <string>

class base64
{
public:
static std::string Encode(const std::string& data)
{
static constexpr char sEncodingTable[] = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '0', '1', '2', '3',
'4', '5', '6', '7', '8', '9', '+', '/'};

size_t in_len = data.size();
size_t out_len = 4 * ((in_len + 2) / 3);
std::string ret(out_len, '\0');
size_t i;
char *p = const_cast<char *>(ret.c_str());

for (i = 0; i < in_len - 2; i += 3)
{
*p++ = sEncodingTable[(data[i] >> 2) & 0x3F];
*p++ = sEncodingTable[((data[i] & 0x3) << 4) | ((int)(data[i + 1] & 0xF0) >> 4)];
*p++ = sEncodingTable[((data[i + 1] & 0xF) << 2) | ((int)(data[i + 2] & 0xC0) >> 6)];
*p++ = sEncodingTable[data[i + 2] & 0x3F];
}
if (i < in_len)
{
*p++ = sEncodingTable[(data[i] >> 2) & 0x3F];
if (i == (in_len - 1))
{
*p++ = sEncodingTable[((data[i] & 0x3) << 4)];
*p++ = '=';
}
else
{
*p++ = sEncodingTable[((data[i] & 0x3) << 4) | ((int)(data[i + 1] & 0xF0) >> 4)];
*p++ = sEncodingTable[((data[i + 1] & 0xF) << 2)];
}
*p++ = '=';
}

return ret;
}
};

#endif /* _MACARON_BASE64_H_ */
64 changes: 55 additions & 9 deletions src/blaze.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <curl/curl.h>

#include "argh.h"
#include "base64.h"
#include "../vendor/rapidjson/include/rapidjson/document.h"
#include "../vendor/rapidjson/include/rapidjson/filewritestream.h"
#include "../vendor/rapidjson/include/rapidjson/writer.h"
Expand All @@ -19,13 +20,21 @@

std::mutex mtx_out;

struct auth_options
{
std::string type;
std::string user;
std::string pass;
};

struct dump_options
{
std::string host;
std::string index;
int slice_id;
int slice_max;
int size;
std::string host;
std::string index;
auth_options auth;
int slice_id;
int slice_max;
int size;
};

struct thread_state
Expand Down Expand Up @@ -57,15 +66,27 @@ size_t write_data(
bool get_or_post_data(
CURL * crl,
std::string const & url,
auth_options const & auth,
std::vector<char> * data,
long * response_code,
std::string * error,
std::string body = "")
{
curl_slist* headers = nullptr;
headers = curl_slist_append(headers, "Content-Type: application/json");

curl_easy_setopt(crl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(crl, CURLOPT_URL, url.c_str());
curl_easy_setopt(crl, CURLOPT_WRITEFUNCTION, &write_data);
curl_easy_setopt(crl, CURLOPT_WRITEDATA, reinterpret_cast<void*>(data));

if (auth.type == "basic")
{
std::string user_pass = auth.user + ":" + auth.pass;
curl_easy_setopt(crl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_easy_setopt(crl, CURLOPT_USERPWD, user_pass.c_str());
}

if (!body.empty())
{
curl_easy_setopt(crl, CURLOPT_POSTFIELDS, body.c_str());
Expand Down Expand Up @@ -172,6 +193,7 @@ void dump(
bool res = get_or_post_data(
crl,
options.host + "/" + options.index + "/_search?scroll=1m",
options.auth,
&buffer,
&response_code,
&error,
Expand Down Expand Up @@ -217,6 +239,7 @@ void dump(
res = get_or_post_data(
crl,
options.host + "/_search/scroll",
options.auth,
&buffer,
&response_code,
&error,
Expand Down Expand Up @@ -252,13 +275,13 @@ void dump(
}

int dump_mappings(
std::string const& host,
std::string const& index)
std::string const& host,
std::string const& index,
auth_options const& auth)
{
static char write_buffer[WRITE_BUF_SIZE];
static rapidjson::FileWriteStream stream(stdout, write_buffer, sizeof(write_buffer));


CURL * crl = curl_easy_init();
long response_code;
rapidjson::Document doc;
Expand All @@ -269,6 +292,7 @@ int dump_mappings(
bool res = get_or_post_data(
crl,
url,
auth,
&buffer,
&response_code,
&error);
Expand Down Expand Up @@ -322,11 +346,32 @@ int main(
return 1;
}

auth_options auth;

if (cmdl({"--auth"}) >> auth.type)
{
if (auth.type == "basic")
{
if (!(cmdl({"--basic-username"}) >> auth.user))
{
std::cerr << "Must provide --basic-username when passing --auth=basic" << std::endl;
return 1;
}

if (!(cmdl({"--basic-password"}) >> auth.pass))
{
std::cerr << "Must provide --basic-password when passing --auth=basic" << std::endl;
return 1;
}
}
}

if (cmdl["--dump-mappings"])
{
return dump_mappings(
host,
index);
index,
auth);
}

int slices;
Expand All @@ -340,6 +385,7 @@ int main(
dump_options opts;
opts.host = host;
opts.index = index;
opts.auth = auth;
opts.size = size;
opts.slice_id = i;
opts.slice_max = slices;
Expand Down

0 comments on commit 750a9eb

Please sign in to comment.