@@ -30,57 +30,64 @@ import (
30
30
)
31
31
32
32
const (
33
- InProgress string = "in progress"
34
- DefaultPort int = 443
35
- Limit int = 50
36
- Filename string = "fingerprints.json"
37
- TimeOut time.Duration = time .Second * 10
38
- SleepTime time.Duration = time .Second * 2
39
- GaiaContext string = "gaia_api"
40
- WebContext string = "web_api"
33
+ InProgress string = "in progress"
34
+ DefaultPort int = 443
35
+ Limit int = 50
36
+ Filename string = "fingerprints.json"
37
+ TimeOut time.Duration = time .Second * 10
38
+ SleepTime time.Duration = time .Second * 2
39
+ GaiaContext string = "gaia_api"
40
+ WebContext string = "web_api"
41
+ DefaultProxyPort = - 1
42
+ DefaultProxyHost = ""
41
43
)
42
44
43
45
// Check Point API Client (Management/GAIA)
44
46
type ApiClient struct {
45
- port int
46
- isPortDefault_ bool
47
- fingerprint string
48
- sid string
49
- server string
50
- domain string
51
- proxyHost string
52
- proxyPort int
53
- apiVersion string
54
- ignoreServerCertificate bool
47
+ port int
48
+ isPortDefault_ bool
49
+ fingerprint string
50
+ sid string
51
+ server string
52
+ domain string
53
+ proxyHost string
54
+ proxyPort int
55
+ isProxyUsed bool
56
+ apiVersion string
57
+ ignoreServerCertificate bool
55
58
acceptServerCertificate bool
56
- debugFile string
57
- httpDebugLevel string
58
- context string
59
- autoPublish bool
60
- timeout time.Duration
61
- sleep time.Duration
62
- userAgent string
59
+ debugFile string
60
+ httpDebugLevel string
61
+ context string
62
+ autoPublish bool
63
+ timeout time.Duration
64
+ sleep time.Duration
65
+ userAgent string
63
66
}
64
67
65
68
// Api Client constructor
66
69
// Input ApiClientArgs
67
70
// Returns new client instance
68
71
func APIClient (apiCA ApiClientArgs ) * ApiClient {
69
- isPortDefault := false
72
+ isPortDefault := false
73
+ proxyUsed := true
70
74
71
75
if apiCA .Port == - 1 || apiCA .Port == DefaultPort {
72
76
apiCA .Port = DefaultPort
73
77
isPortDefault = true
74
78
}
79
+ if apiCA .ProxyPort == DefaultProxyPort && apiCA .ProxyHost == DefaultProxyHost {
80
+ proxyUsed = false
81
+ }
75
82
76
83
// The context of using the client - defaults to web api
77
84
if apiCA .Context == "" {
78
85
apiCA .Context = WebContext
79
86
}
80
87
81
- if apiCA .Timeout == - 1 || apiCA .Timeout == TimeOut {
82
- apiCA .Timeout = TimeOut
83
- }else {
88
+ if apiCA .Timeout == - 1 || apiCA .Timeout == TimeOut {
89
+ apiCA .Timeout = TimeOut
90
+ } else {
84
91
apiCA .Timeout = apiCA .Timeout * time .Second
85
92
}
86
93
@@ -89,24 +96,25 @@ func APIClient(apiCA ApiClientArgs) *ApiClient {
89
96
}
90
97
91
98
return & ApiClient {
92
- port : apiCA .Port ,
93
- isPortDefault_ : isPortDefault ,
94
- fingerprint : apiCA .Fingerprint ,
95
- sid : apiCA .Sid ,
96
- server : apiCA .Server ,
97
- domain : "" ,
98
- proxyHost : apiCA .ProxyHost ,
99
- proxyPort : apiCA .ProxyPort ,
100
- apiVersion : apiCA .ApiVersion ,
99
+ port : apiCA .Port ,
100
+ isPortDefault_ : isPortDefault ,
101
+ fingerprint : apiCA .Fingerprint ,
102
+ sid : apiCA .Sid ,
103
+ server : apiCA .Server ,
104
+ domain : "" ,
105
+ proxyHost : apiCA .ProxyHost ,
106
+ proxyPort : apiCA .ProxyPort ,
107
+ isProxyUsed : proxyUsed ,
108
+ apiVersion : apiCA .ApiVersion ,
101
109
ignoreServerCertificate : apiCA .IgnoreServerCertificate ,
102
110
acceptServerCertificate : apiCA .AcceptServerCertificate ,
103
- debugFile : apiCA .DebugFile ,
104
- httpDebugLevel : apiCA .HttpDebugLevel ,
105
- context : apiCA .Context ,
106
- autoPublish : apiCA .AutoPublish ,
107
- timeout : apiCA .Timeout ,
108
- sleep : apiCA .Sleep ,
109
- userAgent : apiCA .UserAgent ,
111
+ debugFile : apiCA .DebugFile ,
112
+ httpDebugLevel : apiCA .HttpDebugLevel ,
113
+ context : apiCA .Context ,
114
+ autoPublish : apiCA .AutoPublish ,
115
+ timeout : apiCA .Timeout ,
116
+ sleep : apiCA .Sleep ,
117
+ userAgent : apiCA .UserAgent ,
110
118
}
111
119
}
112
120
@@ -120,7 +128,6 @@ func (c *ApiClient) GetContext() string {
120
128
return c .context
121
129
}
122
130
123
-
124
131
func (c * ApiClient ) GetAutoPublish () bool {
125
132
return c .autoPublish
126
133
}
@@ -135,6 +142,11 @@ func (c *ApiClient) IsPortDefault() bool {
135
142
return c .isPortDefault_
136
143
}
137
144
145
+ // Returns true if client use proxy
146
+ func (c * ApiClient ) IsProxyUsed () bool {
147
+ return c .isProxyUsed
148
+ }
149
+
138
150
// Set API port
139
151
func (c * ApiClient ) SetPort (portToSet int ) {
140
152
if portToSet == DefaultPort {
@@ -155,13 +167,11 @@ func (c *ApiClient) SetTimeout(timeout time.Duration) {
155
167
c .timeout = timeout
156
168
}
157
169
158
-
159
170
// Returns session id
160
171
func (c * ApiClient ) GetSessionID () string {
161
172
return c .sid
162
173
}
163
174
164
-
165
175
/*
166
176
Performs a 'login' API call to management server
167
177
@@ -174,11 +184,11 @@ payload: [optional] More settings for the login command
174
184
returns: APIResponse, error
175
185
side-effects: updates the class's uid and server variables
176
186
177
- */
187
+ */
178
188
func (c * ApiClient ) Login (username string , password string , continueLastSession bool , domain string , readOnly bool , payload string ) (APIResponse , error ) {
179
189
credentials := map [string ]interface {}{
180
- "user" : username ,
181
- "password" : password ,
190
+ "user" : username ,
191
+ "password" : password ,
182
192
}
183
193
184
194
if c .context == WebContext {
@@ -218,7 +228,7 @@ useProxy: Determines if the user wants to use the proxy server and port provider
218
228
return: APIResponse object
219
229
side-effects: updates the class's uid and server variables
220
230
221
- */
231
+ */
222
232
func (c * ApiClient ) ApiCall (command string , payload map [string ]interface {}, sid string , waitForTask bool , useProxy bool ) (APIResponse , error ) {
223
233
fp , errFP := getFingerprint (c .server , c .port )
224
234
if errFP != nil {
@@ -277,7 +287,7 @@ func (c *ApiClient) ApiCall(command string, payload map[string]interface{}, sid
277
287
var url string
278
288
if c .apiVersion == "" {
279
289
url = "/" + c .context + "/" + command
280
- }else {
290
+ } else {
281
291
url = "/" + c .context + "/" + "v" + c .apiVersion + "/" + command
282
292
}
283
293
@@ -287,7 +297,7 @@ func (c *ApiClient) ApiCall(command string, payload map[string]interface{}, sid
287
297
288
298
spotReader := bytes .NewReader (_data )
289
299
290
- req , err := http .NewRequest ("POST" , "https://" + c .server + ":" + strconv .Itoa (c .port ) + url , spotReader )
300
+ req , err := http .NewRequest ("POST" , "https://" + c .server + ":" + strconv .Itoa (c .port )+ url , spotReader )
291
301
if err != nil {
292
302
return APIResponse {}, err
293
303
}
@@ -308,53 +318,53 @@ func (c *ApiClient) ApiCall(command string, payload map[string]interface{}, sid
308
318
return APIResponse {}, err
309
319
}
310
320
311
- if ! res .Success {
312
- fullErrorMsg := "failed to execute API call" +
313
- "\n Status: " + res .StatusCode +
314
- "\n Code: " + res .GetData ()["code" ].(string ) +
315
- "\n Message: " + res .GetData ()["message" ].(string )
321
+ if ! res .Success {
322
+ fullErrorMsg := "failed to execute API call" +
323
+ "\n Status: " + res .StatusCode +
324
+ "\n Code: " + res .GetData ()["code" ].(string ) +
325
+ "\n Message: " + res .GetData ()["message" ].(string )
316
326
317
- if errorMsg := res .data ["errors" ]; errorMsg != nil {
327
+ if errorMsg := res .data ["errors" ]; errorMsg != nil {
318
328
fullErrorMsg += "\n Errors: "
319
- errorMsgType := reflect .TypeOf (errorMsg ).Kind ()
320
- if errorMsgType == reflect .String {
329
+ errorMsgType := reflect .TypeOf (errorMsg ).Kind ()
330
+ if errorMsgType == reflect .String {
321
331
fullErrorMsg += errorMsg .(string ) + "\n "
322
332
} else {
323
333
errorsList := res .data ["errors" ].([]interface {})
324
334
for i := range errorsList {
325
- fullErrorMsg += "\n " + strconv .Itoa (i + 1 ) + ". " + errorsList [i ].(map [string ]interface {})["message" ].(string )
335
+ fullErrorMsg += "\n " + strconv .Itoa (i + 1 ) + ". " + errorsList [i ].(map [string ]interface {})["message" ].(string )
326
336
}
327
337
}
328
- }
338
+ }
329
339
330
- if warningMsg := res .data ["warnings" ]; warningMsg != nil {
331
- fullErrorMsg += "\n Warnings: "
332
- warningMsgType := reflect .TypeOf (warningMsg ).Kind ()
333
- if warningMsgType == reflect .String {
340
+ if warningMsg := res .data ["warnings" ]; warningMsg != nil {
341
+ fullErrorMsg += "\n Warnings: "
342
+ warningMsgType := reflect .TypeOf (warningMsg ).Kind ()
343
+ if warningMsgType == reflect .String {
334
344
fullErrorMsg += warningMsg .(string ) + "\n "
335
345
} else {
336
346
warningsList := res .data ["warnings" ].([]interface {})
337
347
for i := range warningsList {
338
- fullErrorMsg += "\n " + strconv .Itoa (i + 1 ) + ". " + warningsList [i ].(map [string ]interface {})["message" ].(string )
348
+ fullErrorMsg += "\n " + strconv .Itoa (i + 1 ) + ". " + warningsList [i ].(map [string ]interface {})["message" ].(string )
339
349
}
340
350
}
341
- }
342
-
343
- if blockingError := res .data ["blocking-errors" ]; blockingError != nil {
344
- fullErrorMsg += "\n Blocking errors: "
345
- warningMsgType := reflect .TypeOf (blockingError ).Kind ()
346
- if warningMsgType == reflect .String {
347
- fullErrorMsg += blockingError .(string ) + "\n "
348
- } else {
349
- blockingErrorsList := res .data ["blocking-errors" ].([]interface {})
350
- for i := range blockingErrorsList {
351
- fullErrorMsg += "\n " + strconv .Itoa (i + 1 ) + ". " + blockingErrorsList [i ].(map [string ]interface {})["message" ].(string )
352
- }
353
- }
354
- }
355
-
356
- res .ErrorMsg = fullErrorMsg
357
- }
351
+ }
352
+
353
+ if blockingError := res .data ["blocking-errors" ]; blockingError != nil {
354
+ fullErrorMsg += "\n Blocking errors: "
355
+ warningMsgType := reflect .TypeOf (blockingError ).Kind ()
356
+ if warningMsgType == reflect .String {
357
+ fullErrorMsg += blockingError .(string ) + "\n "
358
+ } else {
359
+ blockingErrorsList := res .data ["blocking-errors" ].([]interface {})
360
+ for i := range blockingErrorsList {
361
+ fullErrorMsg += "\n " + strconv .Itoa (i + 1 ) + ". " + blockingErrorsList [i ].(map [string ]interface {})["message" ].(string )
362
+ }
363
+ }
364
+ }
365
+
366
+ res .ErrorMsg = fullErrorMsg
367
+ }
358
368
359
369
if waitForTask == true && res .Success && command != "show-task" {
360
370
if _ , ok := res .data ["task-id" ]; ok {
@@ -448,7 +458,6 @@ returns: an APIResponse object as detailed above
448
458
*/
449
459
func (c * ApiClient ) genApiQuery (command string , detailsLevel string , containerKeys []string , payload map [string ]interface {}, err_output * error ) []APIResponse {
450
460
451
-
452
461
const objLimit int = Limit
453
462
var finished bool = false
454
463
@@ -536,7 +545,6 @@ func (c *ApiClient) genApiQuery(command string, detailsLevel string, containerKe
536
545
return serverResponse
537
546
}
538
547
539
-
540
548
/**
541
549
When the server needs to perform an API call that may take a long time (e.g. run-script, install-policy,
542
550
publish), the server responds with a 'task-id'.
@@ -669,7 +677,7 @@ func checkTasksStatus(taskResult *APIResponse) {
669
677
@===================@
670
678
| FINGERPRINT AREA |
671
679
@===================@
672
- */
680
+ */
673
681
674
682
/**
675
683
This function checks if the server's certificate is stored in the local fingerprints file.
@@ -716,7 +724,7 @@ func (c *ApiClient) CheckFingerprint() (bool, error) {
716
724
} else {
717
725
fmt .Fprintf (os .Stderr , "The server's fingerprint is different from your local record of this server's fingerprint.\n You maybe a victim to a Man-in-the-Middle attack, please beware.\n " )
718
726
}
719
- fmt .Fprintf (os .Stderr , "Server's fingerprint: %s\n " , (serverFp ), )
727
+ fmt .Fprintf (os .Stderr , "Server's fingerprint: %s\n " , (serverFp ))
720
728
721
729
if c .askYesOrNoQuestion ("Do you accept this fingerprint?\n " ) {
722
730
if c .saveFingerprintToFile (c .server , serverFp ) == nil {
@@ -854,6 +862,6 @@ func (c *ApiClient) createEmptyJsonFile(name string) error {
854
862
func (c * ApiClient ) askYesOrNoQuestion (question string ) bool {
855
863
fmt .Println (question )
856
864
var answer string
857
- _ ,_ = fmt .Scanln (& answer )
865
+ _ , _ = fmt .Scanln (& answer )
858
866
return strings .ToLower (answer ) == "y" || strings .ToLower (answer ) == "yes"
859
867
}
0 commit comments