/
entry.go
154 lines (132 loc) · 3.32 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
package file
import (
"encoding/xml"
"github.com/PaloAltoNetworks/pango/util"
"github.com/PaloAltoNetworks/pango/version"
)
// Entry is a normalized, version independent representation of a
// file blocking security profile.
//
// PAN-OS 8.0+.
type Entry struct {
Name string
Description string
Rules []Rule
}
type Rule struct {
Name string
Applications []string // ordered
FileTypes []string // ordered
Direction string
Action string
}
// 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.Description = s.Description
if s.Rules == nil {
o.Rules = nil
} else {
o.Rules = make([]Rule, 0, len(s.Rules))
for _, x := range s.Rules {
item := Rule{
Name: x.Name,
Direction: x.Direction,
Action: x.Action,
}
if len(x.Applications) > 0 {
item.Applications = make([]string, len(x.Applications))
copy(item.Applications, x.Applications)
}
if len(x.FileTypes) > 0 {
item.FileTypes = make([]string, len(x.FileTypes))
copy(item.FileTypes, x.FileTypes)
}
o.Rules = append(o.Rules, item)
}
}
}
/** Structs / functions for this namespace. **/
func (o Entry) Specify(v version.Number) (string, interface{}) {
_, fn := versioning(v)
return o.Name, fn(o)
}
type normalizer interface {
Normalize() []Entry
Names() []string
}
type container_v1 struct {
Answer []entry_v1 `xml:"entry"`
}
func (o *container_v1) Names() []string {
ans := make([]string, 0, len(o.Answer))
for i := range o.Answer {
ans = append(ans, o.Answer[i].Name)
}
return ans
}
func (o *container_v1) Normalize() []Entry {
arr := make([]Entry, 0, len(o.Answer))
for i := range o.Answer {
arr = append(arr, o.Answer[i].normalize())
}
return arr
}
func (o *entry_v1) normalize() Entry {
ans := Entry{
Name: o.Name,
Description: o.Description,
}
if o.Rules != nil {
rules := make([]Rule, 0, len(o.Rules.Entries))
for _, v := range o.Rules.Entries {
r := Rule{
Name: v.Name,
Applications: util.MemToStr(v.Applications),
FileTypes: util.MemToStr(v.FileTypes),
Direction: v.Direction,
Action: v.Action,
}
rules = append(rules, r)
}
ans.Rules = rules
}
return ans
}
type entry_v1 struct {
XMLName xml.Name `xml:"entry"`
Name string `xml:"name,attr"`
Description string `xml:"description,omitempty"`
Rules *rules_v1 `xml:"rules"`
}
type rules_v1 struct {
Entries []rule_v1 `xml:"entry"`
}
type rule_v1 struct {
Name string `xml:"name,attr"`
Applications *util.MemberType `xml:"application"`
FileTypes *util.MemberType `xml:"file-type"`
Direction string `xml:"direction,omitempty"`
Action string `xml:"action"`
}
func specify_v1(e Entry) interface{} {
ans := entry_v1{
Name: e.Name,
Description: e.Description,
}
if len(e.Rules) > 0 {
rules := make([]rule_v1, 0, len(e.Rules))
for _, er := range e.Rules {
r := rule_v1{
Name: er.Name,
Applications: util.StrToMem(er.Applications),
FileTypes: util.StrToMem(er.FileTypes),
Direction: er.Direction,
Action: er.Action,
}
rules = append(rules, r)
}
ans.Rules = &rules_v1{Entries: rules}
}
return ans
}