Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(filter): add data struct to send to presidio #98

Merged
merged 1 commit into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -8,22 +8,23 @@ import (
"net/http"
)

func PiiAnalysis(presidioSvcURL string, svcName string, bufferBytes []byte) (string, error) {
svcNameBuf, err := json.Marshal(svcName)
if err != nil {
return "", fmt.Errorf("could not marshal service name string into a valid JSON string: %w", err)
}
type PresidioDataFormat struct {
JsonToAnalyze interface{} `json:"json_to_analyze"`
DerivePurpose string `json:"derive_purpose,omitempty"`
}

msgString := fmt.Sprintf(
`{
"json_to_analyze": %s,
"derive_purpose": %s
}`,
bufferBytes,
svcNameBuf,
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 convert data for presidio into json: %w", err)
}

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
Loading