-
Notifications
You must be signed in to change notification settings - Fork 2
/
filter.go
49 lines (36 loc) · 951 Bytes
/
filter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package remote_ip
import (
"errors"
"fmt"
"net"
"strings"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
"github.com/dpb587/ssoca/auth/authz/filter"
"github.com/dpb587/ssoca/config"
)
type Filter struct{}
func (f Filter) Create(cfg interface{}) (filter.Requirement, error) {
requirement := Requirement{}
err := config.RemarshalYAML(cfg, &requirement)
if err != nil {
return nil, bosherr.WrapError(err, "Loading config")
}
if requirement.WithinRaw == "" {
return nil, errors.New("Property must be configured: within")
}
netmask := requirement.WithinRaw
if !strings.Contains(netmask, "/") {
ip := net.ParseIP(netmask)
if ip.To4() != nil {
netmask = fmt.Sprintf("%s/32", netmask)
} else {
netmask = fmt.Sprintf("%s/128", netmask)
}
}
_, cidr, err := net.ParseCIDR(netmask)
if err != nil {
return nil, bosherr.WrapError(err, "Parsing CIDR")
}
requirement.Within = *cidr
return requirement, nil
}