Skip to content

Commit

Permalink
check mtls tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chkp-omerma committed Dec 9, 2024
1 parent eea7fe4 commit f58dd8d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
34 changes: 32 additions & 2 deletions internal/models/web-app-asset/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package models
import (
"encoding/base64"
"fmt"
"mime"
)

const (
Expand Down Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion internal/resources/tests/add-mtls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
14 changes: 7 additions & 7 deletions internal/resources/web-app-asset/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/base64"
"fmt"
"mime"
"strings"

"github.com/CheckPointSW/terraform-provider-infinity-next/internal/api"
Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand Down

0 comments on commit f58dd8d

Please sign in to comment.