Skip to content

refactor(api,executor): configure Redis through Spring Boot auto-configuration - #4055

Open
Macbet wants to merge 6 commits into
refactor/quartz-boot-autoconfigfrom
refactor/redis-boot-autoconfig
Open

Macbet wants to merge 6 commits into
refactor/quartz-boot-autoconfigfrom
refactor/redis-boot-autoconfig

Conversation

@Macbet

@Macbet Macbet commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

What changed and why?

Part 2 of #4052. Stacked on the Quartz pull request.

The API and the executor each built the Jedis connection factory, the trust store and the standalone / sentinel / cluster selection by hand, in two near-identical classes. Boot now builds the connection from spring.data.redis.* and an SSL bundle.

An EnvironmentPostProcessor per module maps the existing io.terrakube.*.redis.* properties, and the TerrakubeRedis* variables behind them, onto the Boot keys, so operator configuration is unchanged. Placeholders alone cannot do this: an empty sentinel or cluster key already switches Boot into that mode.

Boot passes empty SSLParameters to Jedis, which turns off the hostname check Jedis applies by default. A customizer sets endpoint identification to HTTPS again whenever Redis TLS is on.

Behaviour changes:

  • A configured trust store that cannot be read, or has a wrong password, stops startup. It used to be logged and replaced by an empty store.
  • ssl=true without a trust store path uses the JVM default trust store.
  • A sentinel or cluster node that is not host:port stops startup with a message naming the property, instead of being skipped.
  • spring.data.redis.url, .sentinel.*, .cluster.* or .masterreplica.* set directly stops startup. Redis is configured only through the Terrakube properties.
  • The connection factory bean is Boot's. It is still a JedisConnectionFactory.

Not covered: sentinel and cluster over TLS are checked at configuration level only, and the executor has no live TLS handshake test.

Testing

Ran: mvn -pl api test -> 3582 tests, 0 failures. mvn -pl executor test -> 910 tests, 0 failures, 1 skipped (Linux-only PID isolation test, skipped before too).
Checked: Testcontainers Redis with and without a password; a TLS-only Redis with a certificate for localhost accepts a connection to localhost and rejects the same server addressed as 127.0.0.1; values and stream entries written by a template built the old way are readable by the new one.

@Macbet
Macbet added this pull request to stack #4056 November 29, 2026 00:10
@github-actions github-actions Bot added terrakube-api terrakube-executor author/contributor Authored by a returning contributor size/XL 1000 or more lines changed, consider splitting type/refactor Refactor, performance or style change labels Nov 29, 2026
@Macbet
Macbet marked this pull request as ready for review November 29, 2026 00:46
@github-actions github-actions Bot added documentation Improvements or additions to documentation dependencies Pull requests that update a dependency file area/ci Touches CI, containers or dev environment labels Nov 29, 2026
@Macbet
Macbet force-pushed the refactor/redis-boot-autoconfig branch from bf9f9ff to 8f2bd3d Compare November 29, 2026 10:02
Macbet added a commit that referenced this pull request Nov 29, 2026
…st-processor

Addresses Sonar on #4055: cognitive complexity of postProcessEnvironment and nodes (java:S3776),
an unnamed catch variable (java:S7467), and Thread.sleep polling in the cluster test (java:S2925),
now an Awaitility wait.
@Macbet
Macbet force-pushed the refactor/redis-boot-autoconfig branch from 4002992 to 381d8c8 Compare November 29, 2026 10:32
Macbet added a commit that referenced this pull request Nov 29, 2026
…st-processor

Addresses Sonar on #4055: cognitive complexity of postProcessEnvironment and nodes (java:S3776),
an unnamed catch variable (java:S7467), and Thread.sleep polling in the cluster test (java:S2925),
now an Awaitility wait.
@Macbet
Macbet force-pushed the refactor/redis-boot-autoconfig branch from 381d8c8 to d459148 Compare November 29, 2026 12:58
Macbet added a commit that referenced this pull request Nov 29, 2026
…st-processor

Addresses Sonar on #4055: cognitive complexity of postProcessEnvironment and nodes (java:S3776),
an unnamed catch variable (java:S7467), and Thread.sleep polling in the cluster test (java:S2925),
now an Awaitility wait.
@alfespa17 alfespa17 self-assigned this Nov 29, 2026
@alfespa17

Copy link
Copy Markdown
Member

Thanks for this refactor — moving both api and executor onto Spring Boot's native Redis auto-configuration and SSL bundles removes a lot of duplicated wiring, and the attention to detail here is really strong:

  • Catching Spring Boot's empty SSLParameters disabling Jedis 8's default HTTPS hostname verification and restoring it via RedisConfigurationSupport#redisHostnameVerification (with a live TLS handshake test).
  • Fixing the out-of-the-box Redis Cluster crash when TerrakubeRedisClusterMaxRedirects=0 (default in application.properties).
  • Supporting TerrakubeRedisSSL=true without a custom truststore path (falling back to the JVM default cacerts), so operators on managed Redis services no longer have to hardcode the Paketo JRE cacerts path.
  • Keeping passwords as ${...} placeholders so secrets aren't duplicated in the Environment and still resolve after ExecutorSecretEnvironmentPostProcessor loads ephemeral secret files, plus redacting node values in validation errors.
  • Verifying rolling-upgrade JDK serialization compatibility against the legacy template.

I found a few compatibility issues and edge cases that would be good to address before merging:


Findings to Address

1. Relaxed property binding regression on truststorePassword

In common/src/main/java/io/terrakube/common/redis/RedisEnvironmentPostProcessorSupport.java:

String truststorePath = binder.bind(prefix + ".truststore-path", String.class).orElse(null);
...
redis.put(bundle + "password", "${" + prefix + ".truststorePassword:}");

truststorePath is read through Binder (which supports relaxed binding: truststorePath, truststore-path, IO_TERRAKUBE_*_REDIS_TRUSTSTORE_PATH), whereas truststorePassword is emitted as a literal camelCase ${...} placeholder. Spring's placeholder resolver does exact key lookup, not relaxed binding.

Previously, StreamingProperties and RedisProperties used @ConfigurationProperties, so io.terrakube.api.redis.truststore-password=..., io.terrakube.executor.redis.truststore-password=..., and IO_TERRAKUBE_*_REDIS_TRUSTSTORE_PASSWORD=... bound cleanly. With this change, those forms will be silently ignored (resolving to the empty default in application.properties), causing JksSslStoreBundle to fail startup with UnrecoverableKeyException.

Suggestion: Keep placeholder indirection for io.terrakube.executor.redis.truststorePassword (so ExecutorSecretEnvironmentPostProcessor continues to work), while also honoring kebab-case / relaxed keys if set (for example, checking if truststore-password / IO_TERRAKUBE_*_REDIS_TRUSTSTORE_PASSWORD is present in the environment and emitting the matching placeholder key).


2. spring.data.redis.host and spring.data.redis.port are mapped unconditionally in Cluster and Sentinel modes

In RedisEnvironmentPostProcessorSupport#postProcessEnvironment:

redis.put("spring.data.redis.host", "${" + prefix + ".hostname}");
redis.put("spring.data.redis.port", "${" + prefix + ".port}");

Previously, StreamingProperties#port and RedisProperties#port were boxed Integer fields, and getStandaloneConfiguration() was never called when Cluster or Sentinel mode was active — so a Cluster/Sentinel deployment with TerrakubeRedisPort="" bound to null and started cleanly.

Now DataRedisProperties#port is a primitive int, and it is bound unconditionally regardless of mode. If an operator configures Cluster or Sentinel and leaves TerrakubeRedisPort="" (or unset), binding spring.data.redis.port will fail at startup (this also surfaced in ExecutorSecretEnvironmentPostProcessorTest, which had to add a dummy io.terrakube.executor.redis.port=6379).

Suggestion: Call putMode(binder, redis) first, and only emit spring.data.redis.host and spring.data.redis.port when "standalone".equals(mode). In Cluster and Sentinel modes, DataRedisProperties can keep its unused defaults (localhost:6379).


3. Inactive sentinel.nodes is validated eagerly even when Cluster or Standalone mode is active

In RedisEnvironmentPostProcessorSupport#putMode:

List<String> clusterNodes = nodes(binder, "cluster.nodes", "TerrakubeRedisClusterNodes", true);
List<String> sentinelNodes = nodes(binder, "sentinel.nodes", "TerrakubeRedisSentinelNodes", false);
String sentinelMaster = binder.bind(prefix + ".sentinel.master", String.class).orElse(null);

Because sentinelNodes is parsed before checking !clusterNodes.isEmpty() or StringUtils.hasText(sentinelMaster), a deployment running in Cluster mode (or Standalone mode with an empty sentinel.master) that has a stale/placeholder value in TerrakubeRedisSentinelNodes will now fail startup even though Sentinel mode is inactive.

Suggestion: Return early when !clusterNodes.isEmpty(), and only parse/validate sentinel.nodes when StringUtils.hasText(sentinelMaster) is true.


4. Missing executor secret-file tests for TerrakubeRedisTruststorePassword and TerrakubeRedisSentinelPassword

InternalSecretLoader loads three Redis secrets into terrakubeEphemeralSecrets:

  • TerrakubeRedisPassword
  • TerrakubeRedisTruststorePassword
  • TerrakubeRedisSentinelPassword

By inspection all three should resolve cleanly through the placeholders, but RedisEnvironmentPostProcessorTest#passwordFromSecretFileResolvesWhicheverPostProcessorRunsFirst in executor only covers TerrakubeRedisPassword. Adding coverage for TerrakubeRedisTruststorePassword (binding into spring.ssl.bundle.jks.terrakube-redis.truststore.password) and TerrakubeRedisSentinelPassword (in Sentinel mode) would lock down all three secret-file paths.


Minor Edge Cases & Compatibility Notes

  • Whitespace-only username: Previously StreamingConfiguration checked StringUtils.hasText(username), ignoring whitespace-only values. Spring Data Redis's JedisConnectionFactory checks !ObjectUtils.isEmpty(username), so a whitespace-only value (e.g., from a Kubernetes Secret) would now be passed as an ACL username. (RedisPassword.of(...) already uses StringUtils.hasText, so passwords are unaffected.)
  • Sentinel / Cluster over TLS: As noted in the PR description, Sentinel + TLS and Cluster + TLS are only tested at the configuration level. In Sentinel + TLS mode, JedisConnectionFactory applies the SSL socket factory and HTTPS hostname verification to both Sentinel connections and the discovered master; in Cluster + TLS mode, it applies to every node returned by CLUSTER SLOTS. This matches Jedis 8.0.2's previous default, though deployments announcing raw IPs without IP SANs on their certificates should be aware.
  • Empty TerrakubeRedisTruststorePassword with JKS: Because truststore.password defaults to "" (not null), JksSslStoreBundle passes new char[0] rather than null to KeyStore.load(...), so an integrity check is always performed (matching the previous toCharArray() behavior).
  • Removed/renamed beans: StreamingProperties, RedisProperties, and the global SSLSocketFactory bean are removed (verified unused elsewhere in api and executor), and the JedisConnectionFactory bean name changes from jedisConnectionFactory to redisConnectionFactoryVirtualThreads (api) / redisConnectionFactory (executor).
  • Release notes / operator impact: Worth calling out in the release notes that:
    1. Unreadable truststores / wrong truststore passwords and malformed Sentinel/Cluster nodes now fail startup fast.
    2. Any pre-existing spring.data.redis.{url,sentinel,cluster,masterreplica} keys (including empty env vars like SPRING_DATA_REDIS_URL="") now fail startup.
    3. Single-module Maven builds/tests for api and executor now require -am (or a prior mvn install) due to the new common module.

Macbet added a commit that referenced this pull request Nov 29, 2026
Addresses review feedback on #4055:
- resolve the truststore password through a kebab-case placeholder, so
  truststore-password and *_REDIS_TRUSTSTORE_PASSWORD bind again, with
  the camelCase name as fallback for the executor secret files
- map spring.data.redis.host/port only in standalone mode, so cluster and
  sentinel deployments start with a blank TerrakubeRedisPort
- parse sentinel nodes only when a sentinel master is set, and never
  when cluster mode wins
- send no username when it is blank, as the hand-written factories did
- cover the truststore and sentinel password secret files in the executor
@Macbet

Macbet commented Nov 29, 2026

Copy link
Copy Markdown
Contributor Author

@alfespa17 thanks for the thorough review — addressed in ed33359:

  1. Relaxed truststorePassword binding — the bundle password is now ${<prefix>.truststore-password:${<prefix>.truststorePassword:}}. The kebab-case placeholder goes through Boot's relaxed lookup (truststore-password, truststorePassword, *_REDIS_TRUSTSTORE_PASSWORD); the camelCase fallback keeps the executor secret files working without the attached property source.
  2. host/port in Cluster/Sentinel — spring.data.redis.host/port are only emitted in standalone mode, so a blank TerrakubeRedisPort no longer breaks Cluster or Sentinel startup.
  3. Eager sentinel.nodes validation — Cluster returns first; sentinel.nodes is only parsed when sentinel.master is set.
  4. Secret-file coverage — added executor tests for TerrakubeRedisTruststorePassword (into the JKS bundle password) and TerrakubeRedisSentinelPassword (Sentinel mode, no port).

Also fixed the whitespace-only username regression: a blank username is mapped to empty, so no ACL username is sent, as before.

The remaining notes (Sentinel/Cluster TLS hostname verification, bean renames, empty JKS password integrity check, release-notes items) are behavior we're keeping as-is; I'll carry the operator-impact points into the release notes.

…iguration

The API and the executor each built the Jedis connection factory, the
trust store and the standalone/sentinel/cluster selection by hand. Boot
now builds the connection from spring.data.redis.* and an SSL bundle.

An EnvironmentPostProcessor per module maps the existing
io.terrakube.*.redis.* properties (and the TerrakubeRedis* variables
behind them) onto the Boot keys, so operator configuration is unchanged.
Placeholders alone cannot do this: an empty sentinel or cluster key
already switches Boot into that mode.

Boot passes empty SSLParameters to Jedis, which turns off the hostname
check Jedis applies by default, so a customizer sets endpoint
identification to HTTPS again whenever Redis TLS is on.

Behaviour changes:
- A configured trust store that cannot be read, or has a wrong password,
  stops startup. It used to be logged and replaced by an empty store.
- ssl=true without a trust store path uses the JVM default trust store.
- A sentinel or cluster node that is not host:port stops startup with a
  message naming the property, instead of being skipped.
- spring.data.redis.url, .sentinel.*, .cluster.* or .masterreplica.* set
  directly stops startup: Redis is configured only through the Terrakube
  properties.
- The connection factory bean is Boot's (still a JedisConnectionFactory).

Refs #4052
…-redirects 0

Cluster nodes are parsed with Spring Data's RedisNode again, so a node
without a port (default 6379) and bracketed IPv6 work as they did before
the move to Boot auto-configuration.

A cluster max-redirects of 0 or less is treated as not set. Jedis
rejects it ("Max attempts must be positive for cluster mode"), so
cluster mode could not start with the shipped default; a Testcontainers
test now starts a cluster with that default.

Node entries that look like URLs or have a port outside 1..65535 are
rejected, and the error names the property and the position without
echoing the entry, which could contain a password.

Refs #4052
The API and the executor carried near-identical copies of the Redis
environment post-processor and connection configuration, because the
modules had no shared code. A new library module, common, holds one
implementation; each application keeps a thin subclass with its property
prefix, pool mapping and order, and its own spring.factories entry.

Builds that package a single module now pass -am so common is built
first (release workflows, terrakubeBuild.sh, skill docs). The coverage
aggregate and PR labels include the new module. The registry does not
depend on it.

Refs #4052
The API jar already ships a third-party common-0.13.0.jar, so the shared
module's artifact is renamed to terrakube-common. jedis.version moves to
the root pom so the module is always tested against the Jedis the
applications ship.

The shared Redis classes are abstract, so the shared configuration
cannot be registered next to an application's subclass. The remaining
single-module Maven commands in the skill docs and two javadocs now use
-am.

Refs #4052
…st-processor

Addresses Sonar on #4055: cognitive complexity of postProcessEnvironment and nodes (java:S3776),
an unnamed catch variable (java:S7467), and Thread.sleep polling in the cluster test (java:S2925),
now an Awaitility wait.
Addresses review feedback on #4055:
- resolve the truststore password through a kebab-case placeholder, so
  truststore-password and *_REDIS_TRUSTSTORE_PASSWORD bind again, with
  the camelCase name as fallback for the executor secret files
- map spring.data.redis.host/port only in standalone mode, so cluster and
  sentinel deployments start with a blank TerrakubeRedisPort
- parse sentinel nodes only when a sentinel master is set, and never
  when cluster mode wins
- send no username when it is blank, as the hand-written factories did
- cover the truststore and sentinel password secret files in the executor
@Macbet
Macbet force-pushed the refactor/redis-boot-autoconfig branch from ed33359 to dfe552a Compare November 29, 2026 19:34
@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/ci Touches CI, containers or dev environment author/contributor Authored by a returning contributor dependencies Pull requests that update a dependency file documentation Improvements or additions to documentation size/XL 1000 or more lines changed, consider splitting terrakube-api terrakube-executor type/refactor Refactor, performance or style change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants