Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Datastore/tests/Unit/DatastoreClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
namespace Google\Cloud\Datastore\Tests\Unit;

use DateTime;
use Google\ApiCore\InsecureCredentialsWrapper;
use Google\ApiCore\Transport\TransportInterface;
use Google\Cloud\Core\Int64;
use Google\Cloud\Core\Testing\DatastoreOperationRefreshTrait;
Expand Down Expand Up @@ -82,7 +83,7 @@ public function setUp(): void
{
$this->gapicClient = $this->prophesize(GapicClient::class);
$this->client = new DatastoreClient([
'credentials' => Fixtures::KEYFILE_STUB_FIXTURE(),
'credentials' => new InsecureCredentialsWrapper(),
'projectId' => self::PROJECT,
'databaseId' => self::DATABASE,
'datastoreClient' => $this->gapicClient->reveal()
Expand Down
4 changes: 2 additions & 2 deletions Firestore/tests/Unit/BulkWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

namespace Google\Cloud\Firestore\Tests\Unit;

use Google\Cloud\Core\Testing\Snippet\Fixtures;
use Google\ApiCore\InsecureCredentialsWrapper;
use Google\Cloud\Core\Testing\TestHelpers;
use Google\Cloud\Core\Timestamp;
use Google\Cloud\Firestore\BulkWriter;
Expand Down Expand Up @@ -72,7 +72,7 @@ public function setUp(): void
'projectId' => self::PROJECT,
'database' => self::DATABASE,
'firestoreClient' => $this->gapicClient->reveal(),
'credentials' => Fixtures::KEYFILE_STUB_FIXTURE()
'credentials' => new InsecureCredentialsWrapper()
]);

// avoids sleep during unit tests
Expand Down
4 changes: 2 additions & 2 deletions Firestore/tests/Unit/ConformanceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

namespace Google\Cloud\Firestore\Tests\Unit;

use Google\ApiCore\InsecureCredentialsWrapper;
use Google\Cloud\Core\Testing\GrpcTestTrait;
use Google\Cloud\Core\Testing\Snippet\Fixtures;
use Google\Cloud\Core\Timestamp;
use Google\Cloud\Core\TimeTrait;
use Google\Cloud\Firestore\CollectionReference;
Expand Down Expand Up @@ -94,7 +94,7 @@ public function setUp(): void
$this->client = new FirestoreClient([
'firestoreClient' => $this->gapicClient->reveal(),
'projectId' => self::PROJECT_ID,
'credentials' => Fixtures::KEYFILE_STUB_FIXTURE()
'credentials' => new InsecureCredentialsWrapper()
]);
}

Expand Down
4 changes: 2 additions & 2 deletions Firestore/tests/Unit/FirestoreClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@
use Google\Cloud\Firestore\V1\CommitRequest;
use Google\Cloud\Firestore\V1\CommitResponse;
use Google\Cloud\Firestore\V1\ListCollectionIdsRequest;
use Google\ApiCore\InsecureCredentialsWrapper;
use Google\ApiCore\ServerStream;
use Google\Cloud\Core\Exception\BadRequestException;
use Google\Cloud\Core\Exception\ServiceException;
use Google\Cloud\Core\Testing\Snippet\Fixtures;
use Google\Cloud\Firestore\V1\BatchGetDocumentsRequest;
use Google\Cloud\Firestore\V1\BatchGetDocumentsResponse;
use Google\Cloud\Firestore\V1\BeginTransactionRequest;
Expand Down Expand Up @@ -85,7 +85,7 @@ public function setUp(): void
'projectId' => self::PROJECT,
'database' => self::DATABASE,
'firestoreClient' => $this->gapicClient->reveal(),
'credentials' => Fixtures::KEYFILE_STUB_FIXTURE()
'credentials' => new InsecureCredentialsWrapper()
]);
}

Expand Down
6 changes: 3 additions & 3 deletions Gax/src/ClientOptionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,9 @@ private static function determineMtlsEndpoint(string $apiEndpoint)
}

/**
* @param mixed $credentials
* @param string|array|FetchAuthTokenInterface|HeaderCredentialsInterface|null $credentials
* @param array $credentialsConfig
* @return CredentialsWrapper
* @return HeaderCredentialsInterface
* @throws ValidationException
*/
private function createCredentialsWrapper($credentials, array $credentialsConfig, string $universeDomain)
Expand All @@ -290,7 +290,7 @@ private function createCredentialsWrapper($credentials, array $credentialsConfig
return new CredentialsWrapper($credentials, $authHttpHandler, $universeDomain);
}

if ($credentials instanceof CredentialsWrapper) {
if ($credentials instanceof HeaderCredentialsInterface) {
return $credentials;
}

Expand Down
18 changes: 0 additions & 18 deletions Gax/src/CredentialsWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,24 +218,6 @@ public function getProjectId(?callable $httpHandler = null): ?string
return null;
}

/**
* @deprecated
* @return string Bearer string containing access token.
*/
public function getBearerString()
{
$token = $this->credentialsFetcher->getLastReceivedToken();
if (self::isExpired($token)) {
$this->checkUniverseDomain();

$token = $this->credentialsFetcher->fetchAuthToken($this->authHttpHandler);
if (!self::isValid($token)) {
return '';
}
}
return empty($token['access_token']) ? '' : 'Bearer ' . $token['access_token'];
}

/**
* @param string $audience optional audience for self-signed JWTs.
* @return callable Callable function that returns an authorization header.
Expand Down
6 changes: 3 additions & 3 deletions Gax/src/GapicClientTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ protected function getTransport()
* use by customized clients.
*
* @access private
* @return CredentialsWrapper
* @return HeaderCredentialsInterface
*/
protected function getCredentialsWrapper()
{
Expand All @@ -219,12 +219,12 @@ protected function getCredentialsWrapper()
* path to a JSON file, or a PHP array containing the decoded JSON data.
* By default this settings points to the default client config file, which is provided
* in the resources folder.
* @type string|array|FetchAuthTokenInterface|CredentialsWrapper $credentials
* @type string|array|FetchAuthTokenInterface|HeaderCredentialsInterface $credentials
* The credentials to be used by the client to authorize API calls. This option
* accepts either a path to a credentials file, or a decoded credentials file as a
* PHP array.
* *Advanced usage*: In addition, this option can also accept a pre-constructed
* \Google\Auth\FetchAuthTokenInterface object or \Google\ApiCore\CredentialsWrapper
* \Google\Auth\FetchAuthTokenInterface object or \Google\ApiCore\HeaderCredentialsInterface
* object. Note that when one of these objects are provided, any settings in
* $authConfig will be ignored.
* @type array $credentialsConfig
Expand Down
4 changes: 3 additions & 1 deletion Gax/src/HeaderCredentialsInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
*/
namespace Google\ApiCore;

interface HeaderCredentialsInterface
use Google\Auth\GetQuotaProjectInterface;

interface HeaderCredentialsInterface extends GetQuotaProjectInterface
{
/**
* @param string|null $audience optional audience for self-signed JWTs.
Expand Down
11 changes: 9 additions & 2 deletions Gax/src/InsecureCredentialsWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@

/**
* For connect to emulator.
* @TODO: implement HeaderCredentialsInterface instead of extending CredentialsWrapper
*/
class InsecureCredentialsWrapper extends CredentialsWrapper
class InsecureCredentialsWrapper implements HeaderCredentialsInterface
{
public function __construct()
{
Expand All @@ -54,4 +53,12 @@ public function getAuthorizationHeaderCallback($audience = null): ?callable
public function checkUniverseDomain(): void
{
}

/**
* @return string|null The quota project associated with the credentials.
*/
public function getQuotaProject(): ?string
{
return null;
}
}
4 changes: 1 addition & 3 deletions Gax/src/Middleware/CredentialsWrapperMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
namespace Google\ApiCore\Middleware;

use Google\ApiCore\Call;
use Google\ApiCore\CredentialsWrapper;
use Google\ApiCore\HeaderCredentialsInterface;

/**
Expand All @@ -45,8 +44,7 @@ class CredentialsWrapperMiddleware implements MiddlewareInterface
/** @var callable */
private $nextHandler;

/** @var HeaderCredentialsInterface */
private HeaderCredentialsInterface $credentialsWrapper;
private HeaderCredentialsInterface $credentialsWrapper;

public function __construct(
callable $nextHandler,
Expand Down
14 changes: 7 additions & 7 deletions Gax/src/Options/ClientOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

use ArrayAccess;
use Closure;
use Google\ApiCore\CredentialsWrapper;
use Google\ApiCore\HeaderCredentialsInterface;
use Google\ApiCore\Transport\TransportInterface;
use Google\Auth\FetchAuthTokenInterface;
use InvalidArgumentException;
Expand All @@ -47,11 +47,11 @@
* client constructor:
*
* ```
* use Google\ApiCore\ClientOptions;
* use Google\ApiCore\Options\ClientOptions;
* use Google\Cloud\SecretManager\Client\SecretManagerClient;
*
* $options = new ClientOptions([
* 'credentials' => '/path/to/my/credentials.json'
* 'apiEndpoint' => 'my-custom-endpoint.com'
* ]);
* $secretManager = new SecretManagerClient($options->toArray());
* ```
Expand All @@ -69,7 +69,7 @@ class ClientOptions implements ArrayAccess, OptionsInterface

private array $clientConfig;

/** @var string|array|FetchAuthTokenInterface|CredentialsWrapper|null */
/** @var string|array|FetchAuthTokenInterface|HeaderCredentialsInterface|null */
private $credentials;

private array $credentialsConfig;
Expand Down Expand Up @@ -112,9 +112,9 @@ class ClientOptions implements ArrayAccess, OptionsInterface
* path to a JSON file, or a PHP array containing the decoded JSON data.
* By default this settings points to the default client config file, which is provided
* in the resources folder.
* @type FetchAuthTokenInterface|CredentialsWrapper $credentials
* @type string|array|FetchAuthTokenInterface|HeaderCredentialsInterface $credentials
* This option should only be used with a pre-constructed \Google\Auth\FetchAuthTokenInterface
* object or \Google\ApiCore\CredentialsWrapper object. Note that when one of these objects
* object or \Google\ApiCore\HeaderCredentialsInterface object. Note that when one of these objects
* are provided, any settings in $authConfig will be ignored.
* **Important**: If you are providing a path to a credentials file, or a decoded credentials
* file as a PHP array, this usage is now DEPRECATED. Providing an unvalidated credential
Expand Down Expand Up @@ -246,7 +246,7 @@ public function setClientConfig($clientConfig): self
}

/**
* @param string|array|FetchAuthTokenInterface|CredentialsWrapper|null $credentials
* @param string|array|FetchAuthTokenInterface|HeaderCredentialsInterface|null $credentials
*
* @return $this
*/
Expand Down
6 changes: 3 additions & 3 deletions Gax/src/ResumableUpload/ResumableUploadClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
use Google\ApiCore\ApiException;
use Google\ApiCore\ApiStatus;
use Google\ApiCore\Call;
use Google\ApiCore\CredentialsWrapper;
use Google\ApiCore\HeaderCredentialsInterface;
use Google\ApiCore\Middleware\RetryMiddleware;
use Google\ApiCore\RetrySettings;
use Google\ApiCore\ValidationException;
Expand Down Expand Up @@ -75,13 +75,13 @@ class ResumableUploadClient

/**
* @param ResumableUploadTransportInterface $transport Transport implementing buildRequest and sendRawRequest.
* @param CredentialsWrapper $credentialsWrapper The credentials wrapper from GAPIC client.
* @param HeaderCredentialsInterface $credentialsWrapper The credentials wrapper from GAPIC client.
* @param array $headers Custom headers to include with the initial upload request.
* @param string $uploadPrefix Resumable upload path prefix (default: '/resumable/upload').
*/
public function __construct(
private ResumableUploadTransportInterface $transport,
private CredentialsWrapper $credentialsWrapper,
private HeaderCredentialsInterface $credentialsWrapper,
private array $headers = [],
private string $uploadPrefix = '/resumable/upload'
) {
Expand Down
76 changes: 15 additions & 61 deletions Gax/tests/Unit/CredentialsWrapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
namespace Google\ApiCore\Tests\Unit;

use Google\ApiCore\CredentialsWrapper;
use Google\ApiCore\HeaderCredentialsInterface;
use Google\ApiCore\InsecureCredentialsWrapper;
use Google\ApiCore\ValidationException;
use Google\Auth\ApplicationDefaultCredentials;
use Google\Auth\Cache\MemoryCacheItemPool;
Expand All @@ -43,6 +45,7 @@
use Google\Auth\FetchAuthTokenCache;
use Google\Auth\FetchAuthTokenInterface;
use Google\Auth\GCECache;
use Google\Auth\GetQuotaProjectInterface;
use Google\Auth\GetUniverseDomainInterface;
use Google\Auth\HttpHandler\HttpHandlerFactory;
use Google\Auth\ProjectIdProviderInterface;
Expand Down Expand Up @@ -274,39 +277,6 @@ public function testCheckUniverseDomainFails(
$credentialsWrapper->getAuthorizationHeaderCallback()();
}

/**
* Same test as above, but calls the deprecated CredentialsWrapper::getBearerString method
* instead of CredentialsWrapper::getAuthorizationHeaderCallback
* @dataProvider provideCheckUniverseDomainFails
*/
public function testCheckUniverseDomainOnGetBearerStringFails(
?string $universeDomain,
?string $credentialsUniverse,
?string $message = null
) {
$this->expectException(ValidationException::class);
$this->expectExceptionMessage($message ?: sprintf(
'The configured universe domain (%s) does not match the credential universe domain (%s)',
is_null($universeDomain) ? GetUniverseDomainInterface::DEFAULT_UNIVERSE_DOMAIN : $universeDomain,
is_null($credentialsUniverse) ? GetUniverseDomainInterface::DEFAULT_UNIVERSE_DOMAIN : $credentialsUniverse,
));
$fetcher = $this->prophesize(FetchAuthTokenInterface::class);
// When the $credentialsUniverse is null, the fetcher doesn't implement GetUniverseDomainInterface
if (!is_null($credentialsUniverse)) {
$fetcher->willImplement(GetUniverseDomainInterface::class);
$fetcher->getUniverseDomain()->willReturn($credentialsUniverse);
}
$fetcher->getLastReceivedToken()->willReturn(null);
// When $universeDomain is null, it means no $universeDomain argument was provided
if (is_null($universeDomain)) {
$credentialsWrapper = new CredentialsWrapper($fetcher->reveal());
} else {
$credentialsWrapper = new CredentialsWrapper($fetcher->reveal(), null, $universeDomain);
}
// Check getBearerString (deprecated)
$credentialsWrapper->getBearerString();
}

public function provideCheckUniverseDomainFails()
{
return [
Expand Down Expand Up @@ -342,11 +312,6 @@ public function testCheckUniverseDomainPasses(?string $universeDomain, ?string $
['authorization' => ['Bearer abc']],
$credentialsWrapper->getAuthorizationHeaderCallback()()
);
// Check getBearerString (deprecated)
$this->assertEquals(
'Bearer abc',
$credentialsWrapper->getBearerString()
);
}

public function provideCheckUniverseDomainPasses()
Expand All @@ -372,29 +337,6 @@ public function testCheckUniverseDomainOnGceCredentialsDoesNotCheck()
$credentialsWrapper->checkUniverseDomain();
}

/**
* @dataProvider getBearerStringData
* @runInSeparateProcess
*/
public function testGetBearerString(string $fetcherFunc, $expectedBearerString)
{
$fetcher = $this->$fetcherFunc();
$credentialsWrapper = new CredentialsWrapper($fetcher);
$bearerString = $credentialsWrapper->getBearerString();
$this->assertSame($expectedBearerString, $bearerString);
}

public function getBearerStringData()
{
return [
['getExpiredFetcher', 'Bearer 456'],
['getEagerExpiredFetcher', 'Bearer 456'],
['getUnexpiredFetcher', 'Bearer 123'],
['getInsecureFetcher', ''],
['getNullFetcher', ''],
];
}

/**
* @dataProvider getAuthorizationHeaderCallbackData
* @runInSeparateProcess
Expand Down Expand Up @@ -643,6 +585,18 @@ public function testSerializeCredentialsWrapper()
$this->assertIsString($serialized);
}

public function testInsecureCredentialsWrapperImplementsHeaderCredentialsInterface()
{
$wrapper = new InsecureCredentialsWrapper();

$this->assertInstanceOf(HeaderCredentialsInterface::class, $wrapper);
$this->assertInstanceOf(GetQuotaProjectInterface::class, $wrapper);
$this->assertNotInstanceOf(CredentialsWrapper::class, $wrapper);
$this->assertNull($wrapper->getQuotaProject());
$this->assertNull($wrapper->getAuthorizationHeaderCallback());
$wrapper->checkUniverseDomain();
}

private function setEnv(string $env, ?string $value = null)
{
if ($value === null) {
Expand Down
Loading
Loading