Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
68d705c
feat: allow to use environment variables for openid-connect
Jul 31, 2024
2e7df95
feat: allow to use environment variables for openid-connect
Jul 31, 2024
5ab3f3e
feat: allow to use environment variables for openid-connect
Jul 31, 2024
dfd3005
feat: allow to use environment variables for openid-connect plugin
darkSheep404 Sep 25, 2024
cc39db4
feat: allow to use environment variables for openid-connect
Sep 26, 2024
980df99
feat: allow to use environment variables for openid-connect
Sep 26, 2024
a4b9e06
Merge branch 'master' into feat-openid-connect-support-env
Oct 8, 2024
3fe0e00
feat: allow to use environment variables for openid-connect
Oct 8, 2024
6c7a0b9
feat: allow to use environment variables for openid-connect
Nov 15, 2024
1f75535
Update apisix/plugins/openid-connect.lua
darkSheep404 Dec 24, 2024
0303851
Update openid-connect.lua
darkSheep404 Dec 24, 2024
db6bc80
Merge branch 'apache:master' into feat-openid-connect-support-env
darkSheep404 Jan 8, 2025
ea49a27
Merge branch 'apache:master' into feat-openid-connect-support-env
darkSheep404 Mar 25, 2025
9f88583
Merge remote-tracking branch 'origin/master' into feat-openid-connect…
Jul 3, 2025
d9e71c7
feat: allow to use environment variables for openid-connect#try to fi…
Jul 3, 2025
dd60660
feat: allow to use environment variables for openid-connect#try to fi…
Jul 4, 2025
179ec78
Merge branch 'apache:master' into feat-openid-connect-support-env
darkSheep404 Jul 7, 2025
0193368
Merge branch 'apache:master' into feat-openid-connect-support-env
darkSheep404 Jul 10, 2025
5f600a5
Merge branch 'apache:master' into feat-openid-connect-support-env
darkSheep404 Jul 15, 2025
9c84185
feat: allow to use environment variables for openid-connect#add reque…
Jul 15, 2025
7116d09
Merge remote-tracking branch 'origin/feat-openid-connect-support-env'…
Jul 15, 2025
505cc27
feat: allow to use environment variables for openid-connect#add Chine…
Jul 18, 2025
74ad5f1
feat: allow to use environment variables for openid-connect#fix lint
Jul 22, 2025
9fdbbd4
Merge branch 'master' into feat-openid-connect-support-env
darkSheep404 Jul 22, 2025
631cc1d
feat: allow to use environment variables for openid-connect#fix lint
Jul 22, 2025
207617f
Merge remote-tracking branch 'origin/feat-openid-connect-support-env'…
Jul 22, 2025
d5aabfd
feat: allow to use environment variables for openid-connect#fix lint
Jul 22, 2025
a840fdd
feat: allow to use environment variables for openid-connect#fix lint
Jul 22, 2025
e68e0cc
Update apisix/plugins/openid-connect.lua
darkSheep404 Jul 28, 2025
ec2974a
Merge branch 'apache:master' into feat-openid-connect-support-env
darkSheep404 Jul 29, 2025
463c47a
feat: clone openid-connect conf before fetch_secrets
darkSheep404 Aug 22, 2025
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
4 changes: 3 additions & 1 deletion apisix/plugins/openid-connect.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ local core = require("apisix.core")
local ngx_re = require("ngx.re")
local openidc = require("resty.openidc")
local random = require("resty.random")
local fetch_secrets = require("apisix.secret").fetch_secrets
local jsonschema = require('jsonschema')
local string = string
local ngx = ngx
Expand Down Expand Up @@ -554,7 +555,8 @@ local function validate_claims_in_oidcauth_response(resp, conf)
end

function _M.rewrite(plugin_conf, ctx)
local conf = core.table.clone(plugin_conf)
local conf_clone = core.table.clone(plugin_conf)
Copy link
Member

Choose a reason for hiding this comment

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

performance tips:

we can use a lrucache, avoid repeated rendering of plugin_conf configuration

Copy link
Contributor Author

Choose a reason for hiding this comment

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

performance tips:

we can use a lrucache, avoid repeated rendering of plugin_conf configuration

Thank you, but I don't fully understand your suggestion

The original way, I just used fetch_secret without clone the config first , but with this suggestion, I add table.clone before fetch_secret

I think fetch_secret itself will return a clone of conf, and if you want to optimize it, maybe can just keep using fetch_secret directly, instead of add another extra lrucache

Copy link
Member

Choose a reason for hiding this comment

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

local matcher, err = lrucache(authorization, nil, do_extract, authorization)

here is an example in basic-auth plugin, it can help you

local conf = fetch_secrets(conf_clone, true, plugin_conf, "")

-- Previously, we multiply conf.timeout before storing it in etcd.
-- If the timeout is too large, we should not multiply it again.
Expand Down
8 changes: 8 additions & 0 deletions docs/en/latest/plugins/openid-connect.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ The `openid-connect` Plugin supports the integration with [OpenID Connect (OIDC)
| claim_schema | object | False | | | JSON schema of OIDC response claim. Example: `{"type":"object","properties":{"access_token":{"type":"string"}},"required":["access_token"]}` - validates that the response contains a required string field `access_token`. |

NOTE: `encrypt_fields = {"client_secret"}` is also defined in the schema, which means that the field will be stored encrypted in etcd. See [encrypted storage fields](../plugin-develop.md#encrypted-storage-fields).
In addition, you can use Environment Variables or APISIX secret to store and reference plugin attributes. APISIX currently supports storing secrets in two ways - [Environment Variables and HashiCorp Vault](../terminology/secret.md).

For example, use below command to set environment variable
`export keycloak_secret=abc`

and use it in plugin conf like below

`"client_secret": "$ENV://keycloak_secret"`

## Examples

Expand Down
9 changes: 9 additions & 0 deletions docs/zh/latest/plugins/openid-connect.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@ description: openid-connect 插件支持与 OpenID Connect (OIDC) 身份提供

注意:schema 中还定义了 `encrypt_fields = {"client_secret"}`,这意味着该字段将会被加密存储在 etcd 中。具体参考 [加密存储字段](../plugin-develop.md#加密存储字段)。

此外:你可以使用环境变量或者 APISIX secret 来存放和引用插件配置,APISIX 当前支持通过两种方式配置 secrets - [Environment Variables and HashiCorp Vault](../terminology/secret.md)。

例如:你可以使用以下方式来设置环境变量
`export keycloak_secret=abc`

并且像下面这样在插件里使用

`"client_secret": "$ENV://keycloak_secret"`

## 示例

以下示例演示了如何针对不同场景配置 `openid-connect` 插件。
Expand Down
1 change: 0 additions & 1 deletion t/plugin/openid-connect.t
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ add_block_preprocessor(sub {
$block->set_value("request", "GET /t");
}
});

run_tests();

__DATA__
Expand Down
192 changes: 192 additions & 0 deletions t/plugin/openid-connect9.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
use t::APISIX 'no_plan';

log_level('debug');
repeat_each(1);
no_long_string();
no_root_location();
no_shuffle();

add_block_preprocessor(sub {
my ($block) = @_;

if ((!defined $block->error_log) && (!defined $block->no_error_log)) {
$block->set_value("no_error_log", "[error]");
}

if (!defined $block->request) {
$block->set_value("request", "GET /t");
}
});

BEGIN {
$ENV{CLIENT_SECRET_ENV} = "60Op4HFM0I8ajz0WdiStAbziZ-VFQttXuxixHHs2R7r7-CW8GR79l-mmLqMhc-Sa";
$ENV{VAULT_TOKEN} = "root";
}

run_tests();

__DATA__

=== TEST 1: configure oidc plugin with small public key using environment variable
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{ "plugins": {
"openid-connect": {
"client_id": "kbyuFDidLLm280LIwVFiazOqjO3ty8KH",
"client_secret": "$ENV://CLIENT_SECRET_ENV",
"discovery": "https://samples.auth0.com/.well-known/openid-configuration",
"redirect_uri": "https://iresty.com",
"ssl_verify": false,
"timeout": 10,
"bearer_only": true,
"scope": "apisix",
"public_key": "-----BEGIN PUBLIC KEY-----\n]] ..
[[MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANW16kX5SMrMa2t7F2R1w6Bk/qpjS4QQ\n]] ..
[[hnrbED3Dpsl9JXAx90MYsIWp51hBxJSE/EPVK8WF/sjHK1xQbEuDfEECAwEAAQ==\n]] ..
[[-----END PUBLIC KEY-----",
"token_signing_alg_values_expected": "RS256"
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/hello"
}]]
)

if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- response_body
passed



=== TEST 2: store secret into vault
--- exec
VAULT_TOKEN='root' VAULT_ADDR='http://0.0.0.0:8200' vault kv put kv/apisix/foo client_secret=60Op4HFM0I8ajz0WdiStAbziZ-VFQttXuxixHHs2R7r7-CW8GR79l-mmLqMhc-Sa
--- response_body
Success! Data written to: kv/apisix/foo



=== TEST 3: configure oidc plugin with small public key using vault
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{ "plugins": {
"openid-connect": {
"client_id": "kbyuFDidLLm280LIwVFiazOqjO3ty8KH",
"client_secret": "$secret://vault/test1/foo/client_secret",
"discovery": "https://samples.auth0.com/.well-known/openid-configuration",
"redirect_uri": "https://iresty.com",
"ssl_verify": false,
"timeout": 10,
"bearer_only": true,
"scope": "apisix",
"public_key": "-----BEGIN PUBLIC KEY-----\n]] ..
[[MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANW16kX5SMrMa2t7F2R1w6Bk/qpjS4QQ\n]] ..
[[hnrbED3Dpsl9JXAx90MYsIWp51hBxJSE/EPVK8WF/sjHK1xQbEuDfEECAwEAAQ==\n]] ..
[[-----END PUBLIC KEY-----",
"token_signing_alg_values_expected": "RS256"
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/hello"
}]]
)

if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- response_body
passed



=== TEST 4: configure oidc plugin with small public key using vault and request with token should success
--- config
location /hello {
content_by_lua_block {
ngx.say("success")
}
}

location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"plugins": {
"openid-connect": {
"client_id": "kbyuFDidLLm280LIwVFiazOqjO3ty8KH",
"client_secret": "$secret://vault/test1/foo/client_secret",
"discovery": "https://samples.auth0.com/.well-known/openid-configuration",
"redirect_uri": "https://iresty.com",
"ssl_verify": false,
"timeout": 10,
"bearer_only": true,
"scope": "apisix",
"public_key": "-----BEGIN PUBLIC KEY-----\n]] ..
[[MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANW16kX5SMrMa2t7F2R1w6Bk/qpjS4QQ\n]] ..
[[hnrbED3Dpsl9JXAx90MYsIWp51hBxJSE/EPVK8WF/sjHK1xQbEuDfEECAwEAAQ==\n]] ..
[[-----END PUBLIC KEY-----",
"token_signing_alg_values_expected": "RS256"
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/hello"
}]]
)
ngx.status = code
ngx.say(body)
}
}
--- request
GET /hello HTTP/1.1
Authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL3NhbXBsZXMuYXV0aDAuY29tLyIsInN1YiI6InRlc3Qtc3ViamVjdCIsImF1ZCI6ImtieXVG RGlkTExtMjgwTEl3VkZpYXpPcWpPM3R5OEtIIiwic2NvcGUiOiJhcGlzaXgiLCJpYXQiOjEwMDAwMDAwLCJleHAiOjI1MDAwMDAwMDB9.bfcZsd4ABgo0GoLT8EwfnKgf AWbnJZbZ3kOtqyeSkXYqGlSmgMNW3q5Kx1SGjMNhEKVG_KrFfsPrQmcTljSPZA
--- response_body
success
Loading