-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Rolland Csatari
committed
Jun 19, 2024
1 parent
4ab9a5e
commit d181c36
Showing
5 changed files
with
41 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,39 +14,25 @@ | |
namespace Ekino\DataProtectionBundle\Monolog\Processor; | ||
|
||
use Ekino\DataProtectionBundle\Encryptor\EncryptorInterface; | ||
use Monolog\LogRecord; | ||
use Monolog\Processor\ProcessorInterface; | ||
|
||
/** | ||
* Pseudonymize sensitive data inside log contexts. | ||
* | ||
* @author Rémi Marseille <[email protected]> | ||
* @author Benoit Mazière <[email protected]> | ||
* @author Rolland Csatari <[email protected]> | ||
*/ | ||
class GdprProcessor implements ProcessorInterface | ||
{ | ||
/** | ||
* @var EncryptorInterface | ||
*/ | ||
private $encryptor; | ||
|
||
/** | ||
* GdprProcessor constructor. | ||
* | ||
* @param EncryptorInterface $encryptor | ||
*/ | ||
public function __construct(EncryptorInterface $encryptor) | ||
public function __construct(private EncryptorInterface $encryptor) | ||
{ | ||
$this->encryptor = $encryptor; | ||
} | ||
|
||
/** | ||
* @param array<array-key,array> $record | ||
* | ||
* @return array<array-key,array> | ||
*/ | ||
public function __invoke(array $record) | ||
public function __invoke(LogRecord $record): LogRecord | ||
{ | ||
foreach ($record['context'] as $key => &$val) { | ||
foreach ($record->context as $key => &$val) { | ||
if (preg_match('#^private_#', (string) $key)) { | ||
$encoded = json_encode($val); | ||
if (false === $encoded) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,13 +15,16 @@ | |
|
||
use Ekino\DataProtectionBundle\Encryptor\EncryptorInterface; | ||
use Ekino\DataProtectionBundle\Monolog\Processor\GdprProcessor; | ||
use Monolog\Level; | ||
use Monolog\LogRecord; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* Class GdprProcessorTest. | ||
* | ||
* @author Rémi Marseille <[email protected]> | ||
* @author Benoit Mazière <[email protected]> | ||
* @author Rolland Csatari <[email protected]> | ||
*/ | ||
class GdprProcessorTest extends TestCase | ||
{ | ||
|
@@ -33,20 +36,24 @@ public function testProcessor(): void | |
$encryptor = $this->createMock(EncryptorInterface::class); | ||
$encryptor->expects($this->once())->method('encrypt')->willReturn('encrypted_data'); | ||
|
||
$processor = new GdprProcessor($encryptor); | ||
|
||
$this->assertSame(['context' => [ | ||
0 => 'numeric index', | ||
'foo' => 'bar', | ||
'private_data' => 'encrypted_data', | ||
]], $processor->__invoke([ | ||
'context' => [ | ||
$record = new LogRecord( | ||
new \DateTimeImmutable(), | ||
'main', | ||
Level::Debug, | ||
'The log context includes private data.', | ||
[ | ||
0 => 'numeric index', | ||
'foo' => 'bar', | ||
'private_data' => [ | ||
'foo' => 'baz', | ||
], | ||
], | ||
])); | ||
); | ||
|
||
$this->assertSame([ | ||
0 => 'numeric index', | ||
'foo' => 'bar', | ||
'private_data' => 'encrypted_data', | ||
], ((new GdprProcessor($encryptor))($record))->context); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters