Skip to content

Commit 152b941

Browse files
Update Skipper example (#1)
Update example to demonstrate how to reuse [Anubis](https://github.com/TecharoHQ/anubis) botPolicies.json
1 parent 45a5dc7 commit 152b941

File tree

8 files changed

+824
-2
lines changed

8 files changed

+824
-2
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,8 @@ go run github.com/zalando/skipper/cmd/skipper@latest -config-file=./doc/skipper.
9090

9191
and navigate to http://skipper.localtest.me:9090/
9292

93-
See [documentation](https://opensource.zalando.com/skipper/reference/filters/#oauthoidcallclaims) for configuration details.
93+
> [!NOTE]
94+
> This example uses routes generated from [Anubis](https://github.com/TecharoHQ/anubis) `botPolicies.json`
95+
> via [anubis2eskip](./cmd/anubis2eskip/main.go) tool.
96+
97+
See [Skipper documentation](https://opensource.zalando.com/skipper/reference/filters/#oauthoidcallclaims) for configuration details.

cmd/anubis2eskip/go.mod

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
module github.com/AlexanderYastrebov/bot-idp/cmd/anubis2eskip
2+
3+
go 1.24.0
4+
5+
require github.com/zalando/skipper v0.22.12
6+
7+
require (
8+
github.com/beorn7/perks v1.0.1 // indirect
9+
github.com/cenkalti/backoff v2.2.1+incompatible // indirect
10+
github.com/cespare/xxhash/v2 v2.3.0 // indirect
11+
github.com/dchest/siphash v1.2.3 // indirect
12+
github.com/dgryski/go-jump v0.0.0-20211018200510-ba001c3ffce0 // indirect
13+
github.com/dgryski/go-mpchash v0.0.0-20200819201138-7382f34c4cd1 // indirect
14+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
15+
github.com/klauspost/compress v1.17.11 // indirect
16+
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
17+
github.com/oklog/ulid v1.3.1 // indirect
18+
github.com/opentracing/opentracing-go v1.2.0 // indirect
19+
github.com/prometheus/client_golang v1.21.1 // indirect
20+
github.com/prometheus/client_model v0.6.1 // indirect
21+
github.com/prometheus/common v0.62.0 // indirect
22+
github.com/prometheus/procfs v0.15.1 // indirect
23+
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
24+
github.com/redis/go-redis/v9 v9.7.3 // indirect
25+
github.com/sirupsen/logrus v1.9.3 // indirect
26+
go4.org/netipx v0.0.0-20220925034521-797b0c90d8ab // indirect
27+
golang.org/x/crypto v0.36.0 // indirect
28+
golang.org/x/sys v0.31.0 // indirect
29+
google.golang.org/protobuf v1.36.5 // indirect
30+
)

cmd/anubis2eskip/go.sum

Lines changed: 233 additions & 0 deletions
Large diffs are not rendered by default.

cmd/anubis2eskip/main.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"os"
7+
"strings"
8+
9+
"github.com/zalando/skipper/eskip"
10+
)
11+
12+
type config struct {
13+
backend string
14+
policy string
15+
denyFilters string
16+
}
17+
18+
func main() {
19+
config := &config{
20+
backend: withDefault(os.Getenv("BACKEND"), "https://example.com"),
21+
policy: withDefault(os.Getenv("POLICY"), "./testdata/botPolicies.json"),
22+
denyFilters: withDefault(os.Getenv("DENY_FILTERS"), `status(403) -> inlineContent("Forbidden")`),
23+
}
24+
25+
var policies struct {
26+
Bots []struct {
27+
Name string
28+
UserAgent string `json:"user_agent_regex"`
29+
Action string
30+
RemoteAddresses []any `json:"remote_addresses"`
31+
PathRegex string `json:"path_regex"`
32+
}
33+
}
34+
err := json.Unmarshal(must(os.ReadFile(config.policy)), &policies)
35+
if err != nil {
36+
panic(err)
37+
}
38+
39+
denyFilters := eskip.MustParseFilters(config.denyFilters)
40+
41+
var routes []*eskip.Route
42+
for _, bot := range policies.Bots {
43+
if bot.Action == "CHALLENGE" {
44+
// define challenge routes in skipper config
45+
continue
46+
}
47+
48+
route := &eskip.Route{
49+
Id: strings.ReplaceAll(fmt.Sprintf("%s_%s", bot.Name, bot.Action), "-", "_"),
50+
}
51+
52+
if len(bot.RemoteAddresses) > 0 {
53+
route.Predicates = append(route.Predicates, &eskip.Predicate{
54+
Name: "SourceFromLast", Args: bot.RemoteAddresses,
55+
})
56+
}
57+
58+
if bot.UserAgent != "" {
59+
route.Predicates = append(route.Predicates, &eskip.Predicate{
60+
Name: "HeaderRegexp", Args: []any{"User-Agent", bot.UserAgent},
61+
})
62+
}
63+
64+
if bot.PathRegex != "" {
65+
route.Predicates = append(route.Predicates, &eskip.Predicate{
66+
Name: "PathRegexp", Args: []any{bot.PathRegex},
67+
})
68+
}
69+
70+
switch bot.Action {
71+
case "ALLOW":
72+
route.Backend = config.backend
73+
case "DENY":
74+
route.Filters = denyFilters
75+
route.BackendType = eskip.ShuntBackend
76+
default:
77+
panic(fmt.Sprintf("unsupported action type: %q", bot.Action))
78+
}
79+
routes = append(routes, route)
80+
}
81+
fmt.Println("// This file is generated from [Anubis](https://github.com/TecharoHQ/anubis) botPolicies.json")
82+
fmt.Println("//")
83+
fmt.Printf("// (cd ./cmd/anubis2eskip/ && BACKEND=%q POLICY=%q go run . > doc/botPolicies.eskip)\n", config.backend, config.policy)
84+
fmt.Println("//")
85+
fmt.Println(eskip.Print(eskip.PrettyPrintInfo{Pretty: true}, routes...))
86+
}
87+
88+
func must[T any](v T, err error) T {
89+
if err != nil {
90+
panic(err)
91+
}
92+
return v
93+
}
94+
95+
func withDefault[T comparable](val, def T) T {
96+
var zero T
97+
if val == zero {
98+
return def
99+
}
100+
return val
101+
}

0 commit comments

Comments
 (0)