-
-
Notifications
You must be signed in to change notification settings - Fork 21
data protection improvements #645
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
265e8b6
XML support in data protection
unixslayer 90c483d
configure message protection when only property is annotated
unixslayer 934ca08
handle changing property names with custom converters
unixslayer 4e87404
cr changes
unixslayer 3b1aa85
Merge branch 'main' into data-protection
unixslayer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
24 changes: 24 additions & 0 deletions
24
packages/DataProtection/src/Conversion/AbstractDataProtectionConverter.php
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Ecotone\DataProtection\Conversion; | ||
|
|
||
| use Ecotone\DataProtection\Encryption\Key; | ||
| use Ecotone\Messaging\Conversion\Converter; | ||
| use Ecotone\Messaging\Handler\Type; | ||
|
|
||
| /** | ||
| * licence Enterprise | ||
| */ | ||
| abstract class AbstractDataProtectionConverter implements Converter | ||
| { | ||
| public function __construct( | ||
| protected Type $supportedType, | ||
| protected Key $encryptionKey, | ||
| protected array $sensitiveProperties, | ||
| protected array $scalarProperties, | ||
| protected array $sensitivePropertyNames, | ||
| ) { | ||
| } | ||
| } |
31 changes: 31 additions & 0 deletions
31
packages/DataProtection/src/Conversion/AbstractDecryptionConverter.php
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Ecotone\DataProtection\Conversion; | ||
|
|
||
| use Ecotone\DataProtection\Encryption\Crypto; | ||
|
|
||
| /** | ||
| * licence Enterprise | ||
| */ | ||
| abstract class AbstractDecryptionConverter extends AbstractDataProtectionConverter | ||
| { | ||
| protected function decrypt(array $data): array | ||
| { | ||
| foreach ($this->sensitiveProperties as $property) { | ||
| $propertyKey = $this->sensitivePropertyNames[$property] ?? $property; | ||
| if (! array_key_exists($propertyKey, $data)) { | ||
| continue; | ||
| } | ||
|
|
||
| $data[$propertyKey] = Crypto::decrypt($data[$propertyKey], $this->encryptionKey); | ||
|
|
||
| if (! in_array($propertyKey, $this->scalarProperties, true)) { | ||
| $data[$propertyKey] = json_decode($data[$propertyKey], true); | ||
| } | ||
| } | ||
|
|
||
| return $data; | ||
| } | ||
| } |
31 changes: 31 additions & 0 deletions
31
packages/DataProtection/src/Conversion/AbstractEncryptionConverter.php
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Ecotone\DataProtection\Conversion; | ||
|
|
||
| use Ecotone\DataProtection\Encryption\Crypto; | ||
|
|
||
| /** | ||
| * licence Enterprise | ||
| */ | ||
| abstract class AbstractEncryptionConverter extends AbstractDataProtectionConverter | ||
| { | ||
| protected function encrypt(array $data): array | ||
| { | ||
| foreach ($this->sensitiveProperties as $property) { | ||
| $propertyKey = $this->sensitivePropertyNames[$property] ?? $property; | ||
| if (! array_key_exists($propertyKey, $data)) { | ||
| continue; | ||
| } | ||
|
|
||
| if (! in_array($propertyKey, $this->scalarProperties, true)) { | ||
| $data[$propertyKey] = json_encode($data[$propertyKey]); | ||
| } | ||
|
|
||
| $data[$propertyKey] = Crypto::encrypt($data[$propertyKey], $this->encryptionKey); | ||
| } | ||
|
|
||
| return $data; | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
30 changes: 30 additions & 0 deletions
30
packages/DataProtection/src/Conversion/XMLDecryptionConverter.php
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Ecotone\DataProtection\Conversion; | ||
|
|
||
| use Ecotone\DataProtection\Encryption\Crypto; | ||
| use Ecotone\DataProtection\Encryption\Key; | ||
| use Ecotone\Messaging\Conversion\Converter; | ||
| use Ecotone\Messaging\Conversion\MediaType; | ||
| use Ecotone\Messaging\Handler\Type; | ||
|
|
||
| /** | ||
| * licence Enterprise | ||
| */ | ||
| class XMLDecryptionConverter extends AbstractDecryptionConverter | ||
| { | ||
| public function convert($source, Type $sourceType, MediaType $sourceMediaType, Type $targetType, MediaType $targetMediaType) | ||
| { | ||
| $data = XmlHelper::xmlToArray($source); | ||
| $data = $this->decrypt($data); | ||
|
|
||
| return XmlHelper::arrayToXml($data); | ||
| } | ||
|
|
||
| public function matches(Type $sourceType, MediaType $sourceMediaType, Type $targetType, MediaType $targetMediaType): bool | ||
| { | ||
| return $targetType->acceptType($this->supportedType) && $sourceType->isString() && $sourceMediaType->isCompatibleWith(MediaType::createApplicationXml()) && $sourceMediaType->hasParameter('encrypted'); | ||
| } | ||
| } | ||
31 changes: 31 additions & 0 deletions
31
packages/DataProtection/src/Conversion/XMLEncryptionConverter.php
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Ecotone\DataProtection\Conversion; | ||
|
|
||
| use Ecotone\DataProtection\Encryption\Crypto; | ||
| use Ecotone\DataProtection\Encryption\Key; | ||
| use Ecotone\Messaging\Conversion\ConversionService; | ||
| use Ecotone\Messaging\Conversion\Converter; | ||
| use Ecotone\Messaging\Conversion\MediaType; | ||
| use Ecotone\Messaging\Handler\Type; | ||
|
|
||
| /** | ||
| * licence Enterprise | ||
| */ | ||
| class XMLEncryptionConverter extends AbstractEncryptionConverter | ||
| { | ||
| public function convert($source, Type $sourceType, MediaType $sourceMediaType, Type $targetType, MediaType $targetMediaType) | ||
| { | ||
| $data = XmlHelper::xmlToArray($source); | ||
| $data = $this->encrypt($data); | ||
|
|
||
| return XmlHelper::arrayToXml($data); | ||
| } | ||
|
|
||
| public function matches(Type $sourceType, MediaType $sourceMediaType, Type $targetType, MediaType $targetMediaType): bool | ||
| { | ||
| return $sourceType->acceptType($this->supportedType) && $targetType->isString() && $targetMediaType->isCompatibleWith(MediaType::createApplicationXml()) && $targetMediaType->hasParameter('encrypted'); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've tried to use
ConversionServicehere but it does not support serialization between array and xml string loosing parameter names when converting array into xml.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't the
#[Sensitive('someCustomName')]solving this?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or if this is about nesting e.g.
<result><person><someName></someName></person></result>, so we do not know how to go into deeper nesting?Because we could then provide nested structure attribute:
or eventually when we add support for functions (new php feature), that could be modified as needed:
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DataProtectionConverterworks around serialization on root level. Depending on configuration, not all of properties may need protection. Custom name passed with#[Sensitive]does not solve it as it's not for serialization. It only tellsDataProtectionConverterthat property name in serialization is different than property in actual object.Also nested data is not a problem here because it does not matter. Data Protection works only for root and nested properties (non scalar) are always serialized to string. Consider following example:
After serialization it will have nested structure:
[ "sensitive" => [ "argumentA" => "foo", "argumentB" => "bar", ], "property" => "baz", ]Encryption will return following structure:
[ "sensitive": "TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQ=", "property": "baz" ]Once encrypted, data has to be returned as XML.
ConversionServicereturns following XML (handled by JMSConverter)This is the reason why I introduced
XMLHelperas JMS was not cooperative.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, i see. That's weird with
entry, I would expect it to follow structure of the array. Maybe that requires some config switch on jms side.Anyways not a blocker :)