Skip to content

Commit

Permalink
feat: add caching for universe domain
Browse files Browse the repository at this point in the history
  • Loading branch information
bshaffer committed Feb 16, 2024
1 parent 6e9c9fd commit 9d0fd0e
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/FetchAuthTokenCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public function __construct(
$this->cacheConfig = array_merge([
'lifetime' => 1500,
'prefix' => '',
'cacheUniverseDomain' => $fetcher instanceof Credentials\GCECredentials,
], (array) $cacheConfig);
}

Expand Down Expand Up @@ -212,6 +213,9 @@ public function getProjectId(callable $httpHandler = null)
public function getUniverseDomain(): string
{
if ($this->fetcher instanceof GetUniverseDomainInterface) {
if ($this->cacheConfig['cacheUniverseDomain']) {
return $this->getCachedUniverseDomain();
}
return $this->fetcher->getUniverseDomain();
}

Expand Down Expand Up @@ -320,4 +324,16 @@ private function saveAuthTokenInCache($authToken, $authUri = null)
$this->setCachedValue($cacheKey, $authToken);
}
}

private function getCachedUniverseDomain(): string
{
$cacheKey = $this->getFullCacheKey($this->fetcher->getCacheKey() . 'universe_domain');
if ($universeDomain = $this->getCachedValue($cacheKey)) {
return $universeDomain;
}

$universeDomain = $this->fetcher->getUniverseDomain();

Check failure on line 335 in src/FetchAuthTokenCache.php

View workflow job for this annotation

GitHub Actions / PHPStan Static Analysis

Call to an undefined method Google\Auth\FetchAuthTokenInterface::getUniverseDomain().
$this->setCachedValue($cacheKey, $universeDomain);
return $universeDomain;
}
}
98 changes: 98 additions & 0 deletions tests/FetchAuthTokenCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@

namespace Google\Auth\Tests;

use Google\Auth\Cache\CacheItemInterface;
use Google\Auth\Cache\MemoryCacheItemPool;
use Google\Auth\Credentials\GCECredentials;
use Google\Auth\Credentials\ServiceAccountCredentials;
use Google\Auth\CredentialsLoader;
use Google\Auth\FetchAuthTokenCache;
use Google\Auth\FetchAuthTokenInterface;
use Google\Auth\GetUniverseDomainInterface;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\Utils;
Expand All @@ -37,6 +39,7 @@ class FetchAuthTokenCacheTest extends BaseTest
private $mockCacheItem;
private $mockCache;
private $mockSigner;
private static string $cacheKey;

protected function setUp(): void
{
Expand Down Expand Up @@ -700,4 +703,99 @@ public function testGetFetcher()

$this->assertSame($mockFetcher, $fetcher->getFetcher());
}

public function testCacheUniverseDomain()
{
$mockFetcher = $this->prophesize(FetchAuthTokenInterface::class);
$mockFetcher->willImplement(GetUniverseDomainInterface::class);
$mockFetcher->getUniverseDomain()
->shouldBeCalledTimes(2)
->willReturn('example-universe.domain');
$mockFetcher->getCacheKey()
->shouldNotBeCalled();

$fetcher = new FetchAuthTokenCache(
$mockFetcher->reveal(),
['cacheUniverseDomain' => false],
new MemoryCacheItemPool()
);

// Call it twice
$this->assertEquals('example-universe.domain', $fetcher->getUniverseDomain());
$this->assertEquals('example-universe.domain', $fetcher->getUniverseDomain());

// Now set the cache option and ensure it's only called once
$mockFetcher = $this->prophesize(FetchAuthTokenInterface::class);
$mockFetcher->willImplement(GetUniverseDomainInterface::class);
$mockFetcher->getUniverseDomain()
->shouldBeCalledOnce()
->willReturn('example-universe.domain');
$mockFetcher->getCacheKey()
->shouldBeCalledTimes(2)
->willReturn('my-cache-key');

$fetcher = new FetchAuthTokenCache(
$mockFetcher->reveal(),
['cacheUniverseDomain' => true],
new MemoryCacheItemPool()
);
$this->assertEquals('example-universe.domain', $fetcher->getUniverseDomain());
$this->assertEquals('example-universe.domain', $fetcher->getUniverseDomain());
}

public function testCacheUniverseDomainByDefaultForGCECredentials()
{
$mockFetcher = $this->prophesize(GCECredentials::class);
$mockFetcher->getUniverseDomain()
->shouldBeCalledOnce()
->willReturn('example-universe.domain');
$mockFetcher->getCacheKey()
->shouldBeCalledTimes(2)
->willReturn('my-cache-key');

$fetcher = new FetchAuthTokenCache(
$mockFetcher->reveal(),
[], // don't set cacheUniverseDomain, it will be true by default
new MemoryCacheItemPool()
);

$this->assertEquals('example-universe.domain', $fetcher->getUniverseDomain());
$this->assertEquals('example-universe.domain', $fetcher->getUniverseDomain());
}

public function testUniverseDomainWithFileCache()
{
require_once __DIR__ . '/mocks/TestFileCacheItemPool.php';
self::$cacheKey = 'universe-domain-check-' . time() . rand();

$cache = new TestFileCacheItemPool(sys_get_temp_dir() . '/google-auth-test');

$mockFetcher = $this->prophesize(FetchAuthTokenInterface::class);
$mockFetcher->willImplement(GetUniverseDomainInterface::class);
$mockFetcher->getUniverseDomain()
->shouldBeCalledOnce()
->willReturn('example-universe.domain');
$mockFetcher->getCacheKey()
->shouldBeCalledOnce()
->willReturn(self::$cacheKey);

$fetcher = new FetchAuthTokenCache(
$mockFetcher->reveal(),
['cacheUniverseDomain' => true],
$cache
);
$this->assertEquals('example-universe.domain', $fetcher->getUniverseDomain());
}

/**
* @depends testUniverseDomainWithFileCache
*/
public function testUniverseDomainWithFileCacheProcess2()
{
$cmd = sprintf('php %s/mocks/test_file_cache_separate_process.php %s', __DIR__, self::$cacheKey);
exec($cmd, $output, $retVar);

$this->assertEquals(0, $retVar);
$this->assertEquals('example-universe.domain', implode('', $output));
}
}

0 comments on commit 9d0fd0e

Please sign in to comment.