Skip to content

Commit 8a4042b

Browse files
committed
Allow ssl on http transport too
1 parent fabaf24 commit 8a4042b

File tree

3 files changed

+252
-110
lines changed

3 files changed

+252
-110
lines changed

README.md

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,30 @@ return [
9999
// transport. When forgotten or set to null, default path '/gelf'
100100
// is used.
101101
'path' => null,
102+
103+
// This optional option enable or disable ssl on TCP transport.
104+
// Default is false.
105+
'ssl' => false,
106+
107+
// If ssl is enabled on TCP transport, the following configuration
108+
// is used.
109+
'ssl_options' => [
110+
// Enable or disable the peer certificate check. Default is
111+
// null.
112+
'verify_peer' => true,
113+
114+
// Path to a custom CA file (eg: "/path/to/ca.pem"). Default
115+
// is null.
116+
'ca_file' => null,
117+
118+
// List of ciphers the SSL layer may use, formatted as
119+
// specified in ciphers(1). Default is null.
120+
'ciphers' => null,
121+
122+
// Whether self-signed certificates are allowed. Default is
123+
// false.
124+
'allow_self_signed' => false,
125+
],
102126

103127
// This optional option determines the maximum length per message
104128
// field. When forgotten or set to null, the default value of
@@ -114,16 +138,6 @@ return [
114138
// from the Monolog record. Default is null (no extra prefix)
115139
'extra_prefix' => null,
116140

117-
// Optional option to set ssl on tcp requests. On udp requests this is ignored
118-
// This configuration will be added the specified port in this configuration item.
119-
// The base package of graylog is only setting ssl on port 12202.
120-
// When you just want base settings of graylog2/gelf-php then you don't specify this attribute.
121-
'ssl' => [
122-
'verify_peer' => true,
123-
'ca_file' => '/path/to/ca.pem', // or null
124-
'ciphers' => 'TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256', // or null
125-
'allow_self_signed' => false,
126-
]
127141
],
128142
],
129143
];

src/GelfLoggerFactory.php

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function __invoke(array $config): Logger
4343
$config['host'] ?? '127.0.0.1',
4444
$config['port'] ?? 12201,
4545
$config['path'] ?? null,
46-
$config['ssl'] ?? null
46+
$this->enableSsl($config) ? $this->sslOptions($config['ssl_options'] ?? null) : null
4747
)
4848
);
4949

@@ -70,18 +70,43 @@ protected function getTransport(
7070
string $host,
7171
int $port,
7272
?string $path = null,
73-
?array $ssl = null
73+
?SslOptions $sslOptions = null
7474
): AbstractTransport {
7575
switch (strtolower($transport)) {
7676
case 'tcp':
77-
return new TcpTransport($host, $port, $this->createSsl($ssl));
78-
case 'http':
79-
return new HttpTransport($host, $port, $path ?? HttpTransport::DEFAULT_PATH);
77+
return new TcpTransport($host, $port, $sslOptions);
78+
case 'http':
79+
return new HttpTransport($host, $port, $path ?? HttpTransport::DEFAULT_PATH, $sslOptions);
8080
default:
8181
return new UdpTransport($host, $port);
8282
}
8383
}
8484

85+
protected function enableSsl(array $config): bool
86+
{
87+
if (! isset($config['transport']) || $config['transport'] === 'udp') {
88+
return false;
89+
}
90+
91+
return $config['ssl'] ?? false;
92+
}
93+
94+
protected function sslOptions(?array $sslConfig = null): SslOptions
95+
{
96+
$sslOptions = new SslOptions();
97+
98+
if (! $sslConfig) {
99+
return $sslOptions;
100+
}
101+
102+
$sslOptions->setVerifyPeer($sslConfig['verify_peer'] ?? true);
103+
$sslOptions->setCaFile($sslConfig['ca_file'] ?? null);
104+
$sslOptions->setCiphers($sslConfig['ciphers'] ?? null);
105+
$sslOptions->setAllowSelfSigned($sslConfig['allow_self_signed'] ?? false);
106+
107+
return $sslOptions;
108+
}
109+
85110
/** @throws \InvalidArgumentException */
86111
protected function level(array $config): int
87112
{
@@ -120,19 +145,4 @@ protected function getFallbackChannelName(): string
120145
{
121146
return $this->app->bound('env') ? $this->app->environment() : 'production';
122147
}
123-
124-
private function createSsl(?array $ssl): ?SslOptions
125-
{
126-
if (empty($ssl)) {
127-
return null;
128-
}
129-
130-
$sslOptions = new SslOptions();
131-
$sslOptions->setAllowSelfSigned($ssl['allow_self_signed'] ?? false);
132-
$sslOptions->setCaFile($ssl['ca_file'] ?? null);
133-
$sslOptions->setCiphers($ssl['ciphers'] ?? null);
134-
$sslOptions->setVerifyPeer($ssl['verify_peer'] ?? true);
135-
136-
return $sslOptions;
137-
}
138148
}

0 commit comments

Comments
 (0)