Skip to content

Commit

Permalink
fix: universe domain check for grpc transport (#534)
Browse files Browse the repository at this point in the history
  • Loading branch information
bshaffer authored Feb 16, 2024
1 parent dfca60f commit 1026d8a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/CredentialsWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ public function getAuthorizationHeaderCallback($audience = null)
/**
* Verify that the expected universe domain matches the universe domain from the credentials.
*/
private function checkUniverseDomain()
public function checkUniverseDomain()
{
if (false === $this->hasCheckedUniverse) {
$credentialsUniverse = $this->credentialsFetcher instanceof GetUniverseDomainInterface
Expand Down
16 changes: 16 additions & 0 deletions src/Transport/GrpcTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ public static function build(string $apiEndpoint, array $config = [])
*/
public function startBidiStreamingCall(Call $call, array $options)
{
$this->verifyUniverseDomain($options);

return new BidiStream(
$this->_bidiRequest(
'/' . $call->getMethod(),
Expand All @@ -175,6 +177,9 @@ public function startBidiStreamingCall(Call $call, array $options)
*/
public function startClientStreamingCall(Call $call, array $options)
{

$this->verifyUniverseDomain($options);

return new ClientStream(
$this->_clientStreamRequest(
'/' . $call->getMethod(),
Expand All @@ -191,6 +196,8 @@ public function startClientStreamingCall(Call $call, array $options)
*/
public function startServerStreamingCall(Call $call, array $options)
{
$this->verifyUniverseDomain($options);

$message = $call->getMessage();

if (!$message) {
Expand All @@ -216,6 +223,8 @@ public function startServerStreamingCall(Call $call, array $options)
*/
public function startUnaryCall(Call $call, array $options)
{
$this->verifyUniverseDomain($options);

$unaryCall = $this->_simpleRequest(
'/' . $call->getMethod(),
$call->getMessage(),
Expand Down Expand Up @@ -245,6 +254,13 @@ function () use ($unaryCall, $options, &$promise) {
return $promise;
}

private function verifyUniverseDomain(array $options)
{
if (isset($options['credentialsWrapper'])) {
$options['credentialsWrapper']->checkUniverseDomain();
}
}

private function getCallOptions(array $options)
{
$callOptions = $options['transportOptions']['grpcOptions'] ?? [];
Expand Down
2 changes: 2 additions & 0 deletions tests/Tests/Unit/Transport/GrpcTransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,8 @@ public function testAudienceOption()
$call->getDecodeType()->shouldBeCalled();

$credentialsWrapper = $this->prophesize(CredentialsWrapper::class);
$credentialsWrapper->checkUniverseDomain()
->shouldBeCalledOnce();
$credentialsWrapper->getAuthorizationHeaderCallback('an-audience')
->shouldBeCalledOnce();
$hostname = '';
Expand Down

0 comments on commit 1026d8a

Please sign in to comment.