apisix icon indicating copy to clipboard operation
apisix copied to clipboard

fix(stream): initialize env and secret for TLS cert resolution

Open suryaparua-official opened this issue 10 months ago • 11 comments

Description

This PR fixes an issue where $ENV:// and $secret:// certificate references were not resolved in Stream TLS mode.

While the same configuration works correctly in HTTP, Stream TLS was passing the raw reference strings directly to OpenSSL, which resulted in TLS handshake failures.

Root cause

The Stream lifecycle was missing initializations that already exist in HTTP: core.env.init() and apisix_secret.init_worker().

Because of this, environment variables and secrets were not available when certificates were loaded during the TLS handshake.

Fix

This change aligns Stream initialization with HTTP by initializing the environment and secret subsystems for Stream.

Notes

This change follows the same initialization flow already used in HTTP mode and addresses the missing setup in Stream TLS.

Related issue

Fixes #12934

suryaparua-official avatar Jan 23 '26 04:01 suryaparua-official

Hi @suryaparua-official, we need add test case for this fix

Baoyuantop avatar Jan 23 '26 09:01 Baoyuantop

I’ve added a Stream TLS test covering $ENV:// certificate resolution in t/stream-node/tls.t. Please let me know if any adjustments are needed.

suryaparua-official avatar Jan 23 '26 13:01 suryaparua-official

I’ve added test coverage for $secret:// certificate resolution as suggested, followed the pattern from t/node/ssl.t, and removed the END {} block per your recommendation. Please let me know if any further adjustments are needed.

suryaparua-official avatar Jan 26 '26 05:01 suryaparua-official

I’ve fixed the remaining issues. Please let me know if anything else is needed.

suryaparua-official avatar Jan 26 '26 09:01 suryaparua-official

Hi @suryaparua-official, please fix code lint error

Baoyuantop avatar Feb 13 '26 08:02 Baoyuantop

I’ve fixed the code lint related issues. Please let me know if anything else needs adjustment.

suryaparua-official avatar Feb 15 '26 04:02 suryaparua-official

Hi @suryaparua-official, please fix review comments + CI.

Baoyuantop avatar Mar 12 '26 07:03 Baoyuantop

Hi @moonming, thanks for the suggestion.

I set up a local environment (WSL + OpenResty + etcd + Vault dev server) and ran the stream TLS tests to validate the behavior.

From my testing:

  • apisix_secret.init_worker() only initializes the providers and does not perform any external network calls during worker startup.
  • Secret fetching is triggered lazily when referenced (observed during TLS handling rather than startup), so this change should not introduce startup latency.

The $ENV:// cases work as expected. For $secret://, the integration is triggered, but some test cases fail locally due to Vault access differences (403 on internal endpoints), so I could not fully validate the end-to-end secret fetch in this setup.

Based on the above, the initialization itself remains lightweight and should not impact startup latency.

Please let me know if you’d like me to align the Vault setup more closely with the CI environment or make further adjustments.

suryaparua-official avatar Mar 17 '26 00:03 suryaparua-official

Hi @Baoyuantop,

I’ve tried multiple approaches for the Vault test (using @file and inline content), but CI still fails with PEM parsing and TLS handshake errors.

It seems the issue might be related to when secrets are initialized in the stream lifecycle rather than the test itself.

Could you please guide if any additional initialization or ordering is required for stream TLS secret resolution?

Thanks!

suryaparua-official avatar Apr 02 '26 07:04 suryaparua-official

Hi @Baoyuantop, I've addressed all review comments and fixed the CI errors. Could you please approve the workflows to run CI?

suryaparua-official avatar May 12 '26 02:05 suryaparua-official

Hi @Baoyuantop, I have addressed all review comments including @nic-6443's suggestion — added ["/secrets"] = true to STREAM_ETCD_DIRECTORY in apisix/constants.lua and restored the Vault test cases. Could you please approve the workflows to run CI?

suryaparua-official avatar Jun 13 '26 06:06 suryaparua-official

Hi @suryaparua-official, please fix review comments + CI.

The secret part is a real gap — apisix_secret.init_worker() only runs in http_init_worker() today, so $secret:// certs never work in stream TLS. Fix looks right.

Three things:

Rebase. Master picked up #13595 after you branched, which already adds core.env.init() to stream_init_worker(). Yours is in stream_init(). Not a duplicate — the init-phase call captures OS-inherited env, the worker-phase one picks up env NAME=VALUE; directives (nginx replaces environ in the worker with only declared vars). HTTP does both, so after rebase stream matches.

CI. lint needs make reindex committed.

Test nits. add_block_preprocessor injecting GET /t everywhere isn't needed — TEST 7/11 are pure stream; put --- request GET /t on TEST 6/10 instead. set_env_from_file is defined twice, file-scope copy unused.

Worth a line in docs/en/latest/terminology/secret.md too.

AlinsRan avatar Jul 09 '26 06:07 AlinsRan

Hi @AlinsRan, the CI failures are:

t/admin/plugins-reload.t TEST 1 and TEST 2 — these are flaky timing-dependent tests unrelated to this PR t/node/... job — cancelled due to 1h 30m timeout, also unrelated

Could you please re-run the CI or confirm these are pre-existing flaky tests? Thank you!

suryaparua-official avatar Jul 15 '26 13:07 suryaparua-official