Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot - using environment variables in application.yml

I'm trying to parametrize my application.yml file so I can put it on github repo with my project, but not reveal database connection details. I want to use system environment variables to do so. I use OS X El Capitan.

I put following lines in my .bash_profile:

export JDBC_TODO_USER=<my-username>
export JDBC_TODO_PASS=<my-password>

and following lines in my application.yml:

spring:
   datasource:
   username: ${JDBC_TODO_USER}
   password: ${JDBC_TODO_PASS}

I also tried

spring:
   datasource:
   username: ${JDBC.TODO.USER}
   password: ${JDBC.TODO.PASS}

Both of those give me an error during start of the app: java.sql.SQLInvalidAuthorizationSpecException: Could not connect: Access denied for user '${JDBC_TODO_USER'@'<my-ip-address>' (using password: YES)

Of course, when I replace ${JDBC_TODO_USER} with my username and ${JDBC_TODO_PASS} with my password then everything works fine. It looks like Spring does not understand that I refer to environment variables. Do I need to do some additional configuration to make it work?

like image 560
Sebastian Osiński Avatar asked Oct 14 '15 12:10

Sebastian Osiński


1 Answers

It is quite likely that your environment variables are not being made available. Have you tried the following to see if they are accessible?

echo $JDBC_TODO_USER
echo $JDBC_TODO_PASS

or

export

A sure-fire way to ensure they are available is to:

  1. sudo vi /etc/launchd.conf
  2. Add the following lines:
setenv JDBC_TODO_USER 
setenv JDBC_TODO_PASS
  1. Save the file.
  2. Run export to confirm that your entries are there

You can also try system properties:

java -Djdbc.todo.user=myuser -Djdbc.todo.pass=mypass -jar MyProject.jar

with the following in your application.yml:

spring:
   datasource:
   username: ${jdbc.todo.user}
   password: ${jdbc.todo.pass}

But ultimately for storing user credentials in a production environment I would recommend one of the following: http://docs.ansible.com/ansible/playbooks_vault.html https://vaultproject.io/

like image 111
rudolfv Avatar answered Oct 23 '22 17:10

rudolfv