forked from PaloAltoNetworks/prisma-cloud-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
370 lines (320 loc) · 9.28 KB
/
client.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package prismacloud
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os/exec"
"strings"
"time"
)
// Client is a client connection to Prisma Cloud.
type Client struct {
// Properties.
Url string `json:"url"`
Username string `json:"username"`
Password string `json:"password"`
CustomerName string `json:"customer_name"`
Protocol string `json:"protocol"`
Port int `json:"port"`
Timeout int `json:"timeout"`
SkipSslCertVerification bool `json:"skip_ssl_cert_verification"`
Logging map[string]bool `json:"logging"`
DisableReconnect bool `json:"disable_reconnect"`
MaxRetries int `json:"max_retries"`
RetryMaxDelay int `json:"retry_max_delay"`
Retries int `json:"retries"`
// Advanced user config.
Transport *http.Transport `json:"-"`
// Set at runtime.
JsonWebToken string `json:"json_web_token"`
con *http.Client
// Variables for testing.
pcAuthFileContent []byte
pcResponses []*http.Response
pcResponseIndex int
}
/*
Initialize prepares the client connection and attempts a login.
This function can be passed a credentials file to read in settings
that will act as defaults if the given variables are currently unset.
*/
func (c *Client) Initialize(filename string) error {
c2 := Client{}
if filename != "" {
var (
b []byte
err error
)
if len(c.pcResponses) == 0 {
b, err = ioutil.ReadFile(filename)
} else {
b, err = c.pcAuthFileContent, nil
}
if err != nil {
return err
}
if err = json.Unmarshal(b, &c2); err != nil {
return err
}
}
if len(c.Logging) == 0 {
if len(c2.Logging) > 0 {
c.Logging = make(map[string]bool)
for key, val := range c2.Logging {
c.Logging[key] = val
}
} else {
c.Logging = map[string]bool{LogAction: true}
}
}
var tout time.Duration
if c.Timeout == 0 {
if c2.Timeout > 0 {
c.Timeout = c2.Timeout
} else {
c.Timeout = 180
}
}
if c.Timeout < 0 {
return fmt.Errorf("Invalid timeout")
}
tout = time.Duration(time.Duration(c.Timeout) * time.Second)
if c.Port == 0 {
if c2.Port != 0 {
c.Port = c2.Port
}
}
if c.Port > 65535 || c.Port < 0 {
return fmt.Errorf("Invalid port number")
}
if c.Protocol == "" {
if c2.Protocol != "" {
c.Protocol = c2.Protocol
} else {
c.Protocol = "https"
}
}
if c.Protocol != "http" && c.Protocol != "https" {
return fmt.Errorf("Invalid protocol")
}
if c.Url == "" && c2.Url != "" {
c.Url = c2.Url
}
if strings.HasPrefix(c.Url, "http://") || strings.HasPrefix(c.Url, "https://") {
return fmt.Errorf("Specify protocol using the Protocol param, not as the URL")
}
c.Url = strings.TrimRight(c.Url, "/")
if c.Url == "" {
return fmt.Errorf("Prisma Cloud URL is not set")
}
if c.Username == "" && c2.Username != "" {
c.Username = c2.Username
}
if c.Password == "" && c2.Password != "" {
c.Password = c2.Password
}
if c.CustomerName == "" && c2.CustomerName != "" {
c.CustomerName = c2.CustomerName
}
if c.Transport == nil {
c.Transport = &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: c.SkipSslCertVerification,
},
Proxy: http.ProxyFromEnvironment,
}
}
c.con = &http.Client{
Transport: c.Transport,
Timeout: tout,
}
if c.JsonWebToken == "" && c2.JsonWebToken != "" {
c.JsonWebToken = c2.JsonWebToken
return nil
}
return c.Authenticate()
}
// Authenticate retrieves and saves a JSON web token from Prisma Cloud.
func (c *Client) Authenticate() error {
var err error
type initial struct {
Username string `json:"username"`
Password string `json:"password"`
CustomerName string `json:"customerName,omitempty"`
}
ans := AuthResponse{}
/*
Ideally we would do a JSON web token refresh if we got one
previously, but just straight up performing a re-authentication
always works. So just do a full login again if we have the
username and password, falling back to a token refresh.
*/
if c.Username != "" && c.Password != "" {
c.Log(LogAction, "(auth) retrieving jwt")
req := initial{c.Username, c.Password, c.CustomerName}
_, err = c.communicate("POST", []string{"login"}, nil, &req, &ans, false)
} else if c.JsonWebToken != "" {
c.Log(LogAction, "(auth) refreshing jwt")
_, err = c.communicate("GET", []string{"auth_token", "extend"}, nil, nil, &ans, false)
} else {
return fmt.Errorf("no authentication params given")
}
if err != nil {
return err
}
c.JsonWebToken = ans.Token
return nil
}
/*
Communicate handles basic communication with Prisma Cloud.
If a non-nil interface is given as the "ans" variable, then this function
will unmarshal the returned JSON into it, and you can safely discard the
slice of bytes returned.
*/
func (c *Client) Communicate(method string, suffix []string, query, data interface{}, ans interface{}) ([]byte, error) {
return c.communicate(method, suffix, query, data, ans, true)
}
// Log logs a message to the user if the appropriate style is enabled.
func (c *Client) Log(flag, msg string, i ...interface{}) {
if c.Logging[flag] {
log.Printf(msg, i...)
}
}
func (c *Client) do(req *http.Request) (*http.Response, error) {
if len(c.pcResponses) == 0 {
return c.con.Do(req)
}
resp := c.pcResponses[c.pcResponseIndex]
c.pcResponseIndex++
return resp, nil
}
// logSendReceive outputs raw data sent and received, but tries to
// remove sensitive information from being printed.
func (c *Client) logSendReceive(logFlag string, code int, b []byte) {
var desc string
switch logFlag {
case LogSend:
desc = "sending:\n"
case LogReceive:
desc = fmt.Sprintf("received (%d):", code)
default:
return
}
if !c.Logging[logFlag] {
return
} else if len(b) == 0 {
log.Printf("%s", desc)
return
}
var ti interface{}
if err := json.Unmarshal(b, &ti); err != nil {
log.Printf("failed to unmarshal %s: %s", logFlag, err)
log.Printf("%s\n%s", desc, scrubSensitiveData(b))
return
}
b2, _ := json.MarshalIndent(ti, "", " ")
log.Printf("%s\n%s", desc, scrubSensitiveData(b2))
}
func (c *Client) communicate(method string, suffix []string, query, data interface{}, ans interface{}, allowRetry bool) ([]byte, error) {
var err error
var buf bytes.Buffer
if data != nil {
b, err := json.Marshal(data)
if err != nil {
return nil, err
}
buf = *bytes.NewBuffer(b)
c.logSendReceive(LogSend, 0, b)
}
var path strings.Builder
path.Grow(30)
fmt.Fprintf(&path, "%s://%s", c.Protocol, c.Url)
if c.Port != 0 {
fmt.Fprintf(&path, ":%d", c.Port)
}
for _, v := range suffix {
path.WriteString("/")
path.WriteString(v)
}
if query != nil {
qv := query.(url.Values)
path.WriteString("?")
path.WriteString(qv.Encode())
}
c.Log(LogPath, "path: %s", path.String())
req, err := http.NewRequest(method, path.String(), &buf)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
newUUID, err := exec.Command("uuidgen").Output()
uuid := strings.TrimSpace(string(newUUID))
uuidPC := "PrismaCloud-terraform-" + uuid
req.Header.Set("terraform-request-identifier", uuidPC)
if c.JsonWebToken != "" {
req.Header.Set("x-redlock-auth", c.JsonWebToken)
}
resp, err := c.do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
c.logSendReceive(LogReceive, resp.StatusCode, []byte(body))
requestId := "X-Redlock-Request-Id"
traceId := "Trace-Id"
log.Printf("X-Redlock-Request-Id : %v Trace-Id : %v for path: %s terraform-request-identifier : %v", resp.Header[requestId], resp.Header[traceId], path.String(), uuidPC)
switch resp.StatusCode {
case http.StatusOK, http.StatusNoContent, http.StatusCreated:
// Alert rule deletion returns StatusNoContent
case http.StatusUnauthorized:
if !c.DisableReconnect && allowRetry {
if err = c.Authenticate(); err == nil {
return c.communicate(method, suffix, query, data, ans, false)
}
}
return body, InvalidCredentialsError
case http.StatusTooManyRequests:
delay := 1 << c.Retries
if delay <= c.RetryMaxDelay && delay > 0 && c.MaxRetries > 0 {
time.Sleep(time.Duration(delay) * time.Second)
c.MaxRetries = c.MaxRetries - 1
c.Retries = c.Retries + 1
return c.communicate(method, suffix, query, data, ans, false)
} else if delay > c.RetryMaxDelay || c.MaxRetries <= 0 {
return nil, fmt.Errorf("max_retries or retry_max_delay insufficient")
}
default:
errLocation := "X-Redlock-Status"
if _, ok := resp.Header[errLocation]; !ok {
return body, fmt.Errorf("%d error without the %q header - returned HTML:\n%s", resp.StatusCode, errLocation, body)
}
pcel := PrismaCloudErrorList{
Method: method,
StatusCode: resp.StatusCode,
Path: path.String(),
}
info := resp.Header[errLocation][0]
if err = json.Unmarshal([]byte(info), &pcel.Errors); err != nil {
return body, fmt.Errorf("%d error, and could not unmarshal header %q: %s", resp.StatusCode, info, err)
}
if ce := pcel.GenericError(); ce != nil {
return body, ce
}
return body, pcel
}
if ans != nil {
if err = json.Unmarshal(body, ans); err != nil {
return body, err
}
}
return body, nil
}