Skip to content

[DI] Select specific key from an array resolved env var #27157

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 1 commit into from
May 21, 2018

Conversation

bobvandevijver
Copy link
Contributor

@bobvandevijver bobvandevijver commented May 4, 2018

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets Discussed in #25643
License MIT
Doc PR symfony/symfony-docs#9734

As discussed in #25643, it would be convenient to have an key processor for environment variables which have been read from for example a JSON file.

The main advantage of this feature lies in specifying a single file for your secrets, that can be directly used in your configuration, without leaking them into your env.

Example

.secrets.json

{
  "database_password": "xxx"
}

.env

APP_SECRETS=/opt/application/.secrets.json
DATABASE_URL=mysql://myuser:%database_password%@localhost:3306/mydb

services.yaml

parameters:
  database_password: '%env(key:database_password:json:file:APP_SECRETS)%'

This example configuration will result in a database_password parameter being filled with xxx, and due to Doctrine defaults, the database url will be resolved with the correct password.

@nicolas-grekas
Copy link
Member

Looks good, thank you! You'd just need to add "key" to "getProvidedTypes()" - and add tests now to verify this works as expected of course as you mentionned.

@bobvandevijver bobvandevijver force-pushed the env-key-processor branch 3 times, most recently from 0a3a726 to 8d311e8 Compare May 4, 2018 19:58
@bobvandevijver
Copy link
Contributor Author

Tests have been added, key has been added to getProvidedTypes() and an issue has been created for the documentation (currently blocked by another MR).

@@ -41,6 +41,7 @@ public static function getProvidedTypes()
'float' => 'float',
'int' => 'int',
'json' => 'array',
'key' => 'string',
Copy link
Member

@nicolas-grekas nicolas-grekas May 4, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be anything, like const above

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const is not allowed in the returned types (or at least, that is what the tests tell me when I add it). A const in only a const when the processor has interpreted a string as such right?

Would updated this to 'key' => 'bool|int|float|string|array', make sense?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would to me yes

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about fetch for a name? Tend to prefer it over key..

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a verb, all the other are nouns...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolve :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolve is already used to resolve kernel parameters in the env var

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, i meant "resolve" is also a verb.. legitimating to use "fetch" here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, of course... Should have seen that :) I think key is better as you are actually specifying the key you want to select from an array, while fetch can actually be to fetch right about anything.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer "key" as well

Copy link
Member

@javiereguiluz javiereguiluz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great feature! Thanks for contributing it!

@nicolas-grekas
Copy link
Member

Thank you @bobvandevijver.

@nicolas-grekas nicolas-grekas merged commit 42186a2 into symfony:master May 21, 2018
nicolas-grekas added a commit that referenced this pull request May 21, 2018
…r (bobvandevijver)

This PR was squashed before being merged into the 4.2-dev branch (closes #27157).

Discussion
----------

[DI] Select specific key from an array resolved env var

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes <!-- don't forget to update src/**/CHANGELOG.md files -->
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | Discussed in #25643    <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        | symfony/symfony-docs#9734 <!-- required for new features -->

<!--
Write a short README entry for your feature/bugfix here (replace this comment block.)
This will help people understand your PR and can be used as a start of the Doc PR.
Additionally:
 - Bug fixes must be submitted against the lowest branch where they apply
   (lowest branches are regularly merged to upper ones so they get the fixes too).
 - Features and deprecations must be submitted against the master branch.
-->

As discussed in #25643, it would be convenient to have an key processor for environment variables which have been read from for example a JSON file.

The main advantage of this feature lies in specifying a single file for your secrets, that can be directly used in your configuration, without leaking them into your env.

## Example
**.secrets.json**
```json
{
  "database_password": "xxx"
}
```
**.env**
```env
APP_SECRETS=/opt/application/.secrets.json
DATABASE_URL=mysql://myuser:%database_password%@localhost:3306/mydb
```
**services.yaml**
```yaml
parameters:
  database_password: '%env(key:database_password:json:file:APP_SECRETS)%'
```

This example configuration will result in a `database_password` parameter being filled with `xxx`, and due to Doctrine defaults, the database url will be resolved with the correct password.

Commits
-------

42186a2 [DI] Select specific key from an array resolved env var
@bobvandevijver bobvandevijver deleted the env-key-processor branch May 21, 2018 19:27
javiereguiluz added a commit to symfony/symfony-docs that referenced this pull request Jul 4, 2018
…guiluz)

This PR was merged into the master branch.

Discussion
----------

Added key env processor to docs

Not much more to say, this is documenting the feature added in symfony/symfony#27157.

Fixes #9734.

Commits
-------

9c91cf1 Reworded and added other config formats
1d72bc2 Added key env processor to docs
@nicolas-grekas nicolas-grekas modified the milestones: next, 4.1 Nov 1, 2018
This was referenced Nov 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants