|
| 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