ImpersonatedServiceAccountCredentials: cache config is null
Environment details
- OS: Debian 12
- PHP version: 8.4.3
- Package name and version: googleapis/google-auth-library-php 1.46.0
Steps to reproduce
I wanted to use storage from local with impersonated service account - logged into GCP, then generated application_default_credential.json using impersonated SA, but with PHP, I have a problem. Operations works fine, but there is warning:
( ! ) Warning: Trying to access array offset on null in vendor/google/auth/src/CacheTrait.php on line 98
and related code:
$key = $this->cacheConfig['prefix'] . $key;
I've checked code and it makes sense. ImpersonatedServiceAccountCredentials class uses this trait, but does not configure cacheConfig. Looks like it should be injected to fetcher from FetchAuthTokenCache
Code example
require 'vendor/autoload.php';
use Google\Cloud\Storage\StorageClient;
$storage = new StorageClient();
$bucketName = 'some-test-bucket';
$objectName = 'some-test-object';
$content = 'Test content.';
try {
$bucket = $storage->bucket($bucketName);
$object = $bucket->upload($content, ['name' => $objectName]);
echo "Object $objectName uploaded successfully.\n";
} catch (\Google\Cloud\Core\Exception\ServiceException $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
Example fix
Just very quick, should be adjusted for rest:
ImpersonatedServiceAccountCredentials - add cache config setter
public function setCacheConfig(?array $config)
{
$this->cacheConfig = (array) $config;
}
Then FetchAuthTokenCache, pass cache config to fetcher in constructor:
public function __construct(
FetchAuthTokenInterface $fetcher,
?array $cacheConfig = null,
?CacheItemPoolInterface $cache = null
) {
$this->fetcher = $fetcher;
$this->cache = $cache;
$this->cacheConfig = array_merge([
'lifetime' => 1500,
'prefix' => '',
'cacheUniverseDomain' => $fetcher instanceof Credentials\GCECredentials,
], (array) $cacheConfig);
$this->fetcher->setCacheConfig($this->cacheConfig);
}