phax / phase4

phase4 - AS4 client and server for integration into existing systems. Specific support for Peppol and CEF eDelivery built-in.
Apache License 2.0
147 stars 48 forks source link

Adding support for Spring profiles in Spring Phase4 #191

Closed tonytram closed 9 months ago

tonytram commented 10 months ago

Dear Philip,

First of all I would like to say thank you for your amazing effort on Phase4. Currently, our company is using Phase4 as our Peppol solution. I'm the man who is in charge of the implementing of the integration. I would like to use Spring profiles for our environments when deploying to production or other systems for testing. I do see that in the core of Phase4 you are using, all configured values are getting through AS4Configuration.getConfig().getConfiguredValue("$name"), and this will return a value of the property whose name matches in the application.properties file. My question is that is there anyway to combine getting values from AS4Configuration and Spring profiles (through @Value($name))

Thank you so much for your help.

phax commented 10 months ago

@tonytram Thanks for your feedback. phase4 is designed to be independent of specific frameworks like SpringBoot or any other framework. That guarantees its usability in different environments like AWS Lambda etc. That also means, that phase4 does not use the SpringBoot configuration property management.

What you are trying to achieve can also be achieved with "phase4 native" :) phase4 configuration is based on https://github.com/phax/ph-commons/wiki/ph-config and so provides you with different ways to provide your configuration items. You can provide them via Java System Properties, Environment Variables or via properties in a file (see https://github.com/phax/phase4/wiki/Configuration for how ph-config is used in phase4).

So you may want use the configuration property config.file to define a different file containing the phase4 specific properties.

tonytram commented 10 months ago

Thank you for your reply. Actually I have read the Wiki and the source code, but I don't have any ideas to implement it (I totally newbie in Java :) ). So example code how can archive it will be great help. Thank you so much

tonytram commented 10 months ago

Dear @phax , I also want to change the certificate information according to environments as well. Might I consult you in which place that is safe to override the certification information?

phax commented 10 months ago

If you are using phase4 with SpringBoot I suugest you separate all the phase4 specific properties into a separate file called phase4.properties and leave the SpringBoot properties in application.properties - both in the same folder. For development you create a file phase4.dev.properties and set the environment variable CONFIG_FILE in your IDE (refer to the respecitve manual of the IDE how to) to the value phase4.dev.properties (you may also use absolute paths there)

And yes, by having separate configuration files, you can also refer to different key stores and trust stores

tonytram commented 10 months ago

Thank you so much @phax. To set the configuration file using CONFIG_FILE is working for me. Might there be a better way which sticks to Spring profiles rather than this approach?

phax commented 9 months ago

The answer is a clear: that depends 😆

What elements do you want to customize? Is it only the key store and trust store or is it something else as well. The cryptographic parameters are provided via implementations of the IAS4CryptoFactory interface. The default implementation AS4CryptoFactoryProperties simply reads all the properties from an instance of AS4CryptoProperties which in turn is filled from the configuration sources. By manually instantiating AS4CryptoProperties and filling the properties via SpringBoot properties (and then creating "your" instance of AS4CryptoFactoryProperties with it and using it) you can achieve the same result.

See e.g. https://github.com/phax/phase4/blob/master/phase4-peppol-client/src/test/java/com/helger/phase4/peppol/receivers/MainPhase4PeppolSenderExplicitCryptoProperties.java#L59 for an example

If you need more elements to be customized, let me know

tonytram commented 9 months ago

The example is really helpful for me to start with my wrapper. Thank you so much and really appreciated your help @phax