Skip to content

Commit

Permalink
Add AuthKey into ConnectionConfig; add github funding
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Aug 28, 2024
1 parent bdd3747 commit 8af5186
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 18 deletions.
3 changes: 3 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# These are supported funding model platforms

github: spiral
30 changes: 30 additions & 0 deletions src/Config/ConnectionConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,20 @@
*/
final class ConnectionConfig
{
/**
* @param non-empty-string $address
* @param non-empty-string|null $rootCerts
* @param non-empty-string|null $privateKey
* @param non-empty-string|null $certChain
* @param non-empty-string|\Stringable|null $authToken
*/
private function __construct(
public readonly string $address,
public readonly bool $secure = false,
public readonly ?string $rootCerts = null,
public readonly ?string $privateKey = null,
public readonly ?string $certChain = null,
public readonly string|\Stringable|null $authToken = null,
) {}

/**
Expand Down Expand Up @@ -74,4 +82,26 @@ public static function createCloud(
): self {
return new self($address, true, null, $privateKey, $certChain);
}

/**
* Set the authentication token for the service client.
*
* This is the equivalent of providing an "Authorization" header with "Bearer " + the given key.
* This will overwrite any "Authorization" header that may be on the context before each request to the
* Temporal service.
* You may pass your own {@see \Stringable} implementation to be able to change the key dynamically.
*
* @param non-empty-string|\Stringable|null $authToken
*/
public function withAuthKey(string|\Stringable|null $authToken): self
{
return new self(
$this->address,
$this->secure,
$this->rootCerts,
$this->privateKey,
$this->certChain,
$authToken,
);
}
}
30 changes: 12 additions & 18 deletions tests/src/Bootloader/TemporalBridgeBootloaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,32 +113,26 @@ public function testPipelineProvider(): void
public function testConnection(): void
{
$client = $this->getContainer()->get(ServiceClientInterface::class);
\assert($client instanceof ServiceClientInterface);
$connection = $client->getConnection();
\assert($connection instanceof \Temporal\Client\GRPC\Connection\Connection);
$workflowService = (fn() => $connection->workflowService)->call($connection);
\assert($workflowService instanceof WorkflowServiceClient);

$refl = new \ReflectionClass($client);
$baseClient = $refl->getParentClass();
$property = $baseClient->getProperty('workflowService');

$property->setAccessible(true);
/** @var WorkflowServiceClient $stub */
$stub = $property->getValue($client);

$this->assertSame('localhost:7233', $stub->getTarget());
$this->assertSame('localhost:7233', $workflowService->getTarget());
}

#[Env('TEMPORAL_CONNECTION', 'ssl')]
public function testSecureConnection(): void
{
$client = $this->getContainer()->get(ServiceClientInterface::class);
\assert($client instanceof ServiceClientInterface);
$connection = $client->getConnection();
\assert($connection instanceof \Temporal\Client\GRPC\Connection\Connection);
$workflowService = (fn() => $connection->workflowService)->call($connection);
\assert($workflowService instanceof WorkflowServiceClient);

$refl = new \ReflectionClass($client);
$baseClient = $refl->getParentClass();
$property = $baseClient->getProperty('workflowService');

$property->setAccessible(true);
/** @var WorkflowServiceClient $stub */
$stub = $property->getValue($client);

$this->assertSame('ssl:7233', $stub->getTarget());
$this->assertSame('ssl:7233', $workflowService->getTarget());
}

#[Env('TEMPORAL_CONNECTION', 'test')]
Expand Down
94 changes: 94 additions & 0 deletions tests/src/Config/ConnectionConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

declare(strict_types=1);

namespace Spiral\TemporalBridge\Tests\Config;

use Spiral\TemporalBridge\Config\ConnectionConfig;
use Spiral\TemporalBridge\Tests\TestCase;

final class ConnectionConfigTest extends TestCase
{
public function testCreateSecure(): void
{
$config = ConnectionConfig::createSecure(
address: 'localhost:2222',
rootCerts: 'crt',
privateKey: 'clientKey',
certChain: 'clientPem',
);

$this->assertTrue($config->secure);
$this->assertSame('localhost:2222', $config->address);
$this->assertSame('crt', $config->rootCerts);
$this->assertSame('clientKey', $config->privateKey);
$this->assertSame('clientPem', $config->certChain);
}

public function testCreateInsecure(): void
{
$config = ConnectionConfig::createInsecure(
address: 'localhost:1111',
);

$this->assertFalse($config->secure);
$this->assertSame('localhost:1111', $config->address);
}

public function testCreateCloud(): void
{
$config = ConnectionConfig::createCloud(
address: 'localhost:1111',
privateKey: 'clientKey',
certChain: 'clientPem',
);

$this->assertTrue($config->secure);
$this->assertSame('localhost:1111', $config->address);
$this->assertSame('clientKey', $config->privateKey);
$this->assertSame('clientPem', $config->certChain);
}

public function testWithAuthKey(): void
{
$config = ConnectionConfig::createSecure(
address: 'localhost:1111',
certChain: 'clientPem',
);

$newConfig = $config->withAuthKey($key = 'authKey');

$this->assertNotSame($config, $newConfig);
$this->assertNull($config->authToken);
$this->assertSame($key, $newConfig->authToken);
}

public function testWithAuthKeyNull(): void
{
$config = ConnectionConfig::createSecure(
address: 'localhost:1111',
)->withAuthKey('authKey');

$newConfig = $config->withAuthKey(null);

$this->assertNotSame($config, $newConfig);
$this->assertNotNull($config->authToken);
$this->assertNull($newConfig->authToken);
}

public function testWithAuthKeyStringable(): void
{
$config = ConnectionConfig::createSecure(
address: 'localhost:1111',
)->withAuthKey(
$key = new class() implements \Stringable {
public function __toString(): string
{
return 'authKey';
}
}
);

$this->assertSame($key, $config->authToken);
}
}

0 comments on commit 8af5186

Please sign in to comment.