-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolicy.go
More file actions
107 lines (95 loc) · 2.87 KB
/
Copy pathpolicy.go
File metadata and controls
107 lines (95 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//
// Copyright (C) 2025-2026 Holger de Carne
//
// This software may be modified and distributed under the terms
// of the MIT license. See the LICENSE file for details.
package httpserver
import (
"fmt"
"log/slog"
"net"
"net/http"
"net/netip"
)
// AccessPolicy interface is used to define access restrictions
// based on the accessing remote IP.
type AccessPolicy interface {
// Name gets the name of this policy.
Name() string
// Allow checks whether the given remote IP should be granted access.
Allow(remoteIP netip.Addr) bool
}
// WithAllowedNetworksPolicy restricts http server access to the given access policy.
func WithAllowedNetworksPolicy(policy AccessPolicy) OptionSetterFunc {
return func(server *Instance, _ *net.ListenConfig) {
server.allowedNetworksPolicy = policy
}
}
func enableAllowedNetworkPolicy(server *Instance) {
if server.allowedNetworksPolicy != nil {
server.httpServer.Handler = &accessPolicyHandler{
handler: server.httpServer.Handler,
policy: server.allowedNetworksPolicy,
logger: func() *slog.Logger { return server.logger },
}
}
}
type accessPolicyHandler struct {
handler http.Handler
policy AccessPolicy
logger func() *slog.Logger
}
func (h *accessPolicyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
remoteIP, ok := RequestRemoteIP(r)
if !ok {
h.logger().Warn("http server access denied due to missing remote IP", slog.String("remoteAddr", r.RemoteAddr), slog.String("policy", h.policy.Name()))
w.WriteHeader(http.StatusBadRequest)
return
}
if !h.policy.Allow(remoteIP) {
h.logger().Debug("http server access denied by policy", slog.Any("remoteIP", remoteIP), slog.String("policy", h.policy.Name()))
w.WriteHeader(http.StatusForbidden)
return
}
h.handler.ServeHTTP(w, r)
}
// ParseNetworks parses the given CIDR network definitions using
// [net.ParseCIDR] and returns the parsed networks.
func ParseNetworks(cidrs ...string) ([]netip.Prefix, error) {
networks := make([]netip.Prefix, 0, len(cidrs))
for _, cidr := range cidrs {
network, err := netip.ParsePrefix(cidr)
if err != nil {
return nil, fmt.Errorf("failed to parse network: '%s' (cause: %w)", cidr, err)
}
networks = append(networks, network)
}
return networks, nil
}
// AllowNetworks creates an access policy restricting access
// to the given networks.
func AllowNetworks(name string, networks []netip.Prefix) AccessPolicy {
if len(networks) == 0 {
return nil
}
policyName := name
if policyName == "" {
policyName = fmt.Sprintf("allow: %v", networks)
}
return &networkAccessPolicy{name: policyName, networks: networks}
}
type networkAccessPolicy struct {
name string
networks []netip.Prefix
}
func (p *networkAccessPolicy) Name() string {
return p.name
}
func (p *networkAccessPolicy) Allow(remoteIP netip.Addr) bool {
for _, network := range p.networks {
if network.Contains(remoteIP) {
return true
}
}
return false
}