Skip to content
Snippets Groups Projects
Commit 611464c6 authored by Grzegorz Rola's avatar Grzegorz Rola
Browse files

feature(processor): sensitive data processor

parent 96e6052f
No related branches found
No related tags found
1 merge request!27feature(processor): sensitive data processor
Pipeline #340951 passed with warnings with stages
in 31 seconds
## [1.12.0] - 2023-07-10
### Added
- sensitive data replacer
## [1.11.0] - 2023-07-10
### Updated
- monolog to 2.9.1
......
......@@ -8,22 +8,23 @@
],
"config": {
"platform": {
"php": "7.2"
"php": "7.4"
},
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
},
"require": {
"php": ">=7.2|^8",
"php": ">=7.4|^8",
"psr/log": "^1",
"monolog/monolog": "^2.9.1",
"wpdesk/wp-notice": "^3.0"
},
"require-dev": {
"phpunit/phpunit": "^5",
"phpunit/phpunit": "^5|^6|^7|^8|^9",
"squizlabs/php_codesniffer": "^3.0.2",
"wpdesk/wp-code-sniffer": "^1.2.3"
"wpdesk/wp-code-sniffer": "^1.2.3",
"10up/wp_mock": "^1.0"
},
"autoload": {
"psr-4": {"WPDesk\\Logger\\": "src/"}
......
<?php
namespace WPDesk\Logger\Processor;
use Monolog\Processor\ProcessorInterface;
/**
* Can replace data in log.
* Ie. sensitive data.
*
* @package WPDesk\Logger\Processor
*/
class SensitiveDataProcessor implements ProcessorInterface {
/**
* Replace array.
*
* @var array
*/
private array $replace;
public function __construct( array $replace ) {
$this->replace = $replace;
}
public function __invoke( array $record ): array {
return $this->replace_array( $record );
}
private function replace_array( array $value ): array {
foreach ( $value as $key => $item ) {
if ( is_array( $item ) ) {
$value[ $key ] = $this->replace_array( $item );
}
if ( is_string( $item ) ) {
$value[ $key ] = $this->replace( $item );
}
}
return $value;
}
private function replace( string $value ): string {
foreach ( $this->replace as $search => $replace ) {
$value = str_replace( $search, $replace, $value );
}
return $value;
}
}
<?php
namespace unit\Processor;
use WP_Mock;
use WP_Mock\Tools\TestCase;
use WPDesk\Logger\Processor\SensitiveDataProcessor;
class TestSensitiveDataProcessor extends TestCase
{
public function setUp(): void {
WP_Mock::setUp();
}
public function tearDown(): void {
WP_Mock::tearDown();
}
public function testShouldReplaceWhenMatched() {
$processor = new SensitiveDataProcessor( array( 'password' => '***' ) );
$record = array(
'password' => 'password',
'array' => array(
'password' => 'password',
),
);
$expected = array(
'password' => '***',
'array' => array(
'password' => '***',
),
);
$this->assertEquals( $expected, $processor->__invoke( $record ) );
}
public function testShouldNotReplaceWhenNotMatched() {
$processor = new SensitiveDataProcessor( array( 'password' => '***' ) );
$record = array(
'text' => 'text',
'array' => array(
'text' => 'text',
),
);
$expected = array(
'text' => 'text',
'array' => array(
'text' => 'text',
),
);
$this->assertEquals( $expected, $processor->__invoke( $record ) );
}
}
......@@ -4,3 +4,20 @@
*/
require_once __DIR__ . '/../../vendor/autoload.php';
error_reporting( E_ALL & ~E_DEPRECATED );
if ( getenv( 'PLUGIN_PATH' ) !== false ) {
define( 'PLUGIN_PATH', getenv( 'PLUGIN_PATH' ) );
} else {
define( 'PLUGIN_PATH', __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR );
}
if ( getenv( 'ABSPATH' ) !== false ) {
define( 'ABSPATH', getenv( 'ABSPATH' ) );
} else {
define( 'ABSPATH', PLUGIN_PATH . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR );
}
WP_Mock::setUsePatchwork( true );
WP_Mock::bootstrap();
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment