Skip to content

Commit a6d63a8

Browse files
committed
Add support to Smart-1 Cloud
1 parent 057b48e commit a6d63a8

8 files changed

+55
-50
lines changed

APIFiles/APIClient.go

+28-26
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ type ApiClient struct {
6363
timeout time.Duration
6464
sleep time.Duration
6565
userAgent string
66+
cloudMgmtId string
6667
}
6768

6869
// Api Client constructor
@@ -95,6 +96,10 @@ func APIClient(apiCA ApiClientArgs) *ApiClient {
9596
apiCA.Sleep = SleepTime
9697
}
9798

99+
if apiCA.UserAgent == "" {
100+
apiCA.UserAgent = "golang-api-wrapper"
101+
}
102+
98103
return &ApiClient{
99104
port: apiCA.Port,
100105
isPortDefault_: isPortDefault,
@@ -115,6 +120,7 @@ func APIClient(apiCA ApiClientArgs) *ApiClient {
115120
timeout: apiCA.Timeout,
116121
sleep: apiCA.Sleep,
117122
userAgent: apiCA.UserAgent,
123+
cloudMgmtId: apiCA.CloudMgmtId,
118124
}
119125
}
120126

@@ -254,7 +260,9 @@ func (c *ApiClient) commonLoginLogic(credentials map[string]interface{}, continu
254260
if loginRes.Success {
255261
c.sid = loginRes.data["sid"].(string)
256262
c.domain = domain
257-
c.apiVersion = loginRes.data["api-server-version"].(string)
263+
if c.apiVersion == "" {
264+
c.apiVersion = loginRes.data["api-server-version"].(string)
265+
}
258266
}
259267

260268
return loginRes, nil
@@ -285,7 +293,7 @@ func (c *ApiClient) ApiCall(command string, payload map[string]interface{}, sid
285293
c.fingerprint = fp
286294
fpAuthentication, err := c.CheckFingerprint()
287295
if !fpAuthentication {
288-
return APIResponse{}, errors.New("fingerprint Doesn't match, someone might be trying to steal your information\n")
296+
return APIResponse{}, errors.New("fingerprint doesn't match, someone might be trying to steal your information\n")
289297
}
290298
if err != nil {
291299
return APIResponse{}, err
@@ -294,28 +302,14 @@ func (c *ApiClient) ApiCall(command string, payload map[string]interface{}, sid
294302
if payload == nil {
295303
payload = map[string]interface{}{}
296304
}
297-
if sid == "" {
298-
sid = c.sid
299-
}
300305

301306
_data, err := json.Marshal(payload)
302307
if err != nil {
303308
return APIResponse{}, err
304309
}
305310

306-
if c.userAgent == "" {
307-
c.userAgent = "golang-api-wrapper"
308-
}
309-
310-
headers := map[string]interface{}{
311-
"User-Agent": c.userAgent,
312-
"Accept": "*/*",
313-
"Content-Type": "application/json",
314-
"Content-Length": len(_data),
315-
}
316-
317-
if sid != "" {
318-
headers["X-chkp-sid"] = sid
311+
if sid == "" {
312+
sid = c.sid
319313
}
320314

321315
var client *Client
@@ -331,28 +325,36 @@ func (c *ApiClient) ApiCall(command string, payload map[string]interface{}, sid
331325
}
332326
}
333327

334-
var url string
335-
if c.apiVersion == "" {
336-
url = "/" + c.context + "/" + command
337-
} else {
338-
url = "/" + c.context + "/" + "v" + c.apiVersion + "/" + command
328+
url := "https://" + c.server + ":" + strconv.Itoa(c.port)
329+
330+
if c.cloudMgmtId != "" {
331+
url += "/" + c.cloudMgmtId
339332
}
340333

334+
url += "/" + c.context
335+
336+
if c.apiVersion != "" {
337+
url += "/v" + c.apiVersion
338+
}
339+
340+
url += "/" + command
341+
341342
client.fingerprint = c.fingerprint
342343

343344
client.SetDebugLevel(c.httpDebugLevel)
344345

345346
spotReader := bytes.NewReader(_data)
346347

347-
req, err := http.NewRequest("POST", "https://"+c.server+":"+strconv.Itoa(c.port)+url, spotReader)
348+
req, err := http.NewRequest("POST", url, spotReader)
348349
if err != nil {
349350
return APIResponse{}, err
350351
}
351352

352353
req.Header.Set("Content-Type", "application/json")
353-
req.Header.Set("User-Agent", headers["User-Agent"].(string))
354+
req.Header.Set("User-Agent", c.userAgent)
355+
354356
if command != "login" {
355-
req.Header.Set("X-chkp-sid", c.sid)
357+
req.Header.Set("X-chkp-sid", sid)
356358
}
357359

358360
response, err := client.client.Do(req)

APIFiles/APIClientArgs.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ type ApiClientArgs struct {
1818
Context string
1919
AutoPublish bool
2020
Timeout time.Duration
21-
Sleep time.Duration
22-
UserAgent string
21+
Sleep time.Duration
22+
UserAgent string
23+
CloudMgmtId string
2324
}
2425

2526
/*
@@ -39,7 +40,7 @@ DebugFile: name of debug file
3940
Context: which API to use - Management API = web_api (default) or GAIA API = gaia_api
4041
Timeout: HTTP Client timeout value
4142
*/
42-
func APIClientArgs(port int, fingerprint string, sid string, server string, proxyHost string, proxyPort int, apiVersion string, ignoreServerCertificate bool, acceptServerCertificate bool, debugFile string, context string, timeout time.Duration, sleep time.Duration, userAgent string) ApiClientArgs {
43+
func APIClientArgs(port int, fingerprint string, sid string, server string, proxyHost string, proxyPort int, apiVersion string, ignoreServerCertificate bool, acceptServerCertificate bool, debugFile string, context string, timeout time.Duration, sleep time.Duration, userAgent string, cloudMgmtId string) ApiClientArgs {
4344

4445
return ApiClientArgs{
4546
Port: port,
@@ -56,5 +57,6 @@ func APIClientArgs(port int, fingerprint string, sid string, server string, prox
5657
Timeout: timeout,
5758
Sleep: sleep,
5859
UserAgent: userAgent,
60+
CloudMgmtId: cloudMgmtId,
5961
}
6062
}

Examples/add_access_rule.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package Examples
22

33
import (
4+
api "../APIFiles"
45
"fmt"
5-
api "github.com/CheckPointSW/cp-mgmt-api-go-sdk/APIFiles"
66
"os"
77
)
88

@@ -21,7 +21,7 @@ func AddAccessRule() {
2121
fmt.Printf("Enter password: ")
2222
fmt.Scanln(&password)
2323

24-
args := api.APIClientArgs(443, "", "", apiServer, "194.29.36.43", 8080, "", false, false, "deb.txt", api.WebContext, api.TimeOut, api.SleepTime, "")
24+
args := api.APIClientArgs(api.DefaultPort, "", "", apiServer, "", -1, "", false, false, "deb.txt", api.WebContext, api.TimeOut, api.SleepTime, "", "")
2525
client := api.APIClient(args)
2626

2727
fmt.Printf("Enter the name of the access rule: ")

Examples/add_host.go

+13-12
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package Examples
22

33
import (
4+
api "../APIFiles"
45
"fmt"
5-
api "github.com/CheckPointSW/cp-mgmt-api-go-sdk/APIFiles"
66
"os"
77
)
88

@@ -20,24 +20,16 @@ func AddHost() {
2020
fmt.Printf("Enter password: ")
2121
fmt.Scanln(&password)
2222

23-
args := api.APIClientArgs(443, "", "", apiServer, "194.29.36.43", 8080, "", false, false, "deb.txt", api.WebContext, api.TimeOut, api.SleepTime, "")
23+
args := api.APIClientArgs(api.DefaultPort, "", "", apiServer, "", -1, "", false, false, "deb.txt", api.WebContext, api.TimeOut, api.SleepTime, "", "")
2424

2525
client := api.APIClient(args)
2626

27-
fmt.Printf("Enter the name of the host: ")
28-
var hostName string
29-
fmt.Scanln(&hostName)
30-
31-
fmt.Printf("Enter the ip of the host: ")
32-
var hostIp string
33-
fmt.Scanln(&hostIp)
34-
3527
if x, _ := client.CheckFingerprint(); x == false {
3628
print("Could not get the server's fingerprint - Check connectivity with the server.\n")
3729
os.Exit(1)
3830
}
3931

40-
loginRes, err := client.Login(username, password, false, "", false, "")
32+
loginRes, err := client.Login(username, password,false, "", false, "")
4133
if err != nil {
4234
print("Login error.\n")
4335
os.Exit(1)
@@ -47,7 +39,16 @@ func AddHost() {
4739
print("Login failed:\n" + loginRes.ErrorMsg)
4840
os.Exit(1)
4941
}
50-
// add a rule to the top of the "Network" layer
42+
43+
fmt.Printf("Enter the name of the host: ")
44+
var hostName string
45+
fmt.Scanln(&hostName)
46+
47+
fmt.Printf("Enter the ip of the host: ")
48+
var hostIp string
49+
fmt.Scanln(&hostIp)
50+
51+
// add host
5152
payload := map[string]interface{}{
5253
"name": hostName,
5354
"ip-address": hostIp,

Examples/discard_sessions.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package Examples
22

33
import (
4+
api "../APIFiles"
45
"fmt"
5-
api "github.com/CheckPointSW/cp-mgmt-api-go-sdk/APIFiles"
66
"os"
77
)
88

@@ -21,7 +21,7 @@ func DiscardSessions() {
2121
fmt.Printf("Enter password: ")
2222
fmt.Scanln(&password)
2323

24-
args := api.APIClientArgs(443, "", "", apiServer, "", -1, "", false, false, "deb.txt", api.WebContext, api.TimeOut, api.SleepTime, "")
24+
args := api.APIClientArgs(api.DefaultPort, "", "", apiServer, "", -1, "", false, false, "deb.txt", api.WebContext, api.TimeOut, api.SleepTime, "", "")
2525

2626
client := api.APIClient(args)
2727

Examples/find_dup_ip.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package Examples
22

33
import (
4+
api "../APIFiles"
45
"fmt"
5-
api "github.com/CheckPointSW/cp-mgmt-api-go-sdk/APIFiles"
66
"os"
77
)
88

@@ -20,7 +20,7 @@ func DupIp() {
2020
var pass string
2121
fmt.Scanln(&pass)
2222

23-
args := api.APIClientArgs(443, "", "", apiServer, "", -1, "", false, false, "deb.txt", api.WebContext, api.TimeOut, api.SleepTime, "")
23+
args := api.APIClientArgs(api.DefaultPort, "", "", apiServer, "", -1, "", false, false, "deb.txt", api.WebContext, api.TimeOut, api.SleepTime, "", "")
2424

2525
client := api.APIClient(args)
2626

Examples/show_hosts.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package Examples
22

33
import (
4+
api "../APIFiles"
45
"fmt"
5-
api "github.com/CheckPointSW/cp-mgmt-api-go-sdk/APIFiles"
66
"os"
77
)
88

@@ -21,7 +21,7 @@ func ShowHosts() {
2121
fmt.Printf("Enter password: ")
2222
fmt.Scanln(&password)
2323

24-
args := api.APIClientArgs(443, "", "", apiServer, "", -1, "", false, false, "deb.txt", api.WebContext, api.TimeOut, api.SleepTime, "")
24+
args := api.APIClientArgs(api.DefaultPort, "", "", apiServer, "", -1, "", false, false, "deb.txt", api.WebContext, api.TimeOut, api.SleepTime, "", "")
2525

2626
client := api.APIClient(args)
2727

main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package main
22

33
import (
4+
"./Examples"
45
"fmt"
5-
"github.com/CheckPointSW/cp-mgmt-api-go-sdk/Examples"
66
"os"
77
)
88

0 commit comments

Comments
 (0)