122

Spring Boot uses the properties file, and at least by default, the passwords are in plain text. Is it possible to somehow hide/decrypt these?

0

7 Answers 7

124

You can use Jasypt to encrypt properties, so you could have your property like this:

db.password=ENC(XcBjfjDDjxeyFBoaEPhG14wEzc6Ja+Xx+hNPrJyQT88=)

Jasypt allows you to encrypt your properties using different algorithms, once you get the encrypted property you put inside the ENC(...). For instance, you can encrypt this way through Jasypt using the terminal:

encrypted-pwd$ java -cp ~/.m2/repository/org/jasypt/jasypt/1.9.2/jasypt-1.9.2.jar  org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="contactspassword" password=supersecretz algorithm=PBEWithMD5AndDES

----ENVIRONMENT-----------------

Runtime: Oracle Corporation Java HotSpot(TM) 64-Bit Server VM 24.45-b08



----ARGUMENTS-------------------

algorithm: PBEWithMD5AndDES
input: contactspassword
password: supersecretz



----OUTPUT----------------------

XcBjfjDDjxeyFBoaEPhG14wEzc6Ja+Xx+hNPrJyQT88=

To easily configure it with Spring Boot you can use its starter jasypt-spring-boot-starter with group ID com.github.ulisesbocchio

Keep in mind, that you will need to start your application using the same password you used to encrypt the properties. So, you can start your app this way:

mvn -Djasypt.encryptor.password=supersecretz spring-boot:run

Or using the environment variable (thanks to spring boot relaxed binding):

export JASYPT_ENCRYPTOR_PASSWORD=supersecretz
mvn spring-boot:run

You can check below link for more details:

https://www.north-47.com/knowledge-base/spring-boot-password-encryption-with-jasypt/

To use your encrypted properties in your app just use it as usual, use either method you like (Spring Boot wires the magic, anyway the property must be of course in the classpath):

Using @Value annotation

@Value("${db.password}")
private String password;

Or using Environment

@Autowired
private Environment environment;

public void doSomething(Environment env) {
    System.out.println(env.getProperty("db.password"));
}

Update: for production environment, to avoid exposing the password in the command line, since you can query the processes with ps, previous commands with history, etc etc. You could:

  • Create a script like this: touch setEnv.sh

  • Edit setEnv.sh to export the JASYPT_ENCRYPTOR_PASSWORD variable

    #!/bin/bash

    export JASYPT_ENCRYPTOR_PASSWORD=supersecretz

  • Execute the file with . setEnv.sh

  • Run the app in background with mvn spring-boot:run &

  • Delete the file setEnv.sh

  • Unset the previous environment variable with: unset JASYPT_ENCRYPTOR_PASSWORD

23
  • 2
    Could you please explain more in details using gradle @Frerica Piazza
    – testuser
    Jan 10, 2017 at 12:30
  • 2
    @FedericoPiazza Isn't mvn -Djasypt.encryptor.password=supersecretz spring-boot:run going to show up in the ps output, exposing the password?
    – Srki Rakic
    Dec 27, 2018 at 22:17
  • 1
    @SrkiRakic yes, of course. This is just for development, if you want it for production you should use environment variables. Spring boot allows you to use JASYPT_ENCRYPTOR_PASSWORD Dec 28, 2018 at 11:33
  • 1
    haha and how does it get into environment variables? Probably from another file like service difinition :D Also jasypt is outdated when it comes to password derivation so make sure to use totally random 32 character password Mar 7, 2019 at 3:35
  • 1
    @ChrisF I think you are misunderstanding how the encryption works. The input="this is the text I want to encrypt" and password="this is the key used to encrypt the input" Jan 13, 2020 at 20:12
21

To the already proposed solutions I can add an option to configure an external Secrets Manager such as Vault.

  1. Configure Vault Server vault server -dev (Only for DEV and not for PROD)
  2. Write secrets vault write secret/somename key1=value1 key2=value2
  3. Verify secrets vault read secret/somename

Add the following dependency to your SpringBoot project:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-vault-config</artifactId>
</dependency>

Add Vault config properties:

spring.cloud.vault.host=localhost
spring.cloud.vault.port=8200
spring.cloud.vault.scheme=http
spring.cloud.vault.authentication=token
spring.cloud.vault.token=${VAULT_TOKEN}

Pass VAULT_TOKEN as an environment variable.

Refer to the documentation here.

There is a Spring Vault project which is also can be used for accessing, storing and revoking secrets.

Dependency:

<dependency>
    <groupId>org.springframework.vault</groupId>
    <artifactId>spring-vault-core</artifactId>
</dependency>

Configuring Vault Template:

@Configuration
class VaultConfiguration extends AbstractVaultConfiguration {

  @Override
  public VaultEndpoint vaultEndpoint() {
    return new VaultEndpoint();
  }

  @Override
  public ClientAuthentication clientAuthentication() {
    return new TokenAuthentication("…");
  }
}

Inject and use VaultTemplate:

public class Example {

  @Autowired
  private VaultOperations operations;

  public void writeSecrets(String userId, String password) {
      Map<String, String> data = new HashMap<String, String>();
      data.put("password", password);
      operations.write(userId, data);
  }

  public Person readSecrets(String userId) {
      VaultResponseSupport<Person> response = operations.read(userId, Person.class);
      return response.getBody();
  }
}

Use Vault PropertySource:

@VaultPropertySource(value = "aws/creds/s3",
  propertyNamePrefix = "aws."
  renewal = Renewal.RENEW)
public class Config {

}

Usage example:

public class S3Client {

  // inject the actual values
  @Value("${aws.access_key}")
  private String awsAccessKey;
  @Value("${aws.secret_key}")
  private String awsSecretKey;

  public InputStream getFileFromS3(String filenname) {
    // …
  }
}
5
  • +1 for this solution. Using a system like vault / etcd (or any other) is the way to go. diogomonica.com/2017/03/27/… Jan 30, 2019 at 15:38
  • 4
    -1 because this doesn't explain how the "master" key (VAULT_TOKEN) is secured. Where did the VAULT_TOKEN environment variable come from? How is it secured? Without protecting that key, the attacker can use it to retrieve the secrets from the vault using the code packaged in the Spring Boot jar. Mar 28, 2019 at 2:33
  • Also securing prod is the main issue. So, it has to be spoken here. Guidance for Dev/QA environments if fine.
    – sofs1
    Aug 18, 2019 at 0:41
  • This works when there are many passwords. It works for one password to connect but its funny to say put the vault password in the environment so you don't have to put the other password in the same environment.
    – Lee Meador
    Sep 30, 2019 at 21:04
  • 1
    why we want to use just for dev? prod is important. Jan 24, 2022 at 16:26
19

UPDATE: I noticed folks down-voting this, so I have to say that although this is not an ideal solution, but this works and acceptable in some use-cases. Cloudfoundry uses Environment variables to inject credentials when a Service is binded to an application. More info https://docs.cloudfoundry.org/devguide/services/application-binding.html

And also if your system is not shared, then for local development this is also acceptable. Of course, the more safe and secure way is explained in Answer by @J-Alex.

Answer:

If you want to hide your passwords then the easiest solution is to use Environment variables in application.properties file or directly in your code.

In application.properties:

mypassword=${password}

Then in your configuration class:

@Autowired
private Environment environment;

[...]//Inside a method
System.out.println(environment.getProperty("mypassword"));

In your configuration class:

@Value("${password}")
private String herokuPath;

[...]//Inside a method
System.out.println(herokuPath);

Note: You might have to restart after setting the environment variable. For windows:

In Windows

Refer this Documentation for more info.

6
  • 31
    I do not think setting the master password in the environment vars is such a good idea. The password is now more exposed than necessary. Providing it a startup as shown by Federico is less exposed and more "secure" than setting it in the environment.
    – Jaavaaan
    Sep 14, 2016 at 6:11
  • Yaa, its not if you are using shared computer. But if you are the only administrator of your computer then no other user can see the env vars. I answered the hiding part and the easier one. But yes, I agree Federico's suggested method is way better. Sep 16, 2016 at 5:30
  • Please see: diogomonica.com/2017/03/27/… Jan 30, 2019 at 15:38
  • @Jaavaaan providing JASYPT_ENCRYPTOR_PASSWORD in environment variable to decode db password is equally insecure as this answer.
    – m1ld
    Aug 18, 2021 at 21:18
  • @Jaavaaan Please read the Update. I've already mentioned that this isn't secure in all cases, but there are valid use-cases like CF. Also pointed to a better highly secure solution of using HashiCorp Vault. Aug 20, 2021 at 11:15
12

Spring Cloud Config Server will allow this type of behavior. Using JCE you can setup a key on the server and use it to cipher the apps properties.

http://cloud.spring.io/spring-cloud-config/spring-cloud-config.html

1

In case you are using quite popular in Spring Boot environment Kubernetes (K8S) or OpenShift, there's a possibility to store and retrieve application properties on runtime. This technique called secrets. In your configuration yaml file for Kubernetes or OpenShift you declare variable and placeholder for it, and on K8S\OpenShift side declare actual value which corresponds to this placeholder. For implementation details, see: K8S: https://kubernetes.io/docs/concepts/configuration/secret/ OpenShift: https://docs.openshift.com/container-platform/3.11/dev_guide/secrets.html

1

In additional to the popular K8s, jasypt or vault solutions, there's also Karmahostage. It enables you to do:

@EncryptedValue("${application.secret}")
private String application;

It works the same way jasypt does, but encryption happens on a dedicated saas solution, with a more fine-grained ACL model attached to it.

0

My solution for hiding a DB-Password in Spring Boot apps application.properties is implemented here.

Scenario: some fake password is read from application.properties on start and saved in global Spring object ConfigurableEnvironment. In runtime it will be programmatically replaced with a real DB password. The real password will be read from another config file saved in a safe, project-external place.

Don't forget: call the the Bean from main class with:

@Autowired
private SchedUtilility utl;

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.