-
Notifications
You must be signed in to change notification settings - Fork 58
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
config: Fix overriding options with env vars #514
Conversation
0ce5d1d
to
1adf840
Compare
} | ||
|
||
impl AgentConfig { | ||
pub fn get_map(&self) -> Map<String, Value> { |
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 for me, but maybe we can drop concept of "configuration as a dictionary", and use the struct directly
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.
We use the struct directly, the collection to get the map is only used to implement Source
to allow overriding options with the values obtained from the environment variable.
( | ||
#[derive(Clone, Debug, Deserialize, Serialize)] | ||
pub(crate) struct AgentConfig { | ||
pub version: Option<String>, |
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.
Now everything is optional, can this be a problem?
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.
It is not a problem because since the last big configuration change to TOML format and to use the config
crate, we always base the configuration from the defaults. So basically there is no case when an option is not set.
/packit retest-failed |
Can we move the default value part into the |
Actually all of those are dead code, as the options are always set. The problem is that we need the internal I can replace all of them with We can alternatively make the |
Yeah I think the last approach is probably the best. Then we have the duplication only in one place. |
1adf840
to
cf4b9c5
Compare
@THS-on I rewrote the code to add I removed some checks for options set throughout the code as the options are all required. I had to keep checks for empty strings is some cases, though. I also added instructions to the configuration file on how to override options using env vars. |
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.
@ansasaki thank you for changing it :)
I think the empty strings checks are fine, because that are not that many.
cf4b9c5
to
d686e09
Compare
Read the configuration options set via environment variables separately to initialize an EnvConfig structure and use this structure as a Source to override the values of the KeylimeConfig obtained from the configuration files. For this, change the prefix for the environment variables to KEYLIME_AGENT with the 'prefix_separator' as '_', and 'separator' as '.'. Then the configuration options obtained from the environment variables are used to initialize an EnvConfig structure, which does not contain sub-structures. The EnvConfig structure is a duplicate of AgentConfig structure, but contains Option<T> for all configuration options, allowing values to not be set via environment variables. The remaining optional fields in AgentConfig were replaced to be required (not Option<T>). Note that the verification of required/non-required options was not used since all values are set with the default values and then successively overriden by values from the configuration files and environment variables. The Source trait was implemented for the EnvConfig structure in a way that it is possible to use it as the source for KeylimeConfig structure. Fixes: keylime#510 Signed-off-by: Anderson Toshiyuki Sasaki <[email protected]>
Signed-off-by: Anderson Toshiyuki Sasaki <[email protected]>
d686e09
to
c9263a9
Compare
Read the configuration options set via environment variables separately
to initialize an EnvConfig structure and use this structure as a
Source to override the values of the KeylimeConfig obtained from the
configuration files.
For this, change the prefix for the environment variables to
KEYLIME_AGENT with the 'prefix_separator' as '_', and 'separator' as
'.'.
Then the configuration options obtained from the environment variables
are used to initialize an EnvConfig structure, which does not contain
sub-structures.
The EnvConfig structure is a duplicate of AgentConfig structure, but
contains Option for all configuration options, allowing values to not
be set via environment variables.
The remaining optional fields in AgentConfig were replaced to be
required (not Option).
Note that the verification of required/non-required options
was not used since all values are set with the default values and then
successively overriden by values from the configuration files and
environment variables.
The Source trait was implemented for the EnvConfig structure in a way
that it is possible to use it as the source for KeylimeConfig structure.
Fixes: #510
Signed-off-by: Anderson Toshiyuki Sasaki [email protected]