Skip to content

Commit

Permalink
chore: allow multiple allow subnets
Browse files Browse the repository at this point in the history
  • Loading branch information
ijo42 committed Sep 16, 2024
1 parent 468586a commit 880d4bf
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 17 deletions.
4 changes: 2 additions & 2 deletions .traefik.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ summary: '[Auth] Authorize though ical endpoint'

testData:
HeaderName: Authorization
AllowSubnet: "0.0.0.0/24"

AllowSubnet:
- "0.0.0.0/24"
49 changes: 36 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package icalmiddleware

import (
"container/list"
"context"
"fmt"
"io"
Expand All @@ -12,18 +13,18 @@ import (
)

type Config struct {
ForwardToken bool `json:"forwardToken,omitempty"`
Freshness int64 `json:"freshness,omitempty"`
HeaderName string `json:"headerName,omitempty"`
AllowSubnet string `json:"allowSubnet,omitempty"`
ForwardToken bool `json:"forwardToken,omitempty"`
Freshness int64 `json:"freshness,omitempty"`
HeaderName string `json:"headerName,omitempty"`
AllowSubnet []string `json:"allowSubnet,omitempty"`
}

func CreateConfig() *Config {
return &Config{
HeaderName: "Authorization",
ForwardToken: false,
Freshness: 3600,
AllowSubnet: "0.0.0.0/24",
AllowSubnet: []string{"0.0.0.0/24"},
}
}

Expand All @@ -33,14 +34,27 @@ type ICalMiddleware struct {
forwardToken bool
freshness int64
cache *Cache
allowSubnet netip.Prefix
allowSubnet []netip.Prefix
name string
}

func New(_ context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
network, err := netip.ParsePrefix(config.AllowSubnet)
if err != nil {
return nil, fmt.Errorf("subnet parse error: %v", err)
cidrList := list.New()
for _, cidr := range config.AllowSubnet {
prefix, err := netip.ParsePrefix(cidr)
if err != nil {
fmt.Printf("Subnet parse error %s: %v\n", cidr, err)
} else {
cidrList.PushBack(prefix)
}
}

cidrs := make([]netip.Prefix, cidrList.Len())

i := 0
for e := cidrList.Front(); e != nil; e = e.Next() {
cidrs[i] = e.Value.(netip.Prefix)
i++
}

cache := NewCache(time.Duration(config.Freshness)*time.Second, 8*time.Hour)
Expand All @@ -49,7 +63,7 @@ func New(_ context.Context, next http.Handler, config *Config, name string) (htt
headerName: config.HeaderName,
forwardToken: config.ForwardToken,
freshness: config.Freshness,
allowSubnet: network,
allowSubnet: cidrs,
next: next,
cache: cache,
name: name,
Expand Down Expand Up @@ -123,15 +137,24 @@ func (plugin *ICalMiddleware) containsSubnet(address string) bool {
fmt.Printf("Invalid addr: %v", err)
return false
}
fmt.Printf("%v contains %v %v\n", ip, plugin.allowSubnet, plugin.allowSubnet.Contains(ip))
return plugin.allowSubnet.Contains(ip)

var flag bool
for _, prefix := range plugin.allowSubnet {
flag = prefix.Contains(ip)
if flag {
fmt.Printf("%v contains %v\n", prefix, ip)
break
}
}

return flag
}

// validate validates the request and returns the HTTP status code or an error if the request is not valid. It also sets any headers that should be forwarded to the backend.
func (plugin *ICalMiddleware) validate(request *http.Request) (int, error) {
if !plugin.containsSubnet(ReadUserIP(request)) {
token := plugin.extractTokenFromHeader(request)
if token == "" {
if len(token) != 16 {
// No token provided
fmt.Println("No token provided")
return http.StatusUnauthorized, fmt.Errorf("no token provided")
Expand Down
5 changes: 3 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ experimental:
plugins:
icalmiddleware:
moduleName: github.com/psumaps/icalmiddleware
version: v0.0.1
version: v0.0.5
```
Here is an example of a file provider dynamic configuration (given here in YAML), where the interesting part is the `http.middlewares` section:
Expand Down Expand Up @@ -44,7 +44,8 @@ http:
plugin:
icalmiddleware:
HeaderName: "Authorization"
AllowSubnet: "0.0.0.0/24"
AllowSubnet:
- "0.0.0.0/24"
Freshness: 3600
ForwardToken: false
```
Expand Down

0 comments on commit 880d4bf

Please sign in to comment.