From 91677865b4085ddc1daa0992d2956d78fa30a180 Mon Sep 17 00:00:00 2001 From: Nikita Volodin Date: Thu, 7 Mar 2024 23:01:19 -0500 Subject: [PATCH] refactor(filter): add data struct to send to presidio --- .../internal/common/packet_processing.go | 18 +++++++----- .../internal/common/pii_analysis.go | 29 +++++++++++-------- 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/privacy-profile-composer/pkg/envoyfilter/internal/common/packet_processing.go b/privacy-profile-composer/pkg/envoyfilter/internal/common/packet_processing.go index 8df328b3..f084b309 100644 --- a/privacy-profile-composer/pkg/envoyfilter/internal/common/packet_processing.go +++ b/privacy-profile-composer/pkg/envoyfilter/internal/common/packet_processing.go @@ -127,26 +127,28 @@ func GetDirection(callbacks api.FilterCallbackHandler) (SidecarDirection, error) "check the Envoy docs for the range of values for this key", directionInt) } -func GetJSONBody(headerMetadata HeaderMetadata, body string) ([]byte, error) { +func GetJSONBody(headerMetadata HeaderMetadata, body string) (interface{}, error) { if headerMetadata.ContentType == nil { return nil, fmt.Errorf("cannot analyze body, since 'ContentType' header is not set") } switch *headerMetadata.ContentType { case "application/json": - return []byte(body), nil - case "application/x-www-form-urlencoded": - query, err := url.ParseQuery(body) + var data interface{} + + err := json.Unmarshal([]byte(body), data) if err != nil { - return nil, fmt.Errorf("failed to decode urlencoded form: %w", err) + return nil, fmt.Errorf("failed to unmarshal json body: %w", err) } - jsonBody, err := json.Marshal(query) + return data, nil + case "application/x-www-form-urlencoded": + query, err := url.ParseQuery(body) if err != nil { - return nil, fmt.Errorf("could not marshall decoded urlencoded form into json: %w", err) + return nil, fmt.Errorf("failed to decode urlencoded form: %w", err) } - return jsonBody, nil + return query, nil default: return nil, fmt.Errorf("cannot analyze a body with ContentType '%s'", *headerMetadata.ContentType) } diff --git a/privacy-profile-composer/pkg/envoyfilter/internal/common/pii_analysis.go b/privacy-profile-composer/pkg/envoyfilter/internal/common/pii_analysis.go index 02911e39..26024f3a 100644 --- a/privacy-profile-composer/pkg/envoyfilter/internal/common/pii_analysis.go +++ b/privacy-profile-composer/pkg/envoyfilter/internal/common/pii_analysis.go @@ -5,25 +5,30 @@ import ( "encoding/json" "fmt" "io" + "log" "net/http" ) -func PiiAnalysis(presidioSvcURL string, svcName string, bufferBytes []byte) (string, error) { - svcNameBuf, err := json.Marshal(svcName) +type PresidioDataFormat struct { + JsonToAnalyze interface{} `json:"json_to_analyze"` + DerivePurpose string `json:"derive_purpose,omitempty"` +} + +func PiiAnalysis(presidioSvcURL string, svcName string, bufferBytes interface{}) (string, error) { + msgString, err := json.Marshal( + PresidioDataFormat{ + JsonToAnalyze: bufferBytes, + DerivePurpose: svcName, + }, + ) if err != nil { - return "", fmt.Errorf("could not marshal service name string into a valid JSON string: %w", err) + return "", fmt.Errorf("could not convert data for presidio into json: %w", err) } - msgString := fmt.Sprintf( - `{ - "json_to_analyze": %s, - "derive_purpose": %s - }`, - bufferBytes, - svcNameBuf, - ) + log.Println("about to send this object to presidio:") + log.Println(msgString) - resp, err := http.Post(presidioSvcURL, "application/json", bytes.NewBufferString(msgString)) + resp, err := http.Post(presidioSvcURL, "application/json", bytes.NewBuffer(msgString)) if err != nil { return "", fmt.Errorf("presidio post error: %w", err) }