Skip to content

Commit

Permalink
add event types table for audit logs
Browse files Browse the repository at this point in the history
  • Loading branch information
ezekg committed Dec 13, 2024
1 parent eb0887e commit 6145d7d
Show file tree
Hide file tree
Showing 13 changed files with 275 additions and 220 deletions.
6 changes: 3 additions & 3 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Version:
cfg.License.EnabledAudit = !disableAudit

// init database connection in PersistentPreRun hook for getting persistent flags
var store licenses.Store
var store *db.Store

store, conn, err = initStore(ctx, cfg)
if err != nil {
Expand All @@ -68,7 +68,7 @@ Version:
return err
}

manager.AttachStore(store)
manager.AttachStore(*store)

return nil
},
Expand Down Expand Up @@ -105,7 +105,7 @@ Version:
return 0
}

func initStore(ctx context.Context, cfg *config.Config) (licenses.Store, *sql.DB, error) {
func initStore(ctx context.Context, cfg *config.Config) (*db.Store, *sql.DB, error) {
conn, err := sql.Open("sqlite3", cfg.DB.DatabaseFilePath)
if err != nil {
slog.Error("failed to open database", "error", err)
Expand Down
27 changes: 27 additions & 0 deletions db/migrations/1733866970_add_event_types.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-- re-add the old column
ALTER TABLE
audit_logs
ADD
COLUMN action TEXT NOT NULL DEFAULT 'unknown';

-- revert data migration
UPDATE
audit_logs
SET
action = CASE
event_type_id
WHEN 1 THEN 'added'
WHEN 2 THEN 'removed'
WHEN 3 THEN 'claimed'
WHEN 4 THEN 'released'
WHEN 5 THEN 'activated'
WHEN 6 THEN 'ping'
WHEN 7 THEN 'culled'
END;

-- drop the new column
ALTER TABLE
audit_logs DROP COLUMN event_type_id;

-- drop the new table
DROP TABLE event_types;
59 changes: 59 additions & 0 deletions db/migrations/1733866970_add_event_types.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
-- create events table
CREATE TABLE event_types (id TINYINT PRIMARY KEY, name TEXT NOT NULL);

-- insert events
INSERT INTO
event_types (id, name)
VALUES
(0, 'unknown'),
(1, 'license.added'),
(2, 'license.removed'),
(3, 'license.claimed'),
(4, 'license.released'),
(5, 'node.activated'),
(6, 'node.ping'),
(7, 'node.culled');

This comment has been minimized.

Copy link
@ezekg

ezekg Dec 13, 2024

Author Member

Add node.deactivated?


-- rebuild the table with the new schema (this is a workaround for sqlite not supporting ALTER TABLE x ALTER COLUMN y NOT NULL)
CREATE TABLE _audit_logs (

This comment has been minimized.

Copy link
@ezekg

ezekg Dec 13, 2024

Author Member

Update readme to show examples of joins.

id INTEGER PRIMARY KEY AUTOINCREMENT,
event_type_id TINYINT NOT NULL REFERENCES event_types (id),
entity_type TEXT NOT NULL,
entity_id TEXT NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);

-- copy data from the old table to the new
INSERT INTO
_audit_logs (
id,
event_type_id,
entity_type,
entity_id,
created_at
)
SELECT
id,
CASE
action
WHEN 'added' THEN 1
WHEN 'removed' THEN 2
WHEN 'claimed' THEN 3
WHEN 'released' THEN 4
WHEN 'activated' THEN 5
WHEN 'ping' THEN 6
WHEN 'culled' THEN 7
ELSE 0
END AS event_type_id,
entity_type,
entity_id,
created_at
FROM
audit_logs;

-- drop the old table
DROP TABLE audit_logs;

-- replace with new table
ALTER TABLE
_audit_logs RENAME TO audit_logs;
6 changes: 3 additions & 3 deletions db/queries/audit_logs.sql
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
-- name: InsertAuditLog :exec
INSERT INTO audit_logs (action, entity_type, entity_id)
INSERT INTO audit_logs (event_type_id, entity_type, entity_id)
VALUES (?, ?, ?);

-- name: GetAuditLogs :many
SELECT id, action, entity_type, entity_id, created_at
SELECT id, event_type_id, entity_type, entity_id, created_at
FROM audit_logs
ORDER BY created_at DESC
LIMIT ?;

-- name: GetAuditLogsByEntity :many
SELECT id, action, entity_type, entity_id, created_at
SELECT id, event_type_id, entity_type, entity_id, created_at
FROM audit_logs
WHERE entity_id = ? AND entity_type = ?
ORDER BY created_at DESC;
12 changes: 6 additions & 6 deletions internal/cmd/ls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"errors"
"testing"

"github.com/keygen-sh/keygen-relay/internal/licenses"
"github.com/keygen-sh/keygen-relay/internal/db"
"github.com/keygen-sh/keygen-relay/internal/testutils"

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

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

func TestLsCmd_NoLicenses(t *testing.T) {
manager := &testutils.FakeManager{
ListLicensesFn: func(ctx context.Context) ([]licenses.License, error) {
return []licenses.License{}, nil
ListLicensesFn: func(ctx context.Context) ([]db.License, error) {
return []db.License{}, nil
},
}

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

func TestLsCmd_Error(t *testing.T) {
manager := &testutils.FakeManager{
ListLicensesFn: func(ctx context.Context) ([]licenses.License, error) {
ListLicensesFn: func(ctx context.Context) ([]db.License, error) {
return nil, errors.New("failed to list licenses")
},
}
Expand Down
10 changes: 5 additions & 5 deletions internal/cmd/stat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"time"

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

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

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

Expand Down
47 changes: 32 additions & 15 deletions internal/db/audit_logs.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 10 additions & 5 deletions internal/db/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6145d7d

Please sign in to comment.