Skip to content

Commit 8f4c193

Browse files
Merge pull request #6 from data219/fix_jsonpath_usage
Fixed JSONPath lib handling.
2 parents 352ba3a + b5f3bdc commit 8f4c193

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

src/KubernetesClient/Config.php

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -507,12 +507,20 @@ protected function getAuthProviderToken()
507507
$user = $this->getUser();
508508

509509
// gcp, azure, etc
510-
//$name = (new JSONPath($user))->find('$.auth-provider.name')[0];
510+
//$name = (new JSONPath($user))->find('$.auth-provider.name')->first();
511511

512512
// build command
513-
$cmd_path = (new JSONPath($user))->find('$.auth-provider.config.cmd-path')[0];
514-
$cmd_args = (new JSONPath($user))->find('$.auth-provider.config.cmd-args')[0];
515-
$command = "${cmd_path} ${cmd_args}";
513+
$cmd_path = (new JSONPath($user))->find('$.auth-provider.config.cmd-path')->first();
514+
$cmd_args = (new JSONPath($user))->find('$.auth-provider.config.cmd-args')->first();
515+
516+
if (!$cmd_path) {
517+
throw new \Error('error finding access token command. No command found.');
518+
}
519+
520+
$command = $cmd_path;
521+
if ($cmd_args) {
522+
$command .= ' ' . $cmd_args;
523+
}
516524

517525
// execute command and store output
518526
$output = [];
@@ -530,17 +538,25 @@ protected function getAuthProviderToken()
530538
throw new \Error("error retrieving token: auth provider failed to return valid data");
531539
}
532540

533-
$expiry_key = (new JSONPath($user))->find('$.auth-provider.config.expiry-key')[0];
534-
$token_key = (new JSONPath($user))->find('$.auth-provider.config.token-key')[0];
541+
$expiry_key = (new JSONPath($user))->find('$.auth-provider.config.expiry-key')->first();
542+
$token_key = (new JSONPath($user))->find('$.auth-provider.config.token-key')->first();
535543

536544
if ($expiry_key) {
537545
$expiry_key = '$' . trim($expiry_key, "{}");
538-
$this->setExpiry((new JSONPath($output))->find($expiry_key)[0]);
546+
$expiry = (new JSONPath($output))->find($expiry_key)->first();
547+
if ($expiry) {
548+
// No expiry should be ok, thus never expiring token
549+
$this->setExpiry($expiry);
550+
}
539551
}
540552

541553
if ($token_key) {
542554
$token_key = '$' . trim($token_key, "{}");
543-
$this->setToken((new JSONPath($output))->find($token_key)[0]);
555+
$token = (new JSONPath($output))->find($token_key)->first();
556+
if (!$token) {
557+
throw new \Error(sprintf('error retrieving token: token not found. Searching for key: "%s"', $token_key));
558+
}
559+
$this->setToken($token);
544560
}
545561
}
546562

0 commit comments

Comments
 (0)