(plugin) apps::java::jvm::actuator - manage new centreon-map new auth endpoint

This commit is contained in:
YanMeddour 2023-04-18 15:52:20 +02:00 committed by GitHub
parent 69737a11dd
commit 659e49d2d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -158,59 +158,70 @@ sub clean_session {
my $datas = {}; my $datas = {};
$options{statefile}->write(data => $datas); $options{statefile}->write(data => $datas);
$self->{studio_session} = undef;
} }
sub get_session { sub get_session {
my ($self, %options) = @_; my ($self, %options) = @_;
my $has_cache_file = $options{statefile}->read(statefile => 'centreonmap_session_' . md5_hex($self->{option_results}->{hostname}) . '_' . md5_hex($self->{option_results}->{api_username})); my $has_cache_file = $options{statefile}->read(statefile => 'centreonmap_session_' . md5_hex($self->{option_results}->{hostname}) . '_' . md5_hex($self->{api_username}));
my $studio_session = $options{statefile}->get(name => 'studio_session'); my $studio_session = $options{statefile}->get(name => 'studioSession');
my $jwt_token = $options{statefile}->get(name => 'jwtToken');
my $md5_secret_cache = $self->{cache}->get(name => 'md5_secret');
my $md5_secret = md5_hex($self->{api_username} . $self->{api_password});
if ($has_cache_file == 0 || !defined($studio_session)) { if ($has_cache_file == 0 ||
(!defined($studio_session) && !defined($jwt_token)) ||
(defined($md5_secret_cache) && $md5_secret_cache ne $md5_secret)
) {
my $login = { login => $self->{api_username}, password => $self->{api_password} }; my $login = { login => $self->{api_username}, password => $self->{api_password} };
my $post_json = JSON::XS->new->utf8->encode($login); my $post_json = JSON::XS->new->utf8->encode($login);
my ($content) = $self->{http}->request( my @urls = ('/auth/sign-in', '/authentication');
method => 'POST', my $content;
url_path => $self->{url_path} . '/authentication', for my $endpoint (@urls) {
header => ['Content-type: application/json'], ($content) = $self->{http}->request(
query_form_post => $post_json, method => 'POST',
warning_status => '', unknown_status => '', critical_status => '' url_path => $self->{url_path} . $endpoint,
); header => ['Content-type: application/json'],
query_form_post => $post_json,
warning_status => '', unknown_status => '', critical_status => ''
);
last if ($self->{http}->get_code() == 200);
}
if ($self->{http}->get_code() != 200) { if ($self->{http}->get_code() != 200) {
$self->{output}->add_option_msg(short_msg => "Authentication error [code: '" . $self->{http}->get_code() . "'] [message: '" . $self->{http}->get_message() . "']"); $self->{output}->add_option_msg(short_msg => "All authentication URLs failed");
$self->{output}->option_exit(); $self->{output}->option_exit();
} }
my $decoded = $self->json_decode(content => $content); my $decoded = $self->json_decode(content => $content);
if (!defined($decoded->{studioSession})) { if (!defined($decoded->{studioSession}) && !defined($decoded->{jwtToken})) {
$self->{output}->add_option_msg(short_msg => 'Cannot studio session'); $self->{output}->add_option_msg(short_msg => 'Cannot get session token');
$self->{output}->option_exit(); $self->{output}->option_exit();
} }
$studio_session = $decoded->{studioSession}; if (defined($decoded->{studioSession})) {
$options{statefile}->write(data => { studio_session => $studio_session }); $studio_session = $decoded->{studioSession};
$options{statefile}->write(data => { studio_session => $studio_session, md5_secret => $md5_secret });
} else {
$jwt_token = $decoded->{jwtToken};
$options{statefile}->write(data => { jwtToken => $jwt_token, md5_secret => $md5_secret });
}
} }
$self->{studio_session} = $studio_session; return defined($studio_session) ? 'studio-session: ' . $studio_session : 'Authorization: Bearer ' . $jwt_token;
} }
sub request_api { sub request_api {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->settings(); $self->settings();
if (!defined($self->{studio_session})) { my $header = $self->get_session(statefile => $self->{cache});
$self->get_session(statefile => $self->{cache});
}
my $content = $self->{http}->request( my $content = $self->{http}->request(
url_path => $self->{url_path} . '/actuator' . $options{endpoint}, url_path => $self->{url_path} . '/actuator' . $options{endpoint},
get_param => $options{get_param}, get_param => $options{get_param},
header => [ header => [ $header ],
'studio-session: ' . $self->{studio_session}
],
warning_status => '', warning_status => '',
unknown_status => '', unknown_status => '',
critical_status => '' critical_status => ''
@ -219,13 +230,12 @@ sub request_api {
# Maybe there is an issue with the token. So we retry. # Maybe there is an issue with the token. So we retry.
if ($self->{http}->get_code() < 200 || $self->{http}->get_code() >= 300) { if ($self->{http}->get_code() < 200 || $self->{http}->get_code() >= 300) {
$self->clean_session(statefile => $self->{cache}); $self->clean_session(statefile => $self->{cache});
$self->get_session(statefile => $self->{cache}); $header = $self->get_session(statefile => $self->{cache});
$content = $self->{http}->request( $content = $self->{http}->request(
url_path => $self->{url_path} . '/actuator' . $options{endpoint}, url_path => $self->{url_path} . '/actuator' . $options{endpoint},
get_param => $options{get_param}, get_param => $options{get_param},
header => [ header => [ $header ],
'studio-session: ' . $self->{studio_session}
],
unknown_status => $self->{unknown_http_status}, unknown_status => $self->{unknown_http_status},
warning_status => $self->{warning_http_status}, warning_status => $self->{warning_http_status},
critical_status => $self->{critical_http_status} critical_status => $self->{critical_http_status}