Skip to content

Commit

Permalink
check tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chkp-omerma committed Dec 9, 2024
1 parent ceb8d66 commit 0c7f90d
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 125 deletions.
230 changes: 115 additions & 115 deletions internal/resources/tests/add-mtls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,121 +8,121 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccWebApplicationAssetWithmTLSBasic(t *testing.T) {
assetNameAttribute := acctest.GenerateResourceName()
profileNameAttribute := acctest.GenerateResourceName()
trustedSourcesNameAttribute := acctest.GenerateResourceName()
practiceNameAttribute := acctest.GenerateResourceName()
logTriggerNameAttribute := acctest.GenerateResourceName()
exceptionsNameAttribute := acctest.GenerateResourceName()
assetResourceName := "inext_web_app_asset." + assetNameAttribute
profileResourceName := "inext_appsec_gateway_profile." + profileNameAttribute
trustedSourcesResourceName := "inext_trusted_sources." + trustedSourcesNameAttribute
practiceResourceName := "inext_web_app_practice." + practiceNameAttribute
logTriggerResourceName := "inext_log_trigger." + logTriggerNameAttribute
exceptionsResourceName := "inext_exceptions." + exceptionsNameAttribute
resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ProviderFactories: acctest.ProviderFactories,
CheckDestroy: acctest.CheckResourceDestroyed([]string{assetResourceName, profileResourceName, trustedSourcesResourceName,
practiceResourceName, logTriggerResourceName, exceptionsResourceName}),
Steps: []resource.TestStep{
{
Config: webApplicationAssetmTLSBasicConfig(assetNameAttribute),
Check: resource.ComposeTestCheckFunc(
append(acctest.ComposeTestCheckResourceAttrsFromMap(assetResourceName, map[string]string{
"name": assetNameAttribute,
"urls.0": fmt.Sprintf("http://host/%s/path1", assetNameAttribute),
"urls.#": "1",
"%": "25",
"urls_ids.#": "1",
"main_attributes": fmt.Sprintf("{\"applicationUrls\":\"http://host/%s/path1\"}", assetNameAttribute),
}),
resource.TestCheckResourceAttrSet(assetResourceName, "id"),
)...,
),
ExpectNonEmptyPlan: true,
},
{
ResourceName: assetResourceName,
ImportState: true,
},
{
Config: webApplicationAssetUpdatemTLSBasicConfig(assetNameAttribute, profileNameAttribute, trustedSourcesNameAttribute,
practiceNameAttribute, logTriggerNameAttribute, exceptionsNameAttribute),
Check: resource.ComposeTestCheckFunc(
append(acctest.ComposeTestCheckResourceAttrsFromMap(assetResourceName, map[string]string{
"name": assetNameAttribute,
"%": "25", // was 24
"read_only": "false",
"upstream_url": "some url 5",
"urls.#": "2",
"urls_ids.#": "2",
"profiles.#": "1",
"practice.#": "1",
"practice.0.%": "5",
"practice.0.triggers.#": "1",
"practice.0.sub_practices_modes.IPS": "AccordingToPractice",
"practice.0.sub_practices_modes.WebBot": "AccordingToPractice",
"practice.0.sub_practices_modes.Snort": "Disabled",
"practice.0.main_mode": "Prevent",
// "practice.0.exceptions.#": "1",
"source_identifier.0.%": "4",
"source_identifier.1.%": "4",
"source_identifier.2.%": "4",
"source_identifier.2.values.#": "1",
"source_identifier.#": "3",
"source_identifier.2.values_ids.#": "1",
"source_identifier.1.values_ids.#": "1",
"source_identifier.1.values.#": "1",
"source_identifier.0.values.#": "1",
"source_identifier.0.values_ids.#": "1",
"proxy_setting.#": "3", //was 3
"proxy_setting.0.%": "3",
"proxy_setting.1.%": "3",
"proxy_setting.2.%": "3",
//"trusted_sources.#": "1",
"class": "workload",
"category": "cloud",
"group": "",
"order": "",
"kind": "",
"family": "Web Application",
"main_attributes": fmt.Sprintf("{\"applicationUrls\":\"http://host/%[1]s/path2;http://host/%[1]s/path3\"}", assetNameAttribute),
"asset_type": "WebApplication",
"intelligence_tags": "",
"tags.#": "1",
"tags.0.key": "tagkey1",
"tags.0.value": "tagvalue1",

"mtls.#": "1",
"mtls.0.filename": "cert.pem",
"mtls.0.data": "data:application/octet-stream;base64,Y2VydCBkYXRh",
"mtls.0.type": "client",
"mtls.0.enable": "true",
}),
resource.TestCheckResourceAttrSet(assetResourceName, "id"),
resource.TestCheckResourceAttrSet(assetResourceName, "practice.0.id"),
resource.TestCheckResourceAttrSet(assetResourceName, "source_identifier.1.id"),
resource.TestCheckResourceAttrSet(assetResourceName, "source_identifier.2.id"),
resource.TestCheckResourceAttrSet(assetResourceName, "source_identifier.0.id"),
resource.TestCheckResourceAttrSet(assetResourceName, "proxy_setting.0.id"),
resource.TestCheckResourceAttrSet(assetResourceName, "proxy_setting.1.id"),
resource.TestCheckResourceAttrSet(assetResourceName, "proxy_setting.2.id"),
resource.TestCheckResourceAttrSet(assetResourceName, "id"),
resource.TestCheckTypeSetElemAttr(assetResourceName, "urls.*", fmt.Sprintf("http://host/%s/path2", assetNameAttribute)),
resource.TestCheckTypeSetElemAttr(assetResourceName, "urls.*", fmt.Sprintf("http://host/%s/path3", assetNameAttribute)),
resource.TestCheckResourceAttrSet(assetResourceName, "tags.0.id"),
resource.TestCheckResourceAttrSet(assetResourceName, "mtls.0.filename_id"),
resource.TestCheckResourceAttrSet(assetResourceName, "mtls.0.data_id"),
resource.TestCheckResourceAttrSet(assetResourceName, "mtls.0.enable_id"),
)...,
),
ExpectNonEmptyPlan: true,
},
},
})
}
//func TestAccWebApplicationAssetWithmTLSBasic(t *testing.T) {
// assetNameAttribute := acctest.GenerateResourceName()
// profileNameAttribute := acctest.GenerateResourceName()
// trustedSourcesNameAttribute := acctest.GenerateResourceName()
// practiceNameAttribute := acctest.GenerateResourceName()
// logTriggerNameAttribute := acctest.GenerateResourceName()
// exceptionsNameAttribute := acctest.GenerateResourceName()
// assetResourceName := "inext_web_app_asset." + assetNameAttribute
// profileResourceName := "inext_appsec_gateway_profile." + profileNameAttribute
// trustedSourcesResourceName := "inext_trusted_sources." + trustedSourcesNameAttribute
// practiceResourceName := "inext_web_app_practice." + practiceNameAttribute
// logTriggerResourceName := "inext_log_trigger." + logTriggerNameAttribute
// exceptionsResourceName := "inext_exceptions." + exceptionsNameAttribute
// resource.Test(t, resource.TestCase{
// PreCheck: func() { acctest.PreCheck(t) },
// ProviderFactories: acctest.ProviderFactories,
// CheckDestroy: acctest.CheckResourceDestroyed([]string{assetResourceName, profileResourceName, trustedSourcesResourceName,
// practiceResourceName, logTriggerResourceName, exceptionsResourceName}),
// Steps: []resource.TestStep{
// {
// Config: webApplicationAssetmTLSBasicConfig(assetNameAttribute),
// Check: resource.ComposeTestCheckFunc(
// append(acctest.ComposeTestCheckResourceAttrsFromMap(assetResourceName, map[string]string{
// "name": assetNameAttribute,
// "urls.0": fmt.Sprintf("http://host/%s/path1", assetNameAttribute),
// "urls.#": "1",
// "%": "25",
// "urls_ids.#": "1",
// "main_attributes": fmt.Sprintf("{\"applicationUrls\":\"http://host/%s/path1\"}", assetNameAttribute),
// }),
// resource.TestCheckResourceAttrSet(assetResourceName, "id"),
// )...,
// ),
// ExpectNonEmptyPlan: true,
// },
// {
// ResourceName: assetResourceName,
// ImportState: true,
// },
// {
// Config: webApplicationAssetUpdatemTLSBasicConfig(assetNameAttribute, profileNameAttribute, trustedSourcesNameAttribute,
// practiceNameAttribute, logTriggerNameAttribute, exceptionsNameAttribute),
// Check: resource.ComposeTestCheckFunc(
// append(acctest.ComposeTestCheckResourceAttrsFromMap(assetResourceName, map[string]string{
// "name": assetNameAttribute,
// "%": "25", // was 24
// "read_only": "false",
// "upstream_url": "some url 5",
// "urls.#": "2",
// "urls_ids.#": "2",
// "profiles.#": "1",
// "practice.#": "1",
// "practice.0.%": "5",
// "practice.0.triggers.#": "1",
// "practice.0.sub_practices_modes.IPS": "AccordingToPractice",
// "practice.0.sub_practices_modes.WebBot": "AccordingToPractice",
// "practice.0.sub_practices_modes.Snort": "Disabled",
// "practice.0.main_mode": "Prevent",
// // "practice.0.exceptions.#": "1",
// "source_identifier.0.%": "4",
// "source_identifier.1.%": "4",
// "source_identifier.2.%": "4",
// "source_identifier.2.values.#": "1",
// "source_identifier.#": "3",
// "source_identifier.2.values_ids.#": "1",
// "source_identifier.1.values_ids.#": "1",
// "source_identifier.1.values.#": "1",
// "source_identifier.0.values.#": "1",
// "source_identifier.0.values_ids.#": "1",
// "proxy_setting.#": "3", //was 3
// "proxy_setting.0.%": "3",
// "proxy_setting.1.%": "3",
// "proxy_setting.2.%": "3",
// //"trusted_sources.#": "1",
// "class": "workload",
// "category": "cloud",
// "group": "",
// "order": "",
// "kind": "",
// "family": "Web Application",
// "main_attributes": fmt.Sprintf("{\"applicationUrls\":\"http://host/%[1]s/path2;http://host/%[1]s/path3\"}", assetNameAttribute),
// "asset_type": "WebApplication",
// "intelligence_tags": "",
// "tags.#": "1",
// "tags.0.key": "tagkey1",
// "tags.0.value": "tagvalue1",
//
// "mtls.#": "1",
// "mtls.0.filename": "cert.pem",
// "mtls.0.data": "data:application/octet-stream;base64,Y2VydCBkYXRh",
// "mtls.0.type": "client",
// "mtls.0.enable": "true",
// }),
// resource.TestCheckResourceAttrSet(assetResourceName, "id"),
// resource.TestCheckResourceAttrSet(assetResourceName, "practice.0.id"),
// resource.TestCheckResourceAttrSet(assetResourceName, "source_identifier.1.id"),
// resource.TestCheckResourceAttrSet(assetResourceName, "source_identifier.2.id"),
// resource.TestCheckResourceAttrSet(assetResourceName, "source_identifier.0.id"),
// resource.TestCheckResourceAttrSet(assetResourceName, "proxy_setting.0.id"),
// resource.TestCheckResourceAttrSet(assetResourceName, "proxy_setting.1.id"),
// resource.TestCheckResourceAttrSet(assetResourceName, "proxy_setting.2.id"),
// resource.TestCheckResourceAttrSet(assetResourceName, "id"),
// resource.TestCheckTypeSetElemAttr(assetResourceName, "urls.*", fmt.Sprintf("http://host/%s/path2", assetNameAttribute)),
// resource.TestCheckTypeSetElemAttr(assetResourceName, "urls.*", fmt.Sprintf("http://host/%s/path3", assetNameAttribute)),
// resource.TestCheckResourceAttrSet(assetResourceName, "tags.0.id"),
// resource.TestCheckResourceAttrSet(assetResourceName, "mtls.0.filename_id"),
// resource.TestCheckResourceAttrSet(assetResourceName, "mtls.0.data_id"),
// resource.TestCheckResourceAttrSet(assetResourceName, "mtls.0.enable_id"),
// )...,
// ),
// ExpectNonEmptyPlan: true,
// },
// },
// })
//}

func TestAccWebApplicationAssetWithmTLSFull(t *testing.T) {
assetNameAttribute := acctest.GenerateResourceName()
Expand Down
51 changes: 41 additions & 10 deletions internal/resources/web-app-asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package resources

import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"

"github.com/CheckPointSW/terraform-provider-infinity-next/internal/api"
Expand All @@ -11,9 +12,27 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

const (
mTLSServer = "server"
mTLSClient = "client"

mTLSFileTypePEM = ".pem"
mTLSFileTypeCRT = ".crt"
mTLSFileTypeDER = ".der"
mTLSFileTypeP12 = ".p12"
mTLSFileTypePFX = ".pfx"
mTLSFileTypeP7B = ".p7b"
mTLSFileTypeP7C = ".p7c"
mTLSFileTypeCER = ".cer"
)

func ResourceWebAppAsset() *schema.Resource {
validateStateFunc := validation.ToDiagFunc(validation.StringInSlice(
[]string{suggestedState, activeState, headerKey, inactiveState}, false))
mTLSTypeValidation := validation.ToDiagFunc(validation.StringInSlice(
[]string{mTLSServer, mTLSClient}, false))
mTLSFileTypeValidation := validation.ToDiagFunc(validation.StringInSlice(
[]string{mTLSFileTypePEM, mTLSFileTypeCRT, mTLSFileTypeDER, mTLSFileTypeP12, mTLSFileTypePFX, mTLSFileTypeP7B, mTLSFileTypeP7C, mTLSFileTypeCER}, false))
return &schema.Resource{
Description: "Web Application Asset",

Expand Down Expand Up @@ -267,29 +286,41 @@ func ResourceWebAppAsset() *schema.Resource {
Computed: true,
},
"filename": {
Type: schema.TypeString,
Optional: true,
Description: "The name of the certificate file",
Type: schema.TypeString,
Optional: true,
},
"filetype": {
Description: "The type of the certificate file - .pem, .crt, .der, .p12, .pfx, .p7b, .p7c, .cer",
Type: schema.TypeString,
Optional: true,
ValidateDiagFunc: mTLSFileTypeValidation,
},
"data_id": {
Type: schema.TypeString,
Computed: true,
},
"data": {
Type: schema.TypeString,
Sensitive: true,
Optional: true,
Description: "The certificate data",
Type: schema.TypeString,
Sensitive: true,
Optional: true,
},
"type": {
Type: schema.TypeString,
Required: true,
Description: "The type of the mTLS - server or client",
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: mTLSTypeValidation,
},
"enable_id": {
Type: schema.TypeString,
Computed: true,
},
"enable": {
Type: schema.TypeBool,
Optional: true,
Description: "Whether the mTLS is enabled",
Type: schema.TypeBool,
Optional: true,
Default: false,
},
},
},
Expand Down Expand Up @@ -319,7 +350,7 @@ func resourceWebAppAssetCreate(ctx context.Context, d *schema.ResourceData, meta
return utils.DiagError("unable to perform WebAppAsset Create", err, diags)
}

//fmt.Printf("created asset: %v\n", asset)
fmt.Printf("created asset: %v\n", asset)

isValid, err := c.PublishChanges()
if err != nil || !isValid {
Expand Down

0 comments on commit 0c7f90d

Please sign in to comment.