Search code examples
javajerseyenvironment-variablesdropwizard

Dropwizard Environement variables


I am having problems finding good resources for the dropwizard environment configuration, the manuals from dropwizard are not really helpful for me.

I am trying to save my jwtSecret in my configuration config.yml as a environment variable so it stays a secret even if I make my code open source as

jwtSecret: ${JWT_SECRET}

I have read the manuals and I know I need to add SubstitutingSourceProvider to successfully substitute the config with my environment variables. However I do not find where to save my environment variables. Is there a specific place where dropwizard finds it or do I need to add the path to the environment variables somewhere?


Solution

  • I'm not entirely sure which part you need help with, but adding environmental variables touches many points.

    First we enable this feature by adding it to the Application's initialize function:

    bootstrap.setConfigurationSourceProvider(new SubstitutingSourceProvider(bootstrap.getConfigurationSourceProvider(), new EnvironmentVariableSubstitutor(false)));
    

    Then there is the config.yml:

    jwtSecret: ${JWT_SECRET}
    

    To access the variable in your application, you need to add this to the Application's Configuration class:

    private String jwtSecret;
    

    (+ getter and setter for it)

    And lastly you need to add the JWT_SECRET variable to your system environmental variables. This varies based on which operating system you are using and whether you want to set it temporarily or permanently.