Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ jobs:
- "lowest"
- "highest"
php-version:
- "8.1"
- "8.2"
- "8.3"
operating-system:
- "ubuntu-latest"
Expand Down
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
"require": {
"php": "^8.3",
"juststeveking/php-sdk": "^3.0",
"treblle/cloudevent-php": "dev-main",
"thecodingmachine/safe": "^2.5"
},
"require-dev": {
Expand Down
58 changes: 1 addition & 57 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
cacheResult="false"
>
<testsuites>
<testsuite name="Test Suite">
Expand Down
2 changes: 1 addition & 1 deletion pint.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"declare_parentheses": true,
"declare_strict_types": true,
"explicit_string_variable": true,
"final_class": true,
"final_class": false,
"fully_qualified_strict_types": true,
"global_namespace_import": {
"import_classes": true,
Expand Down
2 changes: 1 addition & 1 deletion src/Contracts/MaskingEngineContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

interface MaskingEngineContract
{
public function mask(array $payload): array;
public function mask(array $payload, string $type = 'body'): array;
}
6 changes: 6 additions & 0 deletions src/DataObjects/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@

namespace Treblle\Runtime\DataObjects;

/**
* @package Treblle Runtime
* @author Steve McDougall <[email protected]>
*/
final readonly class Config
{
/**
* @param string $api_key
* @param string $project_id
* @param array<int,string> $ignored_environments
* @param array<string,mixed> $masking
* @param array<int,string> $headers
*/
public function __construct(
public string $api_key,
public string $project_id,
public array $ignored_environments,
public array $masking,
public array $headers,
) {}
}
2 changes: 2 additions & 0 deletions src/Factories/ConfigFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ final class ConfigFactory
* project_id:string,
* ignored_environments:array<int,string>,
* masking:array<string,mixed>,
* headers:array<int,string>,
* } $config
* @return Config
*/
Expand All @@ -24,6 +25,7 @@ public static function make(array $config): Config
project_id: $config['project_id'],
ignored_environments: $config['ignored_environments'],
masking: $config['masking'],
headers: $config['headers'],
);
}
}
34 changes: 31 additions & 3 deletions src/Masking/MaskingEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,32 @@ public function __construct(
private Config $config,
) {}

public function mask(array $payload): array
public function mask(array $payload, string $type = 'body'): array
{
return $this->maskArray($payload, []);
if ('body' === $type) {
return $this->maskArray($payload, []);
}

if('headers' === $type) {
return $this->maskHeaders($payload);
}

return $payload;
}

private function maskHeaders(array $payload): array
{
$data = [];

foreach ($payload as $key => $value) {
if (in_array($key, $this->config->headers)) {
$data[$key] = (new StringMatcher())->input((string) $value)->mask();
} else {
$data[$key] = $value;
}
}

return $data;
}

private function maskArray(array $data, array $path): array
Expand Down Expand Up @@ -49,18 +72,23 @@ private function shouldMask(string $dotNotationKey): bool
return true;
}
}

return false;
}

private function getMaskerClass(string $dotNotationKey): string
{
/**
* @var string $pattern
* @var string $maskerClass
*/
foreach ($this->config->masking as $pattern => $maskerClass) {
$regex = $this->convertPatternToRegex($pattern);
if (preg_match($regex, $dotNotationKey)) {
return $maskerClass;
}
}
throw new \RuntimeException("No masker class found for key: $dotNotationKey");
throw new RuntimeException("No masker class found for key: {$dotNotationKey}");
}

private function convertPatternToRegex(string $pattern): string
Expand Down
2 changes: 1 addition & 1 deletion src/Masking/StringMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class StringMatcher implements MasksInput
{
private string $pattern;
protected string $pattern;

protected string $input;

Expand Down
Loading