Skip to content

Commit 3ee8ae9

Browse files
authored
Merge pull request #130 from OpenConext/bugfix/exception-from-logger
Do not throw an exception when the samlAuthenticationLogger is used before forAuthentication() is called on it. - Pro: this does not hide the error being logged at runtime - Con: It is harder for a developer to notice that they forgot to call forAuthentication(). In this case it will still log, but will not log a sari, defeating the purpose of this logger.
2 parents 61eeee5 + 6671293 commit 3ee8ae9

File tree

6 files changed

+96
-7
lines changed

6 files changed

+96
-7
lines changed

src/Metadata/MetadataFactory.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Surfnet\SamlBundle\Signing\KeyPair;
2626
use Symfony\Component\Routing\RouterInterface;
2727
use Twig\Environment;
28+
use Surfnet\SamlBundle\Exception\RuntimeException;
2829

2930
class MetadataFactory
3031
{
@@ -114,6 +115,10 @@ private function getCertificateData(string $publicKeyFile): string
114115
$matches = [];
115116
preg_match(Certificate::CERTIFICATE_PATTERN, $certificate, $matches);
116117

118+
if (! isset($matches[1])) {
119+
throw new RuntimeException(sprintf('Could not parse PEM certificate in %s', $publicKeyFile));
120+
}
121+
117122
return str_replace([' ', "\n"], '', $matches[1]);
118123
}
119124

src/Monolog/SamlAuthenticationLogger.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,10 @@ public function log($level, $message, array $context = []): void
9696
*/
9797
private function modifyContext(array $context): array
9898
{
99-
if (!$this->sari) {
100-
throw new RuntimeException('Authentication logging context is unknown');
99+
if ($this->sari) {
100+
$context['sari'] = $this->sari;
101101
}
102102

103-
$context['sari'] = $this->sari;
104-
105103
return $context;
106104
}
107105
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
4+
namespace Surfnet\SamlBundle\Tests\Unit\Metadata;
5+
6+
use PHPUnit\Framework\TestCase;
7+
use ReflectionMethod;
8+
use Surfnet\SamlBundle\Metadata\MetadataFactory;
9+
10+
class MetadataFactoryTest extends TestCase
11+
{
12+
public function testGetCertificateData(): void
13+
{
14+
$publicKeyFile = __DIR__ . '/certificate.pem'; // File with test certificate in PEM format
15+
// Read the public key file and remove the first and last lines and all newlines
16+
$expectedCertificate = str_replace("\n", '', implode("", array_slice(file($publicKeyFile), 1, -1)));
17+
18+
// Setup a mock for the MetadataFactory with the real getCertificateData method
19+
// and add the mocked File class to it
20+
$metadataFactoryMock = $this->getMockBuilder(MetadataFactory::class)
21+
->disableOriginalConstructor()
22+
->onlyMethods(['getCertificateData'])
23+
->getMock();
24+
25+
// Setup a reflection to call the private method
26+
$reflectionMethod = new ReflectionMethod($metadataFactoryMock::class, 'getCertificateData');
27+
28+
// Test getCertificateData method with a valid certificate
29+
$result = $reflectionMethod->invoke($metadataFactoryMock, $publicKeyFile);
30+
$this->assertEquals($expectedCertificate, $result);
31+
32+
// Test with an invalid certificate
33+
$invalidPublicKeyFile = __DIR__ . '/invalid_certificate.pem'; // File with invalid certificate
34+
$this->expectException(\RuntimeException::class);
35+
$this->expectExceptionMessage('Could not parse PEM certificate in ' . $invalidPublicKeyFile);
36+
$reflectionMethod->invoke($metadataFactoryMock, $invalidPublicKeyFile);
37+
}
38+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
-----BEGIN CERTIFICATE-----
2+
MIIDwTCCAqmgAwIBAgIUYuSUugwc4J4NyW9WGqYJ/liwM4owDQYJKoZIhvcNAQEL
3+
BQAwcDELMAkGA1UEBhMCTkwxEDAOBgNVBAgMB1V0cmVjaHQxEDAOBgNVBAcMB1V0
4+
cmVjaHQxJzAlBgNVBAoMHkRldmVsb3BtZW50IERvY2tlciBlbnZpcm9ubWVudDEU
5+
MBIGA1UEAwwLR2F0ZXdheSBJRFAwHhcNMjMwNTE3MTIxNTEyWhcNMzMwNTE0MTIx
6+
NTEyWjBwMQswCQYDVQQGEwJOTDEQMA4GA1UECAwHVXRyZWNodDEQMA4GA1UEBwwH
7+
VXRyZWNodDEnMCUGA1UECgweRGV2ZWxvcG1lbnQgRG9ja2VyIGVudmlyb25tZW50
8+
MRQwEgYDVQQDDAtHYXRld2F5IElEUDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCC
9+
AQoCggEBAM2ulQVs5WpbJOAf7Cv/VPDTJqbWHVdUxAmdwZJlcNTRKNFVp4aJzQ3d
10+
piyiGghI5odnzU0/BWBoHZFNYPU/OFr/gzn6iJGxL63L9+mFgE8PR9HpkV5TaRnr
11+
21+nZ0EXWjDZk9Px0enERicCItTeQzAUJeA0A9miIcK5IKIz/zSBSR3c802SGD/V
12+
elUqY7Z2/UJM97cT92L+4Fz+4zhxxoThbPbrR0CweiROIt82grdwg7zf0+b62MOu
13+
VtqFh0yPLRAFfLc4LjHuxFUdUvOHVta7x74dwdmHikqfujM10XN+sNns3LDJde2y
14+
PWchU6ktq7cjgbYfIW/vzVzafP1Jk40CAwEAAaNTMFEwHQYDVR0OBBYEFGYn6LWR
15+
DZa7+YryUncIlwJB2VorMB8GA1UdIwQYMBaAFGYn6LWRDZa7+YryUncIlwJB2Vor
16+
MA8GA1UdEwEB/wQFMAMBAf8wDQYJKoZIhvcNAQELBQADggEBAJ57lcOF6PWWW56m
17+
S2s5gKFImtfRFzlfiyHsF14L7+nQ5NjfOhpU0wRpnTjK91KP0wCwlxzGFXR8yfqf
18+
BFJryIV7aDdYPH/RIkwVaNBI0fsD/ozlYb18seieDEGLvQtTlrmc0UNHtWz6FW3L
19+
2geM3ENaqpOATl1Ywp4EPML7Dh0CbhhyM8PnPCEsdclouIeP5/B9Swfk3omXehof
20+
6bkFbntqA03msFBiW50twkfKeKULcJGXo667hto27KNxZUauqtPbnAGpUQmge8nx
21+
SQlN8RPwlvygVM4LVMF9qP9YxloTH0xVNwN4noZUhfMNsKoJ7Hg5Xulaok8oCqmz
22+
EiSroEg=
23+
-----END CERTIFICATE-----
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,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CwlxzGFXR8yfqf
18+
BFJryIV7aDdYPH/RIkwVaNBI0fsD/ozlYb18seieDEGLvQtTlrmc0UNHtWz6FW3L
19+
2geM3ENaqpOATl1Ywp4EPML7Dh0CbhhyM8PnPCEsdclouIeP5/B9Swfk3omXehof
20+
6bkFbntqA03msFBiW50twkfKeKULcJGXo667hto27KNxZUauqtPbnAGpUQmge8nx
21+
SQlN8RPwlvygVM4LVMF9qP9YxloTH0xVNwN4noZUhfMNsKoJ7Hg5Xulaok8oCqmz
22+
EiSroEg=
23+
-----END CERTIFICATE-----

src/Tests/Unit/Monolog/SamlAuthenticationLoggerTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,12 @@ public function it_returns_a_logger_for_an_authentication(): void
4747
/**
4848
* @test
4949
*/
50-
public function it_errors_when_no_authentication(): void
50+
public function it_does_not_throw_when_no_authentication(): void
5151
{
52-
self::expectException(RuntimeException::class);
53-
$logger = new SamlAuthenticationLogger(m::mock(LoggerInterface::class));
52+
$innerLogger = m::mock(LoggerInterface::class);
53+
$innerLogger->shouldReceive('emergency')->with('message2', [])->once();
54+
55+
$logger = new SamlAuthenticationLogger($innerLogger);
5456
$logger->emergency('message2');
5557
}
5658
}

0 commit comments

Comments
 (0)