App-ElasticSearch-Utilities
view release on metacpan or search on metacpan
lib/App/ElasticSearch/Utilities.pm view on Meta::CPAN
string => $DEF{PATTERN},
};
}
sub _get_ssl_opts {
es_utils_initialize() unless keys %DEF;
my %opts = ();
$opts{SSL_ca_file} = $DEF{CACERT} if $DEF{CACERT};
$opts{SSL_ca_path} = $DEF{CAPATH} if $DEF{CAPATH};
$opts{SSL_cert_file} = $DEF{CERT} if $DEF{CERT};
$opts{SSL_key_file} = $DEF{KEY} if $DEF{KEY};
# Disable Certificate Verification
if ( $DEF{INSECURE} ) {
$opts{verify_hostname} = 0;
$opts{SSL_verify_mode} = 0x00;
}
return \%opts;
}
sub _get_es_version {
return $CURRENT_VERSION if defined $CURRENT_VERSION;
my $conn = es_connect();
# Build the request
my $req = App::ElasticSearch::Utilities::HTTPRequest->new(
GET => sprintf "%s://%s:%d",
$conn->proto, $conn->host, $conn->port
);
# Check if we're doing auth
my @auth = $DEF{PASSEXEC} ? es_basic_auth($conn->host) : ();
# Add authentication if we get a password
$req->authorization_basic( @auth ) if @auth;
# Retry with TLS and/or Auth
my %try = map { $_ => 1 } qw( tls auth );
my $resp;
while( not defined $CURRENT_VERSION ) {
$resp = $conn->ua->request($req);
if( $resp->is_success ) {
my $ver;
eval {
$ver = $resp->content->{version};
};
if( $ver ) {
if( $ver->{distribution} and $ver->{distribution} eq 'opensearch' ) {
$CURRENT_VERSION = version->parse($ver->{minimum_wire_compatibility_version});
}
else {
$CURRENT_VERSION = version->parse($ver->{number});
}
}
}
elsif( $resp->code == 500 && $resp->message eq "Server closed connection without sending any data back" ) {
# Try TLS
last unless $try{tls};
delete $try{tls};
$conn->proto('https');
warn "Attempting promotion to HTTPS, try setting 'proto: https' in ~/.es-utils.yaml";
}
elsif( $resp->code == 401 ) {
# Retry with credentials
last unless $try{auth};
delete $try{auth};
warn "Authorization required, try setting 'password-exec: /home/user/bin/get-password.sh` in ~/.es-utils.yaml'"
unless $DEF{PASSEXEC};
$req->authorization_basic( es_basic_auth($conn->host) );
}
else {
warn "Failed getting version";
last;
}
}
if( !defined $CURRENT_VERSION || $CURRENT_VERSION <= 2 ) {
output({color=>'red',stderr=>1}, sprintf "[%d] Unable to determine Elasticsearch version, something has gone terribly wrong: aborting.", $resp->code);
output({color=>'red',stderr=>1}, ref $resp->content ? YAML::XS::Dump($resp->content) : $resp->content) if $resp->content;
exit 1;
}
debug({color=>'magenta'}, "FOUND VERISON '$CURRENT_VERSION'");
return $CURRENT_VERSION;
}
my $ES = undef;
sub es_connect {
my ($override_servers) = @_;
es_utils_initialize() unless keys %DEF;
my %conn = (
host => $DEF{HOST},
port => $DEF{PORT},
proto => $DEF{PROTO},
timeout => $DEF{TIMEOUT},
ssl_opts => _get_ssl_opts,
);
# Only authenticate over TLS
if( $DEF{PROTO} eq 'https' ) {
$conn{username} = $DEF{USERNAME};
$conn{password} = es_pass_exec(@DEF{qw(HOST USERNAME)}) if $DEF{PASSEXEC};
}
# If we're overriding, return a unique handle
if(defined $override_servers) {
my @overrides = is_arrayref($override_servers) ? @$override_servers : $override_servers;
my @servers;
foreach my $entry ( @overrides ) {
my ($s,$p) = split /\:/, $entry;
$p ||= $conn{port};
push @servers, { %conn, host => $s, port => $p };
}
if( @servers > 0 ) {
my $pick = @servers > 1 ? $servers[int(rand(@servers))] : $servers[0];
return App::ElasticSearch::Utilities::Connection->new(%{$pick});
}
}
else {
# Check for index metadata
foreach my $k ( keys %conn ) {
foreach my $name ( $DEF{INDEX}, $DEF{BASE} ) {
lib/App/ElasticSearch/Utilities.pm view on Meta::CPAN
Defaults to 9200.
=item B<proto>
Defaults to 'http', can also be 'https'.
=item B<http-username>
If HTTP Basic Authentication is required, use this username.
See also the L<HTTP Basic Authentication> section for more details
=item B<password-exec>
If HTTP Basic Authentication is required, run this command, passing the arguments:
<command_to_run> <es_host> <es_username>
The script expects the last line to contain the password in plaintext.
=item B<noop>
Prevents any communication to the cluster from making changes to the settings or data contained therein.
In short, it prevents anything but HEAD and GET requests, B<except> POST requests to the _search endpoint.
=item B<timeout>
Timeout for connections and requests, defaults to 10.
=item B<keep-proxy>
By default, HTTP proxy environment variables are stripped. Use this option to keep your proxy environment variables
in tact.
=item B<insecure>
Don't verify TLS certificates
=item B<cacert>
Specify a file with the TLS CA certificates.
=item B<capath>
Specify a directory containing the TLS CA certificates.
=item B<cert>
Specify the path to the TLS client certificate file..
=item B<key>
Specify the path to the TLS client private key file.
=back
=head1 AUTHENTICATION
HTTP Basic Authorization is only supported when the C<proto> is set to B<https>
as not to leak credentials all over.
The username is selected by going through these mechanisms until one is found:
--http-username
'http-username' in /etc/es-utils.yml or ~/.es-utils.yml
Netrc element matching the hostname of the request
CLI::Helpers prompt()
Once the username has been resolved, the following mechanisms are tried in order:
Netrc element matching the hostname of the request
Password executable defined by --password-exec
'password-exec' in /etc/es-utils.yml, ~/.es-utils.yml
CLI::Helpers prompt()
=head2 Password Exec
It is B<BAD> practice to specify passwords as a command line argument, or store it in a plaintext
file. There are cases where this may be necessary, but it is not recommended. The best method for securing your
password is to use the B<password-exec> option.
This option must point to an executable script. That script will be passed two arguments, the hostname and the username
for the request. It expects the password printed to STDOUT as the last line of output. Here's an example password-exec setup
using Apple Keychain:
#!/bin/sh
HOSTNAME=$1;
USERNAME=$2;
/usr/bin/security find-generic-password -w -a "$USERNAME" -s "$HOSTNAME"
If we save this to "$HOME/bin/get-passwd.sh" we can execute a script
like this:
$ es-search.pl --http-username bob --password-exec $HOME/bin/get-passwd.sh \
--base secure-data --fields
Though it's probably best to set this in your ~/.es-utils.yml file:
---
host: secured-cluster.example.org
port: 443
proto: https
http-username: bob
password-exec: /home/bob/bin/get-passwd.sh
=head3 CLI::Helpers and Password Prompting
If all the fails to yield a password, the last resort is to use CLI::Helpers::prompt() to ask the user for their
password. If the user is using version 1.1 or higher of CLI::Helpers, this call will turn off echo and readline magic
for the password prompt.
=head1 INDEX SELECTION ARGUMENTS
=over
=item B<base>
In an environment using monthly, weekly, daily, or hourly indexes. The base index name is everything without the date.
( run in 1.143 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )