Skip to content

Commit

Permalink
Auto publish feature
Browse files Browse the repository at this point in the history
  • Loading branch information
chkp-royl committed May 11, 2023
1 parent a6d63a8 commit d865a84
Show file tree
Hide file tree
Showing 10 changed files with 250 additions and 28 deletions.
137 changes: 123 additions & 14 deletions APIFiles/APIClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"reflect"
"strconv"
"strings"
"sync"
"time"
)

Expand All @@ -40,6 +41,7 @@ const (
WebContext string = "web_api"
DefaultProxyPort = -1
DefaultProxyHost = ""
AutoPublishBatchSize int = 100
)

// Check Point API Client (Management/GAIA)
Expand All @@ -59,11 +61,17 @@ type ApiClient struct {
debugFile string
httpDebugLevel string
context string
autoPublish bool
timeout time.Duration
sleep time.Duration
userAgent string
cloudMgmtId string
autoPublishBatchSize int
activeCallsLock sync.Mutex
autoPublishLock sync.Mutex
totalCallsLock sync.Mutex
duringPublish bool
activeCallsCtr int
totalCallsCtr int
}

// Api Client constructor
Expand Down Expand Up @@ -116,7 +124,7 @@ func APIClient(apiCA ApiClientArgs) *ApiClient {
debugFile: apiCA.DebugFile,
httpDebugLevel: apiCA.HttpDebugLevel,
context: apiCA.Context,
autoPublish: apiCA.AutoPublish,
autoPublishBatchSize: apiCA.AutoPublishBatchSize,
timeout: apiCA.Timeout,
sleep: apiCA.Sleep,
userAgent: apiCA.UserAgent,
Expand All @@ -134,10 +142,6 @@ func (c *ApiClient) GetContext() string {
return c.context
}

func (c *ApiClient) GetAutoPublish() bool {
return c.autoPublish
}

// Returns the fingerprint of API client
func (c *ApiClient) getFingerprint() string {
return c.fingerprint
Expand Down Expand Up @@ -178,6 +182,36 @@ func (c *ApiClient) GetSessionID() string {
return c.sid
}

// Returns number of batch size
func (c *ApiClient) GetAutoPublishBatchSize() int {
return c.autoPublishBatchSize
}

func (c *ApiClient) SetAutoPublishBatchSize(autoPublishBatchSize int) {
c.autoPublishBatchSize = autoPublishBatchSize
}

func (c *ApiClient) increaseActiveCalls() {
c.activeCallsLock.Lock()
c.activeCallsCtr++
c.activeCallsLock.Unlock()
}

func (c *ApiClient) decreaseActiveCalls() {
c.activeCallsLock.Lock()
c.activeCallsCtr--
c.activeCallsLock.Unlock()
}

func (c *ApiClient) ResetTotalCallsCounter() {
c.totalCallsCtr = 0
}

func (c *ApiClient) DisableAutoPublish() {
c.autoPublishBatchSize = -1
c.totalCallsCtr = 0
}

// Deprecated: Do not use.
func (c *ApiClient) Login(username string, password string, continueLastSession bool, domain string, readOnly bool, payload string) (APIResponse, error) {
credentials := map[string]interface{}{
Expand Down Expand Up @@ -253,7 +287,7 @@ func (c *ApiClient) commonLoginLogic(credentials map[string]interface{}, continu
}
}

loginRes, errCall := c.ApiCall("login", credentials, "", false, false)
loginRes, errCall := c.apiCall("login", credentials, "", false, c.IsProxyUsed(), true)
if errCall != nil {
return loginRes, errCall
}
Expand Down Expand Up @@ -285,6 +319,14 @@ side-effects: updates the class's uid and server variables
*/
func (c *ApiClient) ApiCall(command string, payload map[string]interface{}, sid string, waitForTask bool, useProxy bool) (APIResponse, error) {
return c.apiCall(command,payload,sid,waitForTask,useProxy,false)
}

func (c *ApiClient) ApiCallSimple(command string, payload map[string]interface{}) (APIResponse, error) {
return c.apiCall(command, payload, c.sid,true, c.IsProxyUsed(),false)
}

func (c *ApiClient) apiCall(command string, payload map[string]interface{}, sid string, waitForTask bool, useProxy bool, internal bool) (APIResponse, error) {
fp, errFP := getFingerprint(c.server, c.port)
if errFP != nil {
return APIResponse{}, errFP
Expand Down Expand Up @@ -357,21 +399,55 @@ func (c *ApiClient) ApiCall(command string, payload map[string]interface{}, sid
req.Header.Set("X-chkp-sid", sid)
}

if !internal && c.autoPublishBatchSize > 0 {
waitToRun := true
for waitToRun {
if c.totalCallsCtr + 1 <= c.autoPublishBatchSize && !c.duringPublish {
c.totalCallsLock.Lock()
if c.totalCallsCtr + 1 <= c.autoPublishBatchSize && !c.duringPublish {
c.totalCallsCtr++
waitToRun = false
}
c.totalCallsLock.Unlock()
}
if waitToRun {
time.Sleep(time.Second)
}
}
c.increaseActiveCalls()
}

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

if err != nil {
if !internal && c.autoPublishBatchSize > 0 {
c.decreaseActiveCalls()
}
return APIResponse{}, err
}

res, err := fromHTTPResponse(response, "")
if err != nil {
if !internal && c.autoPublishBatchSize > 0 {
c.decreaseActiveCalls()
}
return APIResponse{}, err
}

if !res.Success {
resCode := ""
resMsg := ""
if code := res.GetData()["code"]; code != nil {
resCode = code.(string)
}
if msg := res.GetData()["message"]; msg != nil {
resMsg = msg.(string)
}

fullErrorMsg := "failed to execute API call" +
"\nStatus: " + res.StatusCode +
"\nCode: " + res.GetData()["code"].(string) +
"\nMessage: " + res.GetData()["message"].(string)
"\nCode: " + resCode +
"\nMessage: " + resMsg

if errorMsg := res.data["errors"]; errorMsg != nil {
fullErrorMsg += "\nErrors: "
Expand Down Expand Up @@ -419,6 +495,9 @@ func (c *ApiClient) ApiCall(command string, payload map[string]interface{}, sid
if _, ok := res.data["task-id"]; ok {
res, err = c.waitForTask(res.data["task-id"].(string))
if err != nil {
if !internal && c.autoPublishBatchSize > 0 {
c.decreaseActiveCalls()
}
return APIResponse{}, err
}
} else if _, ok := res.data["tasks"]; ok {
Expand All @@ -428,6 +507,36 @@ func (c *ApiClient) ApiCall(command string, payload map[string]interface{}, sid
}
}
}

if !internal && c.autoPublishBatchSize > 0 {
c.decreaseActiveCalls()
if c.totalCallsCtr > 0 && c.totalCallsCtr % c.autoPublishBatchSize == 0 && !c.duringPublish {
c.autoPublishLock.Lock()
if c.totalCallsCtr > 0 && c.totalCallsCtr % c.autoPublishBatchSize == 0 && !c.duringPublish {
c.duringPublish = true
c.autoPublishLock.Unlock()
for c.activeCallsCtr > 0 {
// Waiting for other calls to finish
fmt.Println("Waiting to start auto publish (Active calls " + strconv.Itoa(c.activeCallsCtr) + ")")
time.Sleep(time.Second)
}
// Going to publish
fmt.Println("Start auto publish...")
publishRes, _ := c.apiCall("publish", map[string]interface{}{},c.GetSessionID(),true,c.IsProxyUsed(), true)

if !publishRes.Success {
fmt.Println("Auto publish failed. Message: " + publishRes.ErrorMsg)
}else{
fmt.Println("Auto publish finished successfully")
}
c.totalCallsCtr = 0
c.duringPublish = false
}else{
c.autoPublishLock.Unlock()
}
}
}

return res, nil
}

Expand Down Expand Up @@ -527,7 +636,7 @@ func (c *ApiClient) genApiQuery(command string, detailsLevel string, containerKe
payload["limit"] = objLimit
payload["offset"] = iterations * objLimit
payload["details-level"] = detailsLevel
apiRes, err := c.ApiCall(command, payload, "", false, false)
apiRes, err := c.apiCall(command, payload, c.sid, false, c.IsProxyUsed(), true)

if err != nil {
print(err.Error())
Expand Down Expand Up @@ -581,7 +690,7 @@ func (c *ApiClient) genApiQuery(command string, detailsLevel string, containerKe
payload["limit"] = objLimit
payload["offset"] = iterations * objLimit
payload["details-level"] = detailsLevel
apiRes, err = c.ApiCall(command, payload, "", false, false)
apiRes, err = c.apiCall(command, payload, c.sid, false, c.IsProxyUsed(), true)

if err != nil {
print("Error communicating with server, please check your connection.")
Expand Down Expand Up @@ -613,7 +722,7 @@ func (c *ApiClient) waitForTask(taskId string) (APIResponse, error) {
payload := map[string]interface{}{"task-id": taskId, "details-level": "full"}

for !taskComplete {
taskResult, err = c.ApiCall("show-task", payload, c.sid, false, false)
taskResult, err = c.apiCall("show-task", payload, c.sid, false, c.IsProxyUsed(), true)

if err != nil {
return APIResponse{}, err
Expand All @@ -625,7 +734,7 @@ func (c *ApiClient) waitForTask(taskId string) (APIResponse, error) {
if attemptsCounter < 5 {
attemptsCounter++
time.Sleep(c.sleep)
taskResult, err = c.ApiCall("show-task", payload, c.sid, false, false)
taskResult, err = c.apiCall("show-task", payload, c.sid, false, c.IsProxyUsed(), true)

if err != nil {
return APIResponse{}, err
Expand Down Expand Up @@ -679,7 +788,7 @@ func (c *ApiClient) waitForTasks(taskObjects []interface{}) APIResponse {
"task-id": tasks,
"details-level": "full",
}
taskRes, err := c.ApiCall("show-task", payload, c.GetSessionID(), false, c.proxyHost != "")
taskRes, err := c.apiCall("show-task", payload, c.GetSessionID(), false, c.IsProxyUsed(), true)

if err != nil {
fmt.Println("Problem showing tasks, try again")
Expand Down
9 changes: 7 additions & 2 deletions APIFiles/APIClientArgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ type ApiClientArgs struct {
AcceptServerCertificate bool
DebugFile string
Context string
AutoPublish bool
Timeout time.Duration
Sleep time.Duration
UserAgent string
CloudMgmtId string
AutoPublishBatchSize int
}

/*
Expand All @@ -39,8 +39,12 @@ AcceptServerCertificate: indicates that the client should automatically accept a
DebugFile: name of debug file
Context: which API to use - Management API = web_api (default) or GAIA API = gaia_api
Timeout: HTTP Client timeout value
Sleep: Interval size in seconds of the task update
UserAgent: User agent will be use in api call request header
CloudMgmtId: Smart-1 Cloud management UID
AutoPublishBatchSize: Number of batch size for auto publish
*/
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 {
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, autoPublishBatchSize int) ApiClientArgs {

return ApiClientArgs{
Port: port,
Expand All @@ -58,5 +62,6 @@ func APIClientArgs(port int, fingerprint string, sid string, server string, prox
Sleep: sleep,
UserAgent: userAgent,
CloudMgmtId: cloudMgmtId,
AutoPublishBatchSize: autoPublishBatchSize,
}
}
4 changes: 2 additions & 2 deletions Examples/add_access_rule.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package Examples

import (
api "../APIFiles"
api "cp-mgmt-api-go-sdk/APIFiles"
"fmt"
"os"
)
Expand All @@ -21,7 +21,7 @@ func AddAccessRule() {
fmt.Printf("Enter password: ")
fmt.Scanln(&password)

args := api.APIClientArgs(api.DefaultPort, "", "", apiServer, "", -1, "", false, false, "deb.txt", api.WebContext, api.TimeOut, api.SleepTime, "", "")
args := api.APIClientArgs(api.DefaultPort, "", "", apiServer, "", -1, "", false, false, "deb.txt", api.WebContext, api.TimeOut, api.SleepTime, "", "", -1)
client := api.APIClient(args)

fmt.Printf("Enter the name of the access rule: ")
Expand Down
4 changes: 2 additions & 2 deletions Examples/add_host.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package Examples

import (
api "../APIFiles"
api "cp-mgmt-api-go-sdk/APIFiles"
"fmt"
"os"
)
Expand All @@ -20,7 +20,7 @@ func AddHost() {
fmt.Printf("Enter password: ")
fmt.Scanln(&password)

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

client := api.APIClient(args)

Expand Down
Loading

0 comments on commit d865a84

Please sign in to comment.