Skip to content

Commit

Permalink
docs: add public-api related content (#6218)
Browse files Browse the repository at this point in the history
  • Loading branch information
bzp2010 authored Feb 8, 2022
1 parent 75ed53e commit ec0fc2c
Show file tree
Hide file tree
Showing 17 changed files with 150 additions and 136 deletions.
7 changes: 2 additions & 5 deletions docs/en/latest/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@
"plugins/ua-restriction",
"plugins/referer-restriction",
"plugins/consumer-restriction",
"plugins/csrf"
"plugins/csrf",
"plugins/public-api"
]
},
{
Expand Down Expand Up @@ -243,10 +244,6 @@
"type": "doc",
"id": "wasm"
},
{
"type": "doc",
"id": "plugin-interceptors"
},
{
"type": "link",
"label": "CODE_STYLE",
Expand Down
3 changes: 1 addition & 2 deletions docs/en/latest/plugin-develop.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
55 changes: 0 additions & 55 deletions docs/en/latest/plugin-interceptors.md

This file was deleted.

2 changes: 1 addition & 1 deletion docs/en/latest/plugins/batch-requests.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion docs/en/latest/plugins/jwt-auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion docs/en/latest/plugins/node-status.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 3 additions & 4 deletions docs/en/latest/plugins/prometheus.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,16 @@ 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:
prometheus:
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.
Expand Down
132 changes: 132 additions & 0 deletions docs/en/latest/plugins/public-api.md
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion docs/en/latest/plugins/wolf-rbac.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 0 additions & 4 deletions docs/zh/latest/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,6 @@
"type": "doc",
"id": "plugin-develop"
},
{
"type": "doc",
"id": "plugin-interceptors"
},
{
"type": "doc",
"id": "CODE_STYLE"
Expand Down
3 changes: 1 addition & 2 deletions docs/zh/latest/plugin-develop.md
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,7 @@ function _M.api()
end
```

注意注册的接口会暴露到外网。
你可能需要使用 [interceptors](plugin-interceptors.md) 来保护它。
注意,注册的接口将不会默认暴露,需要使用[public-api 插件](../../en/latest/plugins/public-api.md)来暴露它。

## 注册控制接口

Expand Down
50 changes: 0 additions & 50 deletions docs/zh/latest/plugin-interceptors.md

This file was deleted.

3 changes: 1 addition & 2 deletions docs/zh/latest/plugins/batch-requests.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) 插件来暴露它。

## 如何启用

Expand Down
3 changes: 1 addition & 2 deletions docs/zh/latest/plugins/jwt-auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) 插件来暴露它。

## 如何启用

Expand Down
2 changes: 1 addition & 1 deletion docs/zh/latest/plugins/node-status.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ title: node-status

## 插件接口

插件增加接口 `/apisix/status`可通过 [interceptors](../plugin-interceptors.md) 保护该接口
插件增加接口 `/apisix/status`需要通过 [public-api](../../../en/latest/plugins/public-api.md) 插件来暴露它

## 启用插件

Expand Down
7 changes: 3 additions & 4 deletions docs/zh/latest/plugins/prometheus.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,16 @@ plugin_attr:

假设环境变量 `INTRANET_IP``172.1.1.1`,现在 APISIX 会在 `172.1.1.1:9092` 上暴露指标。

**在 2.6 版本之前,指标会直接暴露到数据面的端口上,你可能需要通过 [interceptors](../plugin-interceptors.md)
来保护它。**

如果你依然想要这样的行为,你可以这么配置:
如果你依然想要让指标暴露在数据面的端口(默认:9080)上,你可以这么配置:

```
plugin_attr:
prometheus:
enable_export_server: false
```

同时,您还需要使用 [public-api](../../../en/latest/plugins/public-api.md) 插件来暴露它。

## 如何开启插件

`prometheus` 插件可以使用空 {} 开启。
Expand Down
Loading

0 comments on commit ec0fc2c

Please sign in to comment.