- Fix 314 errcheck violations (blank identifier for unrecoverable errors) - Fix errorlint violation (errors.Is for io.EOF) - Remove unused serveL7Route test helper - Simplify Duration.Seconds() selectors in tests - Remove unnecessary fmt.Sprintf in test - Migrate exclusion rules from issues.exclusions to linters.exclusions (v2 schema) - Add gosec test exclusions (G115, G304, G402, G705) - Disable fieldalignment govet analyzer (optimization, not correctness) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
75 lines
2.1 KiB
Go
75 lines
2.1 KiB
Go
package db
|
|
|
|
import "fmt"
|
|
|
|
// L7Policy is a database L7 policy record.
|
|
type L7Policy struct {
|
|
ID int64
|
|
RouteID int64
|
|
Type string // "block_user_agent" or "require_header"
|
|
Value string
|
|
}
|
|
|
|
// ListL7Policies returns all L7 policies for a route.
|
|
func (s *Store) ListL7Policies(routeID int64) ([]L7Policy, error) {
|
|
rows, err := s.db.Query(
|
|
"SELECT id, route_id, type, value FROM l7_policies WHERE route_id = ? ORDER BY id",
|
|
routeID,
|
|
)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("querying l7 policies: %w", err)
|
|
}
|
|
defer func() { _ = rows.Close() }()
|
|
|
|
var policies []L7Policy
|
|
for rows.Next() {
|
|
var p L7Policy
|
|
if err := rows.Scan(&p.ID, &p.RouteID, &p.Type, &p.Value); err != nil {
|
|
return nil, fmt.Errorf("scanning l7 policy: %w", err)
|
|
}
|
|
policies = append(policies, p)
|
|
}
|
|
return policies, rows.Err()
|
|
}
|
|
|
|
// CreateL7Policy inserts an L7 policy and returns its ID.
|
|
func (s *Store) CreateL7Policy(routeID int64, policyType, value string) (int64, error) {
|
|
result, err := s.db.Exec(
|
|
"INSERT INTO l7_policies (route_id, type, value) VALUES (?, ?, ?)",
|
|
routeID, policyType, value,
|
|
)
|
|
if err != nil {
|
|
return 0, fmt.Errorf("inserting l7 policy: %w", err)
|
|
}
|
|
return result.LastInsertId()
|
|
}
|
|
|
|
// DeleteL7Policy deletes an L7 policy by route ID, type, and value.
|
|
func (s *Store) DeleteL7Policy(routeID int64, policyType, value string) error {
|
|
result, err := s.db.Exec(
|
|
"DELETE FROM l7_policies WHERE route_id = ? AND type = ? AND value = ?",
|
|
routeID, policyType, value,
|
|
)
|
|
if err != nil {
|
|
return fmt.Errorf("deleting l7 policy: %w", err)
|
|
}
|
|
n, _ := result.RowsAffected()
|
|
if n == 0 {
|
|
return fmt.Errorf("l7 policy not found (route %d, type %q, value %q)", routeID, policyType, value)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// GetRouteID returns the route ID for a listener/hostname pair.
|
|
func (s *Store) GetRouteID(listenerID int64, hostname string) (int64, error) {
|
|
var id int64
|
|
err := s.db.QueryRow(
|
|
"SELECT id FROM routes WHERE listener_id = ? AND hostname = ?",
|
|
listenerID, hostname,
|
|
).Scan(&id)
|
|
if err != nil {
|
|
return 0, fmt.Errorf("looking up route %q on listener %d: %w", hostname, listenerID, err)
|
|
}
|
|
return id, nil
|
|
}
|