Skip to content

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

Open
suryaparua-official wants to merge 5 commits intoapache:masterfrom
suryaparua-official:fix-stream-env-secret-init
Open

fix(stream): initialize env and secret for TLS cert resolution#12935
suryaparua-official wants to merge 5 commits intoapache:masterfrom
suryaparua-official:fix-stream-env-secret-init

Conversation

@suryaparua-official
Copy link

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

@dosubot dosubot bot added size:XS This PR changes 0-9 lines, ignoring generated files. bug Something isn't working labels Jan 23, 2026
@Baoyuantop
Copy link
Contributor

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

@dosubot dosubot bot added size:M This PR changes 30-99 lines, ignoring generated files. and removed size:XS This PR changes 0-9 lines, ignoring generated files. labels Jan 23, 2026
@suryaparua-official
Copy link
Author

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.

Comment on lines +28 to +33
BEGIN {
use t::APISIX;

$ENV{APISIX_STREAM_ENV_CERT} = t::APISIX::read_file("t/certs/apisix.crt");
$ENV{APISIX_STREAM_ENV_KEY} = t::APISIX::read_file("t/certs/apisix.key");
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @suryaparua-official, this still needs some tweaking.

@Baoyuantop Baoyuantop added the wait for update wait for the author's response in this issue/PR label Jan 26, 2026
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. and removed size:M This PR changes 30-99 lines, ignoring generated files. labels Jan 26, 2026
@suryaparua-official
Copy link
Author

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
Copy link
Author

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

@Baoyuantop
Copy link
Contributor

Hi @suryaparua-official, please fix code lint error

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes Stream TLS certificate/key resolution for $ENV://... and $secret://... references by aligning Stream initialization with the existing HTTP initialization flow.

Changes:

  • Initialize core.env during stream_init so env references can be resolved during Stream TLS handshakes.
  • Initialize apisix_secret during stream_init_worker so secret references are available in Stream TLS mode.
  • Add Stream TLS regression tests covering both $ENV:// and $secret:// certificate/key references.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
apisix/init.lua Adds missing env + secret subsystem initialization for Stream lifecycle to enable $ENV:///$secret:// TLS cert resolution.
t/stream-node/tls.t Adds regression tests validating Stream TLS works with env-backed and Vault-backed certificate references.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +190 to +195
if code >= 300 then
ngx.status = code
end

ngx.say("passed")
}
Copy link

Copilot AI Feb 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this test block, if creating the stream_route fails (code >= 300) you still respond with body "passed". Even though the status is set to the error code, this hides the actual error response and makes failures harder to debug. Consider returning early on error and outputting the admin API response body instead of always printing "passed".

Copilot uses AI. Check for mistakes.
Comment on lines +290 to +295
if code >= 300 then
ngx.status = code
end

ngx.say("passed")
}
Copy link

Copilot AI Feb 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same pattern here: when the stream_route creation fails (code >= 300), the handler still prints "passed". This discards the actual error payload from the admin API and makes the test output misleading. Return early and print the error body when code indicates failure.

Copilot uses AI. Check for mistakes.
@suryaparua-official
Copy link
Author

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

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

Labels

bug Something isn't working size:L This PR changes 100-499 lines, ignoring generated files. user responded wait for update wait for the author's response in this issue/PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: $ENV and $secret SSL cert references not working in Stream TLS mode

3 participants