Skip to content

Commit 4181322

Browse files
Added tests for resource_site_ssl_settings (#623)
* added tests to resource_site_ssl_settings * updated const to be inside the tests and not as global
1 parent e1693d7 commit 4181322

File tree

1 file changed

+187
-106
lines changed

1 file changed

+187
-106
lines changed
Lines changed: 187 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,189 @@
11
package incapsula
22

3-
// TODO Uncomment those tests when we have the possibility to approve site ssl certificate
4-
// TODO SSL settings endpoint will not work if the site does not have configured certificate.
5-
//
6-
//const sslSettingsResourceName = "incapsula_site_ssl_settings"
7-
//
8-
//func TestAccSiteSSLSettings_Basic(t *testing.T) {
9-
// resource.Test(t, resource.TestCase{
10-
// PreCheck: func() { testAccPreCheck(t) },
11-
// Providers: testAccProviders,
12-
// CheckDestroy: testAccSiteCheckIncapsulaSiteSSLSettingsDestroy,
13-
// Steps: []resource.TestStep{
14-
// {
15-
// Config: testSiteCheckIncapsulaIncapSiteSSLSettingsConfigBasic(t),
16-
// Check: resource.ComposeTestCheckFunc(
17-
// testCheckIncapsulaIncapSiteSSLSettingsExists(),
18-
// resource.TestCheckResourceAttr(sslSettingsResourceName, "site_id", "true"),
19-
// resource.TestCheckResourceAttr(sslSettingsResourceName, "hsts", "true"),
20-
// ),
21-
// },
22-
// },
23-
// })
24-
//}
25-
//
26-
//func testAccSiteCheckIncapsulaSiteSSLSettingsDestroy(state *terraform.State) error {
27-
// client := testAccProvider.Meta().(*Client)
28-
//
29-
// for _, res := range state.RootModule().Resources {
30-
// if res.Type != "incapsula_site_ssl_settings" {
31-
// continue
32-
// }
33-
//
34-
// ruleID, err := strconv.Atoi(res.Primary.ID)
35-
// if err != nil {
36-
// return fmt.Errorf("error parsing ID %v to int", res.Primary.ID)
37-
// }
38-
//
39-
// siteID, ok := res.Primary.Attributes["site_id"]
40-
// if !ok {
41-
// return fmt.Errorf("incapsula Site ID does not exist for SSL settings")
42-
// }
43-
//
44-
// var siteIDToInt, _ = strconv.Atoi(siteID)
45-
//
46-
// _, statusCode, err := client.ReadSiteSSLSettings(siteIDToInt)
47-
// if statusCode != 404 {
48-
// return fmt.Errorf("incapsula Incap Site ssl settings %d (site id: %s) should have received 404 status code", ruleID, siteID)
49-
// }
50-
// if err == nil {
51-
// return fmt.Errorf("incapsula Incap Site ssl settings still exists for Site ID %s", siteID)
52-
// }
53-
// }
54-
//
55-
// return nil
56-
//}
57-
//
58-
//func testSiteCheckIncapsulaIncapSiteSSLSettingsConfigBasic(t *testing.T) string {
59-
// return fmt.Sprintf(`
60-
// resource "incapsula_site" "testacc-terraform-site" {
61-
// domain = "%s"
62-
// force_ssl = "true"
63-
// domain_validation = "dns"
64-
// }
65-
//
66-
// resource "incapsula_site_ssl_settings" "incapsula_site_ssl_settings" {
67-
// site_id = "${incapsula_site.testacc-terraform-site.id}"
68-
// hsts {
69-
// is_enabled = false
70-
// max_age = 31536000
71-
// sub_domains_included = false
72-
// pre_loaded = false
73-
// }
74-
// }
75-
//`, GenerateTestDomain(t))
76-
//}
77-
//
78-
//func testCheckIncapsulaIncapSiteSSLSettingsExists() resource.TestCheckFunc {
79-
// return func(state *terraform.State) error {
80-
// res, ok := state.RootModule().Resources[sslSettingsResourceName]
81-
// if !ok {
82-
// return fmt.Errorf("incapsula Site SSL settings resource not found")
83-
// }
84-
//
85-
// ruleID, err := strconv.Atoi(res.Primary.ID)
86-
// if err != nil {
87-
// return fmt.Errorf("error parsing ID %v to int", res.Primary.ID)
88-
// }
89-
//
90-
// siteID, ok := res.Primary.Attributes["site_id"]
91-
// if !ok || siteID == "" {
92-
// return fmt.Errorf("incapsula Site ID does not exist for ssl settings %d", ruleID)
93-
// }
94-
//
95-
// var siteIDToInt, _ = strconv.Atoi(siteID);
96-
//
97-
// client := testAccProvider.Meta().(*Client)
98-
// _, statusCode, err := client.ReadSiteSSLSettings(siteIDToInt)
99-
// if statusCode != 200 {
100-
// return fmt.Errorf("incapsula site ssl settings (site id: %s) should have received 200 status code", siteID)
101-
// }
102-
// if err != nil {
103-
// return fmt.Errorf("incapsula site ssl settings (site id: %s) does not exist", siteID)
104-
// }
105-
//
106-
// return nil
107-
// }
108-
//}
3+
import (
4+
"fmt"
5+
"strconv"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
10+
)
11+
12+
func TestAccSiteSSLSettings_Basic(t *testing.T) {
13+
const sslSettingsFullResourceName = "incapsula_site_ssl_settings.testacc-terraform-site-ssl-settings"
14+
domainName = GenerateTestDomain(t)
15+
cert, pkey := generateKeyPairBase64(domainName)
16+
resource.Test(t, resource.TestCase{
17+
PreCheck: func() { testAccPreCheck(t) },
18+
Providers: testAccProviders,
19+
CheckDestroy: testAccSiteCheckIncapsulaSiteSSLSettingsDestroy,
20+
Steps: []resource.TestStep{
21+
{
22+
Config: testSSLSettingsConfig(domainName, cert, pkey, 31536000, true, true, true),
23+
Check: resource.ComposeTestCheckFunc(
24+
testCheckIncapsulaIncapSiteSSLSettingsExists(sslSettingsFullResourceName),
25+
resource.TestCheckResourceAttr(sslSettingsFullResourceName, "hsts.0.is_enabled", "true"),
26+
resource.TestCheckResourceAttr(sslSettingsFullResourceName, "hsts.0.max_age", "31536000"),
27+
resource.TestCheckResourceAttr(sslSettingsFullResourceName, "hsts.0.sub_domains_included", "true"),
28+
resource.TestCheckResourceAttr(sslSettingsFullResourceName, "hsts.0.pre_loaded", "true"),
29+
resource.TestCheckResourceAttr(sslSettingsFullResourceName, "inbound_tls_settings.0.configuration_profile", "CUSTOM"),
30+
resource.TestCheckResourceAttr(sslSettingsFullResourceName, "inbound_tls_settings.0.tls_configuration.0.tls_version", "TLS_1_3"),
31+
resource.TestCheckResourceAttr(sslSettingsFullResourceName, "inbound_tls_settings.0.tls_configuration.0.ciphers_support.0", "TLS_AES_128_GCM_SHA256"),
32+
resource.TestCheckResourceAttr(sslSettingsFullResourceName, "inbound_tls_settings.0.tls_configuration.0.ciphers_support.1", "TLS_AES_256_GCM_SHA384"),
33+
resource.TestCheckResourceAttr(sslSettingsFullResourceName, "disable_pqc_support", "true"),
34+
),
35+
},
36+
},
37+
})
38+
}
39+
40+
func TestAccSiteSSLSettings_Update(t *testing.T) {
41+
const sslSettingsFullResourceName = "incapsula_site_ssl_settings.testacc-terraform-site-ssl-settings"
42+
domainName = GenerateTestDomain(t)
43+
cert, pkey := generateKeyPairBase64(domainName)
44+
resource.Test(t, resource.TestCase{
45+
PreCheck: func() { testAccPreCheck(t) },
46+
Providers: testAccProviders,
47+
CheckDestroy: testAccSiteCheckIncapsulaSiteSSLSettingsDestroy,
48+
Steps: []resource.TestStep{
49+
{
50+
Config: testSSLSettingsConfig(domainName, cert, pkey, 31536000, true, true, true),
51+
Check: resource.ComposeTestCheckFunc(
52+
testCheckIncapsulaIncapSiteSSLSettingsExists(sslSettingsFullResourceName),
53+
resource.TestCheckResourceAttr(sslSettingsFullResourceName, "hsts.0.max_age", "31536000"),
54+
resource.TestCheckResourceAttr(sslSettingsFullResourceName, "hsts.0.sub_domains_included", "true"),
55+
resource.TestCheckResourceAttr(sslSettingsFullResourceName, "hsts.0.pre_loaded", "true"),
56+
resource.TestCheckResourceAttr(sslSettingsFullResourceName, "disable_pqc_support", "true"),
57+
),
58+
},
59+
{
60+
Config: testSSLSettingsConfig(domainName, cert, pkey, 86400, false, false, false),
61+
Check: resource.ComposeTestCheckFunc(
62+
testCheckIncapsulaIncapSiteSSLSettingsExists(sslSettingsFullResourceName),
63+
resource.TestCheckResourceAttr(sslSettingsFullResourceName, "hsts.0.max_age", "86400"),
64+
resource.TestCheckResourceAttr(sslSettingsFullResourceName, "hsts.0.sub_domains_included", "false"),
65+
resource.TestCheckResourceAttr(sslSettingsFullResourceName, "hsts.0.pre_loaded", "false"),
66+
resource.TestCheckResourceAttr(sslSettingsFullResourceName, "disable_pqc_support", "false"),
67+
),
68+
},
69+
},
70+
})
71+
}
72+
73+
func testAccSiteCheckIncapsulaSiteSSLSettingsDestroy(state *terraform.State) error {
74+
client := testAccProvider.Meta().(*Client)
75+
76+
for _, res := range state.RootModule().Resources {
77+
if res.Type != "incapsula_site_ssl_settings" {
78+
continue
79+
}
80+
81+
siteID, ok := res.Primary.Attributes["site_id"]
82+
if !ok || siteID == "" {
83+
return fmt.Errorf("incapsula Site ID does not exist for SSL settings")
84+
}
85+
86+
accountID, ok := res.Primary.Attributes["account_id"]
87+
if !ok || accountID == "" {
88+
return fmt.Errorf("incapsula Account ID does not exist for ssl settings site id: %s", siteID)
89+
}
90+
91+
siteIDToInt, err := strconv.Atoi(siteID)
92+
if err != nil {
93+
return fmt.Errorf("failed to parse site_id %s: %v", siteID, err)
94+
}
95+
accountIDToInt, err := strconv.Atoi(accountID)
96+
if err != nil {
97+
return fmt.Errorf("failed to parse account_id %s: %v", accountID, err)
98+
}
99+
100+
_, statusCode, err := client.ReadSiteSSLSettings(siteIDToInt, accountIDToInt)
101+
if statusCode != 404 {
102+
return fmt.Errorf("incapsula Incap Site ssl settings (site id: %s) should have received 404 status code", siteID)
103+
}
104+
if err == nil {
105+
return fmt.Errorf("incapsula Incap Site ssl settings still exists for Site ID %s", siteID)
106+
}
107+
}
108+
109+
return nil
110+
}
111+
112+
func testCheckIncapsulaIncapSiteSSLSettingsExists(resourceName string) resource.TestCheckFunc {
113+
return func(state *terraform.State) error {
114+
res, ok := state.RootModule().Resources[resourceName]
115+
if !ok {
116+
return fmt.Errorf("incapsula Site SSL settings resource not found")
117+
}
118+
119+
siteID, ok := res.Primary.Attributes["site_id"]
120+
if !ok || siteID == "" {
121+
return fmt.Errorf("incapsula Site ID does not exist for ssl settings")
122+
}
123+
124+
accountID, ok := res.Primary.Attributes["account_id"]
125+
if !ok || accountID == "" {
126+
return fmt.Errorf("incapsula Account ID does not exist for ssl settings, site id: %s", siteID)
127+
}
128+
129+
siteIDToInt, err := strconv.Atoi(siteID)
130+
if err != nil {
131+
return fmt.Errorf("failed to parse site_id %s: %v", siteID, err)
132+
}
133+
accountIDToInt, err := strconv.Atoi(accountID)
134+
if err != nil {
135+
return fmt.Errorf("failed to parse account_id %s: %v", accountID, err)
136+
}
137+
138+
client := testAccProvider.Meta().(*Client)
139+
_, statusCode, err := client.ReadSiteSSLSettings(siteIDToInt, accountIDToInt)
140+
if statusCode != 200 {
141+
return fmt.Errorf("incapsula site ssl settings (site id: %s) should have received 200 status code", siteID)
142+
}
143+
if err != nil {
144+
return fmt.Errorf("incapsula site ssl settings (site id: %s) does not exist", siteID)
145+
}
146+
147+
return nil
148+
}
149+
}
150+
151+
// NOTE: SSL settings endpoint requires the site to have SSL configured with a custom certificate.
152+
// This test uploads a self-signed certificate before configuring SSL settings.
153+
func testSSLSettingsConfig(domain, cert, pkey string, maxAge int, subDomainsIncluded, preLoaded, disablePQC bool) string {
154+
return fmt.Sprintf(`
155+
resource "incapsula_site" "testacc-terraform-site" {
156+
domain = "%s"
157+
}
158+
159+
resource "incapsula_custom_certificate" "ssl-settings-test-certificate" {
160+
site_id = incapsula_site.testacc-terraform-site.id
161+
certificate = "%s"
162+
private_key = "%s"
163+
depends_on = ["incapsula_site.testacc-terraform-site"]
164+
}
165+
166+
resource "incapsula_site_ssl_settings" "testacc-terraform-site-ssl-settings" {
167+
site_id = incapsula_site.testacc-terraform-site.id
168+
account_id = incapsula_site.testacc-terraform-site.account_id
169+
hsts {
170+
is_enabled = true
171+
max_age = %d
172+
sub_domains_included = %t
173+
pre_loaded = %t
174+
}
175+
inbound_tls_settings {
176+
configuration_profile = "CUSTOM"
177+
tls_configuration {
178+
tls_version = "TLS_1_3"
179+
ciphers_support = [
180+
"TLS_AES_128_GCM_SHA256",
181+
"TLS_AES_256_GCM_SHA384"
182+
]
183+
}
184+
}
185+
disable_pqc_support = %t
186+
depends_on = ["incapsula_custom_certificate.ssl-settings-test-certificate"]
187+
}
188+
`, domain, cert, pkey, maxAge, subDomainsIncluded, preLoaded, disablePQC)
189+
}

0 commit comments

Comments
 (0)