fix: use sql.NullString for reason field in audit log
Update reason field handling in auth.go and db.go to properly support NULL values using sql.NullString. When inserting expired temp entries and serializing logs to JSON, extract the string value to avoid JSON marshaling issues with nullable fields.
This commit is contained in:
14
db.go
14
db.go
@@ -205,11 +205,14 @@ func CleanupExpiredTemp(db *sql.DB) error {
|
||||
return err
|
||||
}
|
||||
|
||||
var reason string
|
||||
reason = "expired"
|
||||
|
||||
for _, ip := range expiredIPs {
|
||||
_, err = db.Exec(`
|
||||
INSERT INTO audit_log(action, ip)
|
||||
VALUES('expire_temp', ?)
|
||||
`, ip)
|
||||
INSERT INTO audit_log(action, ip, reason)
|
||||
VALUES('expire_temp', ?, ?)
|
||||
`, ip, reason)
|
||||
if err != nil {
|
||||
slog.Warn("failed to log expired temp entry", "ip", ip, "error", err)
|
||||
}
|
||||
@@ -231,15 +234,16 @@ func ListTempEntries(db *sql.DB) ([]map[string]interface{}, error) {
|
||||
defer rows.Close()
|
||||
var list []map[string]interface{} = make([]map[string]interface{}, 0)
|
||||
for rows.Next() {
|
||||
var ip, reason string
|
||||
var ip string
|
||||
var expiresAt time.Time
|
||||
var reason sql.NullString
|
||||
if err := rows.Scan(&ip, &expiresAt, &reason); err != nil {
|
||||
continue
|
||||
}
|
||||
list = append(list, map[string]interface{}{
|
||||
"ip": ip,
|
||||
"expires": expiresAt,
|
||||
"reason": reason,
|
||||
"reason": reason.String,
|
||||
})
|
||||
}
|
||||
return list, nil
|
||||
|
||||
Reference in New Issue
Block a user