ナレッジベース
公開されました Sep. 10, 2025

How to use environment variables for passwords within OSGi config files

written-by

Sorin Pop

How To articles are not official guidelines or officially supported documentation. They are community-contributed content and may not always reflect the latest updates to Liferay DXP. We welcome your feedback to improve How To articles!

While we make every effort to ensure this Knowledge Base is accurate, it may not always reflect the most recent updates or official guidelines.We appreciate your understanding and encourage you to reach out with any feedback or concerns.

Issue

  • For security reasons, we would like to store passwords using environment variables within OSGi .config files. For example, to store the Elasticsearch server's password.

Environment

  • 7.3+

Resolution

  • In Liferay versions containing LPS-123057, it is possible to use environment variables in OSGi configuration files. To do so, use the syntax ${env:VARIABLE_NAME}. This is the recommended approach for handling sensitive information like passwords.
  • For example, to configure your Elasticsearch connection password using an environment variable: 
    1. Create or edit the OSGi configuration file for the Elasticsearch connector. For Liferay versions using Elasticsearch 8, this would be named com.liferay.portal.search.elasticsearch8.configuration.ElasticsearchConfiguration.config
    2. In this file, set the password property as follows:
      authenticationEnabled=B"true"
      username="elastic"
      password="${env:ELASTIC_PASSWORD}"
      
    3. Then, ensure the ELASTIC_PASSWORD environment variable is set in the environment where your Liferay DXP instance is running. For example:
      export ELASTIC_PASSWORD=your_secret_password

The Liferay OSGi runtime will automatically substitute the placeholder with the value of the environment variable upon startup.

You can also provide a default value in case the environment variable is not set:

authenticationEnabled=B"true"
username="elastic"
password="${env:ELASTIC_PASSWORD}"
password="${env:ELASTIC_PASSWORD:defaultPassword123}"

Additional Information

did-this-article-resolve-your-issue

ナレッジベース