@@ -30,57 +30,64 @@ import (
3030)
3131
3232const (
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 = ""
4143)
4244
4345// Check Point API Client (Management/GAIA)
4446type 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
5558 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
6366}
6467
6568// Api Client constructor
6669// Input ApiClientArgs
6770// Returns new client instance
6871func APIClient (apiCA ApiClientArgs ) * ApiClient {
69- isPortDefault := false
72+ isPortDefault := false
73+ proxyUsed := true
7074
7175 if apiCA .Port == - 1 || apiCA .Port == DefaultPort {
7276 apiCA .Port = DefaultPort
7377 isPortDefault = true
7478 }
79+ if apiCA .ProxyPort == DefaultProxyPort && apiCA .ProxyHost == DefaultProxyHost {
80+ proxyUsed = false
81+ }
7582
7683 // The context of using the client - defaults to web api
7784 if apiCA .Context == "" {
7885 apiCA .Context = WebContext
7986 }
8087
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 {
8491 apiCA .Timeout = apiCA .Timeout * time .Second
8592 }
8693
@@ -89,24 +96,25 @@ func APIClient(apiCA ApiClientArgs) *ApiClient {
8996 }
9097
9198 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 ,
101109 ignoreServerCertificate : apiCA .IgnoreServerCertificate ,
102110 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 ,
110118 }
111119}
112120
@@ -120,7 +128,6 @@ func (c *ApiClient) GetContext() string {
120128 return c .context
121129}
122130
123-
124131func (c * ApiClient ) GetAutoPublish () bool {
125132 return c .autoPublish
126133}
@@ -135,6 +142,11 @@ func (c *ApiClient) IsPortDefault() bool {
135142 return c .isPortDefault_
136143}
137144
145+ // Returns true if client use proxy
146+ func (c * ApiClient ) IsProxyUsed () bool {
147+ return c .isProxyUsed
148+ }
149+
138150// Set API port
139151func (c * ApiClient ) SetPort (portToSet int ) {
140152 if portToSet == DefaultPort {
@@ -155,13 +167,11 @@ func (c *ApiClient) SetTimeout(timeout time.Duration) {
155167 c .timeout = timeout
156168}
157169
158-
159170// Returns session id
160171func (c * ApiClient ) GetSessionID () string {
161172 return c .sid
162173}
163174
164-
165175/*
166176Performs a 'login' API call to management server
167177
@@ -174,11 +184,11 @@ payload: [optional] More settings for the login command
174184returns: APIResponse, error
175185side-effects: updates the class's uid and server variables
176186
177- */
187+ */
178188func (c * ApiClient ) Login (username string , password string , continueLastSession bool , domain string , readOnly bool , payload string ) (APIResponse , error ) {
179189 credentials := map [string ]interface {}{
180- "user" : username ,
181- "password" : password ,
190+ "user" : username ,
191+ "password" : password ,
182192 }
183193
184194 if c .context == WebContext {
@@ -218,7 +228,7 @@ useProxy: Determines if the user wants to use the proxy server and port provider
218228return: APIResponse object
219229side-effects: updates the class's uid and server variables
220230
221- */
231+ */
222232func (c * ApiClient ) ApiCall (command string , payload map [string ]interface {}, sid string , waitForTask bool , useProxy bool ) (APIResponse , error ) {
223233 fp , errFP := getFingerprint (c .server , c .port )
224234 if errFP != nil {
@@ -277,7 +287,7 @@ func (c *ApiClient) ApiCall(command string, payload map[string]interface{}, sid
277287 var url string
278288 if c .apiVersion == "" {
279289 url = "/" + c .context + "/" + command
280- }else {
290+ } else {
281291 url = "/" + c .context + "/" + "v" + c .apiVersion + "/" + command
282292 }
283293
@@ -287,7 +297,7 @@ func (c *ApiClient) ApiCall(command string, payload map[string]interface{}, sid
287297
288298 spotReader := bytes .NewReader (_data )
289299
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 )
291301 if err != nil {
292302 return APIResponse {}, err
293303 }
@@ -308,53 +318,53 @@ func (c *ApiClient) ApiCall(command string, payload map[string]interface{}, sid
308318 return APIResponse {}, err
309319 }
310320
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 )
316326
317- if errorMsg := res .data ["errors" ]; errorMsg != nil {
327+ if errorMsg := res .data ["errors" ]; errorMsg != nil {
318328 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 {
321331 fullErrorMsg += errorMsg .(string ) + "\n "
322332 } else {
323333 errorsList := res .data ["errors" ].([]interface {})
324334 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 )
326336 }
327337 }
328- }
338+ }
329339
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 {
334344 fullErrorMsg += warningMsg .(string ) + "\n "
335345 } else {
336346 warningsList := res .data ["warnings" ].([]interface {})
337347 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 )
339349 }
340350 }
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+ }
358368
359369 if waitForTask == true && res .Success && command != "show-task" {
360370 if _ , ok := res .data ["task-id" ]; ok {
@@ -448,7 +458,6 @@ returns: an APIResponse object as detailed above
448458*/
449459func (c * ApiClient ) genApiQuery (command string , detailsLevel string , containerKeys []string , payload map [string ]interface {}, err_output * error ) []APIResponse {
450460
451-
452461 const objLimit int = Limit
453462 var finished bool = false
454463
@@ -536,7 +545,6 @@ func (c *ApiClient) genApiQuery(command string, detailsLevel string, containerKe
536545 return serverResponse
537546}
538547
539-
540548/**
541549When the server needs to perform an API call that may take a long time (e.g. run-script, install-policy,
542550publish), the server responds with a 'task-id'.
@@ -669,7 +677,7 @@ func checkTasksStatus(taskResult *APIResponse) {
669677 @===================@
670678 | FINGERPRINT AREA |
671679 @===================@
672- */
680+ */
673681
674682/**
675683This function checks if the server's certificate is stored in the local fingerprints file.
@@ -716,7 +724,7 @@ func (c *ApiClient) CheckFingerprint() (bool, error) {
716724 } else {
717725 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 " )
718726 }
719- fmt .Fprintf (os .Stderr , "Server's fingerprint: %s\n " , (serverFp ), )
727+ fmt .Fprintf (os .Stderr , "Server's fingerprint: %s\n " , (serverFp ))
720728
721729 if c .askYesOrNoQuestion ("Do you accept this fingerprint?\n " ) {
722730 if c .saveFingerprintToFile (c .server , serverFp ) == nil {
@@ -854,6 +862,6 @@ func (c *ApiClient) createEmptyJsonFile(name string) error {
854862func (c * ApiClient ) askYesOrNoQuestion (question string ) bool {
855863 fmt .Println (question )
856864 var answer string
857- _ ,_ = fmt .Scanln (& answer )
865+ _ , _ = fmt .Scanln (& answer )
858866 return strings .ToLower (answer ) == "y" || strings .ToLower (answer ) == "yes"
859867}
0 commit comments