5

I'm wondering if, in a Hybris properties file, there is a way to reference environment variables.

For example, in local.properties: my.property = ${MY_ENVIRONMENT_VARIABLE}

My searching has concluded that this is possible in Spring, but doesn't appear to work in Hybris.

Any thoughts would be appreciated.

rohrlach
  • 53
  • 1
  • 4

3 Answers3

3

It's possible to access to environment variables within hybris. A partner of my company found out this deep down in hybris documentation:

Platform allows you to specify properties also as environment variables

# security: make the platform *abort startup* if no one overrides the settings below
db.url=<CHANGE_ME>
db.username=<CHANGE_ME>
db.password=<CHANGE_ME>
# security: end

The special property value has been introduced exactly for such use cases where administrators want to ensure that: a) files do not contain sensible settings, and b) the system doesn't start up if those settings haven't been overridden.

On the server, those settings are to be exposed as environment variables just before starting Platform:

 foo$ export y_db_url=jdbc:mysql://my.secret-db-host.com/AlfavaMetraxis?useConfigs=maxPerformance&characterEncoding=utf8
 foo$ export y_db_username=Amy
 foo$ export y_db_password=Rory
 foo$ ./hybrisserver.sh start
...

As you can see, settings need to be prefixed and escaped in order to work as environment variable.

https://help.sap.com/viewer/a74589c3a81a4a95bf51d87258c0ab15/1905/en-US/8b8e13c9866910149d40b151a9196543.html?q=CHANGE_ME

Hope this is useful for you.

cbelizon
  • 234
  • 2
  • 13
1

I found this related to your question on Hybris Experts. Hope it helps.

It is not possible to access shell environment variables inside local.properties file. As a workaround you can do echo "my.property = $MY_ENVIRONMENT_VARIABLE" >> local.properties before starting platform.

Sanchit Khera
  • 1,005
  • 1
  • 16
  • 47
0

No it's not possible.

Actually hybris use org.apache.commons.configuration package. In the documentation you'll find that it's possible to use environment variable.

user.file = ${sys:user.home}/settings.xml
action.key = ${const:java.awt.event.KeyEvent.VK_CANCEL}
java.home = ${env:JAVA_HOME}

Unfortunately hybris has done something I can't explain, they have overidded the default implementation and removed all interpolation features.

If we analyze the issue further, the configuration class used is called HybrisConfiguration. This class extends AbstractConfiguration from Apache Commons Configuration. The getProperty method use an other interface called ConfigIntf. The implementation is found in the class AbstractConfig. There every getString getInteger, etc... methods are overrided.

For example, for String, the method does not call the interpolate method but instead you'll find a really simple...

StringUtils.isEmpty(value) ? def : value;

So if you want to use all features of Apache API, then try to replace hybris implementation... However I think that it won't be so easy to do that without modifying the platform since I don't see any beans there that could be injected.

alain.janinm
  • 19,951
  • 10
  • 65
  • 112
  • 1
    Thanks @alain.janinm, I actually found that you can add environment-like variables in the same file as HYBRIS_DIR is defined... But this doesn't help if you want to say, move database passwords out of a version controlled properties file as they aren't available until AFTER the system is running. – rohrlach Feb 02 '17 at 13:31
  • Correct solution is the one I've posted – cbelizon Feb 24 '20 at 13:46