/
main.go
99 lines (77 loc) · 2.12 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"context"
"fmt"
"log"
dlp "cloud.google.com/go/dlp/apiv2"
"github.com/grokify/mogo/fmt/fmtutil"
"github.com/jessevdk/go-flags"
"google.golang.org/api/option"
//dlp "cloud.google.com/go/dlp/v2"
gu "github.com/grokify/goauth/google"
dlpu "github.com/grokify/googleutil/dlp/v2"
//dlp "google.golang.org/api/dlp/v2"
dlppb "google.golang.org/genproto/googleapis/privacy/dlp/v2"
)
/*
This is based on the following Quickstart
https://github.com/GoogleCloudPlatform/golang-samples/blob/master/dlp/dlp_quickstart/quickstart.go
*/
type Args struct {
// Service Account Credentials File
CredentialsFile string `short:"c" long:"credentials" description:"Path to crdentials file." required:"true"`
}
func main() {
args := Args{}
_, err := flags.Parse(&args)
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
opts := option.WithCredentialsFile(args.CredentialsFile)
client, err := dlp.NewClient(ctx, opts)
if err != nil {
log.Fatal(err)
}
credsContainer, err := gu.CredentialsContainerFromFile(args.CredentialsFile)
if err != nil {
log.Fatal(err)
}
creds := credsContainer.Credentials()
projectID := creds.ProjectID
input := "Hello World 680-26-5240"
inspectConfig := &dlppb.InspectConfig{
InfoTypes: []*dlppb.InfoType{
{
Name: dlpu.InfoTypeAllBasic,
},
},
MinLikelihood: dlppb.Likelihood_POSSIBLE,
Limits: &dlppb.InspectConfig_FindingLimits{
MaxFindingsPerRequest: int32(0),
},
IncludeQuote: true}
req := &dlppb.InspectContentRequest{
Parent: "projects/" + projectID,
InspectConfig: inspectConfig,
Item: dlpu.NewContentDataItemSimple(input)}
fmtutil.PrintJSON(req)
// Run request.
resp, err := client.InspectContent(ctx, req)
if err != nil {
log.Fatal(err)
}
findings := resp.GetResult().GetFindings()
if len(findings) == 0 {
fmt.Println("No findings.")
}
fmt.Println("Findings:")
for _, f := range findings {
if inspectConfig.IncludeQuote {
fmt.Println("\tQuote: ", f.GetQuote())
}
fmt.Println("\tInfo type: ", f.GetInfoType().GetName())
fmt.Println("\tLikelihood: ", f.GetLikelihood())
}
fmt.Println("DONE")
}