feat: anonymization command for request/response XML dumps
This commit is contained in:
@@ -0,0 +1,301 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use Faker\Factory;
|
||||
use Faker\Generator;
|
||||
|
||||
class BpnXmlAnonymizer
|
||||
{
|
||||
private const SAFE_EMAIL_DOMAIN = 'example.com';
|
||||
|
||||
private Generator $faker;
|
||||
|
||||
/**
|
||||
* @var array<string, array{firstName: string, lastName: string, street: string, postalCode: string, city: string, email: string, mobilePhone: string}>
|
||||
*/
|
||||
private array $identities = [];
|
||||
|
||||
private int $fallbackIdentityCounter = 0;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->faker = Factory::create('de_DE');
|
||||
}
|
||||
|
||||
public function anonymize(string $xml, bool $resetState = true): string
|
||||
{
|
||||
if (true === $resetState) {
|
||||
$this->resetState();
|
||||
}
|
||||
|
||||
$document = new \DOMDocument();
|
||||
$document->preserveWhiteSpace = true;
|
||||
$document->formatOutput = false;
|
||||
|
||||
if (false === @$document->loadXML($xml)) {
|
||||
throw new \InvalidArgumentException('Invalid XML input.');
|
||||
}
|
||||
|
||||
$xpath = new \DOMXPath($document);
|
||||
$personNodes = $xpath->query('//anmelder | //teilnehmerliste/teilnehmer | //kundennamen/kunde');
|
||||
|
||||
if (false === $personNodes) {
|
||||
throw new \RuntimeException('Unable to query person nodes.');
|
||||
}
|
||||
|
||||
foreach ($personNodes as $index => $personNode) {
|
||||
if (false === $personNode instanceof \DOMElement) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$personId = $this->resolvePersonId($personNode, (int) $index);
|
||||
$identity = $this->getOrCreateIdentity($personId);
|
||||
|
||||
$this->replaceNameFields($personNode, $identity);
|
||||
$this->replaceEmailFields($personNode, $identity);
|
||||
$this->replaceAddressFields($personNode, $identity);
|
||||
}
|
||||
|
||||
$result = $document->saveXML();
|
||||
if (false === $result) {
|
||||
throw new \RuntimeException('Unable to serialize anonymized XML.');
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function getLastIdentityCount(): int
|
||||
{
|
||||
return \count($this->identities);
|
||||
}
|
||||
|
||||
public function resetState(): void
|
||||
{
|
||||
$this->identities = [];
|
||||
$this->fallbackIdentityCounter = 0;
|
||||
}
|
||||
|
||||
private function resolvePersonId(\DOMElement $personNode, int $nodeIndex): string
|
||||
{
|
||||
foreach (['personid', 'idperson', 'idadresseperson'] as $tagName) {
|
||||
$child = $this->getDirectChild($personNode, $tagName);
|
||||
if (null !== $child) {
|
||||
$value = trim($child->textContent);
|
||||
if ('' !== $value) {
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$fingerprintKey = $this->buildFingerprintKey($personNode);
|
||||
if (null !== $fingerprintKey) {
|
||||
return $fingerprintKey;
|
||||
}
|
||||
|
||||
if (true === $personNode->hasAttribute('id')) {
|
||||
$attributeValue = trim((string) $personNode->getAttribute('id'));
|
||||
if ('' !== $attributeValue) {
|
||||
return sprintf('attr-id:%s', $attributeValue);
|
||||
}
|
||||
}
|
||||
|
||||
++$this->fallbackIdentityCounter;
|
||||
|
||||
return sprintf('fallback:%d:%d', $nodeIndex, $this->fallbackIdentityCounter);
|
||||
}
|
||||
|
||||
private function buildFingerprintKey(\DOMElement $personNode): ?string
|
||||
{
|
||||
$nameValues = [];
|
||||
$nameNode = $this->getDirectChild($personNode, 'name');
|
||||
if (null !== $nameNode) {
|
||||
$nameValues[] = trim($nameNode->textContent);
|
||||
}
|
||||
if (true === $personNode->hasAttribute('name')) {
|
||||
$nameValues[] = trim((string) $personNode->getAttribute('name'));
|
||||
}
|
||||
|
||||
$firstNameNode = $this->getDirectChild($personNode, 'vorname');
|
||||
if (null !== $firstNameNode) {
|
||||
$nameValues[] = trim($firstNameNode->textContent);
|
||||
}
|
||||
if (true === $personNode->hasAttribute('vorname')) {
|
||||
$nameValues[] = trim((string) $personNode->getAttribute('vorname'));
|
||||
}
|
||||
|
||||
$communicationNode = $this->getDirectChild($personNode, 'kommunikation');
|
||||
if (null !== $communicationNode) {
|
||||
$emailNode = $this->getDirectChild($communicationNode, 'email');
|
||||
if (null !== $emailNode) {
|
||||
$nameValues[] = trim($emailNode->textContent);
|
||||
}
|
||||
}
|
||||
|
||||
$addressNode = $this->getDirectChild($personNode, 'anschrift');
|
||||
if (null !== $addressNode) {
|
||||
foreach (['strasse', 'plz', 'ort'] as $fieldName) {
|
||||
$fieldNode = $this->getDirectChild($addressNode, $fieldName);
|
||||
if (null !== $fieldNode) {
|
||||
$nameValues[] = trim($fieldNode->textContent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$normalizedParts = [];
|
||||
foreach ($nameValues as $value) {
|
||||
if ('' === $value) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$normalizedParts[] = mb_strtolower($value);
|
||||
}
|
||||
|
||||
if (0 === \count($normalizedParts)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return sprintf('fp:%s', md5(implode('|', $normalizedParts)));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $personId
|
||||
*
|
||||
* @return array{firstName: string, lastName: string, street: string, postalCode: string, city: string, email: string, mobilePhone: string}
|
||||
*/
|
||||
private function getOrCreateIdentity(string $personId): array
|
||||
{
|
||||
if (true === isset($this->identities[$personId])) {
|
||||
return $this->identities[$personId];
|
||||
}
|
||||
|
||||
$firstName = $this->faker->firstName();
|
||||
$lastName = $this->faker->lastName();
|
||||
|
||||
$street = sprintf('%s %s', $this->faker->streetName(), $this->faker->buildingNumber());
|
||||
$postalCode = $this->faker->postcode();
|
||||
$city = $this->faker->city();
|
||||
$mobilePhone = $this->faker->numerify('01#########');
|
||||
|
||||
$emailLocal = sprintf('%s.%s', $this->slug($firstName), $this->slug($lastName));
|
||||
$email = sprintf('%s@%s', $emailLocal, self::SAFE_EMAIL_DOMAIN);
|
||||
|
||||
$this->identities[$personId] = [
|
||||
'firstName' => $firstName,
|
||||
'lastName' => $lastName,
|
||||
'street' => $street,
|
||||
'postalCode' => $postalCode,
|
||||
'city' => $city,
|
||||
'email' => strtolower($email),
|
||||
'mobilePhone' => $mobilePhone,
|
||||
];
|
||||
|
||||
return $this->identities[$personId];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array{firstName: string, lastName: string, street: string, postalCode: string, city: string, email: string, mobilePhone: string} $identity
|
||||
*/
|
||||
private function replaceNameFields(\DOMElement $personNode, array $identity): void
|
||||
{
|
||||
$nameNode = $this->getDirectChild($personNode, 'name');
|
||||
if (null !== $nameNode) {
|
||||
$nameNode->nodeValue = $identity['lastName'];
|
||||
}
|
||||
|
||||
$firstNameNode = $this->getDirectChild($personNode, 'vorname');
|
||||
if (null !== $firstNameNode) {
|
||||
$firstNameNode->nodeValue = $identity['firstName'];
|
||||
}
|
||||
|
||||
if (true === $personNode->hasAttribute('name')) {
|
||||
$personNode->setAttribute('name', $identity['lastName']);
|
||||
}
|
||||
|
||||
if (true === $personNode->hasAttribute('vorname')) {
|
||||
$personNode->setAttribute('vorname', $identity['firstName']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array{firstName: string, lastName: string, street: string, postalCode: string, city: string, email: string, mobilePhone: string} $identity
|
||||
*/
|
||||
private function replaceEmailFields(\DOMElement $personNode, array $identity): void
|
||||
{
|
||||
$communicationNode = $this->getDirectChild($personNode, 'kommunikation');
|
||||
if (null === $communicationNode) {
|
||||
return;
|
||||
}
|
||||
|
||||
$emailNode = $this->getDirectChild($communicationNode, 'email');
|
||||
if (null !== $emailNode) {
|
||||
$emailNode->nodeValue = $identity['email'];
|
||||
}
|
||||
|
||||
$mobileNode = $this->getDirectChild($communicationNode, 'telefonmobil');
|
||||
if (null !== $mobileNode) {
|
||||
$mobileNode->nodeValue = $identity['mobilePhone'];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array{firstName: string, lastName: string, street: string, postalCode: string, city: string, email: string, mobilePhone: string} $identity
|
||||
*/
|
||||
private function replaceAddressFields(\DOMElement $personNode, array $identity): void
|
||||
{
|
||||
$addressNode = $this->getDirectChild($personNode, 'anschrift');
|
||||
if (null === $addressNode) {
|
||||
return;
|
||||
}
|
||||
|
||||
$streetNode = $this->getDirectChild($addressNode, 'strasse');
|
||||
if (null !== $streetNode) {
|
||||
$streetNode->nodeValue = $identity['street'];
|
||||
}
|
||||
|
||||
$postalCodeNode = $this->getDirectChild($addressNode, 'plz');
|
||||
if (null !== $postalCodeNode) {
|
||||
$postalCodeNode->nodeValue = $identity['postalCode'];
|
||||
}
|
||||
|
||||
$cityNode = $this->getDirectChild($addressNode, 'ort');
|
||||
if (null !== $cityNode) {
|
||||
$cityNode->nodeValue = $identity['city'];
|
||||
}
|
||||
}
|
||||
|
||||
private function getDirectChild(\DOMElement $parent, string $name): ?\DOMElement
|
||||
{
|
||||
foreach ($parent->childNodes as $childNode) {
|
||||
if (false === $childNode instanceof \DOMElement) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($childNode->tagName === $name) {
|
||||
return $childNode;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private function slug(string $value): string
|
||||
{
|
||||
$normalizedValue = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $value);
|
||||
if (false === $normalizedValue) {
|
||||
$normalizedValue = $value;
|
||||
}
|
||||
|
||||
$normalizedValue = strtolower($normalizedValue);
|
||||
$normalizedValue = preg_replace('/[^a-z0-9]+/', '.', $normalizedValue);
|
||||
$normalizedValue = trim((string) $normalizedValue, '.');
|
||||
|
||||
if ('' === $normalizedValue) {
|
||||
return 'user';
|
||||
}
|
||||
|
||||
return $normalizedValue;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user