mirror of
https://github.com/TecharoHQ/anubis.git
synced 2025-08-05 10:48:10 -04:00
chore: move checker package to top level
Signed-off-by: Xe Iaso <me@xeiaso.net>
This commit is contained in:
parent
dc0dde3053
commit
208ceca723
@ -10,7 +10,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/TecharoHQ/anubis/internal"
|
"github.com/TecharoHQ/anubis/internal"
|
||||||
"github.com/TecharoHQ/anubis/lib/policy/checker"
|
"github.com/TecharoHQ/anubis/lib/checker"
|
||||||
iptoasnv1 "github.com/TecharoHQ/thoth-proto/gen/techaro/thoth/iptoasn/v1"
|
iptoasnv1 "github.com/TecharoHQ/thoth-proto/gen/techaro/thoth/iptoasn/v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/TecharoHQ/anubis/internal/thoth"
|
"github.com/TecharoHQ/anubis/internal/thoth"
|
||||||
"github.com/TecharoHQ/anubis/lib/policy/checker"
|
"github.com/TecharoHQ/anubis/lib/checker"
|
||||||
iptoasnv1 "github.com/TecharoHQ/thoth-proto/gen/techaro/thoth/iptoasn/v1"
|
iptoasnv1 "github.com/TecharoHQ/thoth-proto/gen/techaro/thoth/iptoasn/v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -9,7 +9,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/TecharoHQ/anubis/lib/policy/checker"
|
"github.com/TecharoHQ/anubis/lib/checker"
|
||||||
iptoasnv1 "github.com/TecharoHQ/thoth-proto/gen/techaro/thoth/iptoasn/v1"
|
iptoasnv1 "github.com/TecharoHQ/thoth-proto/gen/techaro/thoth/iptoasn/v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/TecharoHQ/anubis/internal/thoth"
|
"github.com/TecharoHQ/anubis/internal/thoth"
|
||||||
"github.com/TecharoHQ/anubis/lib/policy/checker"
|
"github.com/TecharoHQ/anubis/lib/checker"
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ checker.Impl = &thoth.GeoIPChecker{}
|
var _ checker.Impl = &thoth.GeoIPChecker{}
|
||||||
|
@ -26,14 +26,17 @@ import (
|
|||||||
"github.com/TecharoHQ/anubis/internal/dnsbl"
|
"github.com/TecharoHQ/anubis/internal/dnsbl"
|
||||||
"github.com/TecharoHQ/anubis/internal/ogtags"
|
"github.com/TecharoHQ/anubis/internal/ogtags"
|
||||||
"github.com/TecharoHQ/anubis/lib/challenge"
|
"github.com/TecharoHQ/anubis/lib/challenge"
|
||||||
|
"github.com/TecharoHQ/anubis/lib/checker"
|
||||||
"github.com/TecharoHQ/anubis/lib/localization"
|
"github.com/TecharoHQ/anubis/lib/localization"
|
||||||
"github.com/TecharoHQ/anubis/lib/policy"
|
"github.com/TecharoHQ/anubis/lib/policy"
|
||||||
"github.com/TecharoHQ/anubis/lib/policy/checker"
|
|
||||||
"github.com/TecharoHQ/anubis/lib/policy/config"
|
"github.com/TecharoHQ/anubis/lib/policy/config"
|
||||||
|
|
||||||
// challenge implementations
|
// challenge implementations
|
||||||
_ "github.com/TecharoHQ/anubis/lib/challenge/metarefresh"
|
_ "github.com/TecharoHQ/anubis/lib/challenge/metarefresh"
|
||||||
_ "github.com/TecharoHQ/anubis/lib/challenge/proofofwork"
|
_ "github.com/TecharoHQ/anubis/lib/challenge/proofofwork"
|
||||||
|
|
||||||
|
// checker implementations
|
||||||
|
_ "github.com/TecharoHQ/anubis/lib/checker/remoteaddress"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -8,8 +8,8 @@ import (
|
|||||||
"net/netip"
|
"net/netip"
|
||||||
|
|
||||||
"github.com/TecharoHQ/anubis/internal"
|
"github.com/TecharoHQ/anubis/internal"
|
||||||
|
"github.com/TecharoHQ/anubis/lib/checker"
|
||||||
"github.com/TecharoHQ/anubis/lib/policy"
|
"github.com/TecharoHQ/anubis/lib/policy"
|
||||||
"github.com/TecharoHQ/anubis/lib/policy/checker"
|
|
||||||
"github.com/TecharoHQ/anubis/lib/policy/config"
|
"github.com/TecharoHQ/anubis/lib/policy/config"
|
||||||
"github.com/gaissmai/bart"
|
"github.com/gaissmai/bart"
|
||||||
)
|
)
|
||||||
@ -18,7 +18,9 @@ var (
|
|||||||
ErrNoRemoteAddresses = errors.New("remoteaddress: no remote addresses defined")
|
ErrNoRemoteAddresses = errors.New("remoteaddress: no remote addresses defined")
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {}
|
func init() {
|
||||||
|
checker.Register("remote_address", Factory{})
|
||||||
|
}
|
||||||
|
|
||||||
type Factory struct{}
|
type Factory struct{}
|
||||||
|
|
||||||
@ -50,7 +52,7 @@ func (Factory) Create(inp json.RawMessage) (checker.Impl, error) {
|
|||||||
table.Insert(cidr)
|
table.Insert(cidr)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &RemoteAddrChecker{
|
return &Impl{
|
||||||
prefixTable: table,
|
prefixTable: table,
|
||||||
hash: internal.FastHash(string(inp)),
|
hash: internal.FastHash(string(inp)),
|
||||||
}, nil
|
}, nil
|
||||||
@ -80,12 +82,12 @@ func (fc fileConfig) Valid() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type RemoteAddrChecker struct {
|
type Impl struct {
|
||||||
prefixTable *bart.Lite
|
prefixTable *bart.Lite
|
||||||
hash string
|
hash string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rac *RemoteAddrChecker) Check(r *http.Request) (bool, error) {
|
func (rac *Impl) Check(r *http.Request) (bool, error) {
|
||||||
host := r.Header.Get("X-Real-Ip")
|
host := r.Header.Get("X-Real-Ip")
|
||||||
if host == "" {
|
if host == "" {
|
||||||
return false, fmt.Errorf("%w: header X-Real-Ip is not set", policy.ErrMisconfiguration)
|
return false, fmt.Errorf("%w: header X-Real-Ip is not set", policy.ErrMisconfiguration)
|
||||||
@ -99,6 +101,6 @@ func (rac *RemoteAddrChecker) Check(r *http.Request) (bool, error) {
|
|||||||
return rac.prefixTable.Contains(addr), nil
|
return rac.prefixTable.Contains(addr), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rac *RemoteAddrChecker) Hash() string {
|
func (rac *Impl) Hash() string {
|
||||||
return rac.hash
|
return rac.hash
|
||||||
}
|
}
|
@ -5,10 +5,15 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/netip"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/TecharoHQ/anubis/lib/policy/checker"
|
"github.com/TecharoHQ/anubis/internal"
|
||||||
|
"github.com/TecharoHQ/anubis/lib/checker"
|
||||||
|
"github.com/TecharoHQ/anubis/lib/policy"
|
||||||
"github.com/TecharoHQ/anubis/lib/policy/config"
|
"github.com/TecharoHQ/anubis/lib/policy/config"
|
||||||
|
"github.com/gaissmai/bart"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestFactoryIsCheckerFactory(t *testing.T) {
|
func TestFactoryIsCheckerFactory(t *testing.T) {
|
||||||
@ -137,80 +142,97 @@ func TestFactoryCreate(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// func TestRemoteAddrChecker(t *testing.T) {
|
func racFromCidrs(t *testing.T, inp []string) *Impl {
|
||||||
// for _, tt := range []struct {
|
t.Helper()
|
||||||
// err error
|
|
||||||
// name string
|
|
||||||
// ip string
|
|
||||||
// cidrs []string
|
|
||||||
// ok bool
|
|
||||||
// }{
|
|
||||||
// {
|
|
||||||
// name: "match_ipv4",
|
|
||||||
// cidrs: []string{"0.0.0.0/0"},
|
|
||||||
// ip: "1.1.1.1",
|
|
||||||
// ok: true,
|
|
||||||
// err: nil,
|
|
||||||
// },
|
|
||||||
// {
|
|
||||||
// name: "match_ipv6",
|
|
||||||
// cidrs: []string{"::/0"},
|
|
||||||
// ip: "cafe:babe::",
|
|
||||||
// ok: true,
|
|
||||||
// err: nil,
|
|
||||||
// },
|
|
||||||
// {
|
|
||||||
// name: "not_match_ipv4",
|
|
||||||
// cidrs: []string{"1.1.1.1/32"},
|
|
||||||
// ip: "1.1.1.2",
|
|
||||||
// ok: false,
|
|
||||||
// err: nil,
|
|
||||||
// },
|
|
||||||
// {
|
|
||||||
// name: "not_match_ipv6",
|
|
||||||
// cidrs: []string{"cafe:babe::/128"},
|
|
||||||
// ip: "cafe:babe:4::/128",
|
|
||||||
// ok: false,
|
|
||||||
// err: nil,
|
|
||||||
// },
|
|
||||||
// {
|
|
||||||
// name: "no_ip_set",
|
|
||||||
// cidrs: []string{"::/0"},
|
|
||||||
// ok: false,
|
|
||||||
// err: policy.ErrMisconfiguration,
|
|
||||||
// },
|
|
||||||
// {
|
|
||||||
// name: "invalid_ip",
|
|
||||||
// cidrs: []string{"::/0"},
|
|
||||||
// ip: "According to all natural laws of aviation",
|
|
||||||
// ok: false,
|
|
||||||
// err: policy.ErrMisconfiguration,
|
|
||||||
// },
|
|
||||||
// } {
|
|
||||||
// t.Run(tt.name, func(t *testing.T) {
|
|
||||||
// rac, err := NewRemoteAddrChecker(tt.cidrs)
|
|
||||||
// if err != nil && !errors.Is(err, tt.err) {
|
|
||||||
// t.Fatalf("creating RemoteAddrChecker failed: %v", err)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// r, err := http.NewRequest(http.MethodGet, "/", nil)
|
var result Impl
|
||||||
// if err != nil {
|
result.prefixTable = new(bart.Lite)
|
||||||
// t.Fatalf("can't make request: %v", err)
|
result.hash = internal.FastHash(strings.Join(inp, ","))
|
||||||
// }
|
|
||||||
|
|
||||||
// if tt.ip != "" {
|
for _, cidr := range inp {
|
||||||
// r.Header.Add("X-Real-Ip", tt.ip)
|
pfx, err := netip.ParsePrefix(cidr)
|
||||||
// }
|
if err != nil {
|
||||||
|
t.Errorf("prefix %q is invalid: %v", cidr, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
// ok, err := rac.Check(r)
|
result.prefixTable.Insert(pfx)
|
||||||
|
}
|
||||||
|
|
||||||
// if tt.ok != ok {
|
return &result
|
||||||
// t.Errorf("ok: %v, wanted: %v", ok, tt.ok)
|
}
|
||||||
// }
|
|
||||||
|
|
||||||
// if err != nil && tt.err != nil && !errors.Is(err, tt.err) {
|
func TestRemoteAddrChecker(t *testing.T) {
|
||||||
// t.Errorf("err: %v, wanted: %v", err, tt.err)
|
for _, tt := range []struct {
|
||||||
// }
|
err error
|
||||||
// })
|
name string
|
||||||
// }
|
ip string
|
||||||
// }
|
cidrs []string
|
||||||
|
ok bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "match_ipv4",
|
||||||
|
cidrs: []string{"0.0.0.0/0"},
|
||||||
|
ip: "1.1.1.1",
|
||||||
|
ok: true,
|
||||||
|
err: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "match_ipv6",
|
||||||
|
cidrs: []string{"::/0"},
|
||||||
|
ip: "cafe:babe::",
|
||||||
|
ok: true,
|
||||||
|
err: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "not_match_ipv4",
|
||||||
|
cidrs: []string{"1.1.1.1/32"},
|
||||||
|
ip: "1.1.1.2",
|
||||||
|
ok: false,
|
||||||
|
err: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "not_match_ipv6",
|
||||||
|
cidrs: []string{"cafe:babe::/128"},
|
||||||
|
ip: "cafe:babe:4::/128",
|
||||||
|
ok: false,
|
||||||
|
err: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "no_ip_set",
|
||||||
|
cidrs: []string{"::/0"},
|
||||||
|
ok: false,
|
||||||
|
err: policy.ErrMisconfiguration,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "invalid_ip",
|
||||||
|
cidrs: []string{"::/0"},
|
||||||
|
ip: "According to all natural laws of aviation",
|
||||||
|
ok: false,
|
||||||
|
err: policy.ErrMisconfiguration,
|
||||||
|
},
|
||||||
|
} {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
rac := racFromCidrs(t, tt.cidrs)
|
||||||
|
|
||||||
|
r, err := http.NewRequest(http.MethodGet, "/", nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("can't make request: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if tt.ip != "" {
|
||||||
|
r.Header.Add("X-Real-Ip", tt.ip)
|
||||||
|
}
|
||||||
|
|
||||||
|
ok, err := rac.Check(r)
|
||||||
|
|
||||||
|
if tt.ok != ok {
|
||||||
|
t.Errorf("ok: %v, wanted: %v", ok, tt.ok)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil && tt.err != nil && !errors.Is(err, tt.err) {
|
||||||
|
t.Errorf("err: %v, wanted: %v", err, tt.err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
@ -4,7 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/TecharoHQ/anubis/internal"
|
"github.com/TecharoHQ/anubis/internal"
|
||||||
"github.com/TecharoHQ/anubis/lib/policy/checker"
|
"github.com/TecharoHQ/anubis/lib/checker"
|
||||||
"github.com/TecharoHQ/anubis/lib/policy/config"
|
"github.com/TecharoHQ/anubis/lib/policy/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -9,7 +9,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/TecharoHQ/anubis/internal"
|
"github.com/TecharoHQ/anubis/internal"
|
||||||
"github.com/TecharoHQ/anubis/lib/policy/checker"
|
"github.com/TecharoHQ/anubis/lib/checker"
|
||||||
"github.com/gaissmai/bart"
|
"github.com/gaissmai/bart"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -1,5 +0,0 @@
|
|||||||
{
|
|
||||||
"remote_addresses": [
|
|
||||||
"according to all laws of aviation"
|
|
||||||
]
|
|
||||||
}
|
|
@ -1,3 +0,0 @@
|
|||||||
{
|
|
||||||
"remote_addresses": []
|
|
||||||
}
|
|
@ -1 +0,0 @@
|
|||||||
]
|
|
@ -1,5 +0,0 @@
|
|||||||
{
|
|
||||||
"remote_addresses": [
|
|
||||||
"1.1.1.1/32"
|
|
||||||
]
|
|
||||||
}
|
|
@ -9,7 +9,7 @@ import (
|
|||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
|
||||||
"github.com/TecharoHQ/anubis/internal/thoth"
|
"github.com/TecharoHQ/anubis/internal/thoth"
|
||||||
"github.com/TecharoHQ/anubis/lib/policy/checker"
|
"github.com/TecharoHQ/anubis/lib/checker"
|
||||||
"github.com/TecharoHQ/anubis/lib/policy/config"
|
"github.com/TecharoHQ/anubis/lib/policy/config"
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
"github.com/prometheus/client_golang/prometheus/promauto"
|
"github.com/prometheus/client_golang/prometheus/promauto"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user