Skip to content

Conversation

@darkSheep404
Copy link
Contributor

Description

feat: allow to use environment variables for openid-connect plugin

Checklist

  • I have explained the need for this PR and the problem it solves
  • I have explained the changes or the new features added to this PR
  • I have added tests corresponding to this change
  • I have updated the documentation to reflect this change
  • I have verified that this change is backward compatible (If not, please discuss on the APISIX mailing list first)

@darkSheep404
Copy link
Contributor Author

darkSheep404 commented Sep 24, 2024

hi good morning @shreemaan-abhishek clould you kindly review this PR for me ?

Copy link
Contributor

@shreemaan-abhishek shreemaan-abhishek left a comment

Choose a reason for hiding this comment

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

please write a test case that uses vault as well.


function _M.check_schema(conf)
function _M.check_schema(plugin_conf)
local conf = fetch_secrets(plugin_conf)
Copy link
Contributor

Choose a reason for hiding this comment

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

this is not needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is not needed.↳

This is needed when someone puts a non-string value such as a Boolean into env var, otherwise the type inconsistency will fail the check

Copy link

Choose a reason for hiding this comment

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

This line is giving me some issues after apisix reload.
If line fetch_secrets is done in check_schema, the route fails showing me the following error:

2024/11/12 17:12:51 [error] 240#240: *13728 lua entry thread aborted: runtime error: ...isix/custom-plugins/apisix/plugins/openid-connect.lua:478: attempt to compare nil with number
stack traceback:
coroutine 0:
     ...isix/custom-plugins/apisix/plugins/openid-connect.lua: in function 'phase_func'
     /usr/local/apisix/apisix/plugin.lua:1166: in function 'run_plugin'
     /usr/local/apisix/apisix/init.lua:689: in function 'http_access_phase'
     access_by_lua(nginx.conf:310):2: in main chunk, client: 10.89.2.37, server: _, request: "GET /private/anything HTTP/2.0", host: "XXXX"

If I remove fetch_secrets from check_schema, the route work as expected but the following warning is shown at startup:
[warn] 187#187: *8391 [lua] utils.lua:418: find_and_log(): Using openid-connect discovery with no TLS is a security risk, context: init_worker_by_lua*
I assume that without the fetch_secrets the value of discovery is not resolved and openid checks https for it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So should we remove fetch_secrets from check_schema to avoid this?
By contrast, putting a Boolean value in a secret is not a particularly common case in this plugin. Typically, only string urls and secret keys will be placed in valut

Copy link
Contributor Author

Choose a reason for hiding this comment

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

pushed this change

Copy link
Contributor

@shreemaan-abhishek shreemaan-abhishek left a comment

Choose a reason for hiding this comment

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

please write a test case that uses vault as well.

Copy link
Contributor

Choose a reason for hiding this comment

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

please add new test cases in t/plugin/openid-connect

@dosubot dosubot bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Sep 25, 2024
@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 Sep 26, 2024
@darkSheep404
Copy link
Contributor Author

darkSheep404 commented Sep 26, 2024

please write a test case that uses vault as well.↳
hi @shreemaan-abhishek
I tried to add a test for valut but I am not sure if it is correct, may need your help to have a look

Copy link
Contributor

@shreemaan-abhishek shreemaan-abhishek left a comment

Choose a reason for hiding this comment

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

the test cases for secret resource seems correct to me, please resolve the conflicts with master so that the tests can run

@alberto-corrales
Copy link

Great contribution!! I'm looking forward to this fix, as we are using AWS Secrets Manager as a secrets provider and we need to configure the secret as an environment variable. I hope this fix is merged and released soon 🙏

@darkSheep404
Copy link
Contributor Author

the test cases for secret resource seems correct to me, please resolve the conflicts with master so that the tests can run↳

hi @shreemaan-abhishek
The conflict has been resolved. Please help approve it when you are free so that can run the test

Copy link

@brmejia brmejia left a comment

Choose a reason for hiding this comment

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

This is exactly what I was looking for. I've tested this config on my local environment, and it works as expected.

client_id: "$env://APISIX_OIDC_CLIENT_ID"
client_secret: "$env://APISIX_OIDC_CLIENT_SECRET"
discovery: $env://APISIX_OIDC_DISCOVERY_URL

I hope this will be merged soon

@alberto-corrales
Copy link

Are there any updates on this? At the moment we have to keep a secret in our repo because of the bug that it is fixing this PR, and we need to move it to environment variables. So it would be awesome if we could have this fixed in a patch soon

@darkSheep404
Copy link
Contributor Author

darkSheep404 commented Dec 17, 2024

Are there any updates on this? At the moment we have to keep a secret in our repo because of the bug that it is fixing this PR, and we need to move it to environment variables. So it would be awesome if we could have this fixed in a patch soon

We need another good Samaritan to approve this change, then this PR will have three approve and be merged

@darkSheep404
Copy link
Contributor Author

darkSheep404 commented Dec 17, 2024

Are there any updates on this? At the moment we have to keep a secret in our repo because of the bug that it is fixing this PR, and we need to move it to environment variables. So it would be awesome if we could have this fixed in a patch soon

We need another good Samaritan to approve this change, then this PR will have three approve and be merged

@shreemaan-abhishek @nic-6443

Could any of you help approve this PR at your convenience? It has been pending here for a long time and only needs the last approver

@darkSheep404
Copy link
Contributor Author

darkSheep404 commented Dec 24, 2024

Are there any updates on this? At the moment we have to keep a secret in our repo because of the bug that it is fixing this PR, and we need to move it to environment variables. So it would be awesome if we could have this fixed in a patch soon

We need another good Samaritan to approve this change, then this PR will have three approve and be merged

@shreemaan-abhishek @nic-6443

Could any of you help approve this PR at your convenience? It has been pending here for a long time and only needs the last approver

hi @kayx23 @membphis @Revolyssup Could any of you help?

@shreemaan-abhishek
Copy link
Contributor

I have left a comment: https://github.com/apache/apisix/pull/11451/files#r1896868940

@Baoyuantop Baoyuantop removed the wait for update wait for the author's response in this issue/PR label Jul 29, 2025
@darkSheep404
Copy link
Contributor Author

Hi @darkSheep404, please merge the master branch code, and the failed test will be fixed.

merged

Baoyuantop
Baoyuantop previously approved these changes Aug 1, 2025
membphis
membphis previously approved these changes Aug 4, 2025
Copy link
Member

@membphis membphis left a comment

Choose a reason for hiding this comment

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

LGTM

SkyeYoung
SkyeYoung previously approved these changes Aug 4, 2025
@SkyeYoung
Copy link
Member

@nic-6443 @bzp2010 @Revolyssup @AlinsRan pls review this PR.

@darkSheep404
Copy link
Contributor Author

darkSheep404 commented Aug 5, 2025

@nic-6443 @bzp2010 @Revolyssup @AlinsRan pls review this PR.

image

seems can merge now, need someone help to click "merge" button or need to wait all those members approve it, then it will be merged automatic ?

Copy link
Contributor

@bzp2010 bzp2010 left a comment

Choose a reason for hiding this comment

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

only a minor issue


function _M.rewrite(plugin_conf, ctx)
local conf = core.table.clone(plugin_conf)
local conf = fetch_secrets(plugin_conf, true, plugin_conf, "")
Copy link
Contributor

Choose a reason for hiding this comment

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

Please still clone the table.
While it is true that fetch_secrets does imply this fact, it cannot be inferred from the name and is not a feature of any mandatory guarantee.
As soon as someone modifies fetch_secrets in the future and doesn't clone there anymore, something will go wrong with your code.

Therefore, it's best to explicitly copy this table here anyway to ensure that, no matter how much external conditions change, the logic of this plugin doesn't break.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please still clone the table. While it is true that fetch_secrets does imply this fact, it cannot be inferred from the name and is not a feature of any mandatory guarantee. As soon as someone modifies fetch_secrets in the future and doesn't clone there anymore, something will go wrong with your code.

Therefore, it's best to explicitly copy this table here anyway to ensure that, no matter how much external conditions change, the logic of this plugin doesn't break.

hi @Baoyuantop please advice

Copy link
Contributor

Choose a reason for hiding this comment

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

I previously referred to the implementation of other plugin codes:

There are many other locations, so I think there is no problem with the current PR from the implementation point of view.

As soon as someone modifies fetch_secrets in the future and doesn't clone there anymore, something will go wrong with your code.

If this happens, many plugin codes will need to be adjusted. If my understanding is wrong, please correct me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hi @Baoyuantop already 5 approval more than 3 approval required now, cloud you help to merge this PR?

@SkyeYoung SkyeYoung requested a review from bzp2010 August 6, 2025 01:11

function _M.rewrite(plugin_conf, ctx)
local conf = core.table.clone(plugin_conf)
local conf = fetch_secrets(plugin_conf, true, plugin_conf, "")
Copy link
Contributor

@bzp2010 bzp2010 Aug 22, 2025

Choose a reason for hiding this comment

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

I previously referred to the implementation of other plugin codes:

There are many other locations, so I think there is no problem with the current PR from the implementation point of view.

As soon as someone modifies fetch_secrets in the future and doesn't clone there anymore, something will go wrong with your code.

If this happens, many plugin codes will need to be adjusted. If my understanding is wrong, please correct me.

I do not agree with this conclusion.

The examples you listed are insufficient to prove this point. The reason we need to clone conf is that this plugin may temporarily and locally modify fields in conf.

Lua tables use reference passing (pointer), and modifying the conf within the plugin would have side effects on the global configuration cache, which is unacceptable.

You can check the two examples you provided; they do not involve any scenarios where the conf is modified, so there is no need to explicitly clone the conf. Whether fetch_secrets clones the conf internally has no impact on this.

Looking at the openid-connect plugin, there are modifications to conf everywhere, so it undoubtedly needs to know whether conf has been cloned. This is a necessary measure to ensure that the global cache is not polluted.
However, we cannot rely on external behavior (fetch_secrets) to guarantee this, as it violates the principle of low coupling.

https://github.com/darkSheep404/apisix/blob/ec2974aa3e1d486b8416e5010f3d1046db093f37/apisix/plugins/openid-connect.lua#L562-L585


I still stand by this view, and unless this is modified, I will not merge this PR.

  1. I do not believe that fetch_secrets implies that it will perform a clone and guarantee that this behavior will always be valid.
  2. Performing an explicit clone (which is only a shallow clone) is inexpensive. At the same time, it ensures that it will conform to the logic before this modification rather than relying on external conditions for assurance.
  3. Even if anybody does modify fetch_secrets in the future, the fact that conf is not cloned cannot be easily detected.

    This will lead to unexpected behavior rather than an error.

    I do not have the time to track all code changes in this area, and I do not want to wait until a bad situation occurs to discover it.

If this is still a matter of controversy, then let more maintainers express their opinions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hi @bzp2010
just add a commit to clone plugin_conf before fetch_secrets

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@bzp2010 may i ask what is the apisix PR merge flow? more than 3 approvals will merge it automatic
or need someone manually click merge button,or else ?

Copy link
Contributor

Choose a reason for hiding this comment

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

more than 3 approvals and manually click merge button.

There is also a CI error, but it doesn't seem to be related to this PR. I've seen similar errors in other PRs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

more than 3 approvals and manually click merge button.

There is also a CI error, but it doesn't seem to be related to this PR. I've seen similar errors in other PRs.

then could you help to merge it now ?🙂

Copy link
Contributor

Choose a reason for hiding this comment

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

Merged, thanks for your contribution.

@darkSheep404 darkSheep404 dismissed stale reviews from SkyeYoung, membphis, and Baoyuantop via 463c47a August 22, 2025 05:31
@Baoyuantop
Copy link
Contributor

Merge first, the failed CI will be fixed at #12530 (comment)

@Baoyuantop Baoyuantop merged commit 54418e1 into apache:master Aug 25, 2025
38 of 42 checks passed
@github-project-automation github-project-automation bot moved this from 👀 In review to ✅ Done in ⚡️ Apache APISIX Roadmap Aug 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L This PR changes 100-499 lines, ignoring generated files.

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

feat: support client_secret in openid-connect plugin use secret bug: openid-connect yml conf could not use $env to set plugin properties

9 participants