Merge pull request #860 from cgagnaire/lib-http-authent

improve authentification handling
This commit is contained in:
qgarnier 2018-01-19 17:23:21 +01:00 committed by GitHub
commit 4faecc33da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 62 additions and 26 deletions

View File

@ -198,10 +198,6 @@ Set path to get Webpage (Default: '/')
Specify this option if you access webpage over basic authentication
=item B<--ntlm>
Specify this option if you access webpage over ntlm authentication (Use with --credentials option)
=item B<--ntlmv2>
Specify this option if you access webpage over ntlmv2 authentication (Use with --credentials and --port options)

View File

@ -395,10 +395,6 @@ Set path to get Webpage (Default: '/')
Specify this option if you access webpage over basic authentication
=item B<--ntlm>
Specify this option if you access webpage over ntlm authentication (Use with --credentials option)
=item B<--ntlmv2>
Specify this option if you access webpage over ntlmv2 authentication (Use with --credentials and --port options)

View File

@ -176,10 +176,6 @@ Set path to get webpage (Default: '/')
Specify this option if you access webpage over basic authentication
=item B<--ntlm>
Specify this option if you access webpage over ntlm authentication (Use with --credentials option)
=item B<--ntlmv2>
Specify this option if you access webpage over ntlmv2 authentication (Use with --credentials and --port options)

View File

@ -422,10 +422,6 @@ Set path to get Webpage (Default: '/')
Specify this option if you access webpage over basic authentication
=item B<--ntlm>
Specify this option if you access webpage over ntlm authentication (Use with --credentials option)
=item B<--ntlmv2>
Specify this option if you access webpage over ntlmv2 authentication (Use with --credentials and --port options)

View File

@ -22,7 +22,7 @@ package centreon::plugins::http;
use strict;
use warnings;
use LWP::UserAgent;
use centreon::plugins::useragent;
use HTTP::Cookies;
use URI;
use IO::Socket::SSL;
@ -211,7 +211,8 @@ sub request {
$self->check_options(request => $request_options);
if (!defined($self->{ua})) {
$self->{ua} = LWP::UserAgent->new(keep_alive => 1, protocols_allowed => ['http', 'https'], timeout => $request_options->{timeout});
$self->{ua} = centreon::plugins::useragent->new(keep_alive => 1, protocols_allowed => ['http', 'https'], timeout => $request_options->{timeout},
credentials => $request_options->{credentials}, username => $request_options->{username}, password => $request_options->{password});
if (defined($request_options->{cookies_file})) {
$self->{ua}->cookie_jar(HTTP::Cookies->new(file => $request_options->{cookies_file},
autosave => 1));
@ -264,16 +265,11 @@ sub request {
$req->content($uri_post->query);
}
}
if (defined($request_options->{credentials}) && defined($request_options->{ntlm})) {
$self->{ua}->credentials($request_options->{hostname} . ':' . $request_options->{port}, '', $request_options->{username}, $request_options->{password});
} elsif (defined($request_options->{credentials}) && defined($request_options->{ntlmv2})) {
if (defined($request_options->{credentials}) && defined($request_options->{ntlmv2})) {
centreon::plugins::misc::mymodule_load(output => $self->{output}, module => 'Authen::NTLM',
error_msg => "Cannot load module 'Authen::NTLM'.");
Authen::NTLM::ntlmv2(1);
$self->{ua}->credentials($request_options->{hostname} . ':' . $request_options->{port}, '', $request_options->{username}, $request_options->{password});
} elsif (defined($request_options->{credentials})) {
$req->authorization_basic($request_options->{username}, $request_options->{password});
}
$self->set_proxy(request => $request_options, url => $url);
@ -316,9 +312,15 @@ sub request {
}
if (!$self->{output}->is_status(value => $status, compare => 'ok', litteral => 1)) {
my $short_msg = $response->status_line;
if ($short_msg =~ /^401/) {
my ($authenticate) = $response->www_authenticate =~ /(\S+)/;
$short_msg .= ' (' . $authenticate . ' authentification expected)';
}
$self->{output}->output_add(long_msg => $response->content, debug => 1);
$self->{output}->output_add(severity => $status,
short_msg => $response->status_line);
short_msg => $short_msg);
$self->{output}->display();
$self->{output}->exit();
}

View File

@ -0,0 +1,50 @@
#
# Copyright 2017 Centreon (http://www.centreon.com/)
#
# Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and application monitoring for
# service performance.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
package centreon::plugins::useragent;
use strict;
use warnings;
use base 'LWP::UserAgent';
sub new {
my ($class, %options) = @_;
my $self = {};
bless $self, $class;
$self = LWP::UserAgent::new(@_);
$self->agent("centreon::plugins::useragent");
$self->{credentials} = $options{credentials} if defined($options{credentials});
$self->{username} = $options{username} if defined($options{username});
$self->{password} = $options{password} if defined($options{password});
return $self;
}
sub get_basic_credentials {
my($self, $realm, $uri, $proxy) = @_;
return if $proxy;
return $self->{username}, $self->{password} if $self->{credentials} and wantarray;
return $self->{username}.":".$self->{password} if $self->{credentials};
return undef;
}
1;