forked from mistifyio/lochness
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fwgroup.go
211 lines (173 loc) · 4.35 KB
/
fwgroup.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package lochness
import (
"encoding/json"
"net"
"path/filepath"
"github.com/coreos/go-etcd/etcd"
"github.com/pborman/uuid"
)
var (
// FWGroupPath is the path in the config store
FWGroupPath = "lochness/fwgroups/"
)
// XXX: should individual rules be their own keys??
type (
// FWRule represents a single firewall rule
FWRule struct {
Source *net.IPNet `json:"source,omitempty"`
Group string `json:"group"`
PortStart uint `json:"portStart"`
PortEnd uint `json:"portEnd"`
Protocol string `json:"protocol"`
Action string `json:"action"`
}
// FWRules is an alias to a slice of *FWRule
FWRules []*FWRule
// FWGroup represents a group of firewall rules
FWGroup struct {
context *Context
modifiedIndex uint64
ID string `json:"id"`
Metadata map[string]string `json:"metadata"`
Rules FWRules `json:"rules"`
}
// FWGroups is an alias to FWGroup slices
FWGroups []*FWGroup
fwRuleJSON struct {
Source string `json:"source,omitempty"`
Group string `json:"group,omitempty"`
PortStart uint `json:"portStart"`
PortEnd uint `json:"portEnd"`
Protocol string `json:"protocol"`
Action string `json:"action"`
}
fwGroupJSON struct {
ID string `json:"id"`
Metadata map[string]string `json:"metadata"`
Rules []*fwRuleJSON `json:"rules"`
}
)
// MarshalJSON is a helper for marshalling a FWGroup
func (f FWGroup) MarshalJSON() ([]byte, error) {
data := fwGroupJSON{
ID: f.ID,
Metadata: f.Metadata,
Rules: make([]*fwRuleJSON, 0, len(f.Rules)),
}
for _, r := range f.Rules {
rule := fwRuleJSON{
Group: r.Group,
PortStart: r.PortStart,
PortEnd: r.PortEnd,
Protocol: r.Protocol,
Action: r.Action,
}
if r.Source != nil {
rule.Source = r.Source.String()
}
data.Rules = append(data.Rules, &rule)
}
return json.Marshal(data)
}
// UnmarshalJSON is a helper for unmarshalling a FWGroup
func (f *FWGroup) UnmarshalJSON(input []byte) error {
data := fwGroupJSON{}
if err := json.Unmarshal(input, &data); err != nil {
return err
}
f.ID = data.ID
f.Metadata = data.Metadata
f.Rules = make(FWRules, 0, len(data.Rules))
for _, r := range data.Rules {
rule := &FWRule{
Group: r.Group,
PortStart: r.PortStart,
PortEnd: r.PortEnd,
Protocol: r.Protocol,
Action: r.Action,
}
if r.Source != "" {
_, n, err := net.ParseCIDR(r.Source)
if err != nil {
return err
}
rule.Source = n
}
f.Rules = append(f.Rules, rule)
}
return nil
}
// NewFWGroup creates a new, blank FWGroup
func (c *Context) NewFWGroup() *FWGroup {
f := &FWGroup{
context: c,
ID: uuid.New(),
Metadata: make(map[string]string),
}
return f
}
// FWGroup fetches a FWGroup from the config store
func (c *Context) FWGroup(id string) (*FWGroup, error) {
var err error
id, err = canonicalizeUUID(id)
if err != nil {
return nil, err
}
f := &FWGroup{
context: c,
ID: id,
}
err = f.Refresh()
if err != nil {
return nil, err
}
return f, nil
}
// key is a helper to generate the config store key
func (f *FWGroup) key() string {
return filepath.Join(FWGroupPath, f.ID, "metadata")
}
// fromResponse is a helper to unmarshal a FWGroup
func (f *FWGroup) fromResponse(resp *etcd.Response) error {
f.modifiedIndex = resp.Node.ModifiedIndex
return json.Unmarshal([]byte(resp.Node.Value), &f)
}
// Refresh reloads from the data store
func (f *FWGroup) Refresh() error {
resp, err := f.context.etcd.Get(f.key(), false, false)
if err != nil {
return err
}
if resp == nil || resp.Node == nil {
// should this be an error??
return nil
}
return f.fromResponse(resp)
}
// Validate ensures a FWGroup has reasonable data. It currently does nothing.
func (f *FWGroup) Validate() error {
// do validation stuff...
return nil
}
// Save persists a FWGroup. It will call Validate.
func (f *FWGroup) Save() error {
if err := f.Validate(); err != nil {
return err
}
v, err := json.Marshal(f)
if err != nil {
return err
}
// if we changed something, don't clobber
var resp *etcd.Response
if f.modifiedIndex != 0 {
resp, err = f.context.etcd.CompareAndSwap(f.key(), string(v), 0, "", f.modifiedIndex)
} else {
resp, err = f.context.etcd.Create(f.key(), string(v), 0)
}
if err != nil {
return err
}
f.modifiedIndex = resp.EtcdIndex
return nil
}