diff --git a/src/Bootloader/TemporalBridgeBootloader.php b/src/Bootloader/TemporalBridgeBootloader.php index 210fef6..e421b2f 100644 --- a/src/Bootloader/TemporalBridgeBootloader.php +++ b/src/Bootloader/TemporalBridgeBootloader.php @@ -15,9 +15,8 @@ use Spiral\Core\FactoryInterface; use Spiral\RoadRunnerBridge\Bootloader\RoadRunnerBootloader; use Spiral\TemporalBridge\Commands; +use Spiral\TemporalBridge\Config\ConnectionConfig; use Spiral\TemporalBridge\Config\TemporalConfig; -use Spiral\TemporalBridge\Connection\Connection; -use Spiral\TemporalBridge\Connection\SslConnection; use Spiral\TemporalBridge\DeclarationLocator; use Spiral\TemporalBridge\DeclarationLocatorInterface; use Spiral\TemporalBridge\DeclarationRegistryInterface; @@ -156,11 +155,11 @@ protected function initConfig(EnvironmentInterface $env): void $this->config->setDefaults( TemporalConfig::CONFIG, [ - // 'address' => $env->get('TEMPORAL_ADDRESS', '127.0.0.1:7233'), - // 'namespace' => 'App\\Endpoint\\Temporal\\Workflow', 'connection' => $env->get('TEMPORAL_CONNECTION', 'default'), 'connections' => [ - 'default' => new Connection(address: $env->get('TEMPORAL_ADDRESS', '127.0.0.1:7233')), + 'default' => ConnectionConfig::createInsecure( + address: $env->get('TEMPORAL_ADDRESS', '127.0.0.1:7233'), + ), ], 'defaultWorker' => (string)$env->get( 'TEMPORAL_TASK_QUEUE', @@ -176,17 +175,14 @@ protected function initServiceClient(TemporalConfig $config): ServiceClientInter { $connection = $config->getConnection($config->getDefaultConnection()); - if ($connection instanceof SslConnection) { - return ServiceClient::createSSL( + return $connection->secure + ? ServiceClient::createSSL( address: $connection->address, - crt: $connection->crt, - clientKey: $connection->clientKey, - clientPem: $connection->clientPem, - overrideServerName: $connection->overrideServerName, - ); - } - - return ServiceClient::create(address: $connection->address); + crt: $connection->rootCerts, + clientKey: $connection->privateKey, + clientPem: $connection->certChain, + ) + : ServiceClient::create(address: $connection->address); } protected function initPipelineProvider(TemporalConfig $config, FactoryInterface $factory): PipelineProvider diff --git a/src/Config/ConnectionConfig.php b/src/Config/ConnectionConfig.php new file mode 100644 index 0000000..95ca571 --- /dev/null +++ b/src/Config/ConnectionConfig.php @@ -0,0 +1,77 @@ +<?php + +declare(strict_types=1); + +namespace Spiral\TemporalBridge\Config; + +/** + * Temporal connection configuration. + * + * How to connect to local Temporal server: + * + * ```php + * ConnectionConfig::createInsecure('localhost:7233') + * ``` + * + * How to connect to Temporal Cloud: + * + * ```php + * ConnectionConfig::createCloud( + * address: 'foo-bar-default.baz.tmprl.cloud:7233', + * privateKey: '/my-project.key', + * certChain: '/my-project.pem', + * ) + * ``` + */ +final class ConnectionConfig +{ + 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, + ) {} + + /** + * @param non-empty-string $address + */ + public static function createInsecure( + string $address, + ): self { + return new self($address); + } + + /** + * @param non-empty-string $address + * @param non-empty-string|null $rootCerts Root certificates string or file in PEM format. + * If null provided, default gRPC root certificates are used. + * @param non-empty-string|null $privateKey Client private key string or file in PEM format. + * @param non-empty-string|null $certChain Client certificate chain string or file in PEM format. + */ + public static function createSecure( + string $address, + ?string $rootCerts = null, + ?string $privateKey = null, + ?string $certChain = null, + ): self { + return new self($address, true, $rootCerts, $privateKey, $certChain); + } + + /** + * Used to connect to Temporal Cloud. + * + * @link https://docs.temporal.io/cloud/get-started + * + * @param non-empty-string $address + * @param non-empty-string $privateKey Client private key string or file in PEM format. + * @param non-empty-string $certChain Client certificate chain string or file in PEM format. + */ + public static function createCloud( + string $address, + string $privateKey, + string $certChain, + ): self { + return new self($address, true, null, $privateKey, $certChain); + } +} diff --git a/src/Config/TemporalConfig.php b/src/Config/TemporalConfig.php index 4ebdf13..b9f30d0 100644 --- a/src/Config/TemporalConfig.php +++ b/src/Config/TemporalConfig.php @@ -6,9 +6,6 @@ use Spiral\Core\Container\Autowire; use Spiral\Core\InjectableConfig; -use Spiral\TemporalBridge\Connection\Connection; -use Spiral\TemporalBridge\Connection\DsnConnection; -use Spiral\TemporalBridge\Connection\SslConnection; use Temporal\Client\ClientOptions; use Temporal\Exception\ExceptionInterceptorInterface; use Temporal\Internal\Interceptor\Interceptor; @@ -26,7 +23,7 @@ * @property array{ * address?: non-empty-string|null, * connection: non-empty-string, - * connections: array<non-empty-string, Connection>, + * connections: array<non-empty-string, ConnectionConfig>, * temporalNamespace: non-empty-string, * defaultWorker: non-empty-string, * workers: array<non-empty-string, WorkerOptions|TWorker>, @@ -62,7 +59,7 @@ public function getDefaultConnection(): string return $this->config['connection'] ?? 'default'; } - public function getConnection(string $name): Connection + public function getConnection(string $name): ConnectionConfig { // Legacy support. Will be removed in further versions. // If you read this, please remove address from your configuration and use connections instead. @@ -72,7 +69,7 @@ public function getConnection(string $name): Connection 'Using `address` is deprecated, use `connections` instead.', \E_USER_DEPRECATED, ); - return new Connection(address: $address); + return ConnectionConfig::createInsecure(address: $address); } if (isset($this->config['connections'][$name])) { diff --git a/src/Connection/Connection.php b/src/Connection/Connection.php deleted file mode 100644 index 4b4e27b..0000000 --- a/src/Connection/Connection.php +++ /dev/null @@ -1,20 +0,0 @@ -<?php - -declare(strict_types=1); - -namespace Spiral\TemporalBridge\Connection; - -class Connection -{ - /** - * @param non-empty-string $address - * - * ```php - * new Connection('localhost:7233'); - * ``` - */ - public function __construct( - public readonly string $address, - ) { - } -} diff --git a/src/Connection/SslConnection.php b/src/Connection/SslConnection.php deleted file mode 100644 index f85f0c3..0000000 --- a/src/Connection/SslConnection.php +++ /dev/null @@ -1,25 +0,0 @@ -<?php - -declare(strict_types=1); - -namespace Spiral\TemporalBridge\Connection; - -class SslConnection extends Connection -{ - /** - * @param non-empty-string $address - * @param non-empty-string $crt Full path to the certificate file - * @param non-empty-string|null $clientKey Full path to the client key file - * @param non-empty-string|null $clientPem Full path to the client pem file - * @param string|null $overrideServerName - */ - public function __construct( - string $address, - public readonly ?string $crt = null, - public readonly ?string $clientKey = null, - public readonly ?string $clientPem = null, - public readonly ?string $overrideServerName = null, - ) { - parent::__construct($address); - } -} diff --git a/src/Connection/TemporalCloudConnection.php b/src/Connection/TemporalCloudConnection.php deleted file mode 100644 index d64c284..0000000 --- a/src/Connection/TemporalCloudConnection.php +++ /dev/null @@ -1,30 +0,0 @@ -<?php - -declare(strict_types=1); - -namespace Spiral\TemporalBridge\Connection; - -/** - * This connection is used to connect to Temporal Cloud. - * - * @see https://docs.temporal.io/cloud/get-started - */ -final class TemporalCloudConnection extends SslConnection -{ - /** - * @param non-empty-string $address - * @param non-empty-string $clientKey Full path to the client key file. - * @param non-empty-string $clientPem Full path to the client pem file. - */ - public function __construct( - string $address, - string $clientKey, - string $clientPem, - ) { - parent::__construct( - address: $address, - clientKey: $clientKey, - clientPem: $clientPem, - ); - } -} diff --git a/tests/app/config/temporal.php b/tests/app/config/temporal.php index 10e0be0..36bea50 100644 --- a/tests/app/config/temporal.php +++ b/tests/app/config/temporal.php @@ -2,27 +2,24 @@ declare(strict_types=1); -use Spiral\TemporalBridge\Connection\Connection; -use Spiral\TemporalBridge\Connection\SslConnection; -use Spiral\TemporalBridge\Connection\TemporalCloudConnection; +use Spiral\TemporalBridge\Config\ConnectionConfig; return [ 'connection' => env('TEMPORAL_CONNECTION', 'default'), 'connections' => [ - 'default' => new Connection( + 'default' => ConnectionConfig::createInsecure( address: 'localhost:7233', ), - 'ssl' => new SslConnection( + 'ssl' => ConnectionConfig::createSecure( address: 'ssl:7233', - crt: '/path/to/crt', - clientKey: '/path/to/clientKey', - clientPem: '/path/to/clientPem', - overrideServerName: 'overrideServerName', + rootCerts: '/path/to/crt', + privateKey: '/path/to/clientKey', + certChain: '/path/to/clientPem', ), - 'temporal_cloud' => new TemporalCloudConnection( + 'temporal_cloud' => ConnectionConfig::createCloud( address: 'ssl:7233', - clientKey: '/path/to/clientKey', - clientPem: '/path/to/clientPem', + privateKey: '/path/to/clientKey', + certChain: '/path/to/clientPem', ), ], ]; diff --git a/tests/src/Bootloader/TemporalBridgeBootloaderTest.php b/tests/src/Bootloader/TemporalBridgeBootloaderTest.php index 16959f9..c2eb152 100644 --- a/tests/src/Bootloader/TemporalBridgeBootloaderTest.php +++ b/tests/src/Bootloader/TemporalBridgeBootloaderTest.php @@ -126,7 +126,7 @@ public function testConnection(): void } #[Env('TEMPORAL_CONNECTION', 'ssl')] - public function testSslConnection(): void + public function testSecureConnection(): void { $client = $this->getContainer()->get(ServiceClientInterface::class); diff --git a/tests/src/Config/TemporalConfigTest.php b/tests/src/Config/TemporalConfigTest.php index 11b5eeb..45f9170 100644 --- a/tests/src/Config/TemporalConfigTest.php +++ b/tests/src/Config/TemporalConfigTest.php @@ -4,10 +4,8 @@ namespace Spiral\TemporalBridge\Tests\Config; +use Spiral\TemporalBridge\Config\ConnectionConfig; use Spiral\TemporalBridge\Config\TemporalConfig; -use Spiral\TemporalBridge\Connection\Connection; -use Spiral\TemporalBridge\Connection\DsnConnection; -use Spiral\TemporalBridge\Connection\SslConnection; use Spiral\TemporalBridge\Tests\TestCase; use Temporal\Client\ClientOptions; use Temporal\Worker\WorkerFactoryInterface; @@ -38,7 +36,7 @@ public function testGetConnectionFromAddress(): void ]); $connection = $config->getConnection('default'); - $this->assertSame(Connection::class, $connection::class); + $this->assertSame(ConnectionConfig::class, $connection::class); $this->assertSame('localhost:1111', $connection->address); } @@ -47,25 +45,22 @@ public function testGetSslConnection(): void { $config = new TemporalConfig([ 'connections' => [ - 'default' => new SslConnection( + 'default' => ConnectionConfig::createSecure( address: 'localhost:2222', - crt: 'crt', - clientKey: 'clientKey', - clientPem: 'clientPem', - overrideServerName: 'overrideServerName', + rootCerts: 'crt', + privateKey: 'clientKey', + certChain: 'clientPem', ), ], ]); $connection = $config->getConnection('default'); - $this->assertSame(SslConnection::class, $connection::class); - + $this->assertTrue($connection->secure); $this->assertSame('localhost:2222', $connection->address); - $this->assertSame('crt', $connection->crt); - $this->assertSame('clientKey', $connection->clientKey); - $this->assertSame('clientPem', $connection->clientPem); - $this->assertSame('overrideServerName', $connection->overrideServerName); + $this->assertSame('crt', $connection->rootCerts); + $this->assertSame('clientKey', $connection->privateKey); + $this->assertSame('clientPem', $connection->certChain); } public function testGetsDefaultWorker(): void