Skip to content
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

Passing config using environment variables #99

Open
femtotrader opened this issue Oct 3, 2015 · 6 comments
Open

Passing config using environment variables #99

femtotrader opened this issue Oct 3, 2015 · 6 comments

Comments

@femtotrader
Copy link
Contributor

Hello,

I'm using Travis-CI for Continuous Integration in many projects.
I'm planning to use traitlets as a way to configure some tools (passing API_KEY, ...)

To be able to test my projects, I need to pass some settings using environnement variables (only for Continuous Integration) because it's necessary to "hide" (from Git repository) some config data (API_KEY, password...)

see http://docs.travis-ci.com/user/environment-variables/

Is it something which is possible with traitlets ?
Is it something which could be considered in traitlets ?

Kind regards

@femtotrader femtotrader changed the title Passing config using environnement variables Passing config using environment variables Oct 3, 2015
@minrk
Copy link
Member

minrk commented Oct 3, 2015

I use env variables to populate traitlets in a few projects (e.g.). I've thought about a way to support it officially in traitlets, but I haven't though of a way that ends up being better than what already works right now: a one-line default generator that reads a given env.

@ssanderson
Copy link
Member

but I haven't though of a way that ends up being better than what already works right now: a one-line default generator that reads a given env.

What about just enshrining this common pattern as universally-supported behavior? For example:

class MyClass(Configurable):
    api_key = Unicode(config=True, envvar="MY_ENVVAR_NAME")

The expected behavior here would be that this behaves equivalently to:

class MyClass(Configurable):
    api_key = Unicode(config=True)
    def _api_key_default(self):
        return os.environ['MY_ENVVAR_NAME']

@minrk
Copy link
Member

minrk commented Dec 30, 2015

I think that should work.

@ankostis
Copy link
Contributor

ankostis commented Feb 4, 2017

(I'm writting it here, because is a general discussion, not related to proposed PRs)

On #246, Carreau wrote:

[...] I think we should need to be extra careful for the precedence rule of ENV var vs config, vs command line argument as well [...]

I want to put my (light)weight on it: generally programs (e.g. gpg, git) receive configuration values from the following "sources", in that order (later ones win over previous):

  • program defaults (trait.default_value, then dynamic_defaults),
  • config-files,
  • env-var,
  • command-line arguments.
  • values assigned by code [edit: of course...]

So far, command-line argument take precedence over config-files because they are merged on top of them.
But with the proposed changes (i.e. #157, #158, #246) I don't see env-vars taking precedence over config-files.

Am i correct to worry?

@ankostis ankostis mentioned this issue Jun 25, 2017
ankostis added a commit to ankostis/traitlets that referenced this issue Aug 12, 2017
@ankostis ankostis mentioned this issue Aug 12, 2017
ankostis added a commit to ankostis/traitlets that referenced this issue Aug 12, 2017
+ When getting trait-value, try 1st env-var, then fallback to defaults.
ankostis added a commit to ankostis/traitlets that referenced this issue Aug 12, 2017
+ Don't load config-values on traits with existing ENV-VAR.
+ Print ENV-VAR used in class-help, config and RsT msgs.
+ Add/update TCs.
ankostis added a commit to ankostis/traitlets that referenced this issue Aug 12, 2017
+ Cmd-line configs override ENV-VARS
+ Add/update TCs.
ankostis added a commit to ankostis/traitlets that referenced this issue Aug 19, 2017
+ When getting trait-value, try 1st env-var, then fallback to defaults.
ankostis added a commit to ankostis/traitlets that referenced this issue Aug 19, 2017
ankostis added a commit to ankostis/traitlets that referenced this issue Aug 19, 2017
ankostis added a commit to ankostis/traitlets that referenced this issue Aug 22, 2017
ankostis added a commit to ankostis/traitlets that referenced this issue Jan 12, 2018
+ Now FIX app.ipython#99 respecting CFG < ENV < CLI order.
+ Add config & app layer TCs.
@Carreau
Copy link
Member

Carreau commented Aug 10, 2023

This came up again in jupyter/nbconvert#2026 I've came up with this in the config file:

$ cat ~/.jupyter/jupyter_nbconvert_config.py
import os

for k, v in os.environ.items():
    if k.startswith("NBCONVERT"):
        # use __ instead of . as separator in env variable.
        _, klass, attribute = k.split("__")
        # c.Klass.attribute=v
        setattr(getattr(c, klass), attribute, v)

I believe we might be able to adapt it by pulling the app name in traitlets and have a default config loader that loads values from env variables.

My main concern is how could this be abused and could it be a security risk ? My thoughts is that it is more common to give write access to env variables to users than access to CLI flags.

@Carreau
Copy link
Member

Carreau commented Aug 28, 2023

See #856 as a prototype of how this could work.

I'd appreciate reviews.

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

No branches or pull requests

5 participants