Skip to content

Conversation

aaronburtle
Copy link
Contributor

@aaronburtle aaronburtle commented Jun 10, 2022

Summary

This change allows for a user to place the string @env('<Environment Variable Name>') into values in the runtime config so that they can avoid exposing those values directly. We then take the <Environment Variable Name> and retrieve the associated environment variable, which replaces @env('<Environment Variable Name>') in the config.

Changes

When the runtime config file is first read in RuntimeConfigPath.cs we parse that string and do replacements as necessary. To facilitate this parsing, we use the NewtonSoft library, and re-write the same json string as it is read. System.Json.Text lacks the needed functionality for reading and re-writing the json string in a convenient way. For any string values in the json, we look to replace the pattern we search for with the environment variable found from within that pattern if one exists. If the user has provided the wrong environment variable names, or they otherwise are not found, we throw an exception.

The regex used for matches has comments which explain each part. The replacement function we call takes a delegate which will call out replacement helper function, that has another regex with comments that explain each part.

Testing

Integration testing will be added once we have dynamic config generation in our tests. In the meantime we test many cases with the unit tests which validate we can parse an entire config file correctly. Many of these cases attempt to break our regex pattern and may look confusing at first glance.

Copy link
Contributor

@jarupatj jarupatj left a comment

Choose a reason for hiding this comment

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

:shipit:

Copy link
Collaborator

@Aniruddh25 Aniruddh25 left a comment

Choose a reason for hiding this comment

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

Please double check if there is absolutely no alternative in System.Text.Json - even if it may be inconvenient, we will have to adopt it for better performance..

Copy link
Collaborator

@Aniruddh25 Aniruddh25 left a comment

Choose a reason for hiding this comment

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

Thanks for using System.Text.Json!

@aaronburtle aaronburtle merged commit 78bdd0c into main Jun 24, 2022
@aaronburtle aaronburtle deleted the dev/aaronburtle/EnvParsingForRuntimeConfig branch June 24, 2022 04:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Parse Environment Variables for any of the values in runtime config set via @env()
4 participants