|
| 1 | +package eu.europa.ec.dgc.businessrule.config.btp; |
| 2 | + |
| 3 | +import io.pivotal.cfenv.core.CfCredentials; |
| 4 | +import io.pivotal.cfenv.core.CfService; |
| 5 | +import io.pivotal.cfenv.spring.boot.CfEnvProcessor; |
| 6 | +import io.pivotal.cfenv.spring.boot.CfEnvProcessorProperties; |
| 7 | +import java.util.Map; |
| 8 | + |
| 9 | +/** |
| 10 | + * Custom implementation of {@link CfEnvProcessor} for reading the SAP credential store parameters from the <code> |
| 11 | + * VCAP_SERVICES</code> environment variable and making them available as properties in the spring context. |
| 12 | + * <br/><br/> |
| 13 | + * The following properties are available in the context after the processor is done: |
| 14 | + * <code> |
| 15 | + * <ul> |
| 16 | + * <li>sap.btp.credstore.url</li> |
| 17 | + * <li>sap.btp.credstore.password</li> |
| 18 | + * <li>sap.btp.credstore.username</li> |
| 19 | + * <li>sap.btp.credstore.clientPrivateKey</li> |
| 20 | + * <li>sap.btp.credstore.serverPublicKey</li> |
| 21 | + * </ul> |
| 22 | + * </code> |
| 23 | + * |
| 24 | + * @see CfEnvProcessor |
| 25 | + */ |
| 26 | +public class SapCredentialStoreCfEnvProcessor implements CfEnvProcessor { |
| 27 | + |
| 28 | + private static final String CRED_STORE_SCHEME = "credstore"; |
| 29 | + private static final String CRED_STORE_PROPERTY_PREFIX = "sap.btp.credstore"; |
| 30 | + |
| 31 | + @Override |
| 32 | + public boolean accept(CfService service) { |
| 33 | + return service.existsByTagIgnoreCase(CRED_STORE_SCHEME, "securestore", "keystore", "credentials") |
| 34 | + || service.existsByLabelStartsWith(CRED_STORE_SCHEME) |
| 35 | + || service.existsByUriSchemeStartsWith(CRED_STORE_SCHEME); |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public void process(CfCredentials cfCredentials, Map<String, Object> properties) { |
| 40 | + properties.put(CRED_STORE_PROPERTY_PREFIX + ".url", cfCredentials.getString("url")); |
| 41 | + properties.put(CRED_STORE_PROPERTY_PREFIX + ".password", cfCredentials.getString("password")); |
| 42 | + properties.put(CRED_STORE_PROPERTY_PREFIX + ".username", cfCredentials.getString("username")); |
| 43 | + |
| 44 | + @SuppressWarnings("unchecked") |
| 45 | + Map<String, Object> encryption = (Map<String, Object>) cfCredentials.getMap().get("encryption"); |
| 46 | + if (encryption == null) { |
| 47 | + // Encryption features have been disabled on this BTP instance. |
| 48 | + properties.put(CRED_STORE_PROPERTY_PREFIX + ".clientPrivateKey", "encryption-disabled"); |
| 49 | + properties.put(CRED_STORE_PROPERTY_PREFIX + ".serverPublicKey", "encryption-disabled"); |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + String clientPrivateKey = encryption.get("client_private_key").toString(); |
| 54 | + String serverPublicKey = encryption.get("server_public_key").toString(); |
| 55 | + |
| 56 | + properties.put(CRED_STORE_PROPERTY_PREFIX + ".clientPrivateKey", clientPrivateKey); |
| 57 | + properties.put(CRED_STORE_PROPERTY_PREFIX + ".serverPublicKey", serverPublicKey); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public CfEnvProcessorProperties getProperties() { |
| 62 | + return CfEnvProcessorProperties.builder() |
| 63 | + .propertyPrefixes(CRED_STORE_PROPERTY_PREFIX) |
| 64 | + .serviceName("CredentialStore") |
| 65 | + .build(); |
| 66 | + } |
| 67 | + |
| 68 | +} |
0 commit comments