tokers commented on a change in pull request #4710: URL: https://github.com/apache/apisix/pull/4710#discussion_r680495886
########## File path: docs/en/latest/plugins/authz-casbin.md ########## @@ -0,0 +1,205 @@ +--- +title: authz-casbin +--- + +<!-- +# +# 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. +# +--> + +## Summary + +- [**Name**](#name) +- [**Attributes**](#attributes) +- [**Metadata**](#metadata) +- [**How To Enable**](#how-to-enable) +- [**Test Plugin**](#test-plugin) +- [**Disable Plugin**](#disable-plugin) +- [**Examples**](#examples) + +## Name + +`authz-casbin` is an authorization plugin based on [Lua Casbin](https://github.com/casbin/lua-casbin/). This plugin supports powerful authorization scenarios based on various access control models. + +For detailed documentation on how to create model and policy, refer [Casbin](https://casbin.org/docs/en/supported-models). + +## Attributes + +| Name | Type | Requirement | Default | Valid | Description | +| ----------- | ------ | ----------- | ------- | ----- | ------------------------------------------------------------ | +| model_path | string | required | | | The path of the Casbin model configuration file. | +| policy_path | string | required | | | The path of the Casbin policy file. | +| username | string | required | | | The header you will be using in request to pass the username (subject). | + +**NOTE**: You must either specify both the `model_path` and `policy_path` in plugin config or specify both the `model` and `policy` in the plugin metadata for the schema to be valid. + +## Metadata + +| Name | Type | Requirement | Default | Valid | Description | +| ----------- | ------ | ----------- | ------- | ----- | ---------------------------------------------------------------------- | +| model | string | required | | | The Casbin model configuration in text format. | +| policy | string | required | | | The Casbin policy in text format. | + +## How To Enable + +You can enable the plugin on any route either by using the model/policy file paths or directly using the model/policy text. + +### By using file paths + +```shell +curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d ' +{ + "plugins": { + "authz-casbin": { + "model_path": "/path/to/model.conf", + "policy_path": "/path/to/policy.csv", + "username": "user" + } + }, + "upstream": { + "nodes": { + "127.0.0.1:1980": 1 + }, + "type": "roundrobin" + }, + "uri": "/*" +}' +``` + +This will create a Casbin enforcer from the model and policy files at your first request. + +### By using model/policy text Review comment: Please specify the precedence between this one and the path way. ########## File path: apisix/plugins/authz-casbin.lua ########## @@ -0,0 +1,119 @@ +-- +-- 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 casbin = require("casbin") +local core = require("apisix.core") +local plugin = require("apisix.plugin") +local ngx = ngx +local get_headers = ngx.req.get_headers +local lrucache = core.lrucache.new({ + ttl = 300, count = 32 +}) + +local plugin_name = "authz-casbin" + +local schema = { + type = "object", + properties = { + model_path = { type = "string" }, + policy_path = { type = "string" }, + username = { type = "string"} + }, + required = {"model_path", "policy_path", "username"}, + additionalProperties = false +} + +local metadata_schema = { + type = "object", + properties = { + model = {type = "string"}, + policy = {type = "string"}, + }, + required = {"model", "policy"}, + additionalProperties = false +} + +local _M = { + version = 0.1, + priority = 2560, + name = plugin_name, + schema = schema, + metadata_schema = metadata_schema +} + +function _M.check_schema(conf, schema_type) + if schema_type == core.schema.TYPE_METADATA then + return core.schema.check(metadata_schema, conf) + end + local ok, err = core.schema.check(schema, conf) + if ok then + return true + else + local metadata = plugin.plugin_metadata(plugin_name) + if metadata and metadata.value.model and metadata.value.policy and conf.username then + return true + end + end + return false, err +end + + +local function new_enforcer(conf) + local model_path = conf.model_path + local policy_path = conf.policy_path + + local e + + if model_path and policy_path then + e = casbin:new(model_path, policy_path) + e.type = "file" + end + + local metadata = plugin.plugin_metadata(plugin_name) + if metadata and metadata.value.model and metadata.value.policy and not e then + local model = metadata.value.model + local policy = metadata.value.policy + e = casbin:newEnforcerFromText(model, policy) + e.type = "metadata" + end + + return e +end + + +function _M.rewrite(conf) + -- creates an enforcer when request sent for the first time + + local metadata = plugin.plugin_metadata(plugin_name) + local casbin_enforcer = lrucache(plugin_name, metadata.modifiedIndex, new_enforcer, conf) + + local path = ngx.var.request_uri + local method = ngx.var.request_method + local username = get_headers()[conf.username] + if not username then username = "anonymous" end + + if path and method and username then Review comment: We don't need to check the existence for these three variables if we use them in the HTTP sub-system. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
