forked from PaloAltoNetworks/pango
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pano.go
228 lines (187 loc) · 5.52 KB
/
pano.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
package pango
import (
"encoding/xml"
"fmt"
"strings"
"time"
// Various namespace imports.
"github.com/PaloAltoNetworks/pango/dev"
"github.com/PaloAltoNetworks/pango/licen"
"github.com/PaloAltoNetworks/pango/netw"
"github.com/PaloAltoNetworks/pango/objs"
"github.com/PaloAltoNetworks/pango/pnrm"
"github.com/PaloAltoNetworks/pango/poli"
"github.com/PaloAltoNetworks/pango/predefined"
"github.com/PaloAltoNetworks/pango/userid"
)
// Panorama is a panorama specific client, providing version safe functions
// for the PAN-OS Xpath API methods. After creating the object, invoke
// Initialize() to prepare it for use.
//
// It has the following namespaces:
// * Licensing
// * UserId
type Panorama struct {
Client
// Namespaces
Predefined *predefined.Panorama
Device *dev.PanoDev
Licensing *licen.Licen
UserId *userid.UserId
Panorama *pnrm.Pnrm
Objects *objs.PanoObjs
Policies *poli.Panorama
Network *netw.Panorama
}
// Initialize does some initial setup of the Panorama connection, retrieves
// the API key if it was not already present, then performs "show system
// info" to get the PAN-OS version. The full results are saved into the
// client's SystemInfo map.
//
// If not specified, the following is assumed:
// * Protocol: https
// * Port: (unspecified)
// * Timeout: 10
// * Logging: LogAction | LogUid
func (c *Panorama) Initialize() error {
if len(c.rb) == 0 {
var e error
if e = c.initCon(); e != nil {
return e
} else if e = c.initApiKey(); e != nil {
return e
} else if e = c.initSystemInfo(); e != nil {
return e
}
c.initPlugins()
} else {
c.Hostname = "localhost"
c.ApiKey = "password"
}
c.initNamespaces()
return nil
}
// InitializeUsing does Initialize(), but takes in a filename that contains
// fallback authentication credentials if they aren't specified.
//
// The order of preference for auth / connection settings is:
//
// * explicitly set
// * environment variable (set chkenv to true to enable this)
// * json file
func (c *Panorama) InitializeUsing(filename string, chkenv bool) error {
c.CheckEnvironment = chkenv
c.credsFile = filename
return c.Initialize()
}
// CreateVmAuthKey creates a VM auth key to bootstrap a VM-Series firewall.
//
// VM auth keys are only valid for the number of hours specified.
func (c *Panorama) CreateVmAuthKey(hours int) (VmAuthKey, error) {
clock, err := c.Clock()
if err != nil {
c.LogOp("(op) Failed to get/parse system time: %s", err)
}
type ak_req struct {
XMLName xml.Name `xml:"request"`
Duration int `xml:"bootstrap>vm-auth-key>generate>lifetime"`
}
type ak_resp struct {
Msg string `xml:"result"`
}
req := ak_req{Duration: hours}
ans := ak_resp{}
c.LogOp("(op) generating a vm auth code")
if b, err := c.Op(req, "", nil, &ans); err != nil {
return VmAuthKey{}, err
} else if ans.Msg == "" {
return VmAuthKey{}, fmt.Errorf("No msg: %s", b)
} else if !strings.HasPrefix(ans.Msg, "VM auth key ") {
return VmAuthKey{}, fmt.Errorf("Wrong resp prefix: %s", b)
}
tokens := strings.Fields(ans.Msg)
if len(tokens) != 9 {
return VmAuthKey{}, fmt.Errorf("Got %d of 9 fields from: %s", len(tokens), ans.Msg)
}
key := VmAuthKey{
AuthKey: tokens[3],
Expiry: strings.Join(tokens[7:], " "),
}
key.ParseExpires(clock)
return key, nil
}
// GetVmAuthKeys gets the list of VM auth keys.
func (c *Panorama) GetVmAuthKeys() ([]VmAuthKey, error) {
clock, err := c.Clock()
if err != nil {
c.LogOp("(op) Failed to get/parse system time: %s", err)
}
type l_req struct {
XMLName xml.Name `xml:"request"`
Msg string `xml:"bootstrap>vm-auth-key>show"`
}
type l_resp struct {
List []VmAuthKey `xml:"result>bootstrap-vm-auth-keys>entry"`
}
req := l_req{}
ans := l_resp{}
c.LogOp("(op) listing vm auth codes")
if _, err := c.Op(req, "", nil, &ans); err != nil {
return nil, err
}
for i := range ans.List {
ans.List[i].ParseExpires(clock)
}
return ans.List, nil
}
// RemoveVmAuthKey revokes a VM auth key.
func (c *Panorama) RevokeVmAuthKey(key string) error {
type rreq struct {
XMLName xml.Name `xml:"request"`
Key string `xml:"bootstrap>vm-auth-key>revoke>vm-auth-key"`
}
req := rreq{
Key: key,
}
c.LogOp("(op) revoking vm auth code: %s", key)
_, err := c.Op(req, "", nil, nil)
return err
}
/** Public structs **/
// VmAuthKey is a VM auth key paired with when it expires.
//
// The Expiry field is the string returned from PAN-OS, while the Expires
// field is an attempt at parsing the Expiry field.
type VmAuthKey struct {
AuthKey string `xml:"vm-auth-key"`
Expiry string `xml:"expiry-time"`
Expires time.Time
}
// ParseExpires sets Expires from the Expiry field.
//
// Since PAN-OS does not output timezone information with the expirations,
// the current PAN-OS time is retrieved, which does contain timezone
// information. Then in the string parsing for Expires, the location
// information of the system clock is applied.
func (o *VmAuthKey) ParseExpires(clock time.Time) {
format := "2006/01/02 15:04:05"
if t, err := time.ParseInLocation(format, o.Expiry, clock.Location()); err == nil {
o.Expires = t
}
}
/** Private functions **/
func (c *Panorama) initNamespaces() {
c.Predefined = predefined.PanoramaNamespace(c)
c.Device = &dev.PanoDev{}
c.Device.Initialize(c)
c.Licensing = &licen.Licen{}
c.Licensing.Initialize(c)
c.UserId = &userid.UserId{}
c.UserId.Initialize(c)
c.Panorama = &pnrm.Pnrm{}
c.Panorama.Initialize(c)
c.Objects = &objs.PanoObjs{}
c.Objects.Initialize(c)
c.Policies = poli.PanoramaNamespace(c)
c.Network = netw.PanoramaNamespace(c)
}