docs: add public-api related content (#6218)

This commit is contained in:
Zeping Bai 2022-02-08 11:42:29 +08:00 committed by GitHub
parent 75ed53e88d
commit ec0fc2ceaf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 150 additions and 136 deletions

View File

@ -82,7 +82,8 @@
"plugins/ua-restriction",
"plugins/referer-restriction",
"plugins/consumer-restriction",
"plugins/csrf"
"plugins/csrf",
"plugins/public-api"
]
},
{
@ -243,10 +244,6 @@
"type": "doc",
"id": "wasm"
},
{
"type": "doc",
"id": "plugin-interceptors"
},
{
"type": "link",
"label": "CODE_STYLE",

View File

@ -409,8 +409,7 @@ function _M.api()
end
```
Note that the public API is exposed to the public.
You may need to use [interceptors](plugin-interceptors.md) to protect it.
Note that the public API will not be exposed by default, you will need to use the [public-api plugin](plugins/public-api.md) to expose it.
## register control API

View File

@ -1,55 +0,0 @@
---
title: Plugin interceptors
---
<!--
#
# 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.
#
-->
Some plugins will register API to serve their purposes.
Since these API are not added as regular [Route](admin-api.md), we can't add
plugins to protect them. To solve the problem, we add a new concept called 'interceptors'
to run rules to protect them.
Here is an example to limit the access of `/apisix/prometheus/metrics` (a route introduced via plugin prometheus)
to clients in `10.0.0.0/24`:
```shell
$ curl http://127.0.0.1:9080/apisix/admin/plugin_metadata/prometheus -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -i -X PUT -d '
{
"interceptors": [
{
"name": "ip-restriction",
"conf": {
"whitelist": ["10.0.0.0/24"]
}
}
]
}'
```
You can see that the interceptors are configured like the plugins. The `name` is
the name of plugin which you want to run and the `conf` is the configuration of the
plugin.
Currently we only support a subset of plugins which can be run as interceptors.
Supported interceptors:
* [ip-restriction](./plugins/ip-restriction.md)

View File

@ -47,7 +47,7 @@ None
## API
This plugin will add `/apisix/batch-requests` as the endpoint.
You may need to use [interceptors](../plugin-interceptors.md) to protect it.
You may need to use [public-api](public-api.md) plugin to expose it.
## How To Enable

View File

@ -62,7 +62,7 @@ For more information on JWT, refer to [JWT](https://jwt.io/) for more informatio
## API
This plugin will add `/apisix/plugin/jwt/sign` to sign.
You may need to use [interceptors](../plugin-interceptors.md) to protect it.
You may need to use [public-api](public-api.md) plugin to expose it.
## How To Enable

View File

@ -41,7 +41,7 @@ None
## API
This plugin will add `/apisix/status` to get status information.
You may need to use [interceptors](../plugin-interceptors.md) to protect it.
You may need to use [public-api](public-api.md) plugin to expose it.
## How To Enable

View File

@ -46,10 +46,7 @@ plugin_attr:
Assume environment variable `INTRANET_IP` is `172.1.1.1`, now APISIX will export the metrics via `172.1.1.1:9092`.
**Before version `2.6`, the metrics are exposed via the data plane port,
you may need to use [interceptors](../plugin-interceptors.md) to protect it.**
If you still want this behavior, you can configure it like this:
If you still want to expose the metrics via the data plane port (default: 9080), you can configure it like this:
```
plugin_attr:
@ -57,6 +54,8 @@ plugin_attr:
enable_export_server: false
```
You may need to use [public-api](public-api.md) plugin to expose it.
## How to enable it
`prometheus` plugin could be enable with empty table.

View File

@ -0,0 +1,132 @@
---
title: public-api
---
<!--
#
# 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
- [**Description**](#description)
- [**Attributes**](#attributes)
- [**Example**](#example)
## Description
The `public-api` plugin is used to enhance the plugin public API access control.
When current users develop custom plugins, they can register some public APIs for fixed functionality, such as the `/apisix/plugin/jwt/sign` API in `jwt-auth`. These APIs can only apply limited plugins for access control (currently only `ip-restriction`) by way of plugin interceptors.
With the `public-api` plugin, we put all public APIs into the general HTTP API router, which is consistent with the normal Route registered by the user and can apply any plugin. The public API added in the user plugin is no longer expose by default by APISIX, and the user has to manually configure the Route for it, the user can configure any uri and plugin.
## Attributes
| Name | Type | Requirement | Default | Valid | Description |
| -- | -- | -- | -- | -- | -- |
| uri | string | optional | "" | | The uri of the public API. When you set up the route, you can use this to configure the original API uri if it is used in a way that is inconsistent with the original public API uri. |
## Example
We take the `jwt-auth` token sign API as an example to show how to configure the `public-api` plugin. Also, the `key-auth` will be used to show how to configure the protection plugin for the public API.
### Prerequisites
The use of key-auth and jwt-auth requires the configuration of a consumer that contains the configuration of these plugins, and you need to create one in advance, the process will be omitted here.
### Basic Use Case
First we will setup a route.
```shell
$ curl -X PUT 'http://127.0.0.1:9080/apisix/admin/routes/r1' \
-H 'X-API-KEY: <api-key>' \
-H 'Content-Type: application/json' \
-d '{
"uri": "/apisix/plugin/jwt/sign",
"plugins": {
"public-api": {}
}
}'
```
Let's test it.
```shell
$ curl 'http://127.0.0.1:9080/apisix/plugin/jwt/sign?key=user-key'
```
It will respond to a text JWT.
### Customize URI
Let's setup another route.
```shell
$ curl -X PUT 'http://127.0.0.1:9080/apisix/admin/routes/r2' \
-H 'X-API-KEY: <api-key>' \
-H 'Content-Type: application/json' \
-d '{
"uri": "/gen_token",
"plugins": {
"public-api": {
"uri": "/apisix/plugin/jwt/sign"
}
}
}'
```
Let's test it.
```shell
$ curl 'http://127.0.0.1:9080/gen_token?key=user-key'
```
It will still respond to a text JWT. We can see that users are free to configure URI for the public API to match.
### Protect Route
Let's modify the last route and add `key-auth` authentication to it.
```shell
$ curl -X PUT 'http://127.0.0.1:9080/apisix/admin/routes/r2' \
-H 'X-API-KEY: <api-key>' \
-H 'Content-Type: application/json' \
-d '{
"uri": "/gen_token",
"plugins": {
"public-api": {
"uri": "/apisix/plugin/jwt/sign"
},
"key-auth": {}
}
}'
```
Let's test it.
```shell
$ curl -i 'http://127.0.0.1:9080/gen_token?key=user-key'
-H "apikey: test-apikey"
HTTP/1.1 200 OK
# Failed request
$ curl -i 'http://127.0.0.1:9080/gen_token?key=user-key'
HTTP/1.1 401 UNAUTHORIZED
```
It will still respond to a text JWT. If we don't add `apikey` to the request header, it will respond with a 401 block request. In this way, we have implemented a plugin approach to protect the public API.

View File

@ -51,7 +51,7 @@ This plugin will add several API:
* /apisix/plugin/wolf-rbac/change_pwd
* /apisix/plugin/wolf-rbac/user_info
You may need to use [interceptors](../plugin-interceptors.md) to protect it.
You may need to use [public-api](public-api.md) plugin to expose it.
## Dependencies

View File

@ -225,10 +225,6 @@
"type": "doc",
"id": "plugin-develop"
},
{
"type": "doc",
"id": "plugin-interceptors"
},
{
"type": "doc",
"id": "CODE_STYLE"

View File

@ -326,8 +326,7 @@ function _M.api()
end
```
注意注册的接口会暴露到外网。
你可能需要使用 [interceptors](plugin-interceptors.md) 来保护它。
注意,注册的接口将不会默认暴露,需要使用[public-api 插件](../../en/latest/plugins/public-api.md)来暴露它。
## 注册控制接口

View File

@ -1,50 +0,0 @@
---
title: 插件拦截器
---
<!--
#
# 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.
#
-->
有些插件为实现它的功能会注册额外的接口。
由于这些接口不是通过 admin API 添加的,所以没办法像管理 Route 那样管理它们。为了能够保护这些接口不被利用,我们引入了 interceptors 的概念。
下面是通过 interceptors 来保护由 prometheus 插件引入的 `/apisix/prometheus/metrics` 接口,限定只能由 `10.0.0.0/24` 网段的用户访问:
```shell
$ curl http://127.0.0.1:9080/apisix/admin/plugin_metadata/prometheus -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -i -X PUT -d '
{
"interceptors": [
{
"name": "ip-restriction",
"conf": {
"whitelist": ["10.0.0.0/24"]
}
}
]
}'
```
我们能看到配置 interceptors 就像配置 plugin 一样name 是 interceptor 的名称,而 conf 是它的配置。
当前我们只支持一部分插件作为 interceptor 运行。
支持的 interceptor
* [ip-restriction](./plugins/ip-restriction.md)

View File

@ -52,8 +52,7 @@ title: batch-requests
## 接口
插件会增加 `/apisix/batch-requests` 这个接口,你可能需要通过 [interceptors](../plugin-interceptors.md)
来保护它。
插件会增加 `/apisix/batch-requests` 这个接口,需要通过 [public-api](../../../en/latest/plugins/public-api.md) 插件来暴露它。
## 如何启用

View File

@ -51,8 +51,7 @@ title: jwt-auth
## 接口
插件会增加 `/apisix/plugin/jwt/sign` 这个接口,你可能需要通过 [interceptors](../plugin-interceptors.md)
来保护它。
插件会增加 `/apisix/plugin/jwt/sign` 这个接口,需要通过 [public-api](../../../en/latest/plugins/public-api.md) 插件来暴露它。
## 如何启用

View File

@ -40,7 +40,7 @@ title: node-status
## 插件接口
插件增加接口 `/apisix/status`可通过 [interceptors](../plugin-interceptors.md) 保护该接口
插件增加接口 `/apisix/status`需要通过 [public-api](../../../en/latest/plugins/public-api.md) 插件来暴露它
## 启用插件

View File

@ -46,10 +46,7 @@ plugin_attr:
假设环境变量 `INTRANET_IP``172.1.1.1`,现在 APISIX 会在 `172.1.1.1:9092` 上暴露指标。
**在 2.6 版本之前,指标会直接暴露到数据面的端口上,你可能需要通过 [interceptors](../plugin-interceptors.md)
来保护它。**
如果你依然想要这样的行为,你可以这么配置:
如果你依然想要让指标暴露在数据面的端口默认9080你可以这么配置
```
plugin_attr:
@ -57,6 +54,8 @@ plugin_attr:
enable_export_server: false
```
同时,您还需要使用 [public-api](../../../en/latest/plugins/public-api.md) 插件来暴露它。
## 如何开启插件
`prometheus` 插件可以使用空 {} 开启。

View File

@ -51,7 +51,7 @@ rbac 功能由 [wolf](https://github.com/iGeeky/wolf) 提供, 有关 `wolf` 的
* /apisix/plugin/wolf-rbac/change_pwd
* /apisix/plugin/wolf-rbac/user_info
你可能需要通过 [interceptors](../plugin-interceptors.md) 来保护它们
需要通过 [public-api](../../../en/latest/plugins/public-api.md) 插件来暴露它
## 依赖项