You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by "soulbird (via GitHub)" <gi...@apache.org> on 2023/03/24 08:48:52 UTC

[GitHub] [apisix] soulbird commented on a diff in pull request #9022: feat: add ldap-auth-advanced plugin

soulbird commented on code in PR #9022:
URL: https://github.com/apache/apisix/pull/9022#discussion_r1147252229


##########
apisix/plugins/ldap-auth-advanced.lua:
##########
@@ -0,0 +1,308 @@
+--
+-- 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 consumer_mod = require("apisix.consumer")
+local ok, ldap_cli = pcall(require, "resty.ldap.client")
+local ldap_proto   = require("resty.ldap.protocol")
+
+local pcall         = pcall
+local ipairs        = ipairs
+local str_find      = core.string.find
+local str_format    = string.format
+local decode_base64 = ngx.decode_base64
+local ngx_re_match  = ngx.re.match
+local ngx_re_gsub   = ngx.re.gsub
+local ngx_re_split  = require("ngx.re").split
+
+local LDAP_SEARCH_SCOPE_BASE_OBJECT = ldap_proto.SEARCH_SCOPE_BASE_OBJECT
+local LDAP_SEARCH_DEREF_ALIASES_ALWAYS = ldap_proto.SEARCH_DEREF_ALIASES_ALWAYS
+
+local auth_header_lrucache = core.lrucache.new({
+    ttl = 300, count = 512
+})
+
+local schema = {
+    type = "object",
+    properties = {
+        ldap_uri = { type = "string" },
+        use_ldaps = {
+            type = "boolean",
+            default = false
+        },
+        use_starttls = {
+            type = "boolean",
+            default = false
+        },
+        ssl_verify = {
+            type = "boolean",
+            default = true,
+        },
+        timeout = {
+            type = "integer",
+            minimum = 1,
+            maximum = 60000,
+            default = 3000,
+            description = "timeout in milliseconds",
+        },
+        keepalive = {type = "boolean", default = true},
+        keepalive_timeout = {type = "integer", minimum = 1000, default = 60000},
+        keepalive_pool_size = {type = "integer", minimum = 1, default = 5},
+        keepalive_pool_name = {type = "string", default = nil},
+        ldap_debug = {
+            type = "boolean",
+            default = false,
+        },
+        hide_credentials = {
+            type = "boolean",
+            default = false,
+        },
+        consumer_required = {
+            type = "boolean",
+            default = true,
+        },
+        user_dn_template = {
+            type = "string",
+        },
+        user_membership_attribute = {
+            type = "string",
+            default = "memberOf",
+        }
+    },
+    required = {"ldap_uri", "user_dn_template"}
+}
+
+
+local consumer_schema = {
+    type = "object",
+    properties = {
+        user_dn = { type = "string" },
+        group_dn = { type = "string" },
+    },
+    oneOf = {
+        {required = {"user_dn"}},
+        {required = {"group_dn"}},
+    }
+}
+
+local plugin_name = "ldap-auth-advanced"
+
+local _M = {
+    version = 0.1,
+    priority = 2541,
+    type = "auth",
+    name = plugin_name,
+    schema = schema,
+    consumer_schema = consumer_schema,
+}
+
+
+function _M.check_schema(conf, schema_type)
+    if schema_type == core.schema.TYPE_CONSUMER then
+        return core.schema.check(consumer_schema, conf)
+    end
+
+    local ok, err = core.schema.check(schema, conf)
+    if not ok then
+        return ok, err
+    end
+
+    -- ensure %s in template
+    if not str_find(conf.user_dn_template, "%s") or not ok then
+        return false, "User DN template doesn't contain " ..
+                        "the %s placeholder for the uid variable"
+    end
+
+    -- ensure only one %s in template
+    if not pcall(str_format, conf.user_dn_template, "username") then
+        return false, "User DN template has more than one " ..
+                        "placeholder %s for the username variable."
+    end
+
+    if conf.use_starttls and conf.use_ldaps then
+        return false, "STARTTLS and LDAPS cannot be open at the same time"
+    end
+
+    return true
+end
+
+
+local function extract_auth_header(authorization)

Review Comment:
   This function is used multiple times in different plugins, can we put them in a unified location. Maybe record a todo



##########
apisix/plugins/ldap-auth-advanced.lua:
##########
@@ -0,0 +1,308 @@
+--
+-- 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 consumer_mod = require("apisix.consumer")
+local ok, ldap_cli = pcall(require, "resty.ldap.client")
+local ldap_proto   = require("resty.ldap.protocol")
+
+local pcall         = pcall
+local ipairs        = ipairs
+local str_find      = core.string.find
+local str_format    = string.format
+local decode_base64 = ngx.decode_base64
+local ngx_re_match  = ngx.re.match
+local ngx_re_gsub   = ngx.re.gsub
+local ngx_re_split  = require("ngx.re").split
+
+local LDAP_SEARCH_SCOPE_BASE_OBJECT = ldap_proto.SEARCH_SCOPE_BASE_OBJECT
+local LDAP_SEARCH_DEREF_ALIASES_ALWAYS = ldap_proto.SEARCH_DEREF_ALIASES_ALWAYS
+
+local auth_header_lrucache = core.lrucache.new({
+    ttl = 300, count = 512
+})
+
+local schema = {
+    type = "object",
+    properties = {
+        ldap_uri = { type = "string" },
+        use_ldaps = {
+            type = "boolean",
+            default = false
+        },
+        use_starttls = {
+            type = "boolean",
+            default = false
+        },
+        ssl_verify = {
+            type = "boolean",
+            default = true,
+        },
+        timeout = {
+            type = "integer",
+            minimum = 1,
+            maximum = 60000,
+            default = 3000,
+            description = "timeout in milliseconds",
+        },
+        keepalive = {type = "boolean", default = true},
+        keepalive_timeout = {type = "integer", minimum = 1000, default = 60000},
+        keepalive_pool_size = {type = "integer", minimum = 1, default = 5},
+        keepalive_pool_name = {type = "string", default = nil},
+        ldap_debug = {
+            type = "boolean",
+            default = false,
+        },
+        hide_credentials = {
+            type = "boolean",
+            default = false,
+        },
+        consumer_required = {
+            type = "boolean",
+            default = true,
+        },
+        user_dn_template = {
+            type = "string",
+        },
+        user_membership_attribute = {
+            type = "string",
+            default = "memberOf",
+        }
+    },
+    required = {"ldap_uri", "user_dn_template"}
+}
+
+
+local consumer_schema = {
+    type = "object",
+    properties = {
+        user_dn = { type = "string" },
+        group_dn = { type = "string" },
+    },
+    oneOf = {
+        {required = {"user_dn"}},
+        {required = {"group_dn"}},
+    }
+}
+
+local plugin_name = "ldap-auth-advanced"
+
+local _M = {
+    version = 0.1,
+    priority = 2541,
+    type = "auth",
+    name = plugin_name,
+    schema = schema,
+    consumer_schema = consumer_schema,
+}
+
+
+function _M.check_schema(conf, schema_type)
+    if schema_type == core.schema.TYPE_CONSUMER then
+        return core.schema.check(consumer_schema, conf)
+    end
+
+    local ok, err = core.schema.check(schema, conf)
+    if not ok then
+        return ok, err
+    end
+
+    -- ensure %s in template
+    if not str_find(conf.user_dn_template, "%s") or not ok then
+        return false, "User DN template doesn't contain " ..
+                        "the %s placeholder for the uid variable"
+    end
+
+    -- ensure only one %s in template
+    if not pcall(str_format, conf.user_dn_template, "username") then
+        return false, "User DN template has more than one " ..
+                        "placeholder %s for the username variable."
+    end
+
+    if conf.use_starttls and conf.use_ldaps then
+        return false, "STARTTLS and LDAPS cannot be open at the same time"
+    end
+
+    return true
+end
+
+
+local function extract_auth_header(authorization)
+    local function do_extract(auth)
+        local obj = { username = "", password = "" }
+
+        local m, err = ngx_re_match(auth, "Basic\\s(.+)", "jo")
+        if err then
+            -- error authorization
+            return nil, err
+        end
+
+        if not m then

Review Comment:
   These two judgments can be combined



-- 
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