8
0
Dieser Commit ist enthalten in:
Daniel Seifert 2024-12-21 22:28:56 +01:00
Commit c631f9a6ac
Signiert von: DanielS
GPG-Schlüssel-ID: 6A513E13AEE66170
4 geänderte Dateien mit 145 neuen und 0 gelöschten Zeilen

21
LICENSE Normale Datei
Datei anzeigen

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2024 D3 Data Development (Inh. Thomas Dartsch)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

39
README.md Normale Datei
Datei anzeigen

@ -0,0 +1,39 @@
# Sensitive Message Formatter
A message formatter for Guzzles log middleware. It replaces static sensitive data (e.g. credentials).
## Installation
```
composer require d3/sensitive-message-formatter
```
## Usage
```
$stack->push(
Middleware::log(
$myLogger,
new sensitiveMessageFormatter(
'{method} {uri} {req_body} - RESPONSE: {code} - {res_body}',
['myUser', 'myPassword']
)
),
Logger::INFO
);
```
## Licence of this software (sensitive-message-formatter) [MIT]
(21.12.2024)
```
Copyright (c) D3 Data Development (Inh. Thomas Dartsch)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
```

30
composer.json Normale Datei
Datei anzeigen

@ -0,0 +1,30 @@
{
"name": "d3/sensitive-message-formatter",
"type": "library",
"description": "a message formatter it can replace sensitive data from log",
"keywords": [
"Guzzle",
"log",
"message",
"formatter",
"sensitive",
"credentials",
"replacement"
],
"homepage": "https://www.d3data.de",
"license": "MIT",
"authors": [
{
"name": "Daniel Seifert",
"email": "git@daniel-seifert.com"
}
],
"require": {
"guzzlehttp/guzzle": "^7.0"
},
"autoload": {
"psr-4": {
"D3\\SensitiveMessageFormatter\\": "src/"
}
}
}

Datei anzeigen

@ -0,0 +1,55 @@
<?php
namespace D3\SensitiveMessageFormatter;
use GuzzleHttp\MessageFormatter;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Throwable;
class sensitiveMessageFormatter extends MessageFormatter
{
public function __construct(
?string $template = self::CLF,
protected array $anonymizations = [],
protected string $replaceChar = '*'
) {
$this->createReplacements($this->anonymizations);
parent::__construct($template);
}
protected function createReplacements(array $search = []): void
{
$replacements = [];
array_map(
function ($search) use (&$replacements) {
$replacements[$search] = str_repeat($this->replaceChar, strlen($search));
$replacements[urlencode($search)] = str_repeat($this->replaceChar, strlen($search));
},
$search
);
$this->anonymizations = $replacements;
}
public function format(
RequestInterface $request,
ResponseInterface $response = null,
Throwable $error = null
): string
{
$result = parent::format($request, $response, $error);
if (count($this->anonymizations)) {
$result = str_replace(
array_keys($this->anonymizations),
array_values($this->anonymizations),
$result
);
}
return $result;
}
}