Skip to content

Commit

Permalink
refactor(filter): add data struct to send to presidio
Browse files Browse the repository at this point in the history
  • Loading branch information
qlonik committed Mar 8, 2024
1 parent f88a049 commit 9167786
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 9167786

Please sign in to comment.