-
Notifications
You must be signed in to change notification settings - Fork 43
/
entry.go
303 lines (266 loc) · 8.18 KB
/
entry.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package edl
import (
"encoding/xml"
"github.com/PaloAltoNetworks/pango/util"
)
// Constants for Entry.Type field. Only TypeIp is valid for PAN-OS 7.0 and
// earlier. TypePredefined is valid for PAN-OS 8.0 and later.
const (
TypeIp string = "ip"
TypeDomain string = "domain"
TypeUrl string = "url"
TypePredefined string = "predefined"
)
// Constants for the Repeat field. Option "RepeatEveryFiveMinutes" is valid
// for PAN-OS 8.0 and higher.
const (
RepeatEveryFiveMinutes = "every five minutes"
RepeatHourly = "hourly"
RepeatDaily = "daily"
RepeatWeekly = "weekly"
RepeatMonthly = "monthly"
)
// Entry is a normalized, version independent representation of an
// external dynamic list.
type Entry struct {
Name string
Type string
Description string
Source string
CertificateProfile string
Username string
Password string
Repeat string
RepeatAt string
RepeatDayOfWeek string
RepeatDayOfMonth int
Exceptions []string // ordered
}
// Copy copies the information from source Entry `s` to this object. As the
// Name field relates to the XPATH of this object, this field is not copied.
func (o *Entry) Copy(s Entry) {
o.Type = s.Type
o.Description = s.Description
o.Source = s.Source
o.CertificateProfile = s.CertificateProfile
o.Username = s.Username
o.Password = s.Password
o.Repeat = s.Repeat
o.RepeatAt = s.RepeatAt
o.RepeatDayOfWeek = s.RepeatDayOfWeek
o.RepeatDayOfMonth = s.RepeatDayOfMonth
o.Exceptions = s.Exceptions
}
/** Structs / functions for normalization. **/
type normalizer interface {
Normalize() Entry
}
type container_v1 struct {
Answer entry_v1 `xml:"result>entry"`
}
func (o *container_v1) Normalize() Entry {
ans := Entry{
Name: o.Answer.Name,
Type: o.Answer.Type,
Description: o.Answer.Description,
Source: o.Answer.Source,
}
if o.Answer.Repeat.FiveMinute != nil {
ans.Repeat = RepeatEveryFiveMinutes
} else if o.Answer.Repeat.Hourly != nil {
ans.Repeat = RepeatHourly
ans.RepeatAt = o.Answer.Repeat.Hourly.At
} else if o.Answer.Repeat.Daily != nil {
ans.Repeat = RepeatDaily
ans.RepeatAt = o.Answer.Repeat.Daily.At
} else if o.Answer.Repeat.Weekly != nil {
ans.Repeat = RepeatWeekly
ans.RepeatAt = o.Answer.Repeat.Weekly.At
ans.RepeatDayOfWeek = o.Answer.Repeat.Weekly.DayOfWeek
} else if o.Answer.Repeat.Monthly != nil {
ans.Repeat = RepeatMonthly
ans.RepeatAt = o.Answer.Repeat.Monthly.At
ans.RepeatDayOfMonth = o.Answer.Repeat.Monthly.DayOfMonth
}
return ans
}
type container_v2 struct {
Answer entry_v2 `xml:"result>entry"`
}
func (o *container_v2) Normalize() Entry {
ans := Entry{
Name: o.Answer.Name,
}
var sp *typeSpec
if o.Answer.PredefinedIp != nil {
ans.Type = TypePredefined
ans.Description = o.Answer.PredefinedIp.Description
ans.Source = o.Answer.PredefinedIp.Source
ans.Exceptions = util.MemToStr(o.Answer.PredefinedIp.Exceptions)
} else if o.Answer.Ip != nil {
ans.Type = TypeIp
sp = o.Answer.Ip
} else if o.Answer.Domain != nil {
ans.Type = TypeDomain
sp = o.Answer.Domain
} else if o.Answer.Url != nil {
ans.Type = TypeUrl
sp = o.Answer.Url
}
if sp != nil {
ans.Description = sp.Description
ans.Source = sp.Source
ans.CertificateProfile = sp.CertificateProfile
ans.Exceptions = util.MemToStr(sp.Exceptions)
if sp.Auth != nil {
ans.Username = sp.Auth.Username
ans.Password = sp.Auth.Password
}
if sp.Repeat.FiveMinute != nil {
ans.Repeat = RepeatEveryFiveMinutes
} else if sp.Repeat.Hourly != nil {
ans.Repeat = RepeatHourly
} else if sp.Repeat.Daily != nil {
ans.Repeat = RepeatDaily
ans.RepeatAt = sp.Repeat.Daily.At
} else if sp.Repeat.Weekly != nil {
ans.Repeat = RepeatWeekly
ans.RepeatAt = sp.Repeat.Weekly.At
ans.RepeatDayOfWeek = sp.Repeat.Weekly.DayOfWeek
} else if sp.Repeat.Monthly != nil {
ans.Repeat = RepeatMonthly
ans.RepeatAt = sp.Repeat.Monthly.At
ans.RepeatDayOfMonth = sp.Repeat.Monthly.DayOfMonth
}
}
return ans
}
// Ideally there would be one struct for PAN-OS 6.1 & 7.0 and another for
// PAN-OS 7.1, but since the difference is minimal, I'm using the same struct.
//
// Probably revisit this at a later time..?
type entry_v1 struct {
XMLName xml.Name `xml:"entry"`
Name string `xml:"name,attr"`
Type string `xml:"type"`
Description string `xml:"description,omitempty"`
Source string `xml:"url"`
Repeat rep_v1 `xml:"recurring"`
}
type rep_v1 struct {
FiveMinute *string `xml:"five-minute"`
Hourly *timeAt `xml:"hourly"`
Daily *timeAt `xml:"daily"`
Weekly *timeWeek `xml:"weekly"`
Monthly *timeMonth `xml:"monthly"`
}
type timeAt struct {
At string `xml:"at,omitempty"`
}
type timeWeek struct {
At string `xml:"at"`
DayOfWeek string `xml:"day-of-week"`
}
type timeMonth struct {
At string `xml:"at"`
DayOfMonth int `xml:"day-of-month"`
}
type entry_v2 struct {
XMLName xml.Name `xml:"entry"`
Name string `xml:"name,attr"`
PredefinedIp *typePredefined `xml:"type>predefined-ip"`
Ip *typeSpec `xml:"type>ip"`
Domain *typeSpec `xml:"type>domain"`
Url *typeSpec `xml:"type>url"`
}
type typePredefined struct {
Description string `xml:"description"`
Source string `xml:"url"`
Exceptions *util.MemberType `xml:"exception-list"`
}
type typeSpec struct {
Description string `xml:"description,omitempty"`
Source string `xml:"url"`
CertificateProfile string `xml:"certificate-profile,omitempty"`
Auth *authType `xml:"auth"`
Repeat rep_v2 `xml:"recurring"`
Exceptions *util.MemberType `xml:"exception-list"`
}
type authType struct {
Username string `xml:"username"`
Password string `xml:"password"`
}
type rep_v2 struct {
FiveMinute *string `xml:"five-minute"`
Hourly *string `xml:"hourly"`
Daily *timeAt `xml:"daily"`
Weekly *timeWeek `xml:"weekly"`
Monthly *timeMonth `xml:"monthly"`
}
func specify_v1(e Entry) interface{} {
ans := entry_v1{
Name: e.Name,
Type: e.Type,
Description: e.Description,
Source: e.Source,
}
switch e.Repeat {
case RepeatEveryFiveMinutes:
sp := ""
ans.Repeat.FiveMinute = &sp
case RepeatHourly:
ans.Repeat.Hourly = &timeAt{e.RepeatAt}
case RepeatDaily:
ans.Repeat.Daily = &timeAt{e.RepeatAt}
case RepeatWeekly:
ans.Repeat.Weekly = &timeWeek{e.RepeatAt, e.RepeatDayOfWeek}
case RepeatMonthly:
ans.Repeat.Monthly = &timeMonth{e.RepeatAt, e.RepeatDayOfMonth}
}
return ans
}
func specify_v2(e Entry) interface{} {
ans := entry_v2{
Name: e.Name,
}
switch e.Type {
case TypePredefined:
ans.PredefinedIp = &typePredefined{
Description: e.Description,
Source: e.Source,
Exceptions: util.StrToMem(e.Exceptions),
}
default:
spec := &typeSpec{
Description: e.Description,
Source: e.Source,
CertificateProfile: e.CertificateProfile,
Exceptions: util.StrToMem(e.Exceptions),
}
if e.Username != "" || e.Password != "" {
spec.Auth = &authType{e.Username, e.Password}
}
sp := ""
switch e.Repeat {
case RepeatEveryFiveMinutes:
spec.Repeat.FiveMinute = &sp
case RepeatHourly:
spec.Repeat.Hourly = &sp
case RepeatDaily:
spec.Repeat.Daily = &timeAt{e.RepeatAt}
case RepeatWeekly:
spec.Repeat.Weekly = &timeWeek{e.RepeatAt, e.RepeatDayOfWeek}
case RepeatMonthly:
spec.Repeat.Monthly = &timeMonth{e.RepeatAt, e.RepeatDayOfMonth}
}
switch e.Type {
case TypeIp:
ans.Ip = spec
case TypeDomain:
ans.Domain = spec
case TypeUrl:
ans.Url = spec
}
}
return ans
}