Skip to content

Commit

Permalink
Merge pull request #65 from c-Rolland/fix-gdpr-processor-issue
Browse files Browse the repository at this point in the history
fix(GdprProcessor): Fix readonly issue
  • Loading branch information
mremi authored Jun 19, 2024
2 parents b3b4096 + b677cf6 commit e395245
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/github-actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php: [ '8.1', '8.2' ]
php: [ '8.1', '8.2', '8.3' ]
steps:
- uses: actions/checkout@v2
- name: Setup PHP with tools
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ master

* todo...

v3.1.0
------
* Configure PHP 8.3 in CI
* Fix the context readonly issue into `Monolog/Processor/GdprProcessor`
* Update `Tests/Monolog/Processor/GdprProcessorTest.php`
* Disable bypassReadOnly into `Ekino\DataProtectionBundle\Tests\BypassFinalHook`

v3.0.0
------

Expand Down
25 changes: 17 additions & 8 deletions Monolog/Processor/GdprProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,25 @@ public function __construct(private EncryptorInterface $encryptor)

public function __invoke(LogRecord $record): LogRecord
{
foreach ($record->context as $key => &$val) {
if (preg_match('#^private_#', (string) $key)) {
$encoded = json_encode($val);
if (false === $encoded) {
$encoded = "";
}
$val = $this->encryptor->encrypt($encoded);
$context = [];

foreach ($record->context as $key => $value) {
if (str_starts_with((string) $key, 'private_')) {
$value = json_encode($value);
$value = \is_string($value) ? $this->encryptor->encrypt($value) : '';
}

$context[$key] = $value;
}

return $record;
return new LogRecord(
$record->datetime,
$record->channel,
$record->level,
$record->message,
$context,
$record->extra,
$record->formatted
);
}
}
2 changes: 1 addition & 1 deletion Tests/BypassFinalHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ final class BypassFinalHook implements BeforeTestHook
{
public function executeBeforeTest(string $test): void
{
BypassFinals::enable();
BypassFinals::enable(bypassReadOnly: false);
}
}
21 changes: 15 additions & 6 deletions Tests/Monolog/Processor/GdprProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,33 @@ public function testProcessor(): void
$encryptor = $this->createMock(EncryptorInterface::class);
$encryptor->expects($this->once())->method('encrypt')->willReturn('encrypted_data');

$record = new LogRecord(
new \DateTimeImmutable(),
$originalRecord = new LogRecord(
new \DateTimeImmutable('2024-06-18'),
'main',
Level::Debug,
'The log context includes private data.',
[
0 => 'numeric index',
'foo' => 'bar',
'private_data' => [
'foo' => 'baz',
],
'private_data' => ['foo' => 'baz'],
'data_private' => ['foo' => 'baz'],
],
);

$processedRecord = (new GdprProcessor($encryptor))($originalRecord);

$this->assertSame($originalRecord->datetime, $processedRecord->datetime);
$this->assertSame($originalRecord->channel, $processedRecord->channel);
$this->assertSame($originalRecord->level, $processedRecord->level);
$this->assertSame($originalRecord->message, $processedRecord->message);
$this->assertSame($originalRecord->extra, $processedRecord->extra);
$this->assertSame($originalRecord->formatted, $processedRecord->formatted);

$this->assertSame([
0 => 'numeric index',
'foo' => 'bar',
'private_data' => 'encrypted_data',
], ((new GdprProcessor($encryptor))($record))->context);
'data_private' => ['foo' => 'baz'],
], $processedRecord->context);
}
}

0 comments on commit e395245

Please sign in to comment.