refactor: rename 'entry' to 'ip' in audit log schema

Rename the 'entry' column to 'ip' throughout the database schema and
function signatures for clarity. This affects both the CREATE TABLE
statement and all INSERT queries in the audit_log table.

The parameter naming in AddPermanentEntry, DeletePermanentEntry, and
AddTempEntry functions has also been updated to reflect the more
specific 'ip' terminology. Corresponding log messages have been
adjusted to use 'temp ip' instead of 'temp entry' for consistency.
This commit is contained in:
db123-test
2026-05-05 17:54:07 +03:30
parent 31fc204ef2
commit 3ce20314db
2 changed files with 18 additions and 18 deletions

View File

@@ -89,7 +89,7 @@ func whitelistTempHandler(cfg Config, db *sql.DB) http.HandlerFunc {
} }
clientIP := getClientIP(r) clientIP := getClientIP(r)
if err := AddTempEntry(db, req.IP, req.TTLSeconds, req.Reason, clientIP); err != nil { if err := AddTempEntry(db, req.IP, req.TTLSeconds, req.Reason, clientIP); err != nil {
slog.Error("failed to add temp entry", "error", err) slog.Error("failed to add temp ip", "error", err)
http.Error(w, "internal error", http.StatusInternalServerError) http.Error(w, "internal error", http.StatusInternalServerError)
return return
} }
@@ -153,7 +153,7 @@ func whitelistDeleteHandler(cfg Config, db *sql.DB) http.HandlerFunc {
} }
clientIP := getClientIP(r) clientIP := getClientIP(r)
if err := DeleteTempEntry(db, ip, clientIP); err != nil { if err := DeleteTempEntry(db, ip, clientIP); err != nil {
slog.Error("failed to delete temp entry", "error", err) slog.Error("failed to delete temp ip", "error", err)
http.Error(w, "internal error", http.StatusInternalServerError) http.Error(w, "internal error", http.StatusInternalServerError)
return return
} }
@@ -169,7 +169,7 @@ func logsHandler(cfg Config, db *sql.DB) http.HandlerFunc {
return return
} }
query := `SELECT timestamp, action, entry, ttl_seconds, reason, api_client_ip FROM audit_log` query := `SELECT timestamp, action, ip, ttl_seconds, reason, api_client_ip FROM audit_log`
var args []any var args []any
filterAction := r.URL.Query().Get("action") filterAction := r.URL.Query().Get("action")

View File

@@ -41,7 +41,7 @@ func InitDB(dbPath string) (*sql.DB, error) {
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
timestamp DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, timestamp DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
action TEXT NOT NULL, action TEXT NOT NULL,
entry TEXT, ip TEXT,
ttl_seconds INTEGER, ttl_seconds INTEGER,
reason TEXT, reason TEXT,
api_client_ip TEXT api_client_ip TEXT
@@ -55,14 +55,14 @@ func InitDB(dbPath string) (*sql.DB, error) {
return db, nil return db, nil
} }
func AddPermanentEntry(db *sql.DB, entry, apiClientIP string) (bool, error) { func AddPermanentEntry(db *sql.DB, ip, apiClientIP string) (bool, error) {
_, err := db.Exec(`INSERT INTO perm_whitelist(ip) VALUES(?)`, entry) _, err := db.Exec(`INSERT INTO perm_whitelist(ip) VALUES(?)`, ip)
if err != nil { if err != nil {
if isUniqueConstraintError(err) { if isUniqueConstraintError(err) {
_, err = db.Exec(` _, err = db.Exec(`
INSERT INTO audit_log(action, entry, api_client_ip) INSERT INTO audit_log(action, ip, api_client_ip)
VALUES('add_perm_duplicate', ?, ?) VALUES('add_perm_duplicate', ?, ?)
`, entry, apiClientIP) `, ip, apiClientIP)
if err != nil { if err != nil {
return false, err return false, err
} }
@@ -71,27 +71,27 @@ func AddPermanentEntry(db *sql.DB, entry, apiClientIP string) (bool, error) {
return false, err return false, err
} }
_, err = db.Exec(` _, err = db.Exec(`
INSERT INTO audit_log(action, entry, api_client_ip) INSERT INTO audit_log(action, ip, api_client_ip)
VALUES('add_perm', ?, ?) VALUES('add_perm', ?, ?)
`, entry, apiClientIP) `, ip, apiClientIP)
if err != nil { if err != nil {
return true, err return true, err
} }
return true, nil return true, nil
} }
func DeletePermanentEntry(db *sql.DB, entry, apiClientIP string) error { func DeletePermanentEntry(db *sql.DB, ip, apiClientIP string) error {
_, err := db.Exec(` _, err := db.Exec(`
DELETE FROM perm_whitelist DELETE FROM perm_whitelist
WHERE ip = ? WHERE ip = ?
`, entry) `, ip)
if err != nil { if err != nil {
return err return err
} }
_, err = db.Exec(` _, err = db.Exec(`
INSERT INTO audit_log(action, entry, api_client_ip) INSERT INTO audit_log(action, ip, api_client_ip)
VALUES('delete_perm', ?, ?) VALUES('delete_perm', ?, ?)
`, entry, apiClientIP) `, ip, apiClientIP)
if err != nil { if err != nil {
return err return err
} }
@@ -135,7 +135,7 @@ func AddTempEntry(db *sql.DB, ip string, ttlSeconds int, reason, apiClientIP str
return err return err
} }
_, err = db.Exec(` _, err = db.Exec(`
INSERT INTO audit_log(action, entry, ttl_seconds, reason, api_client_ip) INSERT INTO audit_log(action, ip, ttl_seconds, reason, api_client_ip)
VALUES('add_temp', ?, ?, ?, ?) VALUES('add_temp', ?, ?, ?, ?)
`, ip, ttlSeconds, reason, apiClientIP) `, ip, ttlSeconds, reason, apiClientIP)
if err != nil { if err != nil {
@@ -153,7 +153,7 @@ func DeleteTempEntry(db *sql.DB, ip, apiClientIP string) error {
return err return err
} }
_, err = db.Exec(` _, err = db.Exec(`
INSERT INTO audit_log(action, entry, api_client_ip) INSERT INTO audit_log(action, ip, api_client_ip)
VALUES('delete_temp', ?, ?) VALUES('delete_temp', ?, ?)
`, ip, apiClientIP) `, ip, apiClientIP)
if err != nil { if err != nil {
@@ -209,11 +209,11 @@ func CleanupExpiredTemp(db *sql.DB) error {
for _, ip := range expiredIPs { for _, ip := range expiredIPs {
_, err = db.Exec(` _, err = db.Exec(`
INSERT INTO audit_log(action, entry, reason) INSERT INTO audit_log(action, ip, reason)
VALUES('expire_temp', ?, ?) VALUES('expire_temp', ?, ?)
`, ip, reason) `, 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 ip", "ip", ip, "error", err)
} }
slog.Info("expired temporary whitelist", "ip", ip) slog.Info("expired temporary whitelist", "ip", ip)
} }