Skip to content

Commit 0324a43

Browse files
authored
Merge pull request #125 from imperva/add_risky_header_for_certain_cache_settings
add 'force-risky-operation' header when configuring both perf_mode_ht…
2 parents 9036bca + cd16234 commit 0324a43

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

incapsula/client.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,27 +76,32 @@ func (c *Client) PostFormWithHeaders(url string, data url.Values) (*http.Respons
7676
return nil, fmt.Errorf("Error preparing request: %s", err)
7777
}
7878

79-
SetHeaders(c, req, contentTypeApplicationUrlEncoded)
79+
SetHeaders(c, req, contentTypeApplicationUrlEncoded, nil)
8080
return c.httpClient.Do(req)
8181
}
8282

83-
func (c *Client) DoJsonRequestWithHeaders(method string, url string, data []byte) (*http.Response, error) {
83+
func (c *Client) DoJsonRequestWithCustomHeaders(method string, url string, data []byte, headers map[string]string) (*http.Response, error) {
8484
req, err := PrepareJsonRequest(method, url, data)
8585
if err != nil {
8686
return nil, fmt.Errorf("Error preparing request: %s", err)
8787
}
8888

89-
SetHeaders(c, req, contentTypeApplicationJson)
89+
SetHeaders(c, req, contentTypeApplicationJson, headers)
90+
9091
return c.httpClient.Do(req)
9192
}
9293

94+
func (c *Client) DoJsonRequestWithHeaders(method string, url string, data []byte) (*http.Response, error) {
95+
return c.DoJsonRequestWithCustomHeaders(method, url, data, nil)
96+
}
97+
9398
func (c *Client) DoJsonRequestWithHeadersForm(method string, url string, data []byte, contentType string) (*http.Response, error) {
9499
req, err := PrepareJsonRequest(method, url, data)
95100
if err != nil {
96101
return nil, fmt.Errorf("Error preparing request: %s", err)
97102
}
98103

99-
SetHeaders(c, req, contentType)
104+
SetHeaders(c, req, contentType, nil)
100105
return c.httpClient.Do(req)
101106
}
102107

@@ -108,9 +113,16 @@ func PrepareJsonRequest(method string, url string, data []byte) (*http.Request,
108113
return http.NewRequest(method, url, bytes.NewReader(data))
109114
}
110115

111-
func SetHeaders(c *Client, req *http.Request, contentType string) {
116+
func SetHeaders(c *Client, req *http.Request, contentType string, customHeaders map[string]string) {
112117
req.Header.Set("Content-Type", contentType)
113118
req.Header.Set("x-api-id", c.config.APIID)
114119
req.Header.Set("x-api-key", c.config.APIKey)
115120
req.Header.Set("x-tf-provider-ver", c.providerVersion)
121+
122+
if customHeaders != nil {
123+
for name, value := range customHeaders {
124+
req.Header.Set(name, value)
125+
}
126+
}
127+
116128
}

incapsula/client_performance.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ import (
66
"io/ioutil"
77
"log"
88
"net/http"
9+
"strconv"
910
)
1011

12+
const FORCE_RISKY_OP_HEADER_NAME = "force-risky-operation"
13+
1114
// PerformanceSettings is a struct that encompasses all the properties for performance settings
1215
type PerformanceSettings struct {
1316
Mode struct {
@@ -93,8 +96,15 @@ func (c *Client) UpdatePerformanceSettings(siteID string, performanceSettings *P
9396

9497
// Post request to Incapsula
9598
log.Printf("[DEBUG] Incapsula Update Incap Performance Settings JSON request: %s\n", string(performanceSettingsJSON))
99+
100+
var headers = map[string]string{}
101+
if performanceSettings.Mode.HTTPS == "include_all_resources" && performanceSettings.Mode.Level == "all_resources" {
102+
log.Printf("[DEBUG] Incapsula Update Incap Performance Settings - adding header: %s\n", FORCE_RISKY_OP_HEADER_NAME)
103+
headers[FORCE_RISKY_OP_HEADER_NAME] = strconv.FormatBool(true)
104+
}
105+
96106
reqURL := fmt.Sprintf("%s/sites/%s/settings/cache", c.config.BaseURLRev2, siteID)
97-
resp, err := c.DoJsonRequestWithHeaders(http.MethodPut, reqURL, performanceSettingsJSON)
107+
resp, err := c.DoJsonRequestWithCustomHeaders(http.MethodPut, reqURL, performanceSettingsJSON, headers)
98108
if err != nil {
99109
return nil, fmt.Errorf("Error from Incapsula service when updating Incap Performance Settings for Site ID %s: %s", siteID, err)
100110
}

0 commit comments

Comments
 (0)