refactor: format SQL queries with multi-line style
Improve readability of SQL queries in db.go by formatting them with consistent multi-line style. This change affects all database operations including AddPermanentEntry, DeletePermanentEntry, AddTempEntry, DeleteTempEntry, and CleanupExpiredTemp. Also simplifies DeletePermanentEntry and DeleteTempEntry by removing the RowsAffected() checks - audit logging is now executed unconditionally regardless of whether rows were actually affected.
This commit is contained in:
82
db.go
82
db.go
@@ -60,7 +60,10 @@ func AddPermanentEntry(db *sql.DB, entry, apiClientIP string) (bool, error) {
|
|||||||
_, err := db.Exec(`INSERT INTO perm_whitelist(entry) VALUES(?)`, entry)
|
_, err := db.Exec(`INSERT INTO perm_whitelist(entry) VALUES(?)`, entry)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if isUniqueConstraintError(err) {
|
if isUniqueConstraintError(err) {
|
||||||
_, err = db.Exec(`INSERT INTO audit_log(action, cidr, api_client_ip) VALUES('add_perm_duplicate', ?, ?)`, entry, apiClientIP)
|
_, err = db.Exec(`
|
||||||
|
INSERT INTO audit_log(action, cidr, api_client_ip)
|
||||||
|
VALUES('add_perm_duplicate', ?, ?)
|
||||||
|
`, entry, apiClientIP)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
@@ -68,7 +71,10 @@ func AddPermanentEntry(db *sql.DB, entry, apiClientIP string) (bool, error) {
|
|||||||
}
|
}
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
_, err = db.Exec(`INSERT INTO audit_log(action, cidr, api_client_ip) VALUES('add_perm', ?, ?)`, entry, apiClientIP)
|
_, err = db.Exec(`
|
||||||
|
INSERT INTO audit_log(action, cidr, api_client_ip)
|
||||||
|
VALUES('add_perm', ?, ?)
|
||||||
|
`, entry, apiClientIP)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return true, err
|
return true, err
|
||||||
}
|
}
|
||||||
@@ -76,22 +82,28 @@ func AddPermanentEntry(db *sql.DB, entry, apiClientIP string) (bool, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func DeletePermanentEntry(db *sql.DB, entry, apiClientIP string) error {
|
func DeletePermanentEntry(db *sql.DB, entry, apiClientIP string) error {
|
||||||
res, err := db.Exec(`DELETE FROM perm_whitelist WHERE entry = ?`, entry)
|
_, err := db.Exec(`
|
||||||
|
DELETE FROM perm_whitelist
|
||||||
|
WHERE entry = ?
|
||||||
|
`, entry)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
rows, _ := res.RowsAffected()
|
_, err = db.Exec(`
|
||||||
if rows > 0 {
|
INSERT INTO audit_log(action, cidr, api_client_ip)
|
||||||
_, err = db.Exec(`INSERT INTO audit_log(action, cidr, api_client_ip) VALUES('delete_perm', ?, ?)`, entry, apiClientIP)
|
VALUES('delete_perm', ?, ?)
|
||||||
|
`, entry, apiClientIP)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func IsPermanentWhitelisted(db *sql.DB, ip string) (bool, error) {
|
func IsPermanentWhitelisted(db *sql.DB, ip string) (bool, error) {
|
||||||
rows, err := db.Query(`SELECT entry FROM perm_whitelist`)
|
rows, err := db.Query(`
|
||||||
|
SELECT entry
|
||||||
|
FROM perm_whitelist
|
||||||
|
`)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
@@ -123,8 +135,10 @@ func AddTempEntry(db *sql.DB, ip string, ttlSeconds int, reason, apiClientIP str
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
_, err = db.Exec(`INSERT INTO audit_log(action, ip, ttl_seconds, reason, api_client_ip) VALUES('add_temp', ?, ?, ?, ?)`,
|
_, err = db.Exec(`
|
||||||
ip, ttlSeconds, reason, apiClientIP)
|
INSERT INTO audit_log(action, ip, ttl_seconds, reason, api_client_ip)
|
||||||
|
VALUES('add_temp', ?, ?, ?, ?)
|
||||||
|
`, ip, ttlSeconds, reason, apiClientIP)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -132,23 +146,30 @@ func AddTempEntry(db *sql.DB, ip string, ttlSeconds int, reason, apiClientIP str
|
|||||||
}
|
}
|
||||||
|
|
||||||
func DeleteTempEntry(db *sql.DB, ip, apiClientIP string) error {
|
func DeleteTempEntry(db *sql.DB, ip, apiClientIP string) error {
|
||||||
res, err := db.Exec(`DELETE FROM temp_whitelist WHERE ip = ?`, ip)
|
_, err := db.Exec(`
|
||||||
|
DELETE FROM temp_whitelist
|
||||||
|
WHERE ip = ?
|
||||||
|
`, ip)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
rows, _ := res.RowsAffected()
|
_, err = db.Exec(`
|
||||||
if rows > 0 {
|
INSERT INTO audit_log(action, ip, api_client_ip)
|
||||||
_, err = db.Exec(`INSERT INTO audit_log(action, ip, api_client_ip) VALUES('delete_temp', ?, ?)`, ip, apiClientIP)
|
VALUES('delete_temp', ?, ?)
|
||||||
|
`, ip, apiClientIP)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func IsTempWhitelisted(db *sql.DB, ip string) (bool, error) {
|
func IsTempWhitelisted(db *sql.DB, ip string) (bool, error) {
|
||||||
var expiresAt time.Time
|
var expiresAt time.Time
|
||||||
err := db.QueryRow(`SELECT expires_at FROM temp_whitelist WHERE ip = ?`, ip).Scan(&expiresAt)
|
err := db.QueryRow(`
|
||||||
|
SELECT expires_at
|
||||||
|
FROM temp_whitelist
|
||||||
|
WHERE ip = ?
|
||||||
|
`, ip).Scan(&expiresAt)
|
||||||
if err == sql.ErrNoRows {
|
if err == sql.ErrNoRows {
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
@@ -159,7 +180,11 @@ func IsTempWhitelisted(db *sql.DB, ip string) (bool, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func CleanupExpiredTemp(db *sql.DB) error {
|
func CleanupExpiredTemp(db *sql.DB) error {
|
||||||
rows, err := db.Query(`SELECT ip FROM temp_whitelist WHERE expires_at <= ?`, time.Now())
|
rows, err := db.Query(`
|
||||||
|
SELECT ip
|
||||||
|
FROM temp_whitelist
|
||||||
|
WHERE expires_at <= ?
|
||||||
|
`, time.Now())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -172,13 +197,19 @@ func CleanupExpiredTemp(db *sql.DB) error {
|
|||||||
}
|
}
|
||||||
rows.Close()
|
rows.Close()
|
||||||
|
|
||||||
_, err = db.Exec(`DELETE FROM temp_whitelist WHERE expires_at <= ?`, time.Now())
|
_, err = db.Exec(`
|
||||||
|
DELETE FROM temp_whitelist
|
||||||
|
WHERE expires_at <= ?
|
||||||
|
`, time.Now())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, ip := range expiredIPs {
|
for _, ip := range expiredIPs {
|
||||||
_, err = db.Exec(`INSERT INTO audit_log(action, ip) VALUES('expire_temp', ?)`, ip)
|
_, err = db.Exec(`
|
||||||
|
INSERT INTO audit_log(action, ip)
|
||||||
|
VALUES('expire_temp', ?)
|
||||||
|
`, ip)
|
||||||
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)
|
||||||
}
|
}
|
||||||
@@ -188,7 +219,12 @@ func CleanupExpiredTemp(db *sql.DB) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ListTempEntries(db *sql.DB) ([]map[string]interface{}, error) {
|
func ListTempEntries(db *sql.DB) ([]map[string]interface{}, error) {
|
||||||
rows, err := db.Query(`SELECT ip, expires_at, reason FROM temp_whitelist WHERE expires_at > ? ORDER BY expires_at`, time.Now())
|
rows, err := db.Query(`
|
||||||
|
SELECT ip, expires_at, reason
|
||||||
|
FROM temp_whitelist
|
||||||
|
WHERE expires_at > ?
|
||||||
|
ORDER BY expires_at
|
||||||
|
`, time.Now())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -210,7 +246,11 @@ func ListTempEntries(db *sql.DB) ([]map[string]interface{}, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ListPermEntries(db *sql.DB) ([]map[string]interface{}, error) {
|
func ListPermEntries(db *sql.DB) ([]map[string]interface{}, error) {
|
||||||
rows, err := db.Query(`SELECT entry FROM perm_whitelist ORDER BY created_at`)
|
rows, err := db.Query(`
|
||||||
|
SELECT entry
|
||||||
|
FROM perm_whitelist
|
||||||
|
ORDER BY created_at
|
||||||
|
`)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user