From 659e49d2d8a676feaf62859dd8a7cc2d3a3a2547 Mon Sep 17 00:00:00 2001 From: YanMeddour <83709929+YanMeddour@users.noreply.github.com> Date: Tue, 18 Apr 2023 15:52:20 +0200 Subject: [PATCH] (plugin) apps::java::jvm::actuator - manage new centreon-map new auth endpoint --- .../protocols/actuator/custom/centreonmap.pm | 64 +++++++++++-------- 1 file changed, 37 insertions(+), 27 deletions(-) diff --git a/src/centreon/common/protocols/actuator/custom/centreonmap.pm b/src/centreon/common/protocols/actuator/custom/centreonmap.pm index 7352b56a8..b396a369d 100644 --- a/src/centreon/common/protocols/actuator/custom/centreonmap.pm +++ b/src/centreon/common/protocols/actuator/custom/centreonmap.pm @@ -158,59 +158,70 @@ sub clean_session { my $datas = {}; $options{statefile}->write(data => $datas); - $self->{studio_session} = undef; } sub get_session { 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 $studio_session = $options{statefile}->get(name => 'studio_session'); + 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 => '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 $post_json = JSON::XS->new->utf8->encode($login); - my ($content) = $self->{http}->request( - method => 'POST', - url_path => $self->{url_path} . '/authentication', - header => ['Content-type: application/json'], - query_form_post => $post_json, - warning_status => '', unknown_status => '', critical_status => '' - ); + my @urls = ('/auth/sign-in', '/authentication'); + my $content; + for my $endpoint (@urls) { + ($content) = $self->{http}->request( + method => 'POST', + 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) { - $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(); } my $decoded = $self->json_decode(content => $content); - if (!defined($decoded->{studioSession})) { - $self->{output}->add_option_msg(short_msg => 'Cannot studio session'); + if (!defined($decoded->{studioSession}) && !defined($decoded->{jwtToken})) { + $self->{output}->add_option_msg(short_msg => 'Cannot get session token'); $self->{output}->option_exit(); } - $studio_session = $decoded->{studioSession}; - $options{statefile}->write(data => { studio_session => $studio_session }); + if (defined($decoded->{studioSession})) { + $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 { my ($self, %options) = @_; $self->settings(); - if (!defined($self->{studio_session})) { - $self->get_session(statefile => $self->{cache}); - } + my $header = $self->get_session(statefile => $self->{cache}); my $content = $self->{http}->request( url_path => $self->{url_path} . '/actuator' . $options{endpoint}, get_param => $options{get_param}, - header => [ - 'studio-session: ' . $self->{studio_session} - ], + header => [ $header ], warning_status => '', unknown_status => '', critical_status => '' @@ -219,13 +230,12 @@ sub request_api { # Maybe there is an issue with the token. So we retry. if ($self->{http}->get_code() < 200 || $self->{http}->get_code() >= 300) { $self->clean_session(statefile => $self->{cache}); - $self->get_session(statefile => $self->{cache}); + $header = $self->get_session(statefile => $self->{cache}); + $content = $self->{http}->request( url_path => $self->{url_path} . '/actuator' . $options{endpoint}, get_param => $options{get_param}, - header => [ - 'studio-session: ' . $self->{studio_session} - ], + header => [ $header ], unknown_status => $self->{unknown_http_status}, warning_status => $self->{warning_http_status}, critical_status => $self->{critical_http_status}