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:
6
auth.go
6
auth.go
@@ -182,8 +182,8 @@ func logsHandler(cfg Config, db *sql.DB) http.HandlerFunc {
|
|||||||
logs := make([]map[string]interface{}, 0)
|
logs := make([]map[string]interface{}, 0)
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var ts time.Time
|
var ts time.Time
|
||||||
var action, apiClientIP, reason string
|
var action, apiClientIP string
|
||||||
var ip, cidr sql.NullString
|
var ip, cidr, reason sql.NullString
|
||||||
var ttlSeconds sql.NullInt64
|
var ttlSeconds sql.NullInt64
|
||||||
if err := rows.Scan(&ts, &action, &ip, &cidr, &ttlSeconds, &reason, &apiClientIP); err != nil {
|
if err := rows.Scan(&ts, &action, &ip, &cidr, &ttlSeconds, &reason, &apiClientIP); err != nil {
|
||||||
continue
|
continue
|
||||||
@@ -192,7 +192,7 @@ func logsHandler(cfg Config, db *sql.DB) http.HandlerFunc {
|
|||||||
"timestamp": ts,
|
"timestamp": ts,
|
||||||
"action": action,
|
"action": action,
|
||||||
"api_client_ip": apiClientIP,
|
"api_client_ip": apiClientIP,
|
||||||
"reason": reason,
|
"reason": reason.String,
|
||||||
}
|
}
|
||||||
if ip.Valid {
|
if ip.Valid {
|
||||||
entry["ip"] = ip.String
|
entry["ip"] = ip.String
|
||||||
|
|||||||
14
db.go
14
db.go
@@ -205,11 +205,14 @@ func CleanupExpiredTemp(db *sql.DB) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var reason string
|
||||||
|
reason = "expired"
|
||||||
|
|
||||||
for _, ip := range expiredIPs {
|
for _, ip := range expiredIPs {
|
||||||
_, err = db.Exec(`
|
_, err = db.Exec(`
|
||||||
INSERT INTO audit_log(action, ip)
|
INSERT INTO audit_log(action, ip, reason)
|
||||||
VALUES('expire_temp', ?)
|
VALUES('expire_temp', ?, ?)
|
||||||
`, ip)
|
`, ip, reason)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Warn("failed to log expired temp entry", "ip", ip, "error", err)
|
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()
|
defer rows.Close()
|
||||||
var list []map[string]interface{} = make([]map[string]interface{}, 0)
|
var list []map[string]interface{} = make([]map[string]interface{}, 0)
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var ip, reason string
|
var ip string
|
||||||
var expiresAt time.Time
|
var expiresAt time.Time
|
||||||
|
var reason sql.NullString
|
||||||
if err := rows.Scan(&ip, &expiresAt, &reason); err != nil {
|
if err := rows.Scan(&ip, &expiresAt, &reason); err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
list = append(list, map[string]interface{}{
|
list = append(list, map[string]interface{}{
|
||||||
"ip": ip,
|
"ip": ip,
|
||||||
"expires": expiresAt,
|
"expires": expiresAt,
|
||||||
"reason": reason,
|
"reason": reason.String,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return list, nil
|
return list, nil
|
||||||
|
|||||||
Reference in New Issue
Block a user