Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.apache.camel.component.hashicorp.vault.HashicorpVaultPropertiesFunction;
import org.apache.camel.spi.PropertiesFunction;
import org.apache.camel.spring.boot.AbstractEarlyResolutionPropertiesParser;
import org.apache.camel.support.service.ServiceHelper;
import org.apache.camel.util.ObjectHelper;
import org.apache.camel.vault.HashicorpVaultConfiguration;
import org.springframework.boot.context.properties.bind.Binder;
Expand All @@ -33,7 +32,8 @@
* Resolves the {@code {{hashicorp:...}}} placeholders of the Spring Boot properties before the CamelContext exists. It
* reads the secrets as the properties function of the CamelContext does, with the same settings: the
* {@code CAMEL_HASHICORP_VAULT_*} environment variables when one of them is set, else the {@code camel.vault.hashicorp}
* options. It logs in once, and revokes the token of its login once the properties are resolved.
* options. It logs in once, and revokes the token of its login once the properties are resolved: the base class stops
* the function, which closes its client.
*/
public class SpringBootHashicorpVaultPropertiesParser extends AbstractEarlyResolutionPropertiesParser {

Expand Down Expand Up @@ -80,13 +80,6 @@ protected PropertiesFunction createPropertiesFunction(ConfigurableEnvironment en
}
}

@Override
protected void release(PropertiesFunction propertiesFunction) {
// stopping the function closes its client: it stops renewing the token, and revokes the token of the login (but
// not a token given for the token login)
ServiceHelper.stopService(propertiesFunction);
}

private static void required(String value, String option, String message) {
if (ObjectHelper.isEmpty(value)) {
throw new RuntimeCamelException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.apache.camel.RuntimeCamelException;
import org.apache.camel.spi.PropertiesFunction;
import org.apache.camel.support.service.ServiceHelper;
import org.junit.jupiter.api.Test;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.StandardEnvironment;
Expand Down Expand Up @@ -156,8 +157,9 @@ void anAppRoleLoginNeedsNoToken() {
try {
assertEquals("hashicorp", function.getName());
} finally {
// the client logs in at its first request, so there is no token to revoke
parser.release(function);
// stopping the function closes its client, which logs in at its first request: so there is no token to
// revoke
ServiceHelper.stopService(function);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Properties;
import org.apache.camel.RuntimeCamelException;
import org.apache.camel.spi.PropertiesFunction;
import org.apache.camel.support.service.ServiceHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
Expand Down Expand Up @@ -76,10 +77,15 @@ public abstract class AbstractEarlyResolutionPropertiesParser
protected abstract PropertiesFunction createPropertiesFunction(ConfigurableEnvironment environment);

/**
* Releases what the properties function holds, such as the login of its client, once the properties are resolved,
* also when they could not be. A failure to release is logged and ignored. Does nothing by default.
* Releases what the properties function holds, such as its client, once the properties are resolved, also when
* they could not be. A failure to release is logged and ignored.
* <p/>
* By default, stops the function when it is a Camel {@code Service}: the properties functions of the Camel vaults
* close their client when they stop, also when they were never started. A parser whose function is shared with
* something else can override this to do nothing.
*/
protected void release(PropertiesFunction propertiesFunction) {
ServiceHelper.stopService(propertiesFunction);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Set;
import org.apache.camel.RuntimeCamelException;
import org.apache.camel.spi.PropertiesFunction;
import org.apache.camel.support.service.ServiceSupport;
import org.junit.jupiter.api.Test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.bootstrap.DefaultBootstrapContext;
Expand Down Expand Up @@ -88,6 +89,63 @@ public String apply(String remainder) {
}
}

/**
* A properties function that is a Camel service, as the functions of the Camel vaults are, and counts how often it
* was stopped.
*/
private static final class StubServiceFunction extends ServiceSupport implements PropertiesFunction {
private final Map<String, String> values;
private int stops;

StubServiceFunction(Map<String, String> values) {
this.values = values;
}

@Override
public String getName() {
return "test";
}

@Override
public String apply(String remainder) {
if (!values.containsKey(remainder)) {
throw new IllegalStateException("cannot resolve " + remainder);
}
return values.get(remainder);
}

@Override
protected void doStop() {
stops++;
}
}

/**
* A parser that keeps the default release of the properties function.
*/
private static final class DefaultReleaseParser extends AbstractEarlyResolutionPropertiesParser {
private final PropertiesFunction function;

DefaultReleaseParser(PropertiesFunction function) {
this.function = function;
}

@Override
protected String getEarlyResolutionProperty() {
return GUARD;
}

@Override
protected String getOverridePropertySourceName() {
return OVERRIDE_SOURCE;
}

@Override
protected PropertiesFunction createPropertiesFunction(ConfigurableEnvironment environment) {
return function;
}
}

private static final class TestParser extends AbstractEarlyResolutionPropertiesParser {
private final StubPropertiesFunction function;
private int factoryCalls;
Expand Down Expand Up @@ -202,6 +260,39 @@ void aFailingReleaseDoesNotAbortStartup() {
assertEquals(1, parser.releaseCalls);
}

@Test
void stopsAPropertiesFunctionThatIsAServiceByDefault() {
RecordingEnvironment environment = environmentWith(Map.of(GUARD, "true", "my.secret", "{{test:db}}"));
StubServiceFunction function = new StubServiceFunction(Map.of("db", "pazzword"));

new DefaultReleaseParser(function).onApplicationEvent(eventFor(environment));

assertEquals("pazzword", environment.getProperty("my.secret"));
assertEquals(1, function.stops, "stopping the function closes what it holds, such as the client of a vault");
}

@Test
void stopsAPropertiesFunctionThatIsAServiceByDefaultAlsoWhenResolutionFails() {
RecordingEnvironment environment = environmentWith(Map.of(GUARD, "true", "my.secret", "{{test:missing}}"));
StubServiceFunction function = new StubServiceFunction(Map.of());
DefaultReleaseParser parser = new DefaultReleaseParser(function);

assertThrows(RuntimeCamelException.class, () -> parser.onApplicationEvent(eventFor(environment)));

assertEquals(1, function.stops, "the function is stopped also when startup is aborted");
}

@Test
void releasesAPropertiesFunctionThatIsNotAServiceByDefault() {
RecordingEnvironment environment = environmentWith(Map.of(GUARD, "true", "my.secret", "{{test:db}}"));
DefaultReleaseParser parser
= new DefaultReleaseParser(new StubPropertiesFunction(Map.of("db", "pazzword"), Set.of()));

parser.onApplicationEvent(eventFor(environment));

assertEquals("pazzword", environment.getProperty("my.secret"));
}

@Test
public void guardEnabledBuildsTheClientAndAddsTheOverrideSource() {
RecordingEnvironment environment = environmentWith(Map.of(GUARD, "true"));
Expand Down
Loading