Skip to content

Commit f78a85b

Browse files
authored
Merge branch 'imperva:master' into master
2 parents c2126a6 + 6d3f712 commit f78a85b

19 files changed

+881
-337
lines changed

.github/workflows/acceptance-tests.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ jobs:
6060
key: ${{ runner.os }}-go${{ env.GO_VERSION }}-${{ hashFiles('**/go.sum') }}
6161

6262
#- name: Run unit tests
63-
# run: go test ./incapsula
63+
# run: go test -parallel=1 ./incapsula
6464

6565
- name: Run acceptance tests
66-
run: TF_LOG=debug make testacc
66+
run: make testacc

incapsula/client_delivery_rules_configuration.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func (c *Client) ReadDeliveryRuleConfiguration(siteID string, category string) (
5555
responseBody, err := ioutil.ReadAll(resp.Body)
5656

5757
// Dump JSON
58-
log.Printf("[DEBUG] Incapsula Read Waiting Room JSON response: %s\n", string(responseBody))
58+
log.Printf("[DEBUG] Incapsula Read Delivery Rules JSON response: %s\n", string(responseBody))
5959

6060
if resp.StatusCode != 200 {
6161
diags = append(diags, diag.Diagnostic{

incapsula/client_performance.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ type PerformanceSettings struct {
3030
CacheShield bool `json:"cache_shield"`
3131
CacheResponseHeader struct {
3232
Mode string `json:"mode,omitempty"`
33-
Headers []interface{} `json:"headers,omitempty"`
33+
Headers []interface{} `json:"headers"`
3434
} `json:"cache_response_header,omitempty"`
35-
TagResponseHeader string `json:"tag_response_header,omitempty"`
35+
TagResponseHeader string `json:"tag_response_header"`
3636
CacheEmptyResponses bool `json:"cache_empty_responses"`
3737
Cache300X bool `json:"cache_300x"`
3838
CacheHTTP10Responses bool `json:"cache_http_10_responses"`
@@ -53,14 +53,14 @@ type PerformanceSettings struct {
5353
}
5454

5555
// GetPerformanceSettings gets the site performance settings
56-
func (c *Client) GetPerformanceSettings(siteID string) (*PerformanceSettings, int, error) {
56+
func (c *Client) GetPerformanceSettings(siteID string) (*PerformanceSettings, error) {
5757
log.Printf("[INFO] Getting Incapsula Performance Settings for Site ID %s\n", siteID)
5858

5959
// Post form to Incapsula
6060
reqURL := fmt.Sprintf("%s/sites/%s/settings/cache", c.config.BaseURLRev2, siteID)
6161
resp, err := c.DoJsonRequestWithHeaders(http.MethodGet, reqURL, nil, ReadSitePerformance)
6262
if err != nil {
63-
return nil, 0, fmt.Errorf("Error from Incapsula service when reading Incap Performance Settings for Site ID %s: %s", siteID, err)
63+
return nil, fmt.Errorf("Error from Incapsula service when reading Incap Performance Settings for Site ID %s: %s", siteID, err)
6464
}
6565

6666
// Read the body
@@ -72,17 +72,17 @@ func (c *Client) GetPerformanceSettings(siteID string) (*PerformanceSettings, in
7272

7373
// Check the response code
7474
if resp.StatusCode != 200 {
75-
return nil, resp.StatusCode, fmt.Errorf("Error status code %d from Incapsula service when reading Incap Performance Settings for Site ID %s: %s", resp.StatusCode, siteID, string(responseBody))
75+
return nil, fmt.Errorf("Error status code %d from Incapsula service when reading Incap Performance Settings for Site ID %s: %s", resp.StatusCode, siteID, string(responseBody))
7676
}
7777

7878
// Parse the JSON
7979
var performanceSettings PerformanceSettings
8080
err = json.Unmarshal([]byte(responseBody), &performanceSettings)
8181
if err != nil {
82-
return nil, resp.StatusCode, fmt.Errorf("Error parsing Incap Performance Settings JSON response for Site ID %s: %s\nresponse: %s", siteID, err, string(responseBody))
82+
return nil, fmt.Errorf("Error parsing Incap Performance Settings JSON response for Site ID %s: %s\nresponse: %s", siteID, err, string(responseBody))
8383
}
8484

85-
return &performanceSettings, resp.StatusCode, nil
85+
return &performanceSettings, nil
8686
}
8787

8888
// UpdatePerformanceSettings updates the site performance settings

incapsula/client_performance_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func TestClientGetPerformanceSettingsBadConnection(t *testing.T) {
1717
config := &Config{APIID: "foo", APIKey: "bar", BaseURL: "badness.incapsula.com"}
1818
client := &Client{config: config, httpClient: &http.Client{Timeout: time.Millisecond * 1}}
1919
siteID := "123"
20-
performanceSettings, _, err := client.GetPerformanceSettings(siteID)
20+
performanceSettings, err := client.GetPerformanceSettings(siteID)
2121
if err == nil {
2222
t.Errorf("Should have received an error")
2323
}
@@ -47,7 +47,7 @@ func TestClientGetPerformanceSettingsBadJSON(t *testing.T) {
4747
config := &Config{APIID: apiID, APIKey: apiKey, BaseURL: server.URL, BaseURLRev2: server.URL, BaseURLAPI: server.URL}
4848
client := &Client{config: config, httpClient: &http.Client{}}
4949

50-
performanceSettings, _, err := client.GetPerformanceSettings(siteID)
50+
performanceSettings, err := client.GetPerformanceSettings(siteID)
5151
if err == nil {
5252
t.Errorf("Should have received an error")
5353
}
@@ -78,7 +78,7 @@ func TestClientGetPerformanceSettingsInvalidSite(t *testing.T) {
7878
config := &Config{APIID: apiID, APIKey: apiKey, BaseURL: server.URL, BaseURLRev2: server.URL, BaseURLAPI: server.URL}
7979
client := &Client{config: config, httpClient: &http.Client{}}
8080

81-
performanceSettings, _, err := client.GetPerformanceSettings(siteID)
81+
performanceSettings, err := client.GetPerformanceSettings(siteID)
8282
if err == nil {
8383
t.Errorf("Should have received an error")
8484
}
@@ -108,7 +108,7 @@ func TestClientGetPerformanceSettingsValidSite(t *testing.T) {
108108
config := &Config{APIID: apiID, APIKey: apiKey, BaseURL: server.URL, BaseURLRev2: server.URL, BaseURLAPI: server.URL}
109109
client := &Client{config: config, httpClient: &http.Client{}}
110110

111-
performanceSettings, _, err := client.GetPerformanceSettings(siteID)
111+
performanceSettings, err := client.GetPerformanceSettings(siteID)
112112
if err != nil {
113113
t.Errorf("Should not have received an error")
114114
}

incapsula/provider.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ func Provider() *schema.Provider {
127127
"incapsula_api_security_endpoint_config": resourceApiSecurityEndpointConfig(),
128128
"incapsula_notification_center_policy": resourceNotificationCenterPolicy(),
129129
"incapsula_site_ssl_settings": resourceSiteSSLSettings(),
130-
"incapsula_site_log_configuration": resourceSiteLogConfiguration(),
131130
"incapsula_ssl_validation": resourceDomainsValidation(),
132131
"incapsula_csp_site_configuration": resourceCSPSiteConfiguration(),
133132
"incapsula_csp_site_domain": resourceCSPSiteDomain(),
@@ -154,6 +153,7 @@ func Provider() *schema.Provider {
154153
"incapsula_abp_websites": resourceAbpWebsites(),
155154
"incapsula_delivery_rules_configuration": resourceDeliveryRulesConfiguration(),
156155
"incapsula_simplified_redirect_rules_configuration": resourceSimplifiedRedirectRulesConfiguration(),
156+
"incapsula_site_cache_configuration": resourceSiteCacheConfiguration(),
157157
},
158158
}
159159

incapsula/resource_delivery_rules_configuration_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package incapsula
33
import (
44
"fmt"
55
"log"
6+
"strconv"
67
"testing"
78

89
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
@@ -116,6 +117,16 @@ func testAccCheckIncapsulaDeliveryRuleDestroy(state *terraform.State) error {
116117
}
117118

118119
siteID, ok := res.Primary.Attributes["site_id"]
120+
if siteID == "" {
121+
// There is a bug in Terraform: https://github.com/hashicorp/terraform/issues/23635
122+
// Specifically, upgrades/destroys are happening simulatneously and not honoroing
123+
// dependencies. In this case, it's possible that the site has already been deleted,
124+
// which means that all of the subresources will have been cleared out.
125+
// Ordinarily, this should return an error, but until this gets addressed, we're
126+
// going to simply return nil.
127+
// return fmt.Errorf("Incapsula site ID does not exist")
128+
return nil
129+
}
119130
if !ok {
120131
return fmt.Errorf("Incapsula Site ID does not exist")
121132
}
@@ -126,6 +137,17 @@ func testAccCheckIncapsulaDeliveryRuleDestroy(state *terraform.State) error {
126137

127138
deliveryRulesListDTO, diags := client.ReadDeliveryRuleConfiguration(siteID, category)
128139

140+
// If the site has already been deleted then return nil
141+
// Otherwise check the delivery rules list
142+
siteIdInt, err := strconv.Atoi(siteID)
143+
if err != nil {
144+
return fmt.Errorf("Couldn't parse site ID: %s ", siteID)
145+
}
146+
_, siteStatusErr := client.SiteStatus(domain, siteIdInt)
147+
if siteStatusErr != nil {
148+
return nil
149+
}
150+
129151
if diags != nil {
130152
log.Printf("[ERROR] Failed to read delivery rules in category %s for Site ID %s", category, siteID)
131153
return fmt.Errorf("failed to read delivery rules in category %s for Site ID %s\"", category, siteID)

incapsula/resource_incap_rule.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ func resourceIncapRule() *schema.Resource {
5454
Description: "The filter defines the conditions that trigger the rule action. For action `RULE_ACTION_SIMPLIFIED_REDIRECT` filter is not relevant. For other actions, if left empty, the rule is always run.",
5555
Type: schema.TypeString,
5656
Optional: true,
57+
DiffSuppressFunc: func(k, remoteState, desiredState string, d *schema.ResourceData) bool {
58+
return strings.TrimSpace(remoteState) == strings.TrimSpace(desiredState)
59+
},
5760
},
5861
"response_code": {
5962
Description: "For `RULE_ACTION_REDIRECT` or `RULE_ACTION_SIMPLIFIED_REDIRECT` rule's response code, valid values are `302`, `301`, `303`, `307`, `308`. For `RULE_ACTION_RESPONSE_REWRITE_RESPONSE_CODE` rule's response code, valid values are all 3-digits numbers. For `RULE_ACTION_CUSTOM_ERROR_RESPONSE`, valid values are `400`, `401`, `402`, `403`, `404`, `405`, `406`, `407`, `408`, `409`, `410`, `411`, `412`, `413`, `414`, `415`, `416`, `417`, `419`, `420`, `422`, `423`, `424`, `500`, `501`, `502`, `503`, `504`, `505`, `507`.",

incapsula/resource_simplified_redirect_rules_configuration_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package incapsula
33
import (
44
"fmt"
55
"log"
6+
"strconv"
67
"testing"
78

89
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
@@ -98,12 +99,33 @@ func testAccCheckIncapsulaDeliverySimplifiedRedirectRuleDestroy(state *terraform
9899
}
99100

100101
siteID, ok := res.Primary.Attributes["site_id"]
102+
if siteID == "" {
103+
// There is a bug in Terraform: https://github.com/hashicorp/terraform/issues/23635
104+
// Specifically, upgrades/destroys are happening simulatneously and not honoroing
105+
// dependencies. In this case, it's possible that the site has already been deleted,
106+
// which means that all of the subresources will have been cleared out.
107+
// Ordinarily, this should return an error, but until this gets addressed, we're
108+
// going to simply return nil.
109+
// return fmt.Errorf("Incapsula site ID does not exist")
110+
return nil
111+
}
101112
if !ok {
102113
return fmt.Errorf("Incapsula Site ID does not exist")
103114
}
104115

105116
deliveryRulesListDTO, diags := client.ReadDeliveryRuleConfiguration(siteID, category)
106117

118+
// If the site has already been deleted then return nil
119+
// Otherwise check the delivery rules list
120+
siteIdInt, err := strconv.Atoi(siteID)
121+
if err != nil {
122+
return fmt.Errorf("Couldn't parse site ID: %s ", siteID)
123+
}
124+
_, siteStatusErr := client.SiteStatus(domain, siteIdInt)
125+
if siteStatusErr != nil {
126+
return nil
127+
}
128+
107129
if diags != nil {
108130
log.Printf("[ERROR] Failed to read simplified redirect rules in category %s for Site ID %s", category, siteID)
109131
return fmt.Errorf("failed to read simplified redirect rules in category %s for Site ID %s\"", category, siteID)

incapsula/resource_site.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ package incapsula
22

33
import (
44
"fmt"
5-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
6-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
7-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
85
"log"
96
"strconv"
107
"strings"
118
"time"
9+
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1213
)
1314

1415
const create_retries = 3
@@ -245,7 +246,7 @@ func resourceSite() *schema.Resource {
245246
Optional: true,
246247
},
247248
"perf_response_cache_response_header_mode": {
248-
Description: "The working mode for caching response headers. Options are `all` and `custom`.",
249+
Description: "The working mode for caching response headers. Options are `all`, `custom` and `disabled`.",
249250
Type: schema.TypeString,
250251
Computed: true,
251252
Optional: true,
@@ -514,7 +515,7 @@ func resourceSiteRead(d *schema.ResourceData, m interface{}) error {
514515
d.Set("hash_salt", maskingResponse.HashSalt)
515516

516517
// Get the performance settings for the site
517-
performanceSettingsResponse, _, err := client.GetPerformanceSettings(d.Id())
518+
performanceSettingsResponse, err := client.GetPerformanceSettings(d.Id())
518519
if err != nil {
519520
log.Printf("[ERROR] Could not read Incapsula site peformance settings for domain: %s and site id: %d, %s\n", domain, siteID, err)
520521
return err

0 commit comments

Comments
 (0)