Skip to content

Commit 5174ebd

Browse files
committed
Refactor change handling to use UUIDs instead of integers for IDs
- Updated Change model and related functions to use string UUIDs. - Modified MCP handler to accommodate UUIDs for change creation, updates, and deletions. - Adjusted test cases to reflect the new UUID structure. - Updated audit logging to include target UUIDs. - Changed frontend Help page examples to use UUIDs for consistency.
1 parent 5f3574b commit 5174ebd

11 files changed

Lines changed: 152 additions & 145 deletions

File tree

backend/handlers/apikeys.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func (h *ApiKeyHandler) Create(c echo.Context) error {
7979
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "Failed to create API key"})
8080
}
8181

82-
auditLog(h.DB, c, "apikey.create", "api_key", uint64Ptr(apiKey.ID), strPtr(req.Name))
82+
auditLog(h.DB, c, "apikey.create", "api_key", uint64Ptr(apiKey.ID), nil, strPtr(req.Name))
8383
return c.JSON(http.StatusCreated, createApiKeyResponse{Key: rawKey, ApiKey: apiKey})
8484
}
8585

@@ -118,7 +118,7 @@ func (h *ApiKeyHandler) Revoke(c echo.Context) error {
118118
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "Failed to revoke API key"})
119119
}
120120

121-
auditLog(h.DB, c, "apikey.revoke", "api_key", uint64Ptr(id), nil)
121+
auditLog(h.DB, c, "apikey.revoke", "api_key", uint64Ptr(id), nil, nil)
122122
return c.JSON(http.StatusOK, map[string]string{"message": "API key revoked"})
123123
}
124124

@@ -159,6 +159,6 @@ func (h *ApiKeyHandler) Rotate(c echo.Context) error {
159159
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "Failed to create new API key"})
160160
}
161161

162-
auditLog(h.DB, c, "apikey.rotate", "api_key", uint64Ptr(apiKey.ID), strPtr(existing.Name))
162+
auditLog(h.DB, c, "apikey.rotate", "api_key", uint64Ptr(apiKey.ID), nil, strPtr(existing.Name))
163163
return c.JSON(http.StatusCreated, createApiKeyResponse{Key: rawKey, ApiKey: apiKey})
164164
}

backend/handlers/audit_helpers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ func extractActor(c echo.Context) (string, *uint64) {
2929
return "unknown", nil
3030
}
3131

32-
func auditLog(db *sql.DB, c echo.Context, action, targetType string, targetID *uint64, details *string) {
32+
func auditLog(db *sql.DB, c echo.Context, action, targetType string, targetID *uint64, targetUUID *string, details *string) {
3333
actor, actorID := extractActor(c)
3434
ip := c.RealIP()
3535
go func() {
36-
if err := models.CreateAuditEntry(db, actor, actorID, action, targetType, targetID, details, &ip); err != nil {
36+
if err := models.CreateAuditEntry(db, actor, actorID, action, targetType, targetID, targetUUID, details, &ip); err != nil {
3737
log.Printf("audit: failed to log %s: %v", action, err)
3838
}
3939
}()

backend/handlers/auth.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func (h *AuthHandler) Register(c echo.Context) error {
7272
}
7373

7474
_ = models.UpdateLastLogin(h.DB, user.ID)
75-
auditLog(h.DB, c, "user.register", "user", uint64Ptr(user.ID), strPtr(user.Email))
75+
auditLog(h.DB, c, "user.register", "user", uint64Ptr(user.ID), nil, strPtr(user.Email))
7676

7777
token, err := h.generateToken(user)
7878
if err != nil {
@@ -104,7 +104,7 @@ func (h *AuthHandler) Login(c echo.Context) error {
104104
}
105105

106106
_ = models.UpdateLastLogin(h.DB, user.ID)
107-
auditLog(h.DB, c, "user.login", "user", uint64Ptr(user.ID), strPtr(user.Email))
107+
auditLog(h.DB, c, "user.login", "user", uint64Ptr(user.ID), nil, strPtr(user.Email))
108108

109109
token, err := h.generateToken(user)
110110
if err != nil {
@@ -183,7 +183,7 @@ func (h *AuthHandler) ChangePassword(c echo.Context) error {
183183
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "Failed to update password"})
184184
}
185185

186-
auditLog(h.DB, c, "user.password_change", "user", uint64Ptr(id), strPtr(user.Email))
186+
auditLog(h.DB, c, "user.password_change", "user", uint64Ptr(id), nil, strPtr(user.Email))
187187

188188
return c.JSON(http.StatusOK, map[string]string{"message": "Password updated"})
189189
}

backend/handlers/changes.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ package handlers
22

33
import (
44
"database/sql"
5-
"fmt"
65
"net/http"
76
"strconv"
87
"time"
98

9+
"github.com/google/uuid"
1010
"github.com/labstack/echo/v4"
1111

1212
mw "ops-ledger/backend/middleware"
@@ -77,7 +77,7 @@ func (h *ChangeHandler) Create(c echo.Context) error {
7777
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "Failed to create change"})
7878
}
7979

80-
auditLog(h.DB, c, "change.create", "change", uint64Ptr(change.ID), strPtr(req.System+": "+req.Description))
80+
auditLog(h.DB, c, "change.create", "change", nil, strPtr(change.ID), strPtr(req.System+": "+req.Description))
8181
if h.Hub != nil {
8282
h.Hub.Publish(SSEEvent{Type: "change.created", Data: change})
8383
}
@@ -89,8 +89,8 @@ func (h *ChangeHandler) Update(c echo.Context) error {
8989
return err
9090
}
9191

92-
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
93-
if err != nil {
92+
id := c.Param("id")
93+
if _, err := uuid.Parse(id); err != nil {
9494
return c.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid change ID"})
9595
}
9696

@@ -134,7 +134,7 @@ func (h *ChangeHandler) Update(c echo.Context) error {
134134
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "Failed to update change"})
135135
}
136136

137-
auditLog(h.DB, c, "change.update", "change", uint64Ptr(change.ID), strPtr(req.System+": "+req.Description))
137+
auditLog(h.DB, c, "change.update", "change", nil, strPtr(change.ID), strPtr(req.System+": "+req.Description))
138138
if h.Hub != nil {
139139
h.Hub.Publish(SSEEvent{Type: "change.updated", Data: change})
140140
}
@@ -146,8 +146,8 @@ func (h *ChangeHandler) Delete(c echo.Context) error {
146146
return err
147147
}
148148

149-
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
150-
if err != nil {
149+
id := c.Param("id")
150+
if _, err := uuid.Parse(id); err != nil {
151151
return c.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid change ID"})
152152
}
153153

@@ -164,9 +164,9 @@ func (h *ChangeHandler) Delete(c echo.Context) error {
164164
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "Failed to delete change"})
165165
}
166166

167-
auditLog(h.DB, c, "change.delete", "change", uint64Ptr(id), strPtr(change.System+": "+change.Description))
167+
auditLog(h.DB, c, "change.delete", "change", nil, strPtr(id), strPtr(change.System+": "+change.Description))
168168
if h.Hub != nil {
169-
h.Hub.Publish(SSEEvent{Type: "change.deleted", Data: DeletedPayload{ID: fmt.Sprintf("%d", id)}})
169+
h.Hub.Publish(SSEEvent{Type: "change.deleted", Data: DeletedPayload{ID: id}})
170170
}
171171
return c.JSON(http.StatusOK, map[string]string{"message": "Change deleted"})
172172
}

0 commit comments

Comments
 (0)