-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: removes PII from stored conversations
feat: removes PII from stored conversations
- Loading branch information
Showing
5 changed files
with
191 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
dlp "cloud.google.com/go/dlp/apiv2" | ||
"cloud.google.com/go/dlp/apiv2/dlppb" | ||
) | ||
|
||
var ( | ||
// See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types. | ||
infoTypeNames = []string{"EMAIL_ADDRESS", "AGE", "FIRST_NAME", "LAST_NAME"} | ||
) | ||
|
||
// deidentify cleans sensitive data by replacing infoType. | ||
// | ||
// Taken from here: https://cloud.google.com/sensitive-data-protection/docs/samples/dlp-deidentify-replace-infotype | ||
func deidentify(projectID, item string) (string, error) { | ||
ctx := context.Background() | ||
|
||
client, err := dlp.NewClient(ctx) | ||
if err != nil { | ||
return "", err | ||
} | ||
defer client.Close() | ||
|
||
input := &dlppb.ContentItem{ | ||
DataItem: &dlppb.ContentItem_Value{ | ||
Value: item, | ||
}, | ||
} | ||
|
||
var infoTypes []*dlppb.InfoType | ||
for _, it := range infoTypeNames { | ||
infoTypes = append(infoTypes, &dlppb.InfoType{Name: it}) | ||
} | ||
|
||
// Associate de-identification type with info type. | ||
transformation := &dlppb.DeidentifyConfig_InfoTypeTransformations{ | ||
InfoTypeTransformations: &dlppb.InfoTypeTransformations{ | ||
Transformations: []*dlppb.InfoTypeTransformations_InfoTypeTransformation{ | ||
{ | ||
PrimitiveTransformation: &dlppb.PrimitiveTransformation{ | ||
Transformation: &dlppb.PrimitiveTransformation_ReplaceWithInfoTypeConfig{}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
req := &dlppb.DeidentifyContentRequest{ | ||
Parent: fmt.Sprintf("projects/%s/locations/global", projectID), | ||
InspectConfig: &dlppb.InspectConfig{ | ||
InfoTypes: infoTypes, | ||
}, | ||
DeidentifyConfig: &dlppb.DeidentifyConfig{ | ||
Transformation: transformation, | ||
}, | ||
Item: input, | ||
} | ||
|
||
resp, err := client.DeidentifyContent(ctx, req) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return resp.GetItem().GetValue(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestDeidentify(t *testing.T) { | ||
projectID := os.Getenv("PROJECT_ID") | ||
firstName := "Artemis" | ||
lastName := "Schmidt" | ||
email := "xanthippe@example.com" | ||
testInput := fmt.Sprintf(` | ||
My name is %s %s and my email is %s. I want to go see the Great Pyramids of Egypt. | ||
`, firstName, lastName, email) | ||
|
||
got, err := deidentify(projectID, testInput) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if strings.Contains(strings.ToLower(got), firstName) || | ||
strings.Contains(strings.ToLower(got), lastName) || | ||
strings.Contains(strings.ToLower(got), email) { | ||
t.Errorf("No deidentification; got %s", got) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters