public void onApplicationEvent()

in components-starter/camel-ibm-secrets-manager-starter/src/main/java/org/apache/camel/component/ibm/secrets/manager/springboot/IBMSecretsManagerVaultPropertiesParser.java [24:73]


    public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
        SecretsManager client;
        ConfigurableEnvironment environment = event.getEnvironment();
        String token;
        String serviceUrl;
        if (Boolean.parseBoolean(environment.getProperty("camel.component.ibm-secrets-manager.early-resolve-properties"))) {
            token = environment.getProperty("camel.vault.ibm.token");
            serviceUrl = environment.getProperty("camel.vault.ibm.serviceUrl");
            if (ObjectHelper.isNotEmpty(token) && ObjectHelper.isNotEmpty(serviceUrl)) {
                IamAuthenticator iamAuthenticator = new IamAuthenticator.Builder()
                        .apikey(token)
                        .build();
                client = new SecretsManager("Camel Secrets Manager Service for Properties", iamAuthenticator);
                client.setServiceUrl(serviceUrl);
            } else {
                throw new RuntimeCamelException(
                        "Using the IBM Secrets Manager Properties Function requires setting IBM Credentials and service url as application properties or environment variables");
            }
            IBMSecretsManagerPropertiesFunction secretsManagerPropertiesFunction = new IBMSecretsManagerPropertiesFunction(client);
            final Properties props = new Properties();
            for (PropertySource mutablePropertySources : event.getEnvironment().getPropertySources()) {
                if (mutablePropertySources instanceof MapPropertySource mapPropertySource) {
                    mapPropertySource.getSource().forEach((key, value) -> {
                        String stringValue = null;
                        if ((value instanceof OriginTrackedValue originTrackedValue &&
                                originTrackedValue.getValue() instanceof String v)) {
                            stringValue = v;
                        } else if (value instanceof String v) {
                            stringValue = v;
                        }
                        if (stringValue != null &&
                                stringValue.startsWith("{{ibm:") &&
                                stringValue.endsWith("}}")) {
                            LOG.debug("decrypting and overriding property {}", key);
                            try {
                                String element = secretsManagerPropertiesFunction.apply(stringValue
                                        .replace("{{ibm:", "")
                                        .replace("}}", ""));
                                props.put(key, element);
                            } catch (Exception e) {
                                // Log and do nothing
                                LOG.debug("failed to parse property {}. This exception is ignored.", key, e);
                            }
                        }
                    });
                }
            }
            environment.getPropertySources().addFirst(new PropertiesPropertySource("overridden-ibm-secrets-manager-properties", props));
        }
    }