You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2022/08/31 08:35:50 UTC

[GitHub] [apisix] kingluo opened a new pull request, #7827: feat(saml-auth): add saml-auth plugin

kingluo opened a new pull request, #7827:
URL: https://github.com/apache/apisix/pull/7827

   ### Description
   
   Add saml-auth plugin.
   
   ### Checklist
   
   - [x] I have explained the need for this PR and the problem it solves
   - [x] I have explained the changes or the new features added to this PR
   - [x] I have added tests corresponding to this change
   - [x] I have updated the documentation to reflect this change
   - [x] I have verified that this change is backward compatible (If not, please discuss on the [APISIX mailing list](https://github.com/apache/apisix/tree/master#community) first)
   
   <!--
   
   Note
   
   1. Mark the PR as draft until it's ready to be reviewed.
   2. Always add/update tests for any changes unless you have a good reason.
   3. Always update the documentation to reflect the changes made in the PR.
   4. Make a new commit to resolve conversations instead of `push -f`.
   5. To resolve merge conflicts, merge master instead of rebasing.
   6. Use "request review" to notify the reviewer after making changes.
   7. Only a reviewer can mark a conversation as resolved.
   
   -->
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] kingluo commented on a diff in pull request #7827: feat(saml-auth): add saml-auth plugin

Posted by GitBox <gi...@apache.org>.
kingluo commented on code in PR #7827:
URL: https://github.com/apache/apisix/pull/7827#discussion_r961268162


##########
apisix/plugins/saml-auth.lua:
##########
@@ -0,0 +1,92 @@
+--
+-- 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.
+--
+local core = require("apisix.core")
+local resty_saml = require("resty.saml")
+
+local is_resty_saml_init = false
+
+local lrucache = core.lrucache.new({
+    ttl = 300, count = 512
+})
+
+local schema = {
+    type = "object",
+    title = "work with route or service object",
+    properties = {
+        sp_issuer = { type = "string" },
+        idp_uri = { type = "string" },

Review Comment:
   @soulbird `uri_def` is not suitable in this case, because all uri related properties could be in either full qualified address or path only.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] kingluo commented on a diff in pull request #7827: feat(saml-auth): add saml-auth plugin

Posted by GitBox <gi...@apache.org>.
kingluo commented on code in PR #7827:
URL: https://github.com/apache/apisix/pull/7827#discussion_r966607702


##########
apisix/cli/ngx_tpl.lua:
##########
@@ -432,6 +436,11 @@ http {
             dns_resolver = dns_resolver,
         }
         apisix.http_init(args)
+
+        {% if enabled_plugins["saml-auth"] then %}
+        local saml = require("apisix.plugins.saml-auth")

Review Comment:
   @membphis No matter which place to init the plugin, we need to determine `apisix_lua_home` before saml start, because saml init needs the absolute path of `xsd/` dir.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] soulbird commented on a diff in pull request #7827: feat(saml-auth): add saml-auth plugin

Posted by GitBox <gi...@apache.org>.
soulbird commented on code in PR #7827:
URL: https://github.com/apache/apisix/pull/7827#discussion_r961233971


##########
apisix/plugins/saml-auth.lua:
##########
@@ -0,0 +1,92 @@
+--
+-- 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.
+--
+local core = require("apisix.core")
+local resty_saml = require("resty.saml")
+
+local is_resty_saml_init = false
+
+local lrucache = core.lrucache.new({
+    ttl = 300, count = 512
+})
+
+local schema = {
+    type = "object",
+    title = "work with route or service object",
+    properties = {
+        sp_issuer = { type = "string" },
+        idp_uri = { type = "string" },

Review Comment:
   Should be added with strict verification, like`coree.schema_def.uri_def`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] kingluo commented on a diff in pull request #7827: feat(saml-auth): add saml-auth plugin

Posted by GitBox <gi...@apache.org>.
kingluo commented on code in PR #7827:
URL: https://github.com/apache/apisix/pull/7827#discussion_r962314551


##########
apisix/cli/ngx_tpl.lua:
##########
@@ -660,6 +664,10 @@ http {
             set $dubbo_method                '';
             {% end %}
 
+            {% if enabled_plugins["saml-auth"] then %}
+            set $saml_data_dir               {*apisix_lua_home*}/deps/share/lua/5.1/resty/saml;

Review Comment:
   It depends on `apisix_lua_home`, how to determine it in lua code?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] spacewander commented on a diff in pull request #7827: feat(saml-auth): add saml-auth plugin

Posted by GitBox <gi...@apache.org>.
spacewander commented on code in PR #7827:
URL: https://github.com/apache/apisix/pull/7827#discussion_r963178747


##########
docs/en/latest/plugins/saml-auth.md:
##########
@@ -0,0 +1,123 @@
+---
+title: saml-auth
+keywords:
+  - APISIX
+  - Plugin
+  - SAML AUTH
+  - saml-auth
+description: This document contains information about the Apache APISIX saml-auth Plugin.
+---
+
+<!--
+#
+# 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.
+#
+-->
+
+## Description
+
+The `saml-auth` Plugin can be used to access SAML (Security Assertion Markup Language 2.0) IdP (Identity Provider)
+to do authentication, from the SP (service provider) perspective.
+
+## Attributes
+
+| Name      | Type | Required      | Description |
+| ----------- | ----------- | ----------- | ----------- |
+| `sp_issuer`      | string       | True      | SP name to access IdP.       |
+| `idp_uri`      | string       | True      | URI of IdP.       |
+| `idp_cert`      | string       | True      | IdP Certificate, used to verify saml response.       |
+| `login_callback_uri`      | string       | True      | redirect uri used to callback the SP from IdP after login.       |
+| `logout_uri`      | string       | True      | logout uri to trigger logout.       |
+| `logout_callback_uri`      | string       | True      | redirect uri used to callback the SP from IdP after logout.       |
+| `logout_redirect_uri`      | string       | True      | redirect uri after successful logout.       |
+| `sp_cert`      | string       | True      | SP Certificate, used to sign the saml request.       |
+| `sp_private_key`      | string       | True      | SP private key.       |
+
+## Enabling the Plugin
+
+You can enable the Plugin on a specific Route as shown below:
+
+```shell
+curl http://127.0.0.1:9180/apisix/admin/routes/test_saml_auth -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
+{
+    "methods": ["GET"],
+    "uri": "/anything/*",
+    "plugins": {
+          "saml-auth": {
+            "idp_cert": "-----BEGIN CERTIFICATE-----\nMIIClzCCAX8CBgGC7zvjWjANBgkqhkiG9w0BAQsFADAPMQ0wCwYDVQQDDAR0ZXN0\nMB4XDTIyMDgzMDE0NDkxNVoXDTMyMDgzMDE0NTA1NVowDzENMAsGA1UEAwwEdGVz\ndDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAJelXTy4j6pP++7Bq9Yp\nmkuDYoBQRhUUO/RNVNOBPitVisNEQgdpKS2CFYLENZePo0xsDrN+5IB/8MTEqo3V\ntVu2BYQtq5tS0SUEgH1TLIt6gcnOuqgmmk3+RfUM3WGaUlbEN/j1C50RepFJm03i\nAM8dWCsCTCnZQLq/T4T7wwEIIOuAfz7Zip9+8HuVTOUoC3SxwVqTNb0pVRvdg7gU\nYh0f4CnckhlZG+4AgFvyW5LXxyUWbBtIpYEJN8DZEbEg+QmjuUHT0w+Apdtr6MSM\nKT87tQMxRlIYCqjmwPB9xjW1eZ9FdTGu81WeIhvs3iwTBoH6qcBR9ZJltY/VXEhg\neGECAwEAATANBgkqhkiG9w0BAQsFAAOCAQEAhbVRy4EoJzr/B84g9V6aAobUHoEF\ntv02G2GS/3UxrcaBXMLUl/tJlgti+PwQGDzIEXP0b0LLdAh/5LZFeDFVbphXUk26\nXxj3T2ej9ZcsRbaF+yHi0iisyMNdx9gbosDYg8yx9+XWImQMe6+T6+7fjYnilWE8\n0nYUuxyf1GF20EwQ0sjcJbxuq/K/2DZfOT8eo8a5c1oJFB20rE+KEpZnhBnDTQ+s\nUXPjN0AYTCKtPiAd7D190Pk1CQUbfAUmeII9WstCJhmGqIeiSpHsa9mAa50GVT3B\nnHm5obfM+sBEmy78MnC+s9Kt/EquakxeEi+YZEVfgyORq+LwNaef5MuedA==\n-----END CERTIFICATE---
 --",

Review Comment:
   Yes, let's do the same for other cert/key in this file



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] kingluo closed pull request #7827: feat(saml-auth): add saml-auth plugin

Posted by GitBox <gi...@apache.org>.
kingluo closed pull request #7827: feat(saml-auth): add saml-auth plugin
URL: https://github.com/apache/apisix/pull/7827


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] kingluo commented on a diff in pull request #7827: feat(saml-auth): add saml-auth plugin

Posted by GitBox <gi...@apache.org>.
kingluo commented on code in PR #7827:
URL: https://github.com/apache/apisix/pull/7827#discussion_r961268162


##########
apisix/plugins/saml-auth.lua:
##########
@@ -0,0 +1,92 @@
+--
+-- 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.
+--
+local core = require("apisix.core")
+local resty_saml = require("resty.saml")
+
+local is_resty_saml_init = false
+
+local lrucache = core.lrucache.new({
+    ttl = 300, count = 512
+})
+
+local schema = {
+    type = "object",
+    title = "work with route or service object",
+    properties = {
+        sp_issuer = { type = "string" },
+        idp_uri = { type = "string" },

Review Comment:
   @soulbird `uri_def` is not suitable in this case, because all uri related properties could be in either full qualified address with host or path only.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] kingluo commented on a diff in pull request #7827: feat(saml-auth): add saml-auth plugin

Posted by GitBox <gi...@apache.org>.
kingluo commented on code in PR #7827:
URL: https://github.com/apache/apisix/pull/7827#discussion_r964446222


##########
Makefile:
##########
@@ -151,6 +151,7 @@ deps: runtime
 	$(eval ENV_LUAROCKS_VER := $(shell $(ENV_LUAROCKS) --version | grep -E -o "luarocks [0-9]+."))
 	@if [ '$(ENV_LUAROCKS_VER)' = 'luarocks 3.' ]; then \
 		mkdir -p ~/.luarocks; \
+		$(ENV_LUAROCKS) config $(ENV_LUAROCKS_FLAG_LOCAL) variables.OPENSSL_DIR $(ENV_OPENSSL_PREFIX); \

Review Comment:
   The previous commit already add `OPENSSL_DIR` in `linux-install-luarocks.sh`:
   https://github.com/apache/apisix/pull/7827/files#diff-4fc86c1a9aa8e4ff0b1e5767d2d2bfa32579829c477d034bea4e8f96fe7ab69d



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] membphis commented on a diff in pull request #7827: feat(saml-auth): add saml-auth plugin

Posted by GitBox <gi...@apache.org>.
membphis commented on code in PR #7827:
URL: https://github.com/apache/apisix/pull/7827#discussion_r966602052


##########
apisix/cli/ngx_tpl.lua:
##########
@@ -432,6 +436,11 @@ http {
             dns_resolver = dns_resolver,
         }
         apisix.http_init(args)
+
+        {% if enabled_plugins["saml-auth"] then %}
+        local saml = require("apisix.plugins.saml-auth")

Review Comment:
   we can do this in `init` phase



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] kingluo commented on a diff in pull request #7827: feat(saml-auth): add saml-auth plugin

Posted by GitBox <gi...@apache.org>.
kingluo commented on code in PR #7827:
URL: https://github.com/apache/apisix/pull/7827#discussion_r964518965


##########
utils/install-dependencies.sh:
##########
@@ -84,13 +84,14 @@ function install_dependencies_with_apt() {
     sudo apt-get update
 
     # install OpenResty and some compilation tools
-    sudo apt-get install -y git openresty curl openresty-openssl111-dev make gcc libpcre3 libpcre3-dev libldap2-dev unzip
+    sudo apt-get install -y git openresty curl openresty-openssl111-dev make gcc libpcre3 libpcre3-dev libldap2-dev unzip libxml2-dev libxslt-dev
 }
 
 # Install dependencies on mac osx
 function install_dependencies_on_mac_osx() {
     # install OpenResty, etcd and some compilation tools
-    brew install openresty/brew/openresty luarocks lua@5.1 wget curl git pcre openldap
+    brew install openresty/brew/openresty luarocks lua@5.1 wget curl git pcre openldap libxml2 libxslt
+:qa

Review Comment:
   @tzssangglass yes, it's typo. thanks.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] tzssangglass commented on a diff in pull request #7827: feat(saml-auth): add saml-auth plugin

Posted by GitBox <gi...@apache.org>.
tzssangglass commented on code in PR #7827:
URL: https://github.com/apache/apisix/pull/7827#discussion_r964500857


##########
t/plugin/saml-auth.t:
##########
@@ -0,0 +1,215 @@
+#
+# 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('warn');
+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");
+    }
+});
+
+run_tests;
+
+__DATA__
+
+=== TEST 1: Add route for sp1
+--- config
+    location /t {
+        content_by_lua_block {
+            local kc = require("lib.keycloak_saml")
+            local core = require("apisix.core")
+
+            local default_opts = kc.get_default_opts()
+            local opts = core.table.deepcopy(default_opts)
+            opts.sp_issuer = "sp"
+            local t = require("lib.test_admin").test
+
+            local code, body = t('/apisix/admin/routes/1',
+                 ngx.HTTP_PUT,
+                 [[{
+                        "host" : "127.0.0.1",
+                        "plugins": {
+                            "saml-auth": ]] .. core.json.encode(opts) .. [[
+                        },
+                        "upstream": {
+                            "nodes": {
+                                "127.0.0.1:1980": 1
+                            },
+                            "type": "roundrobin"
+                        },
+                        "uri": "/*"
+                }]]
+                )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- response_body
+passed
+
+
+
+=== TEST 2: login and logout ok
+--- config
+    location /t {
+        content_by_lua_block {
+            local http = require "resty.http"
+            local httpc = http.new()
+            local kc = require "lib.keycloak_saml"
+
+            local path = "/uri"
+            local uri = "http://127.0.0.1:" .. ngx.var.server_port
+            local username = "test"
+            local password = "test"
+
+            local res, err, saml_cookie, keycloak_cookie = kc.login_keycloak(uri .. path, username, password)
+            if err or res.headers['Location'] ~= path then
+                ngx.log(ngx.ERR, err)
+                ngx.exit(500)
+            end
+            res, err = httpc:request_uri(uri .. res.headers['Location'], {
+                method = "GET",
+                headers = {
+                    ["Cookie"] = saml_cookie
+                }
+            })
+            assert(res.status == 200)
+            ngx.say(res.body)
+
+            res, err = kc.logout_keycloak(uri .. "/logout", saml_cookie, keycloak_cookie)
+            if err or res.headers['Location'] ~= "/logout_ok" then
+                ngx.log(ngx.ERR, err)
+                ngx.exit(500)
+            end
+        }
+    }
+--- response_body_like
+uri: /uri
+cookie: .*
+host: 127.0.0.1:1984
+user-agent: .*
+x-real-ip: 127.0.0.1
+
+
+
+=== TEST 3: Add route for sp2
+--- config
+    location /t {
+        content_by_lua_block {
+            local kc = require("lib.keycloak_saml")
+            local core = require("apisix.core")
+
+            local default_opts = kc.get_default_opts()
+            local opts = core.table.deepcopy(default_opts)
+            opts.sp_issuer = "sp2"
+            local t = require("lib.test_admin").test
+
+            local code, body = t('/apisix/admin/routes/2',
+                 ngx.HTTP_PUT,
+                 [[{
+                        "host" : "127.0.0.2",
+                        "plugins": {
+                            "saml-auth": ]] .. core.json.encode(opts) .. [[
+                        },
+                        "upstream": {
+                            "nodes": {
+                                "127.0.0.1:1980": 1
+                            },
+                            "type": "roundrobin"
+                        },
+                        "uri": "/*"
+                }]]
+                )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- response_body
+passed
+
+
+
+=== TEST 4: login sp1 and sp2, then do single logout
+--- config
+    location /t {
+        content_by_lua_block {
+            local http = require "resty.http"
+            local httpc = http.new()
+            local kc = require "lib.keycloak_saml"
+
+            local path = "/uri"
+
+            -- login to sp1
+            local uri = "http://127.0.0.1:" .. ngx.var.server_port
+            local username = "test"
+            local password = "test"
+
+            local res, err, saml_cookie, keycloak_cookie = kc.login_keycloak(uri .. path, username, password)
+            if err or res.headers['Location'] ~= path then
+                ngx.log(ngx.ERR, err)
+                ngx.exit(500)
+            end
+            res, err = httpc:request_uri(uri .. res.headers['Location'], {
+                method = "GET",
+                headers = {
+                    ["Cookie"] = saml_cookie
+                }
+            })
+            assert(res.status == 200)
+
+            -- login to sp2
+            local uri2 = "http://127.0.0.2:" .. ngx.var.server_port
+
+            local res, err, saml_cookie2 = kc.login_keycloak_for_second_sp(uri2 .. path, keycloak_cookie)
+            if err or res.headers['Location'] ~= path then
+                ngx.log(ngx.ERR, err)
+                ngx.exit(500)
+            end
+            res, err = httpc:request_uri(uri2 .. res.headers['Location'], {
+                method = "GET",
+                headers = {
+                    ["Cookie"] = saml_cookie2
+                }
+            })
+            assert(res.status == 200)
+
+            -- SLO (single logout)
+            res, err = kc.single_logout(uri .. "/logout", saml_cookie, saml_cookie2, keycloak_cookie)
+            if err or res.headers['Location'] ~= "/logout_ok" then
+                ngx.log(ngx.ERR, err)
+                ngx.exit(500)
+            end
+        }
+    }

Review Comment:
   add some login failed cases?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] spacewander commented on a diff in pull request #7827: feat(saml-auth): add saml-auth plugin

Posted by GitBox <gi...@apache.org>.
spacewander commented on code in PR #7827:
URL: https://github.com/apache/apisix/pull/7827#discussion_r961353226


##########
apisix/plugins/saml-auth.lua:
##########
@@ -0,0 +1,92 @@
+--
+-- 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.
+--
+local core = require("apisix.core")
+local resty_saml = require("resty.saml")
+
+local is_resty_saml_init = false
+
+local lrucache = core.lrucache.new({
+    ttl = 300, count = 512
+})
+
+local schema = {
+    type = "object",
+    title = "work with route or service object",
+    properties = {
+        sp_issuer = { type = "string" },
+        idp_uri = { type = "string" },
+        idp_cert = { type = "string" },
+        login_callback_uri = { type = "string" },
+        logout_uri = { type = "string" },
+        logout_callback_uri = { type = "string" },
+        logout_redirect_uri = { type = "string" },
+        sp_cert = { type = "string" },
+        sp_private_key = { type = "string" },
+    },
+    required = {
+        "sp_issuer",
+        "idp_uri",
+        "idp_cert",
+        "login_callback_uri",
+        "logout_uri",
+        "logout_callback_uri",
+        "logout_redirect_uri",
+        "sp_cert",
+        "sp_private_key",
+    }
+}
+
+local plugin_name = "saml-auth"
+
+local _M = {
+    version = 0.1,
+    priority = 2598,
+    name = plugin_name,
+    schema = schema,
+}
+
+function _M.check_schema(conf, schema_type)
+    return core.schema.check(schema, conf)
+end
+
+local function create_saml_obj(conf)
+    return resty_saml.new(conf)
+end
+
+function _M.rewrite(conf, ctx)
+    if not is_resty_saml_init then
+        local err = resty_saml.init({
+            debug = true,
+            data_dir = ctx.var.saml_data_dir,
+        })
+        if err then
+            core.log.error("saml init: ", err)
+            return 500
+        end
+        is_resty_saml_init = true
+    end
+
+    core.log.info("plugin rewrite phase, conf: ", core.json.delay_encode(conf))
+
+    local saml = core.lrucache.plugin_ctx(lrucache, ctx, nil, create_saml_obj, conf)
+
+    local data = saml:authenticate()

Review Comment:
   It's weird to let a plugin hijack the request flow when the authentication is failed. It would bypass APISIX's execution code. lua-resty-openidc does this at some corners, but since lua-resty-saml is owned by us, we can do better.
   
   Maybe we can do it in the future PR?



##########
apisix/plugins/saml-auth.lua:
##########
@@ -0,0 +1,92 @@
+--
+-- 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.
+--
+local core = require("apisix.core")
+local resty_saml = require("resty.saml")
+
+local is_resty_saml_init = false
+
+local lrucache = core.lrucache.new({
+    ttl = 300, count = 512
+})
+
+local schema = {
+    type = "object",
+    title = "work with route or service object",
+    properties = {
+        sp_issuer = { type = "string" },
+        idp_uri = { type = "string" },
+        idp_cert = { type = "string" },
+        login_callback_uri = { type = "string" },
+        logout_uri = { type = "string" },
+        logout_callback_uri = { type = "string" },
+        logout_redirect_uri = { type = "string" },
+        sp_cert = { type = "string" },
+        sp_private_key = { type = "string" },
+    },
+    required = {
+        "sp_issuer",
+        "idp_uri",
+        "idp_cert",
+        "login_callback_uri",
+        "logout_uri",
+        "logout_callback_uri",
+        "logout_redirect_uri",
+        "sp_cert",
+        "sp_private_key",
+    }
+}
+
+local plugin_name = "saml-auth"
+
+local _M = {
+    version = 0.1,
+    priority = 2598,
+    name = plugin_name,
+    schema = schema,
+}
+
+function _M.check_schema(conf, schema_type)
+    return core.schema.check(schema, conf)
+end
+
+local function create_saml_obj(conf)
+    return resty_saml.new(conf)
+end
+
+function _M.rewrite(conf, ctx)
+    if not is_resty_saml_init then
+        local err = resty_saml.init({
+            debug = true,
+            data_dir = ctx.var.saml_data_dir,
+        })
+        if err then
+            core.log.error("saml init: ", err)
+            return 500

Review Comment:
   Better avoid using 500. Maybe we can use 503. People keep reporting issues when they get 500 from APISIX, even it is caused by their environment problem.



##########
ci/pod/docker-compose.plugin.yml:
##########
@@ -42,6 +42,19 @@ services:
     networks:
       apisix_net:
 
+  ## keycloak
+  apisix_keycloak_for_saml:
+    container_name: keycloak
+    image: quay.io/keycloak/keycloak:18.0.2

Review Comment:
   Let's add a comment on why we choose a separate keycloak



##########
t/plugin/saml-auth.t:
##########
@@ -0,0 +1,219 @@
+#
+# 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('warn');
+repeat_each(1);
+no_long_string();
+no_root_location();
+no_shuffle();
+run_tests;
+
+__DATA__
+
+=== TEST 1: Add route for sp1
+--- config
+    location /t {
+        content_by_lua_block {
+            local kc = require("lib.keycloak2")
+            local core = require("apisix.core")
+
+            local default_opts = kc.get_default_opts()
+            local opts = core.table.deepcopy(default_opts)
+            opts.sp_issuer = "sp"
+            local t = require("lib.test_admin").test
+
+            local code, body = t('/apisix/admin/routes/1',
+                 ngx.HTTP_PUT,
+                 [[{
+                        "host" : "127.0.0.1",
+                        "plugins": {
+                            "saml-auth": ]] .. core.json.encode(opts) .. [[
+                        },
+                        "upstream": {
+                            "nodes": {
+                                "127.0.0.1:1980": 1
+                            },
+                            "type": "roundrobin"
+                        },
+                        "uri": "/*"
+                }]]
+                )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- request
+GET /t
+--- response_body
+passed
+--- no_error_log
+[error]
+
+
+
+=== TEST 2: login and logout ok
+--- config
+    location /t {
+        content_by_lua_block {
+            local http = require "resty.http"
+            local httpc = http.new()
+            local kc = require "lib.keycloak2"
+
+            local path = "/uri"
+            local uri = "http://127.0.0.1:" .. ngx.var.server_port
+            local username = "test"
+            local password = "test"
+
+            local res, err, saml_cookie, keycloak_cookie = kc.login_keycloak(uri .. path, username, password)
+            if err or res.headers['Location'] ~= path then
+                ngx.log(ngx.ERR, err)
+                ngx.exit(500)
+            end
+            res, err = httpc:request_uri(uri .. res.headers['Location'], {
+                method = "GET",
+                headers = {
+                    ["Cookie"] = saml_cookie
+                }
+            })
+            assert(res.status == 200)
+            ngx.say(res.body)
+
+            res, err = kc.logout_keycloak(uri .. "/logout", saml_cookie, keycloak_cookie)
+            if err or res.headers['Location'] ~= "/logout_ok" then
+                ngx.log(ngx.ERR, err)
+                ngx.exit(500)
+            end
+        }
+    }
+--- request
+GET /t
+--- response_body_like
+uri: /uri
+cookie: .*
+host: 127.0.0.1:1984
+user-agent: .*
+x-real-ip: 127.0.0.1
+--- no_error_log
+[error]
+
+
+
+=== TEST 3: Add route for sp2
+--- config
+    location /t {
+        content_by_lua_block {
+            local kc = require("lib.keycloak2")
+            local core = require("apisix.core")
+
+            local default_opts = kc.get_default_opts()
+            local opts = core.table.deepcopy(default_opts)
+            opts.sp_issuer = "sp2"
+            local t = require("lib.test_admin").test
+
+            local code, body = t('/apisix/admin/routes/2',
+                 ngx.HTTP_PUT,
+                 [[{
+                        "host" : "127.0.0.2",
+                        "plugins": {
+                            "saml-auth": ]] .. core.json.encode(opts) .. [[
+                        },
+                        "upstream": {
+                            "nodes": {
+                                "127.0.0.1:1980": 1
+                            },
+                            "type": "roundrobin"
+                        },
+                        "uri": "/*"
+                }]]
+                )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- request
+GET /t
+--- response_body
+passed
+--- no_error_log
+[error]
+
+
+
+=== TEST 4: login sp1 and sp2, then do single logout
+--- config
+    location /t {
+        content_by_lua_block {
+            local http = require "resty.http"
+            local httpc = http.new()
+            local kc = require "lib.keycloak2"
+
+            local path = "/uri"
+
+            -- login to sp1
+            local uri = "http://127.0.0.1:" .. ngx.var.server_port
+            local username = "test"
+            local password = "test"
+
+            local res, err, saml_cookie, keycloak_cookie = kc.login_keycloak(uri .. path, username, password)
+            if err or res.headers['Location'] ~= path then
+                ngx.log(ngx.ERR, err)
+                ngx.exit(500)
+            end
+            res, err = httpc:request_uri(uri .. res.headers['Location'], {
+                method = "GET",
+                headers = {
+                    ["Cookie"] = saml_cookie
+                }
+            })
+            assert(res.status == 200)
+
+            -- login to sp2
+            local uri2 = "http://127.0.0.2:" .. ngx.var.server_port
+
+            local res, err, saml_cookie2 = kc.login_keycloak_for_second_sp(uri2 .. path, keycloak_cookie)
+            if err or res.headers['Location'] ~= path then
+                ngx.log(ngx.ERR, err)
+                ngx.exit(500)
+            end
+            res, err = httpc:request_uri(uri2 .. res.headers['Location'], {
+                method = "GET",
+                headers = {
+                    ["Cookie"] = saml_cookie2
+                }
+            })
+            assert(res.status == 200)
+
+            -- SLO (single logout)
+            res, err = kc.single_logout(uri .. "/logout", saml_cookie, saml_cookie2, keycloak_cookie)
+            if err or res.headers['Location'] ~= "/logout_ok" then
+                ngx.log(ngx.ERR, err)
+                ngx.exit(500)
+            end
+        }
+    }
+--- request
+GET /t
+--- error_code: 200

Review Comment:
   error_code 200 is checked by default



##########
apisix/plugins/saml-auth.lua:
##########
@@ -0,0 +1,92 @@
+--
+-- 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.
+--
+local core = require("apisix.core")
+local resty_saml = require("resty.saml")
+
+local is_resty_saml_init = false
+
+local lrucache = core.lrucache.new({
+    ttl = 300, count = 512
+})
+
+local schema = {
+    type = "object",
+    title = "work with route or service object",

Review Comment:
   We don't need this title



##########
t/plugin/saml-auth.t:
##########
@@ -0,0 +1,219 @@
+#
+# 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('warn');
+repeat_each(1);
+no_long_string();
+no_root_location();
+no_shuffle();
+run_tests;
+
+__DATA__
+
+=== TEST 1: Add route for sp1
+--- config
+    location /t {
+        content_by_lua_block {
+            local kc = require("lib.keycloak2")
+            local core = require("apisix.core")
+
+            local default_opts = kc.get_default_opts()
+            local opts = core.table.deepcopy(default_opts)
+            opts.sp_issuer = "sp"
+            local t = require("lib.test_admin").test
+
+            local code, body = t('/apisix/admin/routes/1',
+                 ngx.HTTP_PUT,
+                 [[{
+                        "host" : "127.0.0.1",
+                        "plugins": {
+                            "saml-auth": ]] .. core.json.encode(opts) .. [[
+                        },
+                        "upstream": {
+                            "nodes": {
+                                "127.0.0.1:1980": 1
+                            },
+                            "type": "roundrobin"
+                        },
+                        "uri": "/*"
+                }]]
+                )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- request

Review Comment:
   Let's exact the common part like other plugin test.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] tzssangglass commented on a diff in pull request #7827: feat(saml-auth): add saml-auth plugin

Posted by GitBox <gi...@apache.org>.
tzssangglass commented on code in PR #7827:
URL: https://github.com/apache/apisix/pull/7827#discussion_r964495640


##########
utils/install-dependencies.sh:
##########
@@ -84,13 +84,14 @@ function install_dependencies_with_apt() {
     sudo apt-get update
 
     # install OpenResty and some compilation tools
-    sudo apt-get install -y git openresty curl openresty-openssl111-dev make gcc libpcre3 libpcre3-dev libldap2-dev unzip
+    sudo apt-get install -y git openresty curl openresty-openssl111-dev make gcc libpcre3 libpcre3-dev libldap2-dev unzip libxml2-dev libxslt-dev
 }
 
 # Install dependencies on mac osx
 function install_dependencies_on_mac_osx() {
     # install OpenResty, etcd and some compilation tools
-    brew install openresty/brew/openresty luarocks lua@5.1 wget curl git pcre openldap
+    brew install openresty/brew/openresty luarocks lua@5.1 wget curl git pcre openldap libxml2 libxslt
+:qa

Review Comment:
   typo?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] kingluo commented on a diff in pull request #7827: feat(saml-auth): add saml-auth plugin

Posted by GitBox <gi...@apache.org>.
kingluo commented on code in PR #7827:
URL: https://github.com/apache/apisix/pull/7827#discussion_r964450601


##########
apisix/cli/ngx_tpl.lua:
##########
@@ -432,6 +436,11 @@ http {
             dns_resolver = dns_resolver,
         }
         apisix.http_init(args)
+
+        {% if enabled_plugins["saml-auth"] then %}
+        local saml = require("apisix.plugins.saml-auth")

Review Comment:
   So where do you prefer to set the data_dir? Define `set_data_dir()` in lua-resty-saml and call it here? The `apisix.constants` is not suitable according to @membphis.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] soulbird commented on a diff in pull request #7827: feat(saml-auth): add saml-auth plugin

Posted by GitBox <gi...@apache.org>.
soulbird commented on code in PR #7827:
URL: https://github.com/apache/apisix/pull/7827#discussion_r961272054


##########
apisix/plugins/saml-auth.lua:
##########
@@ -0,0 +1,92 @@
+--
+-- 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.
+--
+local core = require("apisix.core")
+local resty_saml = require("resty.saml")
+
+local is_resty_saml_init = false
+
+local lrucache = core.lrucache.new({
+    ttl = 300, count = 512
+})
+
+local schema = {
+    type = "object",
+    title = "work with route or service object",
+    properties = {
+        sp_issuer = { type = "string" },
+        idp_uri = { type = "string" },

Review Comment:
   It's just an example, I think it's a bit too rough to only limit the string.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] membphis commented on a diff in pull request #7827: feat(saml-auth): add saml-auth plugin

Posted by GitBox <gi...@apache.org>.
membphis commented on code in PR #7827:
URL: https://github.com/apache/apisix/pull/7827#discussion_r962418405


##########
apisix/cli/ngx_tpl.lua:
##########
@@ -432,6 +436,11 @@ http {
             dns_resolver = dns_resolver,
         }
         apisix.http_init(args)
+
+        {% if enabled_plugins["saml-auth"] then %}
+        local constants = require("apisix.constants")
+        constants.saml_data_dir = "{*apisix_lua_home*}/deps/share/lua/5.1/resty/saml"

Review Comment:
   confused
   Why can't Lua find the default path?



##########
apisix/plugins/saml-auth.lua:
##########
@@ -0,0 +1,92 @@
+--
+-- 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.
+--
+local core = require("apisix.core")
+local constants = require("apisix.constants")
+local resty_saml = require("resty.saml")
+
+local is_resty_saml_init = false
+
+local lrucache = core.lrucache.new({
+    ttl = 300, count = 512
+})
+
+local schema = {
+    type = "object",
+    properties = {
+        sp_issuer = { type = "string" },
+        idp_uri = { type = "string" },
+        idp_cert = { type = "string" },
+        login_callback_uri = { type = "string" },
+        logout_uri = { type = "string" },
+        logout_callback_uri = { type = "string" },
+        logout_redirect_uri = { type = "string" },
+        sp_cert = { type = "string" },
+        sp_private_key = { type = "string" },
+    },
+    required = {
+        "sp_issuer",
+        "idp_uri",
+        "idp_cert",
+        "login_callback_uri",
+        "logout_uri",
+        "logout_callback_uri",
+        "logout_redirect_uri",
+        "sp_cert",
+        "sp_private_key",
+    }
+}
+
+local plugin_name = "saml-auth"
+
+local _M = {
+    version = 0.1,
+    priority = 2598,
+    name = plugin_name,
+    schema = schema,
+}
+
+function _M.check_schema(conf, schema_type)
+    return core.schema.check(schema, conf)
+end
+
+local function create_saml_obj(conf)

Review Comment:
   `local create_saml_obj = resty_saml.new`
   
   which is better



##########
t/APISIX.pm:
##########
@@ -724,6 +729,7 @@ _EOC_
             set \$upstream_no_cache              '';
             $a6_ngx_vars
 
+

Review Comment:
   it is useless, pls remove this line



##########
docs/en/latest/plugins/saml-auth.md:
##########
@@ -0,0 +1,123 @@
+---
+title: saml-auth
+keywords:
+  - APISIX
+  - Plugin
+  - SAML AUTH
+  - saml-auth
+description: This document contains information about the Apache APISIX saml-auth Plugin.
+---
+
+<!--
+#
+# 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.
+#
+-->
+
+## Description
+
+The `saml-auth` Plugin can be used to access SAML (Security Assertion Markup Language 2.0) IdP (Identity Provider)
+to do authentication, from the SP (service provider) perspective.
+
+## Attributes
+
+| Name      | Type | Required      | Description |
+| ----------- | ----------- | ----------- | ----------- |
+| `sp_issuer`      | string       | True      | SP name to access IdP.       |
+| `idp_uri`      | string       | True      | URI of IdP.       |
+| `idp_cert`      | string       | True      | IdP Certificate, used to verify saml response.       |
+| `login_callback_uri`      | string       | True      | redirect uri used to callback the SP from IdP after login.       |
+| `logout_uri`      | string       | True      | logout uri to trigger logout.       |
+| `logout_callback_uri`      | string       | True      | redirect uri used to callback the SP from IdP after logout.       |
+| `logout_redirect_uri`      | string       | True      | redirect uri after successful logout.       |
+| `sp_cert`      | string       | True      | SP Certificate, used to sign the saml request.       |
+| `sp_private_key`      | string       | True      | SP private key.       |
+
+## Enabling the Plugin
+
+You can enable the Plugin on a specific Route as shown below:
+
+```shell
+curl http://127.0.0.1:9180/apisix/admin/routes/test_saml_auth -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
+{
+    "methods": ["GET"],
+    "uri": "/anything/*",
+    "plugins": {
+          "saml-auth": {
+            "idp_cert": "-----BEGIN CERTIFICATE-----\nMIIClzCCAX8CBgGC7zvjWjANBgkqhkiG9w0BAQsFADAPMQ0wCwYDVQQDDAR0ZXN0\nMB4XDTIyMDgzMDE0NDkxNVoXDTMyMDgzMDE0NTA1NVowDzENMAsGA1UEAwwEdGVz\ndDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAJelXTy4j6pP++7Bq9Yp\nmkuDYoBQRhUUO/RNVNOBPitVisNEQgdpKS2CFYLENZePo0xsDrN+5IB/8MTEqo3V\ntVu2BYQtq5tS0SUEgH1TLIt6gcnOuqgmmk3+RfUM3WGaUlbEN/j1C50RepFJm03i\nAM8dWCsCTCnZQLq/T4T7wwEIIOuAfz7Zip9+8HuVTOUoC3SxwVqTNb0pVRvdg7gU\nYh0f4CnckhlZG+4AgFvyW5LXxyUWbBtIpYEJN8DZEbEg+QmjuUHT0w+Apdtr6MSM\nKT87tQMxRlIYCqjmwPB9xjW1eZ9FdTGu81WeIhvs3iwTBoH6qcBR9ZJltY/VXEhg\neGECAwEAATANBgkqhkiG9w0BAQsFAAOCAQEAhbVRy4EoJzr/B84g9V6aAobUHoEF\ntv02G2GS/3UxrcaBXMLUl/tJlgti+PwQGDzIEXP0b0LLdAh/5LZFeDFVbphXUk26\nXxj3T2ej9ZcsRbaF+yHi0iisyMNdx9gbosDYg8yx9+XWImQMe6+T6+7fjYnilWE8\n0nYUuxyf1GF20EwQ0sjcJbxuq/K/2DZfOT8eo8a5c1oJFB20rE+KEpZnhBnDTQ+s\nUXPjN0AYTCKtPiAd7D190Pk1CQUbfAUmeII9WstCJhmGqIeiSpHsa9mAa50GVT3B\nnHm5obfM+sBEmy78MnC+s9Kt/EquakxeEi+YZEVfgyORq+LwNaef5MuedA==\n-----END CERTIFICATE---
 --",

Review Comment:
   we provide an example, it is enough, which is clear:
   
   ```json
    "idp_cert": "-----BEGIN CERTIFICATE----... ...-----END CERTIFICATE-----",
   ```



##########
.github/workflows/fuzzing-ci.yaml:
##########
@@ -50,7 +50,7 @@ jobs:
         sudo apt-get -y install software-properties-common
         sudo add-apt-repository -y "deb http://openresty.org/package/ubuntu $(lsb_release -sc) main"
         sudo apt-get update
-        sudo apt-get install -y git openresty curl openresty-openssl111-dev unzip make gcc libldap2-dev
+        sudo apt-get install -y git openresty curl openresty-openssl111-dev unzip make gcc libldap2-dev libxml2-dev libxslt-dev libssl-dev

Review Comment:
   I don't like this change, it greatly increases the difficulty of building a user's local development environment.



##########
ci/centos7-ci.sh:
##########
@@ -23,7 +23,7 @@ install_dependencies() {
 
     # install build & runtime deps
     yum install -y wget tar gcc automake autoconf libtool make unzip \
-        git sudo openldap-devel which
+        git sudo openldap-devel which libxml2-devel openssl-devel libxslt-devel

Review Comment:
   ditto



##########
ci/centos7-ci.sh:
##########
@@ -84,6 +84,9 @@ run_case() {
     export_or_prefix
     make init
     set_coredns
+
+    wget https://raw.githubusercontent.com/api7/lua-resty-saml/main/t/lib/keycloak.lua -O t/lib/keycloak2.lua

Review Comment:
   it seems hack



##########
apisix/plugins/saml-auth.lua:
##########
@@ -0,0 +1,92 @@
+--
+-- 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.
+--
+local core = require("apisix.core")
+local constants = require("apisix.constants")
+local resty_saml = require("resty.saml")
+
+local is_resty_saml_init = false
+
+local lrucache = core.lrucache.new({
+    ttl = 300, count = 512
+})
+
+local schema = {
+    type = "object",
+    properties = {
+        sp_issuer = { type = "string" },
+        idp_uri = { type = "string" },
+        idp_cert = { type = "string" },
+        login_callback_uri = { type = "string" },
+        logout_uri = { type = "string" },
+        logout_callback_uri = { type = "string" },
+        logout_redirect_uri = { type = "string" },
+        sp_cert = { type = "string" },
+        sp_private_key = { type = "string" },
+    },
+    required = {
+        "sp_issuer",
+        "idp_uri",
+        "idp_cert",
+        "login_callback_uri",
+        "logout_uri",
+        "logout_callback_uri",
+        "logout_redirect_uri",
+        "sp_cert",
+        "sp_private_key",
+    }
+}
+
+local plugin_name = "saml-auth"
+
+local _M = {
+    version = 0.1,
+    priority = 2598,
+    name = plugin_name,
+    schema = schema,
+}
+
+function _M.check_schema(conf, schema_type)
+    return core.schema.check(schema, conf)
+end
+
+local function create_saml_obj(conf)
+    return resty_saml.new(conf)
+end
+
+function _M.rewrite(conf, ctx)
+    if not is_resty_saml_init then
+        local err = resty_saml.init({
+            debug = true,
+            data_dir = constants.saml_data_dir,
+        })
+        if err then
+            core.log.error("saml init: ", err)
+            return 503
+        end
+        is_resty_saml_init = true
+    end
+
+    core.log.info("plugin rewrite phase, conf: ", core.json.delay_encode(conf))
+
+    local saml = core.lrucache.plugin_ctx(lrucache, ctx, nil, create_saml_obj, conf)

Review Comment:
   do we need to check the `saml` object first?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] spacewander commented on a diff in pull request #7827: feat(saml-auth): add saml-auth plugin

Posted by GitBox <gi...@apache.org>.
spacewander commented on code in PR #7827:
URL: https://github.com/apache/apisix/pull/7827#discussion_r964412359


##########
apisix/cli/ngx_tpl.lua:
##########
@@ -432,6 +436,11 @@ http {
             dns_resolver = dns_resolver,
         }
         apisix.http_init(args)
+
+        {% if enabled_plugins["saml-auth"] then %}
+        local saml = require("apisix.plugins.saml-auth")

Review Comment:
   We should not require a plugin here. This will make it unable to disable the plugin.



##########
Makefile:
##########
@@ -151,6 +151,7 @@ deps: runtime
 	$(eval ENV_LUAROCKS_VER := $(shell $(ENV_LUAROCKS) --version | grep -E -o "luarocks [0-9]+."))
 	@if [ '$(ENV_LUAROCKS_VER)' = 'luarocks 3.' ]; then \
 		mkdir -p ~/.luarocks; \
+		$(ENV_LUAROCKS) config $(ENV_LUAROCKS_FLAG_LOCAL) variables.OPENSSL_DIR $(ENV_OPENSSL_PREFIX); \

Review Comment:
   Let's update https://github.com/apache/apisix/blob/6cc120196f302f2c05de91dbaf988b97a4442c4b/utils/linux-install-luarocks.sh#L62 too



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] spacewander commented on a diff in pull request #7827: feat(saml-auth): add saml-auth plugin

Posted by GitBox <gi...@apache.org>.
spacewander commented on code in PR #7827:
URL: https://github.com/apache/apisix/pull/7827#discussion_r962304495


##########
apisix/plugins/saml-auth.lua:
##########
@@ -0,0 +1,92 @@
+--
+-- 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.
+--
+local core = require("apisix.core")
+local resty_saml = require("resty.saml")
+
+local is_resty_saml_init = false
+
+local lrucache = core.lrucache.new({
+    ttl = 300, count = 512
+})
+
+local schema = {
+    type = "object",
+    title = "work with route or service object",
+    properties = {
+        sp_issuer = { type = "string" },
+        idp_uri = { type = "string" },

Review Comment:
   Can any value be suitable as the input? For example, is `""` suitable for sp_issuer?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] spacewander commented on a diff in pull request #7827: feat(saml-auth): add saml-auth plugin

Posted by GitBox <gi...@apache.org>.
spacewander commented on code in PR #7827:
URL: https://github.com/apache/apisix/pull/7827#discussion_r962304261


##########
apisix/cli/ngx_tpl.lua:
##########
@@ -660,6 +664,10 @@ http {
             set $dubbo_method                '';
             {% end %}
 
+            {% if enabled_plugins["saml-auth"] then %}
+            set $saml_data_dir               {*apisix_lua_home*}/deps/share/lua/5.1/resty/saml;

Review Comment:
   Look like we can put the generated saml_data_dir in the Lua code? So there is no need to use extra variable.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] kingluo commented on a diff in pull request #7827: feat(saml-auth): add saml-auth plugin

Posted by GitBox <gi...@apache.org>.
kingluo commented on code in PR #7827:
URL: https://github.com/apache/apisix/pull/7827#discussion_r966607702


##########
apisix/cli/ngx_tpl.lua:
##########
@@ -432,6 +436,11 @@ http {
             dns_resolver = dns_resolver,
         }
         apisix.http_init(args)
+
+        {% if enabled_plugins["saml-auth"] then %}
+        local saml = require("apisix.plugins.saml-auth")

Review Comment:
   @membphis No matter which place to init the plugin, we need to determine `apisix_home` before apisix start, because saml init needs the absolute path of `xsd/` dir.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] kingluo commented on a diff in pull request #7827: feat(saml-auth): add saml-auth plugin

Posted by GitBox <gi...@apache.org>.
kingluo commented on code in PR #7827:
URL: https://github.com/apache/apisix/pull/7827#discussion_r966651048


##########
apisix/cli/ngx_tpl.lua:
##########
@@ -432,6 +436,11 @@ http {
             dns_resolver = dns_resolver,
         }
         apisix.http_init(args)
+
+        {% if enabled_plugins["saml-auth"] then %}
+        local saml = require("apisix.plugins.saml-auth")

Review Comment:
   That's ok.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] spacewander commented on a diff in pull request #7827: feat(saml-auth): add saml-auth plugin

Posted by GitBox <gi...@apache.org>.
spacewander commented on code in PR #7827:
URL: https://github.com/apache/apisix/pull/7827#discussion_r966633760


##########
apisix/cli/ngx_tpl.lua:
##########
@@ -432,6 +436,11 @@ http {
             dns_resolver = dns_resolver,
         }
         apisix.http_init(args)
+
+        {% if enabled_plugins["saml-auth"] then %}
+        local saml = require("apisix.plugins.saml-auth")

Review Comment:
   What about putting the apisix_lua_home in the constants module, so we can use it in the plugin?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org