Skip to content

Commit

Permalink
Refactor for flex.
Browse files Browse the repository at this point in the history
Add multiple connection definitions
  • Loading branch information
mogilvie committed Feb 20, 2023
1 parent 88f4c3d commit 2cf7d63
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 22 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## 3.0.2 (2022-11-xx) Update
Add attribute support for #[Encrypted] attributes instead of @Encrypted annotations.
Add option to catch doctrine events from multiple connections.
Add encrypt and decrypt CLI commands.
Refactor for symfony flex and Symfony 6 recommended third party bundle structure

## 3.0.1 (2022-03-13) Symfony 6 and PHP 8
Major backward compatibility breaking change to Symfony 6 and PHP 8.
Expand Down
37 changes: 17 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ A bundle to handle encoding and decoding of parameters using OpenSSL and Doctrin
Features include:
- V3 is Symfony 5.4|6 PHP 8
- V2 is Symfony 5.
- v1 is Symfony 3.4 and not active any more.
- v1 is Symfony 3.4 and not active anymore.
- Uses OpenSSL
- Uses Lifecycle events

Expand Down Expand Up @@ -87,39 +87,36 @@ $ bin/console encrypt:genkey
Copy the key into your .env file.
```
###> encrypt-bundle ###
ENCRYPT_KEY=<YOUR KEY HERE>
ENCRYPT_KEY= change_me!
###< encrypt-bundle ###
```

And resolve in your parameters file.
And resolve in your packages yaml file.
```yaml
// app/config/parameters.yml
...
encrypt_key: '%env(resolve:ENCRYPT_KEY)%'
# app/config/packages/spec_shaper_encrypt.yaml
spec_shaper_encrypt:
encrypt_key: '%env(ENCRYPT_KEY)%'
is_disabled: false # Turn this to true to disable the encryption.
connections: # Optional, define the connection name(s) for the subscriber to listen to.
- 'default'
- 'tenant'
subscriber_class: App\Subscriber\MyCustomSubscriber # Optional to override the bundle Doctrine event subscriber.
encryptor_class: App\Encryptors\MyCustomEncryptor # Optional to override the bundle OpenSslEncryptor.
annotation_classes: # Optional to override the default annotation/Attribute object.
- App\Annotation\MyAttribute
```
A config file entry is not required, however there are some options that
can be configured to extend the bundle.
```yaml
# The encryptor service created by the factory according to the passed method and using the encrypt_key
SpecShaper\EncryptBundle\Encryptors\EncryptorInterface:
factory: ['@SpecShaper\EncryptBundle\Encryptors\EncryptorFactory','createService']
arguments:
$encryptKey: '%spec_shaper_encrypt.encrypt_key%'
$encryptorClass: '%spec_shaper_encrypt.encryptor_class%' #optional
```
You can disable encryption by setting the 'is_disabled' option to true. Decryption still continues if any values
contain the \<ENC> suffix.
You can pass the class name of your own encyptor service using the optional encryptorClass option.
You can extend the EncryptBundle default Subscriber and override its methods. Use the 'subscriber_class' option
to point the bundle at your custom subscriber.
If you want to define your own annotation/attribute, then this can be used to trigger encryption by adding the annotation
class name to the 'annotation_classes' option array.
You can pass the class name of your own encyptor service using the optional encryptorClass option.
### Alternative EncryptKeyEvent
The EncryptKey can be set via a dispatched event listener, which overrides any .env or param.yml defined key.
Create a listener for the EncryptKeyEvents::LOAD_KEY event and set your encryption key at that point.
Expand Down Expand Up @@ -161,7 +158,7 @@ will be discontinued in the next major update.
*
* @Encrypted
* Note that the above Encrypted property is a legacy annotation, and while
* it still is supported, it will be deprecated in favour of Attributes .
* it still is supported, it will be deprecated in favour of Attributes.
*
* @ORM\Column(type="string", nullable=true)
*/
Expand Down
5 changes: 3 additions & 2 deletions src/Encryptors/EncryptorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
interface EncryptorInterface
{
public function setSecretKey(string $key): void;

/**
* Must accept data and return encrypted data.
*
* @param string $data Unencrypted string
* @param string|null $data Unencrypted string
*
* @return string Encrypted string
* @return string|null Encrypted string
*/
public function encrypt(?string $data): ?string;

Expand Down
3 changes: 3 additions & 0 deletions src/SpecShaperEncryptBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace SpecShaper\EncryptBundle;

use SpecShaper\EncryptBundle\Annotations\Encrypted;
use SpecShaper\EncryptBundle\Encryptors\OpenSslEncryptor;
use SpecShaper\EncryptBundle\Subscribers\DoctrineEncryptSubscriber;
use SpecShaper\EncryptBundle\Subscribers\EncryptEventSubscriber;
use Symfony\Component\Config\Definition\Configurator\DefinitionConfigurator;
Expand All @@ -28,6 +29,7 @@ public function configure(DefinitionConfigurator $definition): void
->scalarNode('encrypt_key')->end()
->scalarNode('method')->defaultValue('OpenSSL')->end()
->scalarNode('subscriber_class')->defaultValue(DoctrineEncryptSubscriber::class)->end()
->scalarNode('encryptor_class')->defaultValue(OpenSslEncryptor::class)->end()
->scalarNode('is_disabled')->defaultValue(false)->end()
->arrayNode('connections')
->treatNullLike([])
Expand Down Expand Up @@ -60,6 +62,7 @@ public function loadExtension(array $config, ContainerConfigurator $container, C
$container->parameters()->set($this->extensionAlias.'.encrypt_key', $encryptKey);
$container->parameters()->set($this->extensionAlias.'.method', $config['method']);
$container->parameters()->set($this->extensionAlias.'.subscriber_class', $config['subscriber_class']);
$container->parameters()->set($this->extensionAlias.'.encryptor_class', $config['encryptor_class']);
$container->parameters()->set($this->extensionAlias.'.annotation_classes', $config['annotation_classes']);
$container->parameters()->set($this->extensionAlias.'.is_disabled', $config['is_disabled']);

Expand Down

0 comments on commit 2cf7d63

Please sign in to comment.