refactor: move lib/policy/config to lib/config

Signed-off-by: Xe Iaso <me@xeiaso.net>
This commit is contained in:
Xe Iaso
2025-11-19 15:53:30 -05:00
parent 02989f03d0
commit 8912050882
97 changed files with 41 additions and 40 deletions
+1 -1
View File
@@ -31,8 +31,8 @@ import (
"github.com/TecharoHQ/anubis/data" "github.com/TecharoHQ/anubis/data"
"github.com/TecharoHQ/anubis/internal" "github.com/TecharoHQ/anubis/internal"
libanubis "github.com/TecharoHQ/anubis/lib" libanubis "github.com/TecharoHQ/anubis/lib"
"github.com/TecharoHQ/anubis/lib/config"
botPolicy "github.com/TecharoHQ/anubis/lib/policy" botPolicy "github.com/TecharoHQ/anubis/lib/policy"
"github.com/TecharoHQ/anubis/lib/policy/config"
"github.com/TecharoHQ/anubis/lib/thoth" "github.com/TecharoHQ/anubis/lib/thoth"
"github.com/TecharoHQ/anubis/web" "github.com/TecharoHQ/anubis/web"
"github.com/facebookgo/flagenv" "github.com/facebookgo/flagenv"
+1 -1
View File
@@ -12,7 +12,7 @@ import (
"regexp" "regexp"
"strings" "strings"
"github.com/TecharoHQ/anubis/lib/policy/config" "github.com/TecharoHQ/anubis/lib/config"
"sigs.k8s.io/yaml" "sigs.k8s.io/yaml"
) )
+6 -5
View File
@@ -2,6 +2,7 @@ package internal
import ( import (
"fmt" "fmt"
"io"
"log" "log"
"log/slog" "log/slog"
"net/http" "net/http"
@@ -9,7 +10,7 @@ import (
"strings" "strings"
) )
func InitSlog(level string) { func InitSlog(level string, sink io.Writer) *slog.Logger {
var programLevel slog.Level var programLevel slog.Level
if err := (&programLevel).UnmarshalText([]byte(level)); err != nil { if err := (&programLevel).UnmarshalText([]byte(level)); err != nil {
fmt.Fprintf(os.Stderr, "invalid log level %s: %v, using info\n", level, err) fmt.Fprintf(os.Stderr, "invalid log level %s: %v, using info\n", level, err)
@@ -19,11 +20,12 @@ func InitSlog(level string) {
leveler := &slog.LevelVar{} leveler := &slog.LevelVar{}
leveler.Set(programLevel) leveler.Set(programLevel)
h := slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{ h := slog.NewJSONHandler(sink, &slog.HandlerOptions{
AddSource: true, AddSource: true,
Level: leveler, Level: leveler,
}) })
slog.SetDefault(slog.New(h)) result := slog.New(h)
return result
} }
func GetRequestLogger(base *slog.Logger, r *http.Request) *slog.Logger { func GetRequestLogger(base *slog.Logger, r *http.Request) *slog.Logger {
@@ -39,8 +41,7 @@ func GetRequestLogger(base *slog.Logger, r *http.Request) *slog.Logger {
"user_agent", r.UserAgent(), "user_agent", r.UserAgent(),
"accept_language", r.Header.Get("Accept-Language"), "accept_language", r.Header.Get("Accept-Language"),
"priority", r.Header.Get("Priority"), "priority", r.Header.Get("Priority"),
"x-forwarded-for", "x-forwarded-for", r.Header.Get("X-Forwarded-For"),
r.Header.Get("X-Forwarded-For"),
"x-real-ip", r.Header.Get("X-Real-Ip"), "x-real-ip", r.Header.Get("X-Real-Ip"),
) )
} }
+1 -1
View File
@@ -9,7 +9,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/TecharoHQ/anubis/lib/policy/config" "github.com/TecharoHQ/anubis/lib/config"
"github.com/TecharoHQ/anubis/lib/store" "github.com/TecharoHQ/anubis/lib/store"
"github.com/TecharoHQ/anubis/lib/store/memory" "github.com/TecharoHQ/anubis/lib/store/memory"
) )
+1 -1
View File
@@ -11,7 +11,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/TecharoHQ/anubis/lib/policy/config" "github.com/TecharoHQ/anubis/lib/config"
"github.com/TecharoHQ/anubis/lib/store/memory" "github.com/TecharoHQ/anubis/lib/store/memory"
"golang.org/x/net/html" "golang.org/x/net/html"
) )
+1 -1
View File
@@ -7,7 +7,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/TecharoHQ/anubis/lib/policy/config" "github.com/TecharoHQ/anubis/lib/config"
"github.com/TecharoHQ/anubis/lib/store/memory" "github.com/TecharoHQ/anubis/lib/store/memory"
) )
+1 -1
View File
@@ -6,7 +6,7 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/TecharoHQ/anubis/lib/policy/config" "github.com/TecharoHQ/anubis/lib/config"
"github.com/TecharoHQ/anubis/lib/store/memory" "github.com/TecharoHQ/anubis/lib/store/memory"
"golang.org/x/net/html" "golang.org/x/net/html"
) )
+1 -1
View File
@@ -11,7 +11,7 @@ import (
"sync" "sync"
"time" "time"
"github.com/TecharoHQ/anubis/lib/policy/config" "github.com/TecharoHQ/anubis/lib/config"
"github.com/TecharoHQ/anubis/lib/store" "github.com/TecharoHQ/anubis/lib/store"
) )
+1 -1
View File
@@ -7,7 +7,7 @@ import (
"testing" "testing"
"unicode/utf8" "unicode/utf8"
"github.com/TecharoHQ/anubis/lib/policy/config" "github.com/TecharoHQ/anubis/lib/config"
"github.com/TecharoHQ/anubis/lib/store/memory" "github.com/TecharoHQ/anubis/lib/store/memory"
"golang.org/x/net/html" "golang.org/x/net/html"
) )
+1 -1
View File
@@ -22,7 +22,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/TecharoHQ/anubis/lib/policy/config" "github.com/TecharoHQ/anubis/lib/config"
"github.com/TecharoHQ/anubis/lib/store/memory" "github.com/TecharoHQ/anubis/lib/store/memory"
) )
+1 -1
View File
@@ -6,7 +6,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/TecharoHQ/anubis/lib/policy/config" "github.com/TecharoHQ/anubis/lib/config"
"github.com/TecharoHQ/anubis/lib/store/memory" "github.com/TecharoHQ/anubis/lib/store/memory"
"golang.org/x/net/html" "golang.org/x/net/html"
) )
+1 -1
View File
@@ -27,10 +27,10 @@ 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/config"
"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/checker"
"github.com/TecharoHQ/anubis/lib/policy/config"
"github.com/TecharoHQ/anubis/lib/store" "github.com/TecharoHQ/anubis/lib/store"
// challenge implementations // challenge implementations
+1 -1
View File
@@ -20,8 +20,8 @@ import (
"github.com/TecharoHQ/anubis/data" "github.com/TecharoHQ/anubis/data"
"github.com/TecharoHQ/anubis/internal" "github.com/TecharoHQ/anubis/internal"
"github.com/TecharoHQ/anubis/lib/challenge" "github.com/TecharoHQ/anubis/lib/challenge"
"github.com/TecharoHQ/anubis/lib/config"
"github.com/TecharoHQ/anubis/lib/policy" "github.com/TecharoHQ/anubis/lib/policy"
"github.com/TecharoHQ/anubis/lib/policy/config"
"github.com/TecharoHQ/anubis/lib/store" "github.com/TecharoHQ/anubis/lib/store"
"github.com/TecharoHQ/anubis/lib/thoth/thothmock" "github.com/TecharoHQ/anubis/lib/thoth/thothmock"
) )
+1 -1
View File
@@ -6,8 +6,8 @@ import (
"sort" "sort"
"sync" "sync"
"github.com/TecharoHQ/anubis/lib/config"
"github.com/TecharoHQ/anubis/lib/policy" "github.com/TecharoHQ/anubis/lib/policy"
"github.com/TecharoHQ/anubis/lib/policy/config"
"github.com/TecharoHQ/anubis/lib/store" "github.com/TecharoHQ/anubis/lib/store"
"github.com/a-h/templ" "github.com/a-h/templ"
) )
@@ -8,8 +8,8 @@ import (
"testing" "testing"
"github.com/TecharoHQ/anubis/lib/challenge" "github.com/TecharoHQ/anubis/lib/challenge"
"github.com/TecharoHQ/anubis/lib/config"
"github.com/TecharoHQ/anubis/lib/policy" "github.com/TecharoHQ/anubis/lib/policy"
"github.com/TecharoHQ/anubis/lib/policy/config"
) )
func mkRequest(t *testing.T, values map[string]string) *http.Request { func mkRequest(t *testing.T, values map[string]string) *http.Request {
+1 -1
View File
@@ -18,9 +18,9 @@ import (
"github.com/TecharoHQ/anubis/internal" "github.com/TecharoHQ/anubis/internal"
"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/config"
"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/config"
"github.com/TecharoHQ/anubis/web" "github.com/TecharoHQ/anubis/web"
"github.com/TecharoHQ/anubis/xess" "github.com/TecharoHQ/anubis/xess"
"github.com/a-h/templ" "github.com/a-h/templ"
@@ -8,7 +8,7 @@ import (
"testing" "testing"
"github.com/TecharoHQ/anubis/data" "github.com/TecharoHQ/anubis/data"
. "github.com/TecharoHQ/anubis/lib/policy/config" . "github.com/TecharoHQ/anubis/lib/config"
) )
func p[V any](v V) *V { return &v } func p[V any](v V) *V { return &v }
@@ -5,7 +5,7 @@ import (
"errors" "errors"
"testing" "testing"
"github.com/TecharoHQ/anubis/lib/policy/config" "github.com/TecharoHQ/anubis/lib/config"
"github.com/TecharoHQ/anubis/lib/store/bbolt" "github.com/TecharoHQ/anubis/lib/store/bbolt"
"github.com/TecharoHQ/anubis/lib/store/valkey" "github.com/TecharoHQ/anubis/lib/store/valkey"
) )
+5 -5
View File
@@ -18,7 +18,7 @@ func TestInvalidChallengeMethod(t *testing.T) {
} }
func TestBadConfigs(t *testing.T) { func TestBadConfigs(t *testing.T) {
finfos, err := os.ReadDir("policy/config/testdata/bad") finfos, err := os.ReadDir("config/testdata/bad")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@@ -26,7 +26,7 @@ func TestBadConfigs(t *testing.T) {
for _, st := range finfos { for _, st := range finfos {
st := st st := st
t.Run(st.Name(), func(t *testing.T) { t.Run(st.Name(), func(t *testing.T) {
if _, err := LoadPoliciesOrDefault(t.Context(), filepath.Join("policy", "config", "testdata", "bad", st.Name()), anubis.DefaultDifficulty); err == nil { if _, err := LoadPoliciesOrDefault(t.Context(), filepath.Join("config", "testdata", "bad", st.Name()), anubis.DefaultDifficulty); err == nil {
t.Fatal(err) t.Fatal(err)
} else { } else {
t.Log(err) t.Log(err)
@@ -36,7 +36,7 @@ func TestBadConfigs(t *testing.T) {
} }
func TestGoodConfigs(t *testing.T) { func TestGoodConfigs(t *testing.T) {
finfos, err := os.ReadDir("policy/config/testdata/good") finfos, err := os.ReadDir("config/testdata/good")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@@ -46,13 +46,13 @@ func TestGoodConfigs(t *testing.T) {
t.Run(st.Name(), func(t *testing.T) { t.Run(st.Name(), func(t *testing.T) {
t.Run("with-thoth", func(t *testing.T) { t.Run("with-thoth", func(t *testing.T) {
ctx := thothmock.WithMockThoth(t) ctx := thothmock.WithMockThoth(t)
if _, err := LoadPoliciesOrDefault(ctx, filepath.Join("policy", "config", "testdata", "good", st.Name()), anubis.DefaultDifficulty); err != nil { if _, err := LoadPoliciesOrDefault(ctx, filepath.Join("config", "testdata", "good", st.Name()), anubis.DefaultDifficulty); err != nil {
t.Fatal(err) t.Fatal(err)
} }
}) })
t.Run("without-thoth", func(t *testing.T) { t.Run("without-thoth", func(t *testing.T) {
if _, err := LoadPoliciesOrDefault(t.Context(), filepath.Join("policy", "config", "testdata", "good", st.Name()), anubis.DefaultDifficulty); err != nil { if _, err := LoadPoliciesOrDefault(t.Context(), filepath.Join("config", "testdata", "good", st.Name()), anubis.DefaultDifficulty); err != nil {
t.Fatal(err) t.Fatal(err)
} }
}) })
+1 -1
View File
@@ -4,8 +4,8 @@ import (
"fmt" "fmt"
"github.com/TecharoHQ/anubis/internal" "github.com/TecharoHQ/anubis/internal"
"github.com/TecharoHQ/anubis/lib/config"
"github.com/TecharoHQ/anubis/lib/policy/checker" "github.com/TecharoHQ/anubis/lib/policy/checker"
"github.com/TecharoHQ/anubis/lib/policy/config"
) )
type Bot struct { type Bot struct {
+1 -1
View File
@@ -5,7 +5,7 @@ import (
"net/http" "net/http"
"github.com/TecharoHQ/anubis/internal" "github.com/TecharoHQ/anubis/internal"
"github.com/TecharoHQ/anubis/lib/policy/config" "github.com/TecharoHQ/anubis/lib/config"
"github.com/TecharoHQ/anubis/lib/policy/expressions" "github.com/TecharoHQ/anubis/lib/policy/expressions"
"github.com/google/cel-go/cel" "github.com/google/cel-go/cel"
"github.com/google/cel-go/common/types" "github.com/google/cel-go/common/types"
+1 -1
View File
@@ -3,7 +3,7 @@ package policy
import ( import (
"log/slog" "log/slog"
"github.com/TecharoHQ/anubis/lib/policy/config" "github.com/TecharoHQ/anubis/lib/config"
) )
type CheckResult struct { type CheckResult struct {
+1 -1
View File
@@ -8,8 +8,8 @@ import (
"log/slog" "log/slog"
"sync/atomic" "sync/atomic"
"github.com/TecharoHQ/anubis/lib/config"
"github.com/TecharoHQ/anubis/lib/policy/checker" "github.com/TecharoHQ/anubis/lib/policy/checker"
"github.com/TecharoHQ/anubis/lib/policy/config"
"github.com/TecharoHQ/anubis/lib/store" "github.com/TecharoHQ/anubis/lib/store"
"github.com/TecharoHQ/anubis/lib/thoth" "github.com/TecharoHQ/anubis/lib/thoth"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
+5 -5
View File
@@ -26,7 +26,7 @@ func TestDefaultPolicyMustParse(t *testing.T) {
func TestGoodConfigs(t *testing.T) { func TestGoodConfigs(t *testing.T) {
finfos, err := os.ReadDir("config/testdata/good") finfos, err := os.ReadDir("../config/testdata/good")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@@ -35,7 +35,7 @@ func TestGoodConfigs(t *testing.T) {
st := st st := st
t.Run(st.Name(), func(t *testing.T) { t.Run(st.Name(), func(t *testing.T) {
t.Run("with-thoth", func(t *testing.T) { t.Run("with-thoth", func(t *testing.T) {
fin, err := os.Open(filepath.Join("config", "testdata", "good", st.Name())) fin, err := os.Open(filepath.Join("..", "config", "testdata", "good", st.Name()))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@@ -48,7 +48,7 @@ func TestGoodConfigs(t *testing.T) {
}) })
t.Run("without-thoth", func(t *testing.T) { t.Run("without-thoth", func(t *testing.T) {
fin, err := os.Open(filepath.Join("config", "testdata", "good", st.Name())) fin, err := os.Open(filepath.Join("..", "config", "testdata", "good", st.Name()))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@@ -65,7 +65,7 @@ func TestGoodConfigs(t *testing.T) {
func TestBadConfigs(t *testing.T) { func TestBadConfigs(t *testing.T) {
ctx := thothmock.WithMockThoth(t) ctx := thothmock.WithMockThoth(t)
finfos, err := os.ReadDir("config/testdata/bad") finfos, err := os.ReadDir("../config/testdata/bad")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@@ -73,7 +73,7 @@ func TestBadConfigs(t *testing.T) {
for _, st := range finfos { for _, st := range finfos {
st := st st := st
t.Run(st.Name(), func(t *testing.T) { t.Run(st.Name(), func(t *testing.T) {
fin, err := os.Open(filepath.Join("config", "testdata", "bad", st.Name())) fin, err := os.Open(filepath.Join("..", "config", "testdata", "bad", st.Name()))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
+1 -1
View File
@@ -1,7 +1,7 @@
package policy package policy
import ( import (
"github.com/TecharoHQ/anubis/lib/policy/config" "github.com/TecharoHQ/anubis/lib/config"
"github.com/TecharoHQ/anubis/lib/policy/expressions" "github.com/TecharoHQ/anubis/lib/policy/expressions"
"github.com/google/cel-go/cel" "github.com/google/cel-go/cel"
) )
+1 -1
View File
@@ -4,8 +4,8 @@ import (
"github.com/a-h/templ" "github.com/a-h/templ"
"github.com/TecharoHQ/anubis/lib/challenge" "github.com/TecharoHQ/anubis/lib/challenge"
"github.com/TecharoHQ/anubis/lib/config"
"github.com/TecharoHQ/anubis/lib/localization" "github.com/TecharoHQ/anubis/lib/localization"
"github.com/TecharoHQ/anubis/lib/policy/config"
) )
func Base(title string, body templ.Component, impressum *config.Impressum, localizer *localization.SimpleLocalizer) templ.Component { func Base(title string, body templ.Component, impressum *config.Impressum, localizer *localization.SimpleLocalizer) templ.Component {
+1 -1
View File
@@ -3,8 +3,8 @@ package web
import ( import (
"fmt" "fmt"
"github.com/TecharoHQ/anubis" "github.com/TecharoHQ/anubis"
"github.com/TecharoHQ/anubis/lib/config"
"github.com/TecharoHQ/anubis/lib/localization" "github.com/TecharoHQ/anubis/lib/localization"
"github.com/TecharoHQ/anubis/lib/policy/config"
"github.com/TecharoHQ/anubis/xess" "github.com/TecharoHQ/anubis/xess"
) )
+1 -1
View File
@@ -11,8 +11,8 @@ import templruntime "github.com/a-h/templ/runtime"
import ( import (
"fmt" "fmt"
"github.com/TecharoHQ/anubis" "github.com/TecharoHQ/anubis"
"github.com/TecharoHQ/anubis/lib/config"
"github.com/TecharoHQ/anubis/lib/localization" "github.com/TecharoHQ/anubis/lib/localization"
"github.com/TecharoHQ/anubis/lib/policy/config"
"github.com/TecharoHQ/anubis/xess" "github.com/TecharoHQ/anubis/xess"
) )