-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sessions): store session's meatadata (#13990)
Engineering brief: https://docs.google.com/document/d/1r7nSZGOlTVhupoVGQ6Ymqsaw64eGHKSMibko8FgrwAQ/edit?tab=t.0 **### This PR won't bring a break change.** Add two new configurations`hash_subject`(by default false) and `store_metadata`(by default false). Please ref [here](https://github.com/bungle/lua-resty-session?tab=readme-ov-file#session-configuration). Add a new table `session_metadatas` to store the session's metadata. ``` CREATE TABLE IF NOT EXISTS session_metadatas( id uuid, session_id uuid REFERENCES "sessions" ("id") ON DELETE CASCADE, sid text, subject text, audience text, created_at timestamp WITH TIME ZONE, PRIMARY KEY (id) ); ```
- Loading branch information
1 parent
5c78fb8
commit 938747a
Showing
12 changed files
with
280 additions
and
18 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
message: | | ||
**session**: Added two boolean configuration fields `hash_subject` (default `false`) and `store_metadata` (default `false`) to store session's metadata in the database. | ||
type: feature | ||
scope: "Plugin" |
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 |
---|---|---|
@@ -1,20 +1,39 @@ | ||
local typedefs = require "kong.db.schema.typedefs" | ||
|
||
|
||
return { | ||
{ | ||
primary_key = { "id" }, | ||
endpoint_key = "session_id", | ||
name = "sessions", | ||
cache_key = { "session_id" }, | ||
ttl = true, | ||
db_export = false, | ||
fields = { | ||
{ id = typedefs.uuid }, | ||
{ session_id = { type = "string", unique = true, required = true } }, | ||
{ expires = { type = "integer" } }, | ||
{ data = { type = "string" } }, | ||
{ created_at = typedefs.auto_timestamp_s }, | ||
} | ||
local sessions = { | ||
primary_key = { "id" }, | ||
endpoint_key = "session_id", | ||
name = "sessions", | ||
cache_key = { "session_id" }, | ||
ttl = true, | ||
db_export = false, | ||
fields = { | ||
{ id = typedefs.uuid }, | ||
{ session_id = { type = "string", unique = true, required = true } }, | ||
{ expires = { type = "integer" } }, | ||
{ data = { type = "string" } }, | ||
{ created_at = typedefs.auto_timestamp_s }, | ||
} | ||
} | ||
|
||
local session_metadatas = { | ||
primary_key = { "id" }, | ||
name = "session_metadatas", | ||
dao = "kong.plugins.session.daos.session_metadatas", | ||
generate_admin_api = false, | ||
db_export = false, | ||
fields = { | ||
{ id = typedefs.uuid }, | ||
{ session = { type = "foreign", reference = "sessions", required = true, on_delete = "cascade" } }, | ||
{ sid = { type = "string" } }, | ||
{ audience = { type = "string" } }, | ||
{ subject = { type = "string" } }, | ||
{ created_at = typedefs.auto_timestamp_s }, | ||
} | ||
} | ||
|
||
return { | ||
sessions, | ||
session_metadatas, | ||
} |
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,7 @@ | ||
local session_metadatas = {} | ||
|
||
function session_metadatas:select_by_audience_and_subject(audience, subject) | ||
return self.strategy:select_by_audience_and_subject(audience, subject) | ||
end | ||
|
||
return session_metadatas |
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,23 @@ | ||
return { | ||
postgres = { | ||
up = [[ | ||
CREATE TABLE IF NOT EXISTS session_metadatas( | ||
id uuid, | ||
session_id uuid REFERENCES "sessions" ("id") ON DELETE CASCADE, | ||
sid text, | ||
subject text, | ||
audience text, | ||
created_at timestamp WITH TIME ZONE, | ||
PRIMARY KEY (id) | ||
); | ||
DO $$ | ||
BEGIN | ||
CREATE INDEX IF NOT EXISTS "session_id_idx" ON "session_metadatas" ("session_id"); | ||
CREATE INDEX IF NOT EXISTS "subject_audience_idx" ON "session_metadatas" ("subject", "audience"); | ||
EXCEPTION WHEN UNDEFINED_COLUMN THEN | ||
-- Do nothing, accept existing state | ||
END$$; | ||
]], | ||
}, | ||
} |
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 |
---|---|---|
|
@@ -2,4 +2,5 @@ return { | |
"000_base_session", | ||
"001_add_ttl_index", | ||
"002_320_to_330", | ||
"003_330_to_3100", | ||
} |
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
22 changes: 22 additions & 0 deletions
22
kong/plugins/session/strategies/postgres/session_metadatas.lua
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,22 @@ | ||
local fmt = string.format | ||
|
||
local session_metadatas = {} | ||
|
||
function session_metadatas:select_by_audience_and_subject(audience, subject) | ||
if type(audience) ~= "string" then | ||
error("audience must be string") | ||
end | ||
|
||
if type(subject) ~= "string" then | ||
error("subject must be string") | ||
end | ||
|
||
local qs = fmt( | ||
"SELECT * FROM session_metadatas WHERE audience = %s AND subject = %s;", | ||
kong.db.connector:escape_literal(audience), | ||
kong.db.connector:escape_literal(subject)) | ||
|
||
return kong.db.connector:query(qs, "read") | ||
end | ||
|
||
return session_metadatas |
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
15 changes: 15 additions & 0 deletions
15
spec/05-migration/plugins/session/migrations/003_330_to_3100_spec.lua
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,15 @@ | ||
local uh = require "spec/upgrade_helpers" | ||
|
||
describe("database migration", function() | ||
if uh.database_type() == "postgres" then | ||
uh.all_phases("has created the \"session_metadatas\" table", function() | ||
assert.database_has_relation("session_metadatas") | ||
assert.table_has_column("session_metadatas", "id", "uuid") | ||
assert.table_has_column("session_metadatas", "session_id", "uuid") | ||
assert.table_has_column("session_metadatas", "sid", "text") | ||
assert.table_has_column("session_metadatas", "subject", "text") | ||
assert.table_has_column("session_metadatas", "audience", "text") | ||
assert.table_has_column("session_metadatas", "created_at", "timestamp with time zone", "timestamp") | ||
end) | ||
end | ||
end) |
938747a
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.
Bazel Build
Docker image available
kong/kong:938747a141b32aa6ad580dde92ef5fa202830218
Artifacts available https://github.com/Kong/kong/actions/runs/12554917489