From f58dd8db9654be9d938b51e13997700643dc3ae5 Mon Sep 17 00:00:00 2001 From: omerma Date: Mon, 9 Dec 2024 20:13:38 +0200 Subject: [PATCH] check mtls tests --- internal/models/web-app-asset/schema.go | 34 +++++++++++++++++++++-- internal/resources/tests/add-mtls_test.go | 2 +- internal/resources/web-app-asset/read.go | 14 +++++----- 3 files changed, 40 insertions(+), 10 deletions(-) diff --git a/internal/models/web-app-asset/schema.go b/internal/models/web-app-asset/schema.go index 97c3aef..002feec 100644 --- a/internal/models/web-app-asset/schema.go +++ b/internal/models/web-app-asset/schema.go @@ -3,7 +3,6 @@ package models import ( "encoding/base64" "fmt" - "mime" ) const ( @@ -56,11 +55,42 @@ type FileSchema struct { Enable bool `json:"enable,omitempty"` } +func fileExtensionToMimeType(extension string) string { + switch extension { + case ".pem": + return "application/x-pem-file" + case ".der", ".cer", ".crt": + return "application/x-x509-ca-cert" + case ".p12", ".pfx": + return "application/x-pkcs12" + case ".p7b", ".p7c": + return "application/x-pkcs7-certificates" + default: + return "application/octet-stream" + } +} + +func MimeTypeToFileExtension(mimeType string) string { + switch mimeType { + case "application/x-pem-file": + return ".pem" + case "application/x-x509-ca-cert": + return ".cer" + case "application/x-pkcs12": + return ".p12" + case "application/x-pkcs7-certificates": + return ".p7b" + default: + return "" + } +} + type FileSchemas []FileSchema func NewFileSchemaEncode(filename, fileData, mTLSType, certificateType string, fileEnable bool) FileSchema { b64Data := base64.StdEncoding.EncodeToString([]byte(fileData)) - data := fmt.Sprintf(FileDataFormat, mime.TypeByExtension(certificateType), b64Data) + data := fmt.Sprintf(FileDataFormat, fileExtensionToMimeType(certificateType), b64Data) + //data := fmt.Sprintf(FileDataFormat, mime.TypeByExtension(certificateType), b64Data) //filenameFmt := fmt.Sprintf(FileDataFilenameFormat, filepath.Base(filename)) return FileSchema{ diff --git a/internal/resources/tests/add-mtls_test.go b/internal/resources/tests/add-mtls_test.go index 153d534..f57e42f 100644 --- a/internal/resources/tests/add-mtls_test.go +++ b/internal/resources/tests/add-mtls_test.go @@ -290,7 +290,7 @@ func TestAccWebApplicationAssetWithmTLSFull(t *testing.T) { "mtls.#": "2", "mtls.0.filename": "newfile.der", - "mtls.0.certificate_type": ".der", + "mtls.0.certificate_type": ".cer", "mtls.0.data": "new cert data", "mtls.0.type": "server", "mtls.0.enable": "true", diff --git a/internal/resources/web-app-asset/read.go b/internal/resources/web-app-asset/read.go index ddb37ae..89f987b 100644 --- a/internal/resources/web-app-asset/read.go +++ b/internal/resources/web-app-asset/read.go @@ -4,7 +4,6 @@ import ( "context" "encoding/base64" "fmt" - "mime" "strings" "github.com/CheckPointSW/terraform-provider-infinity-next/internal/api" @@ -80,7 +79,7 @@ func ReadWebApplicationAssetToResourceData(asset models.WebApplicationAsset, d * } case mtlsClientData, mtlsServerData: var decodedData string - var fileExtensionsByType []string + var fileExtensionsByType string if strings.Contains(proxySetting.Value, "base64,") { b64Data := strings.SplitN(proxySetting.Value, "base64,", 2)[1] bDecodedData, err := base64.StdEncoding.DecodeString(b64Data) @@ -92,16 +91,17 @@ func ReadWebApplicationAssetToResourceData(asset models.WebApplicationAsset, d * mimeType := strings.SplitN(proxySetting.Value, ":", 2)[1] mimeType = strings.SplitN(mimeType, ";", 2)[0] - fileExtensionsByType, err = mime.ExtensionsByType(mimeType) - if err != nil { - return fmt.Errorf("failed to get file extension by type %s: %w", mimeType, err) - } + fileExtensionsByType = models.MimeTypeToFileExtension(mimeType) + //fileExtensionsByType, err = mime.ExtensionsByType(mimeType) + //if err != nil { + // return fmt.Errorf("failed to get file extension by type %s: %w", mimeType, err) + //} } mTLSsSchemaMap[mTLSType] = models.FileSchema{ FilenameID: mTLSsSchemaMap[mTLSType].FilenameID, Filename: mTLSsSchemaMap[mTLSType].Filename, - CertificateType: fileExtensionsByType[0], + CertificateType: fileExtensionsByType, DataID: proxySetting.ID, Data: decodedData, Type: mTLSType,