-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipenv.go
171 lines (147 loc) · 4.53 KB
/
ipenv.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Copyright (c) 2023 The Go-Enjin Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ipenv
import (
"net"
"net/http"
"github.com/iancoleman/strcase"
"github.com/urfave/cli/v2"
"github.com/go-enjin/be/pkg/feature"
site_environ "github.com/go-enjin/be/pkg/feature/site-environ"
"github.com/go-enjin/be/pkg/log"
"github.com/go-enjin/be/pkg/maps"
beNet "github.com/go-enjin/be/pkg/net"
"github.com/go-enjin/be/pkg/userbase"
)
var (
_ Feature = (*CFeature)(nil)
_ MakeFeature = (*CFeature)(nil)
)
const Tag feature.Tag = "user-base-ipenv"
type Feature interface {
feature.Feature
feature.RequestRewriter
}
type MakeFeature interface {
AddAddresses(group string, cidrs ...string) MakeFeature
AddPermissions(group string, actions ...feature.Action) MakeFeature
Make() Feature
}
type CFeature struct {
feature.CFeature
env *site_environ.CSiteEnviron[MakeFeature]
groupings groupings
}
func New() MakeFeature {
return NewTagged(Tag)
}
func NewTagged(tag feature.Tag) MakeFeature {
f := new(CFeature)
f.Init(f)
f.PackageTag = Tag
f.FeatureTag = tag
f.CFeature.Construct(f)
return f
}
func (f *CFeature) UsageNotes() (notes []string) {
//tag := f.Tag().String()
//notes = []string{
// "this feature looks for dynamic environment variable keys",
// "groups are defined by their permissions and one or more IP addresses or CIDR ranges",
// "in the following examples, \"PRIVATE_USERS\" is the group \"private-users\"",
// "add networks: " + globals.MakeFlagEnvKey(tag, "NETWORKS_PRIVATE_USERS") + "='<space separated CIDR>'",
// "add addresses: " + globals.MakeFlagEnvKey(tag, "ADDRESSES_PRIVATE_USERS") + "='<space separated IP>'",
// "add permissions: " + globals.MakeFlagEnvKey(tag, "PERMISSIONS_PRIVATE_USERS") + "='<space separated actions>'",
//}
notes = f.env.SiteEnvironUsageNotes()
return
}
func (f *CFeature) Init(this interface{}) {
f.CFeature.Init(this)
f.env = site_environ.New[MakeFeature](this,
"networks", "space separated CIDR ranges",
"addresses", "space separated IP addresses",
"permissions", "space separated actions",
)
f.groupings = make(groupings)
return
}
func (f *CFeature) AddNetworks(group string, cidrs ...string) MakeFeature {
if len(cidrs) > 0 {
group = strcase.ToKebab(group)
for _, cidr := range cidrs {
if _, network, err := net.ParseCIDR(cidr); err != nil {
log.FatalDF(1, "error parsing %q CIDR: %v", err)
} else {
f.groupings.addNetwork(group, network)
}
}
}
return f
}
func (f *CFeature) AddAddresses(group string, ips ...string) MakeFeature {
if len(ips) > 0 {
group = strcase.ToKebab(group)
for _, ip := range ips {
if parsed := net.ParseIP(ip); parsed == nil {
log.FatalDF(1, "%q is not an IP address", ip)
} else {
f.groupings.addAddress(group, parsed)
}
}
}
return f
}
func (f *CFeature) AddPermissions(group string, actions ...feature.Action) MakeFeature {
if len(actions) > 0 {
group = strcase.ToKebab(group)
f.groupings.addActions(group, actions...)
}
return f
}
func (f *CFeature) Make() (feat Feature) {
return f
}
func (f *CFeature) Build(b feature.Buildable) (err error) {
if err = f.CFeature.Build(b); err != nil {
return
}
return
}
func (f *CFeature) Startup(ctx *cli.Context) (err error) {
if err = f.CFeature.Startup(ctx); err != nil {
return
} else if err = f.env.StartupSiteEnviron(); err != nil {
return
}
if err = f.loadEnvironment(); err != nil {
return
}
log.InfoF("%v feature has %d IP permission groupings: %v", f.Tag(), len(f.groupings), f.groupings)
return
}
func (f *CFeature) RewriteRequest(w http.ResponseWriter, r *http.Request) (modified *http.Request) {
if address, err := beNet.ParseIpFromRequest(r); err == nil {
modified = r
for _, name := range maps.SortedKeys(f.groupings) {
if group := f.groupings[name]; group != nil {
if group.matches(address) {
log.DebugF("[%v] current address (%v) matches %q group; adding permissions: %v", f.Tag(), address, name, group.actions)
modified = userbase.AppendCurrentPermissions(modified, group.actions...)
}
}
}
}
return
}