Skip to content

Commit

Permalink
Merge pull request #21 from minvws/update
Browse files Browse the repository at this point in the history
moved cacert parser to separate class
  • Loading branch information
jaytaph authored Feb 27, 2023
2 parents fbe9666 + 17be574 commit 2d69af7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 41 deletions.
39 changes: 39 additions & 0 deletions src/CaParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace MinVWS\PUZI\Laravel;

class CaParser
{
/**
* Get the CA certificates from the given path
* @param ?string $path
* @return array
*/
public static function getCertsFromFile(?string $path): array
{
if (empty($path)) {
return [];
}

$fileContent = @file_get_contents($path);
if ($fileContent === false) {
throw new \RuntimeException("Could not read CA certificates from $path");
}

$caCerts = preg_split('/-----BEGIN CERTIFICATE-----/', $fileContent);
if ($caCerts === false) {
return [];
}

// remove empty first element
array_shift($caCerts);

foreach ($caCerts as &$cert) {
$cert = trim($cert);
$cert = substr($cert, 0, strpos($cert, '-----END CERTIFICATE-----') ?: 0);
$cert = str_replace("\n", '', $cert);
}

return $caCerts;
}
}
35 changes: 1 addition & 34 deletions src/UziServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,41 +32,8 @@ public function register(): void
config("uzi.strict_ca_check", true),
config("uzi.allowed_types", []),
config("uzi.allowed_roles", []),
$this->getCACerts(config("uzi.ca_certs_path")),
CaParser::getCertsFromFile(config("uzi.ca_certs_path")),
);
});
}

/**
* Get the CA certificates from the given path
* @param ?string $path
* @return array
*/
public function getCACerts(?string $path): array
{
if (empty($path)) {
return [];
}

$fileContent = @file_get_contents($path);
if ($fileContent === false) {
throw new \RuntimeException("Could not read CA certificates from $path");
}

$caCerts = preg_split('/-----BEGIN CERTIFICATE-----/', $fileContent);
if ($caCerts === false) {
return [];
}

// remove empty first element
array_shift($caCerts);

foreach ($caCerts as &$cert) {
$cert = trim($cert);
$cert = substr($cert, 0, strpos($cert, '-----END CERTIFICATE-----') ?: 0);
$cert = str_replace("\n", '', $cert);
}

return $caCerts;
}
}
11 changes: 4 additions & 7 deletions tests/UziServiceProviderTest.php → tests/CaParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,20 @@
namespace MinVWS\PUZI\Laravel\Tests;

use Illuminate\Support\Facades\App;
use MinVWS\PUZI\Laravel\CaParser;
use MinVWS\PUZI\Laravel\UziServiceProvider;

class UziServiceProviderTest extends TestCase
{
public function testCACertsEmpty(): void
{
$serviceProvider = new UziServiceProvider(app());

$this->assertEmpty($serviceProvider->getCACerts(null));
$this->assertEmpty($serviceProvider->getCACerts(''));
$this->assertEmpty(CaParser::getCertsFromFile(null));
$this->assertEmpty(CaParser::getCertsFromFile(''));
}

public function testCACerts(): void
{
$serviceProvider = new UziServiceProvider(app());

$caCerts = $serviceProvider->getCACerts(__DIR__ . '/Resources/test-fake-ca-file.pem');
$caCerts = CaParser::getCertsFromFile(__DIR__ . '/Resources/test-fake-ca-file.pem');

$this->assertCount(2, $caCerts);
$this->assertSame([
Expand Down

0 comments on commit 2d69af7

Please sign in to comment.