2019-10-31 09:27:28 +08:00
|
|
|
<!--
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
-->
|
|
|
|
|
2019-11-24 21:15:39 +08:00
|
|
|
[Chinese](ip-restriction-cn.md)
|
2019-09-03 13:53:41 +08:00
|
|
|
|
|
|
|
# Summary
|
|
|
|
- [**Name**](#name)
|
|
|
|
- [**Attributes**](#attributes)
|
|
|
|
- [**How To Enable**](#how-to-enable)
|
|
|
|
- [**Test Plugin**](#test-plugin)
|
|
|
|
- [**Disable Plugin**](#disable-plugin)
|
|
|
|
|
|
|
|
|
|
|
|
## Name
|
|
|
|
|
|
|
|
The `ip-restriction` can restrict access to a Service or a Route by either
|
|
|
|
whitelisting or blacklisting IP addresses. Single IPs, multiple IPs or ranges
|
|
|
|
in CIDR notation like 10.10.10.0/24 can be used(will support IPv6 soon).
|
|
|
|
|
|
|
|
## Attributes
|
|
|
|
|
2019-11-24 21:15:39 +08:00
|
|
|
|Name |Requirement |Description|
|
2019-09-03 13:53:41 +08:00
|
|
|
|---------|--------|-----------|
|
2019-11-24 21:15:39 +08:00
|
|
|
|whitelist|optional |List of IPs or CIDR ranges to whitelist|
|
|
|
|
|blacklist|optional |List of IPs or CIDR ranges to blacklist|
|
2019-09-03 13:53:41 +08:00
|
|
|
|
|
|
|
One of `whitelist` or `blacklist` must be specified, and they can not work
|
|
|
|
together.
|
|
|
|
|
|
|
|
## How To Enable
|
|
|
|
|
2019-11-24 21:15:39 +08:00
|
|
|
Creates a route or service object, and enable plugin `ip-restriction`.
|
2019-09-03 13:53:41 +08:00
|
|
|
|
|
|
|
```shell
|
2020-03-05 14:48:27 +08:00
|
|
|
curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
|
2019-09-03 13:53:41 +08:00
|
|
|
{
|
2019-09-23 06:35:58 +08:00
|
|
|
"uri": "/index.html",
|
2019-09-03 13:53:41 +08:00
|
|
|
"upstream": {
|
|
|
|
"type": "roundrobin",
|
|
|
|
"nodes": {
|
|
|
|
"127.0.0.1:1980": 1
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"plugins": {
|
|
|
|
"ip-restriction": {
|
|
|
|
"whitelist": [
|
|
|
|
"127.0.0.1",
|
|
|
|
"113.74.26.106/24"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
```
|
|
|
|
|
|
|
|
## Test Plugin
|
|
|
|
|
|
|
|
Requests to `127.0.0.1`:
|
|
|
|
|
|
|
|
```shell
|
|
|
|
$ curl http://127.0.0.1:9080/index.html
|
|
|
|
HTTP/1.1 200 OK
|
|
|
|
...
|
|
|
|
```
|
|
|
|
|
|
|
|
Requests to `127.0.0.2`:
|
|
|
|
|
|
|
|
```shell
|
|
|
|
$ curl http://127.0.0.2:9080/index.html -i
|
2019-09-23 06:35:58 +08:00
|
|
|
HTTP/1.1 403 Forbidden
|
2019-09-03 13:53:41 +08:00
|
|
|
...
|
|
|
|
{"message":"Your IP address is not allowed"}
|
|
|
|
```
|
|
|
|
|
2020-03-08 13:57:31 +08:00
|
|
|
## Change the restriction
|
|
|
|
|
|
|
|
When you want to change the whitelisted ip, it is very simple,
|
|
|
|
you can send the corresponding json configuration in the plugin configuration,
|
|
|
|
no need to restart the service, it will take effect immediately:
|
|
|
|
|
|
|
|
```shell
|
|
|
|
curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
|
|
|
|
{
|
|
|
|
"uri": "/index.html",
|
|
|
|
"upstream": {
|
|
|
|
"type": "roundrobin",
|
|
|
|
"nodes": {
|
|
|
|
"127.0.0.1:1980": 1
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"plugins": {
|
|
|
|
"ip-restriction": {
|
|
|
|
"whitelist": [
|
|
|
|
"127.0.0.2",
|
|
|
|
"113.74.26.106/24"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
```
|
|
|
|
|
|
|
|
## Test Plugin after restriction change
|
|
|
|
|
|
|
|
Requests to `127.0.0.2`:
|
|
|
|
|
|
|
|
```shell
|
|
|
|
$ curl http://127.0.0.2:9080/index.html
|
|
|
|
HTTP/1.1 200 OK
|
|
|
|
...
|
|
|
|
```
|
|
|
|
|
|
|
|
Requests to `127.0.0.1`:
|
|
|
|
|
|
|
|
```shell
|
|
|
|
$ curl http://127.0.0.1:9080/index.html -i
|
|
|
|
HTTP/1.1 403 Forbidden
|
|
|
|
...
|
|
|
|
{"message":"Your IP address is not allowed"}
|
|
|
|
```
|
|
|
|
|
|
|
|
|
2019-09-03 13:53:41 +08:00
|
|
|
## Disable Plugin
|
|
|
|
|
|
|
|
When you want to disable the `ip-restriction` plugin, it is very simple,
|
2020-03-08 13:57:31 +08:00
|
|
|
you can delete the corresponding json configuration in the plugin configuration,
|
|
|
|
no need to restart the service, it will take effect immediately:
|
2019-09-03 13:53:41 +08:00
|
|
|
|
|
|
|
```shell
|
|
|
|
$ curl http://127.0.0.1:2379/v2/keys/apisix/routes/1 -X PUT -d value='
|
|
|
|
{
|
|
|
|
"uri": "/index.html",
|
|
|
|
"plugins": {},
|
|
|
|
"upstream": {
|
|
|
|
"type": "roundrobin",
|
|
|
|
"nodes": {
|
|
|
|
"39.97.63.215:80": 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
```
|
|
|
|
|
|
|
|
The `ip-restriction` plugin has been disabled now. It works for other plugins.
|