Wrong environment variable value can be saved to Sites\{TenantName}\appsettings.json during tenant creation
Describe the bug
Reproduced in plain old Orchard Core 1.8.2
I have two environment variables set in launchSettings.json.
- OrchardCore__Default__MyEnvVar: DefaultTenantValue
- OrchardCore__MyEnvVar: OtherTenantValue
In a multitenant scenario, I want the default tenant to use one value, and all the other tenants to use another.
The key to reproducing this is to make sure these environment variables are set before the default tenant is created. Create a new site from scratch. I don't think the recipe matters, but I used the Blog recipe.
To Reproduce
- Set the environment variables in launchSettings.json
- Create a new OC 1.8.2 site from scratch using the Blog recipe
The problem is, after the Default tenant is created I'm actually getting the value "OtherTenantValue" back, when I expected "DefaultTenantValue". The problem seems to be that "OtherTenantValue" got saved to Sites\Default\appsettings.json
Screenshots
In Sites\Default\appsettings.json, I see that the wrong value was saved. Since Sites\Default\appsettings.json takes precedence over launchSettings.json, I end up using the wrong value for the Default tenant. Furthermore, it was confusing why changing the value in launchSettings.json wasn't taking effect, and that's because appsettings.json was winning.
Admittedly, I think the scope of this bug is somewhat narrow, as it requires these variables to be set prior to tenant creation. If I go through the tenant setup first and add my environment variables later, everything works as I expect and the value is never stored in appsettings.json.
Values in Sites\Default\appsettings.json is settings and it always takes precedence over configuration. How are you using in recipe or reading in code?
@rjpowers10 I have not looked at the code. But i trust that the docs are still valid "as they should" https://docs.orchardcore.net/en/latest/docs/reference/core/Configuration/#config-sources
The important part is this
The Configuration Sources are loaded in the above order, and settings lower in the hierarchy will override values configured higher up, i.e. an Global Tenant value will always be overridden by an Environment Variable.
This means that you will need to flip variable and test so the order should be like this
- OrchardCore__MyEnvVar: OtherTenantValue
- OrchardCore__Default__MyEnvVar: DefaultTenantValue
With this, the first line will apply to app tenants including the default. But then, we override the configuration for the default tenant using the second variable. "last one wins"
Also, I would not expect these variables to be written to the appsettings file. These settings are loaded into memory and kept that way. So if you are looking to see which settings were loaded and is used, then maybe you should log them somewhere so you can test it out. But these settings should never be written directly to the appsettings file of the tenant.
Closing due to no reply from the author.