-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add plugin attach-consmer-label #11604
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a32707c
feat: add plugin attach-consumer-label
dspo a18bde6
doc lint
dspo d8f5661
doc lint
dspo 119eacb
index doc
dspo d573d07
translate
dspo 90ee5b7
local pairs
dspo d00f112
add "attach-consumer-label" to config.lua
dspo 8c00439
Merge remote-tracking branch 'origin/master' into dspo/attach-consume…
dspo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
-- | ||
-- 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 pairs = pairs | ||
local plugin_name = "attach-consumer-label" | ||
|
||
local schema = { | ||
type = "object", | ||
properties = { | ||
headers = { | ||
type = "object", | ||
additionalProperties = { | ||
type = "string", | ||
pattern = "^\\$.*" | ||
}, | ||
minProperties = 1 | ||
}, | ||
}, | ||
required = {"headers"}, | ||
} | ||
|
||
local _M = { | ||
version = 0.1, | ||
priority = 2399, | ||
name = plugin_name, | ||
schema = schema, | ||
} | ||
|
||
function _M.check_schema(conf) | ||
return core.schema.check(schema, conf) | ||
end | ||
|
||
function _M.before_proxy(conf, ctx) | ||
-- check if the consumer is exists in the context | ||
if not ctx.consumer then | ||
return | ||
end | ||
|
||
local labels = ctx.consumer.labels | ||
core.log.info("consumer username: ", ctx.consumer.username, " labels: ", | ||
core.json.delay_encode(labels)) | ||
if not labels then | ||
return | ||
end | ||
|
||
for header, label_key in pairs(conf.headers) do | ||
-- remove leading $ character | ||
local label_value = labels[label_key:sub(2)] | ||
core.request.set_header(ctx, header, label_value) | ||
end | ||
end | ||
|
||
return _M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
--- | ||
title: attach-consumer-label | ||
keywords: | ||
- Apache APISIX | ||
- API Gateway | ||
- API Consumer | ||
description: This article describes the Apache APISIX attach-consumer-label plugin, which you can use to pass custom consumer labels to upstream services. | ||
--- | ||
|
||
<!-- | ||
# | ||
# 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. | ||
# | ||
--> | ||
|
||
## Description | ||
|
||
The `attach-consumer-label` plugin attaches custom consumer-related labels, in addition to `X-Consumer-Username` and `X-Credential-Indentifier`, to authenticated requests, for upstream services to differentiate between consumers and implement additional logics. | ||
|
||
## Attributes | ||
|
||
| Name | Type | Required | Default | Valid values | Description | | ||
|----------|--------|----------|---------|--------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ||
| headers | object | True | | | Key-value pairs of consumer labels to be attached to request headers, where key is the request header name, such as `X-Consumer-Role`, and the value is a reference to the custom label key, such as `$role`. Note that the value should always start with a dollar sign (`$`). If a referenced consumer value is not configured on the consumer, the corresponding header will not be attached to the request. | | ||
|
||
## Enable Plugin | ||
|
||
The following example demonstrates how you can attach custom labels to request headers before authenticated requests are forwarded to upstream services. If the request is rejected, you should not see any consumer labels attached to request headers. If a certain label value is not configured on the consumer but referenced in the `attach-consumer-label` plugin, the corresponding header will also not be attached. | ||
|
||
:::note | ||
|
||
You can fetch the `admin_key` from `config.yaml` and save to an environment variable with the following command: | ||
|
||
```bash | ||
admin_key=$(yq '.deployment.admin.admin_key[0].key' conf/config.yaml | sed 's/"//g') | ||
``` | ||
|
||
::: | ||
|
||
Create a consumer `john` with custom labels: | ||
|
||
```shell | ||
curl "http://127.0.0.1:9180/apisix/admin/consumers" -X PUT \ | ||
-H "X-API-KEY: ${ADMIN_API_KEY}" \ | ||
-d '{ | ||
"username": "john", | ||
# highlight-start | ||
"labels": { | ||
// Annotate 1 | ||
"department": "devops", | ||
// Annotate 2 | ||
"company": "api7" | ||
} | ||
# highlight-end | ||
}' | ||
``` | ||
|
||
❶ Label the `department` information for the consumer. | ||
|
||
❷ Label the `company` information for the consumer. | ||
|
||
Configure the `key-auth` credential for the consumer `john`: | ||
|
||
```shell | ||
curl "http://127.0.0.1:9180/apisix/admin/consumers/john/credentials" -X PUT \ | ||
-H "X-API-KEY: ${ADMIN_API_KEY}" \ | ||
-d '{ | ||
"id": "cred-john-key-auth", | ||
"plugins": { | ||
"key-auth": { | ||
"key": "john-key" | ||
} | ||
} | ||
}' | ||
``` | ||
|
||
Create a route enabling the `key-auth` and `attach-consumer-label` plugins: | ||
|
||
```shell | ||
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \ | ||
-H "X-API-KEY: ${ADMIN_API_KEY}" \ | ||
-d '{ | ||
"id": "attach-consumer-label-route", | ||
"uri": "/get", | ||
"plugins": { | ||
"key-auth": {}, | ||
# highlight-start | ||
"attach-consumer-label": { | ||
"headers": { | ||
// Annotate 1 | ||
"X-Consumer-Department": "$department", | ||
// Annotate 2 | ||
"X-Consumer-Company": "$company", | ||
// Annotate 3 | ||
"X-Consumer-Role": "$role" | ||
} | ||
} | ||
# highlight-end | ||
}, | ||
"upstream": { | ||
"type": "roundrobin", | ||
"nodes": { | ||
"httpbin.org:80": 1 | ||
} | ||
} | ||
}' | ||
``` | ||
|
||
❶ Attach the `department` consumer label value in the `X-Consumer-Department` request header. | ||
|
||
❷ Attach the `company` consumer label value in the `X-Consumer-Company` request header. | ||
|
||
❸ Attach the `role` consumer label value in the `X-Consumer-Role` request header. As the `role` label is not configured on the consumer, it is expected that the header will not appear in the request forwarded to the upstream service. | ||
|
||
:::tip | ||
|
||
The consumer label references must be prefixed by a dollar sign (`$`). | ||
|
||
::: | ||
|
||
To verify, send a request to the route with the valid credential: | ||
|
||
```shell | ||
curl -i "http://127.0.0.1:9080/get" -H 'apikey: john-key' | ||
``` | ||
|
||
You should see an `HTTP/1.1 200 OK` response similar to the following: | ||
|
||
```text | ||
{ | ||
"args": {}, | ||
"headers": { | ||
"Accept": "*/*", | ||
"Apikey": "john-key", | ||
"Host": "127.0.0.1", | ||
# highlight-start | ||
"X-Consumer-Username": "john", | ||
"X-Credential-Indentifier": "cred-john-key-auth", | ||
"X-Consumer-Company": "api7", | ||
"X-Consumer-Department": "devops", | ||
# highlight-end | ||
"User-Agent": "curl/8.6.0", | ||
"X-Amzn-Trace-Id": "Root=1-66e5107c-5bb3e24f2de5baf733aec1cc", | ||
"X-Forwarded-Host": "127.0.0.1" | ||
}, | ||
"origin": "192.168.65.1, 205.198.122.37", | ||
"url": "http://127.0.0.1/get" | ||
} | ||
``` | ||
|
||
## Delete plugin | ||
|
||
To remove the Plugin, you can delete the corresponding JSON configuration from the Plugin configuration. APISIX will automatically reload and you do not have to restart for this to take effect. | ||
|
||
```shell | ||
curl "http://127.0.0.1:9180/apisix/admin/routes/attach-consumer-label-route" -X PUT \ | ||
-H "X-API-KEY: ${ADMIN_API_KEY}" \ | ||
-d '{ | ||
"uri": "/get", | ||
"upstream": { | ||
"type": "roundrobin", | ||
"nodes": { | ||
"httpbin.org:80": 1 | ||
} | ||
} | ||
}' | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer the Markdown style, which is easy to render by different Markdown tools