refactor: replace PostgreSQL placeholders with generic SQL syntax and add audit logging for duplicate entries
- Replace $%d placeholders with ? in logsHandler for database abstraction - Remove unused argIndex variable in auth.go - Log duplicate permanent whitelist insertions to audit_log in db.go
This commit is contained in:
7
auth.go
7
auth.go
@@ -136,7 +136,6 @@ func logsHandler(cfg Config, db *sql.DB) http.HandlerFunc {
|
||||
|
||||
query := `SELECT timestamp, action, ip, cidr, ttl_seconds, reason, api_client_ip FROM audit_log`
|
||||
var args []any
|
||||
argIndex := 1
|
||||
|
||||
filterAction := r.URL.Query().Get("action")
|
||||
filterIP := r.URL.Query().Get("ip")
|
||||
@@ -145,14 +144,12 @@ func logsHandler(cfg Config, db *sql.DB) http.HandlerFunc {
|
||||
|
||||
var conditions []string
|
||||
if filterAction != "" {
|
||||
conditions = append(conditions, fmt.Sprintf(`action = $%d`, argIndex))
|
||||
conditions = append(conditions, `action = ?`)
|
||||
args = append(args, filterAction)
|
||||
argIndex++
|
||||
}
|
||||
if filterIP != "" {
|
||||
conditions = append(conditions, fmt.Sprintf(`ip = $%d`, argIndex))
|
||||
conditions = append(conditions, `ip = ?`)
|
||||
args = append(args, filterIP)
|
||||
argIndex++
|
||||
}
|
||||
|
||||
if len(conditions) > 0 {
|
||||
|
||||
4
db.go
4
db.go
@@ -60,6 +60,10 @@ func AddPermanentEntry(db *sql.DB, entry, apiClientIP string) (bool, error) {
|
||||
_, err := db.Exec(`INSERT INTO perm_whitelist(entry) VALUES(?)`, entry)
|
||||
if err != nil {
|
||||
if isUniqueConstraintError(err) {
|
||||
_, err = db.Exec(`INSERT INTO audit_log(action, cidr, api_client_ip) VALUES('add_perm_duplicate', ?, ?)`, entry, apiClientIP)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
|
||||
Reference in New Issue
Block a user