Skip to content

Commit 153a415

Browse files
sha512 id
1 parent ef02112 commit 153a415

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

packages/kong-plugin/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ With [ReadMe's Metrics API](https://readme.com/metrics) your team can get deep i
1818

1919
```bash
2020
# Build kong image with the plugin
21-
docker build -t kong-readme-plugin:v1 .
21+
docker build -t kong-readme-plugin:latest .
2222
# Run kong with the plugin
23-
curl -Ls https://get.konghq.com/quickstart | bash -s -- -r "" -i kong-readme-plugin -t v1
23+
curl -Ls https://get.konghq.com/quickstart | bash -s -- -r "" -i kong-readme-plugin -t latest
2424
# Enable the plugin
2525
curl -isX POST http://localhost:8001/plugins -d name=readme-plugin -d 'config.api_key=<Your API Key>'
2626
```

packages/kong-plugin/kong/plugins/readme-plugin/handler.lua

+16-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ local Queue = require "kong.tools.queue"
22
local cjson = require "cjson"
33
local url = require "socket.url"
44
local http = require "resty.http"
5+
local openssl_digest = require "resty.openssl.digest"
56

67
local kong = kong
78
local ngx = ngx
@@ -44,6 +45,17 @@ local function parse_url(host_url)
4445
return parsed_url
4546
end
4647

48+
-- This will generate an integrity hash that looks something like this:
49+
-- sha512-Naxska/M1INY/thefLQ49sExJ8E+89Q2bz/nC4Pet52iSRPtI9w3Cyg0QdZExt0uUbbnfMJZ0qTabiLJxw6Wrg==?1345
50+
-- With the last 4 digits on the end for us to use to identify it later in a list.
51+
local function hash_value(value)
52+
local last_4_digits = value:sub(-4)
53+
local digest = openssl_digest.new("sha512")
54+
local encoded_value = encode_base64(digest:final(value))
55+
return encoded_value .. "?" .. last_4_digits
56+
end
57+
58+
4759
-- Creates a payload for ReadMe
4860
-- @return JSON string in ReadMe API Log format
4961
local function make_readme_payload(conf, entries)
@@ -110,16 +122,17 @@ local function make_readme_payload(conf, entries)
110122
if conf.group_by then
111123
for header_name, group_value in pairs(conf.group_by) do
112124
local header_value = entry.request.headers[header_name]
113-
group[group_value] = header_value or "unknown"
125+
group[group_value] = header_value or "none"
114126
end
115127
end
116128

117129
local id_header = (conf.id_header and conf.id_header:lower()) or "authorization"
118-
local id = entry.request.headers[id_header] or "unknown"
130+
local id = entry.request.headers[id_header] or "none"
119131
if id_header == "authorization" and entry.raw_auth then
120132
id = entry.raw_auth
121133
end
122-
group.id = id
134+
group.id = hash_value(id)
135+
123136
local api_log = {
124137
httpVersion = entry.httpVersion,
125138
requestBody = {},

packages/kong-plugin/kong/plugins/readme-plugin/schema.lua

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ return {
1818
},
1919
{
2020
id_header = {
21-
description = "Select header to be used as a unique identifier for a user. This value will be hashed by ReadMe. The `Authorization` header is used by default. If the configured header was not found then it will be set to 'Unknown'.",
21+
description = "Select header to be used as a unique identifier for a user. This value will be hashed by ReadMe. The `Authorization` header is used by default. If the configured header was not found then it will be set to `none`.",
2222
type = "string",
2323
default = "Authorization"
2424
}
@@ -60,15 +60,15 @@ return {
6060
},
6161
{
6262
group_by = {
63-
description = "A map of headers to group by. The key is the header name and the value is the header value to group by. Applies to request headers only. If the header is not found, it will be set to `unknown`. `email` and `label` are recommended keys to provide.",
63+
description = "A map of headers to group by. The key is the header name and the value is the header value to group by. Applies to request headers only. If the header is not found, it will be set to `none`. `email` and `label` are recommended keys to provide.",
6464
type = "map",
6565
keys = typedefs.header_name,
6666
values = {
6767
type = "string",
6868
match_none = {
6969
{
7070
pattern = "^id$",
71-
err = "cannot map to 'id'",
71+
err = "cannot map to `id`",
7272
}
7373
}
7474
}

0 commit comments

Comments
 (0)