Skip to content

Commit

Permalink
sha512 id
Browse files Browse the repository at this point in the history
  • Loading branch information
anandkumarpatel committed Sep 6, 2024
1 parent ef02112 commit 153a415
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
4 changes: 2 additions & 2 deletions packages/kong-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ With [ReadMe's Metrics API](https://readme.com/metrics) your team can get deep i

```bash
# Build kong image with the plugin
docker build -t kong-readme-plugin:v1 .
docker build -t kong-readme-plugin:latest .
# Run kong with the plugin
curl -Ls https://get.konghq.com/quickstart | bash -s -- -r "" -i kong-readme-plugin -t v1
curl -Ls https://get.konghq.com/quickstart | bash -s -- -r "" -i kong-readme-plugin -t latest
# Enable the plugin
curl -isX POST http://localhost:8001/plugins -d name=readme-plugin -d 'config.api_key=<Your API Key>'
```
Expand Down
19 changes: 16 additions & 3 deletions packages/kong-plugin/kong/plugins/readme-plugin/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local Queue = require "kong.tools.queue"
local cjson = require "cjson"
local url = require "socket.url"
local http = require "resty.http"
local openssl_digest = require "resty.openssl.digest"

local kong = kong
local ngx = ngx
Expand Down Expand Up @@ -44,6 +45,17 @@ local function parse_url(host_url)
return parsed_url
end

-- This will generate an integrity hash that looks something like this:
-- sha512-Naxska/M1INY/thefLQ49sExJ8E+89Q2bz/nC4Pet52iSRPtI9w3Cyg0QdZExt0uUbbnfMJZ0qTabiLJxw6Wrg==?1345
-- With the last 4 digits on the end for us to use to identify it later in a list.
local function hash_value(value)
local last_4_digits = value:sub(-4)
local digest = openssl_digest.new("sha512")
local encoded_value = encode_base64(digest:final(value))
return encoded_value .. "?" .. last_4_digits
end


-- Creates a payload for ReadMe
-- @return JSON string in ReadMe API Log format
local function make_readme_payload(conf, entries)
Expand Down Expand Up @@ -110,16 +122,17 @@ local function make_readme_payload(conf, entries)
if conf.group_by then
for header_name, group_value in pairs(conf.group_by) do
local header_value = entry.request.headers[header_name]
group[group_value] = header_value or "unknown"
group[group_value] = header_value or "none"
end
end

local id_header = (conf.id_header and conf.id_header:lower()) or "authorization"
local id = entry.request.headers[id_header] or "unknown"
local id = entry.request.headers[id_header] or "none"
if id_header == "authorization" and entry.raw_auth then
id = entry.raw_auth
end
group.id = id
group.id = hash_value(id)

local api_log = {
httpVersion = entry.httpVersion,
requestBody = {},
Expand Down
6 changes: 3 additions & 3 deletions packages/kong-plugin/kong/plugins/readme-plugin/schema.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ return {
},
{
id_header = {
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'.",
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`.",
type = "string",
default = "Authorization"
}
Expand Down Expand Up @@ -60,15 +60,15 @@ return {
},
{
group_by = {
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.",
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.",
type = "map",
keys = typedefs.header_name,
values = {
type = "string",
match_none = {
{
pattern = "^id$",
err = "cannot map to 'id'",
err = "cannot map to `id`",
}
}
}
Expand Down

0 comments on commit 153a415

Please sign in to comment.