Skip to content

Commit 8fd45a0

Browse files
authored
fix(apps::backup::commvault::commserve::restapi): fix token refresh handling (#6297)
Refs: CTOR-2443
1 parent 48650db commit 8fd45a0

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

src/apps/backup/commvault/commserve/restapi/custom/api.pm

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use warnings;
2525
use centreon::plugins::http;
2626
use centreon::plugins::statefile;
2727
use Digest::SHA qw(sha256_hex);
28-
use centreon::plugins::misc qw/json_encode/;
28+
use centreon::plugins::misc qw/json_encode is_not_empty/;
2929
use MIME::Base64;
3030

3131
sub new {
@@ -198,8 +198,19 @@ sub refresh_authent_token
198198
if $content =~ /accessToken\":\s*\"([^\"]+)\"/m;
199199
$refreshToken = $1
200200
if $content =~ /refreshToken\":\s*\"([^\"]+)\"/m;
201-
$expiryTime = $1
202-
if $content =~ /tokenExpiryTimes.amp\":\s*(\d+)/m; # Commvault returns Times}amp instead of Timestamp
201+
202+
my $token_expiry_timestamp = $1
203+
if $content =~ /"tokenExpiryTimestamp"\s*:\s*(-?\d+)/m;
204+
my $expires_in = $1
205+
if $content =~ /"expiresIn"\s*:\s*(\d+)/m;
206+
if ( is_not_empty($token_expiry_timestamp) ) {
207+
$expiryTime = $token_expiry_timestamp;
208+
} elsif ( is_not_empty($expires_in) ) {
209+
$expiryTime = time() + $expires_in;
210+
} else {
211+
$expiryTime = $1
212+
if $content =~ /tokenExpiryTimes\.amp"\s*:\s*(\d+)/m; # Commvault returns Times}amp instead of Timestamp
213+
}
203214
}
204215

205216
$self->{output}->option_exit(short_msg => "Cannot extract tokens !")
@@ -284,6 +295,7 @@ sub request_internal {
284295
$self->{output}->option_exit(short_msg => 'Tokens where not found. This check can be run after next execution of "token" mode')
285296
if $authent_token eq '';
286297

298+
$expiry_time = 0 if !defined($expiry_time) || $expiry_time !~ /^\d+$/;
287299
$expiry_time -= time;
288300
$self->{output}->output_add(long_msg => $expiry_time > 0 ? "Token will expire in $expiry_time seconds" : "Token has expired", debug => 1);
289301

src/apps/backup/commvault/commserve/restapi/mode/token.pm

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ sub run {
9696
my $msg = 'Token available';
9797
my $severity = 'OK';
9898
if ($self->{option_results}->{api_token} ne '') {
99-
if ($expiry_time < $self->{option_results}->{refresh_before} || $self->{option_results}->{force_refresh}) {
99+
my $time_remaining = $expiry_time - time();
100+
if ($time_remaining < $self->{option_results}->{refresh_before} || $self->{option_results}->{force_refresh}) {
100101
if ($authent_token eq '') {
101102
$authent_token = $self->{option_results}->{api_token};
102103
$refresh_token = $self->{option_results}->{refresh_token};

0 commit comments

Comments
 (0)