Skip to content

Commit 6145d7d

Browse files
committed
add event types table for audit logs
1 parent eb0887e commit 6145d7d

File tree

13 files changed

+275
-220
lines changed

13 files changed

+275
-220
lines changed

cli/cli.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ Version:
5959
cfg.License.EnabledAudit = !disableAudit
6060

6161
// init database connection in PersistentPreRun hook for getting persistent flags
62-
var store licenses.Store
62+
var store *db.Store
6363

6464
store, conn, err = initStore(ctx, cfg)
6565
if err != nil {
@@ -68,7 +68,7 @@ Version:
6868
return err
6969
}
7070

71-
manager.AttachStore(store)
71+
manager.AttachStore(*store)
7272

7373
return nil
7474
},
@@ -105,7 +105,7 @@ Version:
105105
return 0
106106
}
107107

108-
func initStore(ctx context.Context, cfg *config.Config) (licenses.Store, *sql.DB, error) {
108+
func initStore(ctx context.Context, cfg *config.Config) (*db.Store, *sql.DB, error) {
109109
conn, err := sql.Open("sqlite3", cfg.DB.DatabaseFilePath)
110110
if err != nil {
111111
slog.Error("failed to open database", "error", err)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
-- re-add the old column
2+
ALTER TABLE
3+
audit_logs
4+
ADD
5+
COLUMN action TEXT NOT NULL DEFAULT 'unknown';
6+
7+
-- revert data migration
8+
UPDATE
9+
audit_logs
10+
SET
11+
action = CASE
12+
event_type_id
13+
WHEN 1 THEN 'added'
14+
WHEN 2 THEN 'removed'
15+
WHEN 3 THEN 'claimed'
16+
WHEN 4 THEN 'released'
17+
WHEN 5 THEN 'activated'
18+
WHEN 6 THEN 'ping'
19+
WHEN 7 THEN 'culled'
20+
END;
21+
22+
-- drop the new column
23+
ALTER TABLE
24+
audit_logs DROP COLUMN event_type_id;
25+
26+
-- drop the new table
27+
DROP TABLE event_types;
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
-- create events table
2+
CREATE TABLE event_types (id TINYINT PRIMARY KEY, name TEXT NOT NULL);
3+
4+
-- insert events
5+
INSERT INTO
6+
event_types (id, name)
7+
VALUES
8+
(0, 'unknown'),
9+
(1, 'license.added'),
10+
(2, 'license.removed'),
11+
(3, 'license.claimed'),
12+
(4, 'license.released'),
13+
(5, 'node.activated'),
14+
(6, 'node.ping'),
15+
(7, 'node.culled');
16+
17+
-- rebuild the table with the new schema (this is a workaround for sqlite not supporting ALTER TABLE x ALTER COLUMN y NOT NULL)
18+
CREATE TABLE _audit_logs (
19+
id INTEGER PRIMARY KEY AUTOINCREMENT,
20+
event_type_id TINYINT NOT NULL REFERENCES event_types (id),
21+
entity_type TEXT NOT NULL,
22+
entity_id TEXT NOT NULL,
23+
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
24+
);
25+
26+
-- copy data from the old table to the new
27+
INSERT INTO
28+
_audit_logs (
29+
id,
30+
event_type_id,
31+
entity_type,
32+
entity_id,
33+
created_at
34+
)
35+
SELECT
36+
id,
37+
CASE
38+
action
39+
WHEN 'added' THEN 1
40+
WHEN 'removed' THEN 2
41+
WHEN 'claimed' THEN 3
42+
WHEN 'released' THEN 4
43+
WHEN 'activated' THEN 5
44+
WHEN 'ping' THEN 6
45+
WHEN 'culled' THEN 7
46+
ELSE 0
47+
END AS event_type_id,
48+
entity_type,
49+
entity_id,
50+
created_at
51+
FROM
52+
audit_logs;
53+
54+
-- drop the old table
55+
DROP TABLE audit_logs;
56+
57+
-- replace with new table
58+
ALTER TABLE
59+
_audit_logs RENAME TO audit_logs;

db/queries/audit_logs.sql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
-- name: InsertAuditLog :exec
2-
INSERT INTO audit_logs (action, entity_type, entity_id)
2+
INSERT INTO audit_logs (event_type_id, entity_type, entity_id)
33
VALUES (?, ?, ?);
44

55
-- name: GetAuditLogs :many
6-
SELECT id, action, entity_type, entity_id, created_at
6+
SELECT id, event_type_id, entity_type, entity_id, created_at
77
FROM audit_logs
88
ORDER BY created_at DESC
99
LIMIT ?;
1010

1111
-- name: GetAuditLogsByEntity :many
12-
SELECT id, action, entity_type, entity_id, created_at
12+
SELECT id, event_type_id, entity_type, entity_id, created_at
1313
FROM audit_logs
1414
WHERE entity_id = ? AND entity_type = ?
1515
ORDER BY created_at DESC;

internal/cmd/ls_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"errors"
77
"testing"
88

9-
"github.com/keygen-sh/keygen-relay/internal/licenses"
9+
"github.com/keygen-sh/keygen-relay/internal/db"
1010
"github.com/keygen-sh/keygen-relay/internal/testutils"
1111

1212
"github.com/keygen-sh/keygen-relay/internal/cmd"
@@ -15,8 +15,8 @@ import (
1515

1616
func TestLsCmd_Success(t *testing.T) {
1717
manager := &testutils.FakeManager{
18-
ListLicensesFn: func(ctx context.Context) ([]licenses.License, error) {
19-
return []licenses.License{
18+
ListLicensesFn: func(ctx context.Context) ([]db.License, error) {
19+
return []db.License{
2020
{ID: "License_1", Key: "License_Key_1", Claims: 5},
2121
{ID: "License_2", Key: "License_Key_2", Claims: 10},
2222
}, nil
@@ -38,8 +38,8 @@ func TestLsCmd_Success(t *testing.T) {
3838

3939
func TestLsCmd_NoLicenses(t *testing.T) {
4040
manager := &testutils.FakeManager{
41-
ListLicensesFn: func(ctx context.Context) ([]licenses.License, error) {
42-
return []licenses.License{}, nil
41+
ListLicensesFn: func(ctx context.Context) ([]db.License, error) {
42+
return []db.License{}, nil
4343
},
4444
}
4545

@@ -57,7 +57,7 @@ func TestLsCmd_NoLicenses(t *testing.T) {
5757

5858
func TestLsCmd_Error(t *testing.T) {
5959
manager := &testutils.FakeManager{
60-
ListLicensesFn: func(ctx context.Context) ([]licenses.License, error) {
60+
ListLicensesFn: func(ctx context.Context) ([]db.License, error) {
6161
return nil, errors.New("failed to list licenses")
6262
},
6363
}

internal/cmd/stat_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"time"
99

1010
"github.com/keygen-sh/keygen-relay/internal/cmd"
11-
"github.com/keygen-sh/keygen-relay/internal/licenses"
11+
"github.com/keygen-sh/keygen-relay/internal/db"
1212
"github.com/keygen-sh/keygen-relay/internal/testutils"
1313
"github.com/stretchr/testify/assert"
1414
)
@@ -19,8 +19,8 @@ func TestStatCmd_Success(t *testing.T) {
1919
lastReleasedAt, _ := time.Parse(time.RFC3339, "2024-01-05T10:00:00Z")
2020

2121
manager := &testutils.FakeManager{
22-
GetLicenseByIDFn: func(ctx context.Context, id string) (licenses.License, error) {
23-
return licenses.License{
22+
GetLicenseByIDFn: func(ctx context.Context, id string) (*db.License, error) {
23+
return &db.License{
2424
ID: "License_1",
2525
Key: "License_Key1",
2626
Claims: 5,
@@ -64,8 +64,8 @@ func TestStatCmd_MissingFlag(t *testing.T) {
6464

6565
func TestStatCmd_Error(t *testing.T) {
6666
manager := &testutils.FakeManager{
67-
GetLicenseByIDFn: func(ctx context.Context, id string) (licenses.License, error) {
68-
return licenses.License{}, errors.New("license not found")
67+
GetLicenseByIDFn: func(ctx context.Context, id string) (*db.License, error) {
68+
return nil, errors.New("license not found")
6969
},
7070
}
7171

internal/db/audit_logs.sql.go

Lines changed: 32 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/db/models.go

Lines changed: 10 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)