Skip to content

Provide better 'bad key' value error message#1558

Merged
monfresh merged 1 commit into18F:masterfrom
rlxdev:rlxdev-cfg_validator
Jul 28, 2017
Merged

Provide better 'bad key' value error message#1558
monfresh merged 1 commit into18F:masterfrom
rlxdev:rlxdev-cfg_validator

Conversation

@rlxdev
Copy link
Contributor

@rlxdev rlxdev commented Jul 19, 2017

Why:

  • Provide clearer message when environment variables have
    yes/no values instead of true/false - prior message pointed
    to application.yml instead of environment, wasting debug time

How:

  • Remove incorrect 'config/application.yml' reference, as values
    come from environment instead
  • Indicate source is 'environment' in error message if ENV is used
  • Specifically check for 'yes' or 'no' in environment variable value
  • Remove unnecessary '\' - makes clearer that only '\n' is being
    removed

Copy link
Contributor

@monfresh monfresh Jul 19, 2017

Choose a reason for hiding this comment

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

env will always be ENV since that is the default, and we don't set it to anything else. So, instead of this conditional, what do you think about updating the message in the warning method to say something like "You have invalid values (yes/no) for [bad keys] in config/application.yml or your environment. Please change them to true or false."?

@monfresh
Copy link
Contributor

Thanks for this PR, and congrats on your first GitHub pull request!

Out of curiosity, can you please explain how you are configuring the environment? Are you not using config/application.yml at all? Or are you using it locally, and then once you're ready to push the values to production, you are setting the ENV variables directly on your server, or perhaps you are pushing them to Heroku using Figaro's helper commands?

@rlxdev
Copy link
Contributor Author

rlxdev commented Jul 19, 2017

Thanks, I'll take your suggestion. My edits are partly out of ignorance about how things are working, but I hit this issue and spent time trying to figure it out so I thought I'd save someone else by changing the message.

I configured this on a CentOS 7 VM using your instructions in README. I just wanted to try it locally to see if I could get it up and running. I hit a couple of things that may be environment-related, but I got it going.

BTW, the environment variable I hit this on is IMSETTINGS_INTEGRATE_DESKTOP (was set to 'yes').

I think this is a great idea for common authentication for government agencies and I hope it is well adopted. It would be a win-win for the agencies that use it and also the public users.

@monfresh
Copy link
Contributor

Ah, I see. Thanks for the details. IMSETTINGS_INTEGRATE_DESKTOP is not something this app uses. It was set in your VM, and the bug is that we are checking environment variables that aren't related to the app. So, instead of iterating through all the keys that don't start with Figaro::Application::FIGARO_ENV_PREFIX, we should do the opposite and only select those keys, and then strip the prefix from them.

Do you want to take a stab at fixing that? If not, I'd be happy to work on it.

@rlxdev
Copy link
Contributor Author

rlxdev commented Jul 19, 2017

Exactly, and sure, I'll take a stab at it. Thanks!

@rlxdev
Copy link
Contributor Author

rlxdev commented Jul 21, 2017

Here's my stab at it - hopefully, I got it right. I notice that with the configuration settings, Figaro creates 2 environment variables, one with the setting name as the key and one with the key prefixed with "FIGARO_". If we're only interested in the configuration settings and not the other environment variables, then we can collect up the keys having both definitions (prefixed and not prefixed). These are the changes I've made.

Prior to commit, I've tested this out by printing the key/value combinations I've found and I'm satisfied that we're getting only the configuration values for the application.

I hope this is what you're looking for.

Copy link
Contributor

Choose a reason for hiding this comment

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

Was there a particular reason for switching to regex? It's almost twice as slow as checking the array. Can we do this instead?

%w[yes no].include?(env[key].strip.downcase)

Copy link
Contributor

Choose a reason for hiding this comment

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

Can we rename the keys argument to env, since that is what is passed in?

Copy link
Contributor

Choose a reason for hiding this comment

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

Single space after period?

Copy link
Contributor

Choose a reason for hiding this comment

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

In order to test the stripping and downcasing, can you please add some more candidate keys, such as " YES" and "NO "?

@rlxdev
Copy link
Contributor Author

rlxdev commented Jul 21, 2017

thanks for your patience and suggestions:

Was there a particular reason for switching to regex? It's almost twice as slow as checking the array. Can we do this instead?

You're right.

Can we rename the keys argument to env, since that is what is passed in?

Yes

Single space after period?

Yup, I'm a student of when you put 2 spaces after sentences - hard habit to break.

In order to test the stripping and downcasing, can you please add some more candidate keys, such as " YES" and "NO "?

Added 2 relevant tests.

Thanks!

Copy link
Contributor

@monfresh monfresh left a comment

Choose a reason for hiding this comment

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

Looks great. Thanks! Please squash your commits into one.

**Why**:
- Current implementation looks at all environment variables, both
those associated with application configuration and those that don't apply
(e.g., IMSETTINGS_INTEGRATE_DESKTOP).  Non-configuration settings with
bad values will cause warnings.  This change aims to target validation to
only the configuration values.

**How**:
- Select keys (environment variable names) that have a 2nd definition prefixed
with "FIGARO_" - these are the key/value pairs associated with application
configuration.
- Check values case insensitive, ignoring white space.
- Add test cases.
@rlxdev rlxdev force-pushed the rlxdev-cfg_validator branch from 14cb296 to dad14d9 Compare July 24, 2017 20:41
@rlxdev
Copy link
Contributor Author

rlxdev commented Jul 24, 2017

Thanks! Squash is done.

Copy link
Contributor

@monfresh monfresh left a comment

Choose a reason for hiding this comment

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

LGTM

@monfresh monfresh merged commit 80a0598 into 18F:master Jul 28, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants