Skip to content

Commit

Permalink
Merge pull request #34 from lenmen/feature/specify_custom_ssl_settings
Browse files Browse the repository at this point in the history
Feature/specify custom ssl settings
  • Loading branch information
hedii authored Jan 16, 2022
2 parents e9a76bf + 8a4042b commit d4d4f44
Show file tree
Hide file tree
Showing 3 changed files with 257 additions and 17 deletions.
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ return [

'gelf' => [
'driver' => 'custom',


'via' => \Hedii\LaravelGelfLogger\GelfLoggerFactory::class,

Expand Down Expand Up @@ -100,6 +99,30 @@ return [
// transport. When forgotten or set to null, default path '/gelf'
// is used.
'path' => null,

// This optional option enable or disable ssl on TCP transport.
// Default is false.
'ssl' => false,

// If ssl is enabled on TCP transport, the following configuration
// is used.
'ssl_options' => [
// Enable or disable the peer certificate check. Default is
// null.
'verify_peer' => true,

// Path to a custom CA file (eg: "/path/to/ca.pem"). Default
// is null.
'ca_file' => null,

// List of ciphers the SSL layer may use, formatted as
// specified in ciphers(1). Default is null.
'ciphers' => null,

// Whether self-signed certificates are allowed. Default is
// false.
'allow_self_signed' => false,
],

// This optional option determines the maximum length per message
// field. When forgotten or set to null, the default value of
Expand All @@ -114,6 +137,7 @@ return [
// This optional option determines the prefix for 'extra' fields
// from the Monolog record. Default is null (no extra prefix)
'extra_prefix' => null,

],
],
];
Expand Down
36 changes: 32 additions & 4 deletions src/GelfLoggerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Gelf\Transport\AbstractTransport;
use Gelf\Transport\HttpTransport;
use Gelf\Transport\IgnoreErrorTransportWrapper;
use Gelf\Transport\SslOptions;
use Gelf\Transport\UdpTransport;
use Gelf\Transport\TcpTransport;
use Illuminate\Contracts\Container\Container;
Expand Down Expand Up @@ -41,7 +42,8 @@ public function __invoke(array $config): Logger
$config['transport'] ?? 'udp',
$config['host'] ?? '127.0.0.1',
$config['port'] ?? 12201,
$config['path'] ?? null
$config['path'] ?? null,
$this->enableSsl($config) ? $this->sslOptions($config['ssl_options'] ?? null) : null
)
);

Expand All @@ -67,18 +69,44 @@ protected function getTransport(
string $transport,
string $host,
int $port,
?string $path = null
?string $path = null,
?SslOptions $sslOptions = null
): AbstractTransport {
switch (strtolower($transport)) {
case 'tcp':
return new TcpTransport($host, $port);
return new TcpTransport($host, $port, $sslOptions);
case 'http':
return new HttpTransport($host, $port, $path ?? HttpTransport::DEFAULT_PATH);
return new HttpTransport($host, $port, $path ?? HttpTransport::DEFAULT_PATH, $sslOptions);
default:
return new UdpTransport($host, $port);
}
}

protected function enableSsl(array $config): bool
{
if (! isset($config['transport']) || $config['transport'] === 'udp') {
return false;
}

return $config['ssl'] ?? false;
}

protected function sslOptions(?array $sslConfig = null): SslOptions
{
$sslOptions = new SslOptions();

if (! $sslConfig) {
return $sslOptions;
}

$sslOptions->setVerifyPeer($sslConfig['verify_peer'] ?? true);
$sslOptions->setCaFile($sslConfig['ca_file'] ?? null);
$sslOptions->setCiphers($sslConfig['ciphers'] ?? null);
$sslOptions->setAllowSelfSigned($sslConfig['allow_self_signed'] ?? false);

return $sslOptions;
}

/** @throws \InvalidArgumentException */
protected function level(array $config): int
{
Expand Down
Loading

0 comments on commit d4d4f44

Please sign in to comment.