Skip to content

Commit 7aeea0e

Browse files
committed
TLS Connection Support
1 parent 8c0ce4c commit 7aeea0e

15 files changed

Lines changed: 296 additions & 69 deletions

.github/workflows/ci.yml

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,46 +12,58 @@ jobs:
1212
outputs:
1313
version: ${{ steps.supported-versions-matrix.outputs.version }}
1414
steps:
15-
- uses: actions/checkout@v1
15+
- uses: actions/checkout@v4
1616
- id: supported-versions-matrix
1717
uses: WyriHaximus/github-action-composer-php-versions-in-range@v1
1818
tests:
19-
services:
20-
postgres:
21-
image: postgres:${{ matrix.postgres }}
22-
env:
23-
POSTGRES_PASSWORD: postgres
24-
POSTGRES_INITDB_ARGS: --auth-host=md5
25-
# Set health checks to wait until postgres has started
26-
options: >-
27-
--health-cmd pg_isready
28-
--health-interval 10s
29-
--health-timeout 5s
30-
--health-retries 5
31-
ports:
32-
- 5432:5432
33-
name: Testing on PHP ${{ matrix.php }} with ${{ matrix.composer }} dependency preference against Postgres ${{ matrix.postgres }}
19+
# services:
20+
# postgres:
21+
# image: postgres:${{ matrix.postgres }}
22+
# env:
23+
# POSTGRES_PASSWORD: postgres
24+
# POSTGRES_INITDB_ARGS: --auth-host=md5
25+
# # Set health checks to wait until postgres has started
26+
# options: >-
27+
# --health-cmd pg_isready
28+
# --health-interval 10s
29+
# --health-timeout 5s
30+
# --health-retries 5
31+
# ports:
32+
# - 5432:5432
33+
name: Testing on PHP ${{ matrix.php }} with ${{ matrix.composer }} dependency preference against Postgres ${{ matrix.postgres }} with TLS ${{ matrix.tls }}
3434
strategy:
3535
fail-fast: false
3636
matrix:
37-
php: ${{ fromJson(needs.supported-versions-matrix.outputs.version) }}
38-
postgres: [12, 13, 14, 15]
39-
composer: [lowest, locked, highest]
37+
# php: ${{ fromJson(needs.supported-versions-matrix.outputs.version) }}
38+
# postgres: [14, 15, 16, 17]
39+
# composer: [lowest, locked, highest]
40+
php: ["8.4"]
41+
postgres: [17]
42+
composer: [locked]
43+
tls: [false, true]
4044
needs:
4145
- supported-versions-matrix
4246
runs-on: ubuntu-latest
4347
steps:
44-
- uses: actions/checkout@v3
48+
- uses: actions/checkout@v4
49+
- uses: ikalnytskyi/action-setup-postgres@v7
50+
with:
51+
postgres-version: ${{ matrix.postgres }}
52+
ssl: ${{ matrix.tls }}
53+
id: postgres
54+
- run: |
55+
psql -c "CREATE USER pgasync"
56+
psql -c "ALTER ROLE pgasync PASSWORD 'pgasync'"
57+
psql -c "CREATE USER pgasyncpw"
58+
psql -c "ALTER ROLE pgasyncpw PASSWORD 'example_password'"
59+
psql -c "CREATE USER scram_user"
60+
psql -c "SET password_encryption='scram-sha-256';ALTER ROLE scram_user PASSWORD 'scram_password'"
61+
psql -c "CREATE DATABASE pgasync_test OWNER pgasync"
62+
# cat tests/test_db.sql | xargs -I % psql -c "%"
63+
env:
64+
PGSERVICE: "${{ steps.postgres.outputs.service-name }}"
4565
- run: |
46-
PGPASSWORD=postgres psql -h localhost -U postgres -c "CREATE USER pgasync"
47-
PGPASSWORD=postgres psql -h localhost -U postgres -c "ALTER ROLE pgasync PASSWORD 'pgasync'"
48-
PGPASSWORD=postgres psql -h localhost -U postgres -c "CREATE USER pgasyncpw"
49-
PGPASSWORD=postgres psql -h localhost -U postgres -c "ALTER ROLE pgasyncpw PASSWORD 'example_password'"
50-
PGPASSWORD=postgres psql -h localhost -U postgres -c "CREATE USER scram_user"
51-
PGPASSWORD=postgres psql -h localhost -U postgres -c "SET password_encryption='scram-sha-256';ALTER ROLE scram_user PASSWORD 'scram_password'"
52-
PGPASSWORD=postgres psql -h localhost -U postgres -c "CREATE DATABASE pgasync_test OWNER pgasync"
5366
PGPASSWORD=pgasync psql -h localhost -U pgasync -f tests/test_db.sql pgasync_test
54-
# PGPASSWORD=postgres cat tests/test_db.sql | xargs -I % psql -h localhost -U postgres -c "%"
5567
- uses: shivammathur/setup-php@v2
5668
with:
5769
php-version: ${{ matrix.php }}
@@ -60,4 +72,8 @@ jobs:
6072
with:
6173
dependency-versions: ${{ matrix.composer }}
6274
# - run: vendor/bin/phpunit --testdox
75+
- run: echo "dsn=postgresql://pgasync:pgasync@${{ steps.postgres.outputs.service-name }}/pgasync_test" >> $GITHUB_OUTPUT
76+
id: dsn
6377
- run: vendor/bin/phpunit
78+
env:
79+
TEST_POSTGRES_DSN: ${{ steps.dsn.outputs.dsn }}

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,35 @@ $client->listen('some_channel')
7979
$client->query("NOTIFY some_channel, 'Hello World'")->subscribe();
8080
```
8181

82+
## Example - Connecting over TLS with CA certificate file
83+
```php
84+
85+
$client = new PgAsync\Client([
86+
"host" => "127.0.0.1",
87+
"port" => "5432",
88+
"user" => "matt",
89+
"database" => "matt",
90+
"tls" => "verify-full",
91+
"tls_connector_flags" => [
92+
"cafile" => "/path/to/ca.crt",
93+
],
94+
]);
95+
96+
$client->query('SELECT * FROM channel')->subscribe(
97+
function ($row) {
98+
var_dump($row);
99+
},
100+
function ($e) {
101+
echo "Failed.\n";
102+
},
103+
function () {
104+
echo "Complete.\n";
105+
}
106+
);
107+
108+
109+
```
110+
82111
## Install
83112
With [composer](https://getcomposer.org/) install into you project with:
84113

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@
3636
"php": ">=7.0.0",
3737
"voryx/event-loop": "^3.0 || ^2.0.2",
3838
"reactivex/rxphp": "^2.0",
39-
"react/socket": "^1.0 || ^0.8 || ^0.7",
40-
"evenement/evenement": "^2.0 | ^3.0"
39+
"react/promise-stream": "^1.5",
40+
"evenement/evenement": "^2.0 | ^3.0",
41+
"wyrihaximus/react-opportunistic-tls": "^1"
4142
},
4243
"require-dev": {
4344
"phpunit/phpunit": ">=8.5.23 || ^6.5.5",

src/PgAsync/Connection.php

Lines changed: 82 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PgAsync\Command\PasswordMessage;
1313
use PgAsync\Command\SaslInitialResponse;
1414
use PgAsync\Command\SaslResponse;
15+
use PgAsync\Command\SSLRequest;
1516
use PgAsync\Command\Sync;
1617
use PgAsync\Command\Terminate;
1718
use PgAsync\Message\Authentication;
@@ -32,9 +33,13 @@
3233
use PgAsync\Message\ReadyForQuery;
3334
use PgAsync\Message\RowDescription;
3435
use PgAsync\Command\StartupMessage;
36+
use React\EventLoop\Loop;
3537
use React\EventLoop\LoopInterface;
36-
use React\Socket\Connector;
38+
use React\Promise\Promise;
39+
use React\Socket\ConnectionInterface;
40+
use WyriHaximus\React\Socket\Connector;
3741
use React\Socket\ConnectorInterface;
42+
use WyriHaximus\React\Socket\OpportunisticTlsConnectionInterface;
3843
use React\Stream\DuplexStreamInterface;
3944
use Rx\Disposable\CallbackDisposable;
4045
use Rx\Disposable\EmptyDisposable;
@@ -43,6 +48,8 @@
4348
use Rx\ObserverInterface;
4449
use Rx\SchedulerInterface;
4550
use Rx\Subject\Subject;
51+
use function React\Promise\resolve;
52+
use function React\Promise\Stream\first;
4653

4754
class Connection extends EventEmitter
4855
{
@@ -73,6 +80,16 @@ class Connection extends EventEmitter
7380
const CONNECTION_NEEDED = 8; /* Internal state: connect() needed */
7481
const CONNECTION_CLOSED = 9;
7582

83+
// Reference table: https://www.postgresql.org/docs/current/libpq-ssl.html#LIBPQ-SSL-PROTECTION
84+
const TLS_MODE_DISABLE = 'disable';
85+
const TLS_MODE_ALLOW = 'allow';
86+
const TLS_MODE_PREFER = 'prefer';
87+
const TLS_MODE_REQUIRE = 'require';
88+
const TLS_MODE_VERIFY_CA = 'verify-ca';
89+
const TLS_MODE_VERIFY_FULL = 'verify-full';
90+
const TLS_MODE_LIST_FULL = [self::TLS_MODE_DISABLE, self::TLS_MODE_ALLOW, self::TLS_MODE_PREFER, self::TLS_MODE_REQUIRE, self::TLS_MODE_VERIFY_CA, self::TLS_MODE_VERIFY_FULL];
91+
const TLS_MODE_LIST_REQUIRED = [self::TLS_MODE_REQUIRE, self::TLS_MODE_VERIFY_CA, self::TLS_MODE_VERIFY_FULL];
92+
7693
private $queryState;
7794
private $queryType;
7895
private $connStatus;
@@ -134,6 +151,8 @@ class Connection extends EventEmitter
134151

135152
/** @var bool */
136153
private $auto_disconnect = false;
154+
private $tls = self::TLS_MODE_PREFER;
155+
private $tlsConnectorFlags = [];
137156
private $password;
138157

139158
public function __construct(array $parameters, LoopInterface $loop, ConnectorInterface $connector = null)
@@ -158,6 +177,19 @@ public function __construct(array $parameters, LoopInterface $loop, ConnectorInt
158177
unset($parameters['password']);
159178
}
160179

180+
if (array_key_exists('tls', $parameters)) {
181+
if (!in_array($this->tls, self::TLS_MODE_LIST_FULL)) {
182+
throw new \InvalidArgumentException('TLS mode must be one off "' . implode(', ', self::TLS_MODE_LIST_FULL) . ' but got "' . $parameters['tls'] . '" instead');
183+
}
184+
$this->tls = $parameters['tls'];
185+
unset($parameters['tls']);
186+
}
187+
188+
if (array_key_exists('tls_connector_flags', $parameters)) {
189+
$this->tlsConnectorFlags = $parameters['tls_connector_flags'];
190+
unset($parameters['tls_connector_flags']);
191+
}
192+
161193
if (isset($parameters['auto_disconnect'])) {
162194
$this->auto_disconnect = $parameters['auto_disconnect'];
163195
unset($parameters['auto_disconnect']);
@@ -172,8 +204,17 @@ public function __construct(array $parameters, LoopInterface $loop, ConnectorInt
172204
$this->queryState = static::STATE_BUSY;
173205
$this->queryType = static::QUERY_SIMPLE;
174206
$this->connStatus = static::CONNECTION_NEEDED;
175-
$this->socket = $connector ?: new Connector($loop);
176-
$this->uri = 'tcp://' . $parameters['host'] . ':' . $parameters['port'];
207+
$this->socket = $connector ?: new Connector($loop, [
208+
'tls' => [
209+
'verify_peer' => $this->tls === self::TLS_MODE_VERIFY_FULL,
210+
'verify_peer_name' => $this->tls === self::TLS_MODE_VERIFY_FULL,
211+
'allow_self_signed' => $this->tls !== self::TLS_MODE_VERIFY_FULL,
212+
] + $this->tlsConnectorFlags,
213+
]);
214+
// We always url `opportunistic+tls` as scheme because the logic required for using `tcp` on TLS `disable`
215+
// mode is more complex than worth it when connecting to the server. And the `SecureConnector` gives us a
216+
// plaint text connection with all TLS flags already set and ready to use for all the other modes.
217+
$this->uri = 'opportunistic+tls://' . $parameters['host'] . ':' . $parameters['port'];
177218
$this->notificationSubject = new Subject();
178219
$this->cancelPending = false;
179220
$this->cancelRequested = false;
@@ -191,23 +232,43 @@ private function start()
191232
$this->connStatus = static::CONNECTION_STARTED;
192233

193234
$this->socket->connect($this->uri)->then(
194-
function (DuplexStreamInterface $stream) {
195-
$this->stream = $stream;
196-
$this->connStatus = static::CONNECTION_MADE;
197-
198-
$stream->on('close', [$this, 'onClose']);
235+
function (OpportunisticTlsConnectionInterface $stream) {
236+
(new Promise(function (callable $resolve, callable $reject) use ($stream) {
237+
if ($this->tls !== self::TLS_MODE_DISABLE) {
238+
first($stream)->then(function ($data) use ($resolve, $reject, $stream) {
239+
if (trim($data) === 'S') {
240+
$stream->enableEncryption()->then($resolve, $reject);
241+
return;
242+
}
243+
244+
if (in_array($this->tls, self::TLS_MODE_LIST_REQUIRED)) {
245+
$reject(new \RuntimeException('Failed to encrypt connection while required'));
246+
return;
247+
}
248+
249+
$resolve($stream);
250+
}, $reject);
251+
252+
$ssl = new SSLRequest();
253+
$stream->write($ssl->encodedMessage());
254+
return;
255+
}
199256

200-
$stream->on('data', [$this, 'onData']);
257+
$resolve($stream);
258+
}))->then(function (DuplexStreamInterface $stream) {
259+
$this->stream = $stream;
260+
$this->connStatus = static::CONNECTION_MADE;
201261

202-
// $ssl = new SSLRequest();
203-
// $stream->write($ssl->encodedMessage());
262+
$stream->on('close', [$this, 'onClose']);
263+
$stream->on('data', [$this, 'onData']);
204264

205-
$startupParameters = $this->parameters;
206-
unset($startupParameters['host'], $startupParameters['port']);
265+
$startupParameters = $this->parameters;
266+
unset($startupParameters['host'], $startupParameters['port']);
207267

208-
$startup = new StartupMessage();
209-
$startup->setParameters($startupParameters);
210-
$stream->write($startup->encodedMessage());
268+
$startup = new StartupMessage();
269+
$startup->setParameters($startupParameters);
270+
$stream->write($startup->encodedMessage());
271+
});
211272
},
212273
function ($e) {
213274
// connection error
@@ -596,11 +657,11 @@ function (ObserverInterface $observer, SchedulerInterface $scheduler = null) use
596657
$this->processQueue();
597658

598659
return new CallbackDisposable(function () use ($q) {
599-
if ($this->currentCommand === $q && $q->isActive()) {
600-
$this->cancelRequested = true;
601-
}
602-
$q->cancel();
603-
});
660+
if ($this->currentCommand === $q && $q->isActive()) {
661+
$this->cancelRequested = true;
662+
}
663+
$q->cancel();
664+
});
604665
}
605666
);
606667

tests/Integration/BoolTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class BoolTest extends TestCase
1010
public function testBools()
1111
{
1212

13-
$client = new Client(["user" => $this::getDbUser(), "password" => $this::getDbUser(), "database" => $this::getDbName()]);
13+
$client = self::clientFromEnv(["user" => $this::getDbUser(), "password" => $this::getDbUser(), "database" => $this::getDbName()]);
1414

1515
$count = $client->query("SELECT * FROM thing");
1616

@@ -57,7 +57,7 @@ function () use (&$completes, $client) {
5757
*/
5858
public function testBoolParam()
5959
{
60-
$client = new Client(["user" => $this::getDbUser(), "password" => $this::getDbUser(), "database" => $this::getDbName()]);
60+
$client = self::clientFromEnv(["user" => $this::getDbUser(), "password" => $this::getDbUser(), "database" => $this::getDbName()]);
6161

6262
$args = [false, 1];
6363

tests/Integration/ClientTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class ClientTest extends TestCase
1111
{
1212
public function testClientReusesIdleConnection()
1313
{
14-
$client = new Client(["user" => $this->getDbUser(), "password" => $this::getDbUser(), "database" => $this::getDbName()], $this->getLoop());
14+
$client = self::clientFromEnv(["user" => $this->getDbUser(), "password" => $this::getDbUser(), "database" => $this::getDbName()], $this->getLoop());
1515

1616
$hello = null;
1717

@@ -79,7 +79,7 @@ function () {
7979

8080
public function testAutoDisconnect()
8181
{
82-
$client = new Client([
82+
$client = self::clientFromEnv([
8383
"user" => $this->getDbUser(),
8484
"password" => $this::getDbUser(),
8585
"database" => $this::getDbName(),
@@ -114,7 +114,7 @@ function () {
114114

115115
public function testSendingTwoQueriesRepeatedlyOnlyCreatesTwoConnections()
116116
{
117-
$client = new Client([
117+
$client = self::clientFromEnv([
118118
"user" => $this->getDbUser(),
119119
"password" => $this::getDbUser(),
120120
"database" => $this::getDbName(),
@@ -156,7 +156,7 @@ function () {
156156

157157
public function testMaxConnections()
158158
{
159-
$client = new Client([
159+
$client = self::clientFromEnv([
160160
"user" => $this->getDbUser(),
161161
"password" => $this::getDbUser(),
162162
"database" => $this::getDbName(),
@@ -197,7 +197,7 @@ function () {
197197

198198
public function testListen()
199199
{
200-
$client = new Client([
200+
$client = self::clientFromEnv([
201201
"user" => $this->getDbUser(),
202202
"password" => $this::getDbUser(),
203203
"database" => $this::getDbName(),

tests/Integration/Md5PasswordTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Md5PasswordTest extends TestCase
99
{
1010
public function testMd5Login()
1111
{
12-
$client = new Client([
12+
$client = self::clientFromEnv([
1313
"user" => "pgasyncpw",
1414
"database" => $this->getDbName(),
1515
"auto_disconnect" => true,

tests/Integration/NullPasswordTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public function testNullPassword()
1111
{
1212
$this->markTestSkipped('Not using null password anymore. Maybe should setup tests to twst this again.');
1313

14-
$client = new Client([
14+
$client = self::clientFromEnv([
1515
"user" => $this::getDbUser(),
1616
"database" => $this::getDbName(),
1717
"password" => null

0 commit comments

Comments
 (0)