public static InspectContentResponse Inspect(string projectId, string textToInspect, string customHotword)
    {
        var dlp = DlpServiceClient.Create();

        var byteContentItem = new ByteContentItem
        {
            Type = ByteContentItem.Types.BytesType.TextUtf8,
            Data = Google.Protobuf.ByteString.CopyFromUtf8(textToInspect)
        };

        var contentItem = new ContentItem
        {
            ByteItem = byteContentItem
        };

        var hotwordRule = new DetectionRule.Types.HotwordRule
        {
            HotwordRegex = new Regex {
                Pattern = customHotword
            },
            Proximity = new DetectionRule.Types.Proximity {
                WindowBefore = 50
            },
            LikelihoodAdjustment = new DetectionRule.Types.LikelihoodAdjustment {
                FixedLikelihood = Likelihood.VeryLikely
            }
        };

        var infoType = new InfoType {
            Name = "PERSON_NAME"
        };

        var inspectionRuleSet = new InspectionRuleSet
        {
            InfoTypes = { infoType },
            Rules     = { new InspectionRule {
                              HotwordRule = hotwordRule
                          } }
        };

        var inspectConfig = new InspectConfig
        {
            InfoTypes     = { infoType },
            IncludeQuote  = true,
            RuleSet       = { inspectionRuleSet },
            MinLikelihood = Likelihood.VeryLikely
        };

        var request = new InspectContentRequest
        {
            Parent        = new LocationName(projectId, "global").ToString(),
            Item          = contentItem,
            InspectConfig = inspectConfig
        };

        var response = dlp.InspectContent(request);

        Console.WriteLine($"Findings: {response.Result.Findings.Count}");
        foreach (var f in response.Result.Findings)
        {
            Console.WriteLine("\tQuote: " + f.Quote);
            Console.WriteLine("\tInfo type: " + f.InfoType.Name);
            Console.WriteLine("\tLikelihood: " + f.Likelihood);
        }

        return(response);
    }
Example #2
0
    public static DeidentifyContentResponse Deidentify(string projectId, string text)
    {
        // Instantiate a client.
        var dlp = DlpServiceClient.Create();

        var contentItem = new ContentItem {
            Value = text
        };

        var wordList = new CustomInfoType.Types.Dictionary.Types.WordList
        {
            Words = { new string[] { "*****@*****.**", "*****@*****.**" } }
        };

        var exclusionRule = new ExclusionRule
        {
            MatchingType = MatchingType.FullMatch,
            Dictionary   = new CustomInfoType.Types.Dictionary
            {
                WordList = wordList
            }
        };

        var infoType = new InfoType {
            Name = "EMAIL_ADDRESS"
        };

        var inspectionRuleSet = new InspectionRuleSet
        {
            InfoTypes = { infoType },
            Rules     = { new InspectionRule {
                              ExclusionRule = exclusionRule
                          } }
        };

        var inspectConfig = new InspectConfig
        {
            InfoTypes = { infoType },
            RuleSet   = { inspectionRuleSet }
        };
        var primitiveTransformation = new PrimitiveTransformation
        {
            ReplaceWithInfoTypeConfig = new ReplaceWithInfoTypeConfig {
            }
        };

        var transformation = new InfoTypeTransformations.Types.InfoTypeTransformation
        {
            InfoTypes = { infoType },
            PrimitiveTransformation = primitiveTransformation
        };

        var deidentifyConfig = new DeidentifyConfig
        {
            InfoTypeTransformations = new InfoTypeTransformations
            {
                Transformations = { transformation }
            }
        };

        var request = new DeidentifyContentRequest
        {
            Parent           = new LocationName(projectId, "global").ToString(),
            InspectConfig    = inspectConfig,
            DeidentifyConfig = deidentifyConfig,
            Item             = contentItem
        };

        // Call the API.
        var response = dlp.DeidentifyContent(request);

        // Inspect the results.
        Console.WriteLine($"Deidentified content: {response.Item.Value}");
        return(response);
    }
    public static InspectContentResponse Inspect(string projectId, string textToInspect, List <String> excludedMatchList)
    {
        var dlp = DlpServiceClient.Create();

        var byteContentItem = new ByteContentItem
        {
            Type = ByteContentItem.Types.BytesType.TextUtf8,
            Data = Google.Protobuf.ByteString.CopyFromUtf8(textToInspect)
        };

        var contentItem = new ContentItem {
            ByteItem = byteContentItem
        };

        var infoTypes = new string[] { "PHONE_NUMBER", "EMAIL_ADDRESS", "CREDIT_CARD_NUMBER" }.Select(it => new InfoType {
            Name = it
        });

        var exclusionRule = new ExclusionRule
        {
            MatchingType = MatchingType.FullMatch,
            Dictionary   = new CustomInfoType.Types.Dictionary
            {
                WordList = new CustomInfoType.Types.Dictionary.Types.WordList
                {
                    Words = { excludedMatchList }
                }
            }
        };

        var ruleSet = new InspectionRuleSet
        {
            InfoTypes = { new InfoType {
                              Name = "EMAIL_ADDRESS"
                          } },
            Rules = { new InspectionRule {
                          ExclusionRule = exclusionRule
                      } }
        };

        var config = new InspectConfig
        {
            InfoTypes    = { infoTypes },
            IncludeQuote = true,
            RuleSet      = { ruleSet }
        };

        var request = new InspectContentRequest
        {
            Parent        = new LocationName(projectId, "global").ToString(),
            Item          = contentItem,
            InspectConfig = config
        };

        var response = dlp.InspectContent(request);

        Console.WriteLine($"Findings: {response.Result.Findings.Count}");
        foreach (var f in response.Result.Findings)
        {
            Console.WriteLine("\tQuote: " + f.Quote);
            Console.WriteLine("\tInfo type: " + f.InfoType.Name);
            Console.WriteLine("\tLikelihood: " + f.Likelihood);
        }

        return(response);
    }
    public static InspectContentResponse Inspect(string projectId, string textToInspect)
    {
        // Initialize client that will be used to send requests. This client only needs to be created
        // once, and can be reused for multiple requests. After completing all of your requests, call
        // the "close" method on the client to safely clean up any remaining background resources.
        var dlp = DlpServiceClient.Create();

        // Specify the type and content to be inspected.
        var byteContentItem = new ByteContentItem
        {
            Type = ByteContentItem.Types.BytesType.TextUtf8,
            Data = Google.Protobuf.ByteString.CopyFromUtf8(textToInspect)
        };
        var contentItem = new ContentItem
        {
            ByteItem = byteContentItem
        };

        // Specify the type of info the inspection will look for.
        // See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types.
        var infoTypes = new string[] { "DOMAIN_NAME", "EMAIL_ADDRESS" }.Select(it => new InfoType {
            Name = it
        });

        // Define a custom info type to exclude email addresses
        var customInfoType = new CustomInfoType
        {
            InfoType = new InfoType {
                Name = "EMAIL_ADDRESS"
            },
            ExclusionType = ExclusionType.Exclude
        };

        // Exclude EMAIL_ADDRESS matches
        var exclusionRule = new ExclusionRule
        {
            ExcludeInfoTypes = new ExcludeInfoTypes
            {
                InfoTypes = { new InfoType {
                                  Name = "EMAIL_ADDRESS"
                              } }
            },
            MatchingType = MatchingType.PartialMatch
        };

        // Construct a ruleset that applies the exclusion rule to the DOMAIN_NAME infotype.
        // If a DOMAIN_NAME match is part of an EMAIL_ADDRESS match, the DOMAIN_NAME match will
        // be excluded.
        var ruleSet = new InspectionRuleSet
        {
            InfoTypes = { new InfoType {
                              Name = "DOMAIN_NAME"
                          } },
            Rules = { new InspectionRule {
                          ExclusionRule = exclusionRule
                      } }
        };

        // Construct the configuration for the Inspect request, including the ruleset.
        var config = new InspectConfig
        {
            InfoTypes       = { infoTypes },
            CustomInfoTypes = { customInfoType },
            IncludeQuote    = true,
            RuleSet         = { ruleSet }
        };

        // Construct the Inspect request to be sent by the client.
        var request = new InspectContentRequest
        {
            Parent        = new LocationName(projectId, "global").ToString(),
            Item          = contentItem,
            InspectConfig = config
        };

        // Use the client to send the API request.
        var response = dlp.InspectContent(request);

        return(response);
    }
    public static InspectContentResponse Inspect(string projectId, string textToInspect)
    {
        var dlp = DlpServiceClient.Create();

        var byteContentItem = new ByteContentItem
        {
            Type = ByteContentItem.Types.BytesType.TextUtf8,
            Data = Google.Protobuf.ByteString.CopyFromUtf8(textToInspect)
        };

        var contentItem = new ContentItem
        {
            ByteItem = byteContentItem
        };

        var patientRule = new DetectionRule.Types.HotwordRule
        {
            HotwordRegex = new CustomInfoType.Types.Regex {
                Pattern = "patient"
            },
            Proximity = new DetectionRule.Types.Proximity {
                WindowBefore = 10
            },
            LikelihoodAdjustment = new DetectionRule.Types.LikelihoodAdjustment {
                FixedLikelihood = Likelihood.VeryLikely
            }
        };

        var doctorRule = new DetectionRule.Types.HotwordRule
        {
            HotwordRegex = new CustomInfoType.Types.Regex {
                Pattern = "doctor"
            },
            Proximity = new DetectionRule.Types.Proximity {
                WindowBefore = 10
            },
            LikelihoodAdjustment = new DetectionRule.Types.LikelihoodAdjustment {
                FixedLikelihood = Likelihood.Unlikely
            }
        };

        // Construct exclusion rules
        var quasimodoRule = new ExclusionRule
        {
            Dictionary = new Dictionary {
                WordList = new Dictionary.Types.WordList {
                    Words = { "Quasimodo" }
                }
            },
            MatchingType = MatchingType.PartialMatch
        };

        var redactedRule = new ExclusionRule
        {
            Regex = new CustomInfoType.Types.Regex {
                Pattern = "REDACTED"
            },
            MatchingType = MatchingType.PartialMatch
        };

        var infoType = new InfoType {
            Name = "PERSON_NAME"
        };

        var inspectionRuleSet = new InspectionRuleSet
        {
            InfoTypes = { infoType },
            Rules     =
            {
                new InspectionRule {
                    HotwordRule = patientRule
                },
                new InspectionRule {
                    HotwordRule = doctorRule
                },
                new InspectionRule {
                    ExclusionRule = quasimodoRule
                },
                new InspectionRule {
                    ExclusionRule = redactedRule
                }
            }
        };

        var inspectConfig = new InspectConfig
        {
            InfoTypes    = { infoType },
            IncludeQuote = true,
            RuleSet      = { inspectionRuleSet }
        };

        var request = new InspectContentRequest
        {
            ParentAsProjectName = new ProjectName(projectId),
            Item          = contentItem,
            InspectConfig = inspectConfig
        };

        var response = dlp.InspectContent(request);

        Console.WriteLine($"Findings: {response.Result.Findings.Count}");
        foreach (var f in response.Result.Findings)
        {
            Console.WriteLine("\tQuote: " + f.Quote);
            Console.WriteLine("\tInfo type: " + f.InfoType.Name);
            Console.WriteLine("\tLikelihood: " + f.Likelihood);
        }

        return(response);
    }
    public static InspectContentResponse Inspect(string projectId, string textToInspect)
    {
        // Initialize client that will be used to send requests. This client only needs to be created
        // once, and can be reused for multiple requests. After completing all of your requests, call
        // the "close" method on the client to safely clean up any remaining background resources.
        var dlp = DlpServiceClient.Create();

        // Specify the type and content to be inspected.
        var byteItem = new ByteContentItem
        {
            Type = ByteContentItem.Types.BytesType.TextUtf8,
            Data = Google.Protobuf.ByteString.CopyFromUtf8(textToInspect)
        };
        var contentItem = new ContentItem {
            ByteItem = byteItem
        };

        // Specify the type of info the inspection will look for.
        // See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types.
        var infoTypes = new string[] { "PERSON_NAME", "EMAIL_ADDRESS" }.Select(it => new InfoType {
            Name = it
        });

        // Exclude EMAIL_ADDRESS matches
        var exclusionRule = new ExclusionRule
        {
            ExcludeInfoTypes = new ExcludeInfoTypes {
                InfoTypes = { new InfoType {
                                  Name = "EMAIL_ADDRESS"
                              } }
            },
            MatchingType = MatchingType.PartialMatch
        };

        // Construct a ruleset that applies the exclusion rule to the PERSON_NAME infotype.
        // If a PERSON_NAME match overlaps with an EMAIL_ADDRESS match, the PERSON_NAME match will
        // be excluded.
        var ruleSet = new InspectionRuleSet
        {
            InfoTypes = { new InfoType {
                              Name = "PERSON_NAME"
                          } },
            Rules = { new InspectionRule {
                          ExclusionRule = exclusionRule
                      } }
        };

        // Construct the configuration for the Inspect request, including the ruleset.
        var config = new InspectConfig
        {
            InfoTypes    = { infoTypes },
            IncludeQuote = true,
            RuleSet      = { ruleSet }
        };

        // Construct the Inspect request to be sent by the client.
        var request = new InspectContentRequest
        {
            Parent        = new LocationName(projectId, "global").ToString(),
            Item          = contentItem,
            InspectConfig = config
        };

        // Use the client to send the API request.
        var response = dlp.InspectContent(request);

        // Parse the response and process results
        Console.WriteLine($"Findings: {response.Result.Findings.Count}");
        foreach (var f in response.Result.Findings)
        {
            Console.WriteLine("\tQuote: " + f.Quote);
            Console.WriteLine("\tInfo type: " + f.InfoType.Name);
            Console.WriteLine("\tLikelihood: " + f.Likelihood);
        }

        return(response);
    }
Example #7
0
    public static InspectContentResponse Inspect(string projectId, string textToInspect)
    {
        var dlp = DlpServiceClient.Create();

        var byteItem = new ByteContentItem
        {
            Type = ByteContentItem.Types.BytesType.TextUtf8,
            Data = Google.Protobuf.ByteString.CopyFromUtf8(textToInspect)
        };

        var contentItem = new ContentItem {
            ByteItem = byteItem
        };

        var customInfoType = new CustomInfoType
        {
            InfoType = new InfoType {
                Name = "VIP_DETECTOR"
            },
            Regex = new CustomInfoType.Types.Regex {
                Pattern = "Larry Page|Sergey Brin"
            },
            ExclusionType = ExclusionType.Exclude
        };

        var exclusionRule = new ExclusionRule
        {
            ExcludeInfoTypes = new ExcludeInfoTypes {
                InfoTypes = { customInfoType.InfoType }
            },
            MatchingType = MatchingType.FullMatch
        };

        var ruleSet = new InspectionRuleSet
        {
            InfoTypes = { new InfoType {
                              Name = "PERSON_NAME"
                          } },
            Rules = { new InspectionRule {
                          ExclusionRule = exclusionRule
                      } }
        };

        var config = new InspectConfig
        {
            InfoTypes = { new InfoType {
                              Name = "PERSON_NAME"
                          } },
            CustomInfoTypes = { customInfoType },
            IncludeQuote    = true,
            RuleSet         = { ruleSet }
        };

        var request = new InspectContentRequest
        {
            Parent        = new LocationName(projectId, "global").ToString(),
            Item          = contentItem,
            InspectConfig = config
        };

        var response = dlp.InspectContent(request);

        Console.WriteLine($"Findings: {response.Result.Findings.Count}");
        foreach (var f in response.Result.Findings)
        {
            Console.WriteLine("\tQuote: " + f.Quote);
            Console.WriteLine("\tInfo type: " + f.InfoType.Name);
            Console.WriteLine("\tLikelihood: " + f.Likelihood);
        }

        return(response);
    }
Example #8
0
    public static InspectContentResponse Inspect(string projectId, string textToInspect, string customDetectorPattern, List <String> excludedSubstringList)
    {
        var dlp = DlpServiceClient.Create();

        var byteContentItem = new ByteContentItem
        {
            Type = ByteContentItem.Types.BytesType.TextUtf8,
            Data = Google.Protobuf.ByteString.CopyFromUtf8(textToInspect)
        };

        var contentItem = new ContentItem {
            ByteItem = byteContentItem
        };

        var infoType = new InfoType
        {
            Name = "CUSTOM_NAME_DETECTOR"
        };
        var customInfoType = new CustomInfoType
        {
            InfoType = infoType,
            Regex    = new CustomInfoType.Types.Regex {
                Pattern = customDetectorPattern
            }
        };

        var exclusionRule = new ExclusionRule
        {
            MatchingType = MatchingType.PartialMatch,
            Dictionary   = new CustomInfoType.Types.Dictionary
            {
                WordList = new CustomInfoType.Types.Dictionary.Types.WordList
                {
                    Words = { excludedSubstringList }
                }
            }
        };

        var ruleSet = new InspectionRuleSet
        {
            InfoTypes = { infoType },
            Rules     = { new InspectionRule {
                              ExclusionRule = exclusionRule
                          } }
        };

        var config = new InspectConfig
        {
            CustomInfoTypes = { customInfoType },
            IncludeQuote    = true,
            RuleSet         = { ruleSet }
        };

        var request = new InspectContentRequest
        {
            ParentAsProjectName = new ProjectName(projectId),
            Item          = contentItem,
            InspectConfig = config
        };

        var response = dlp.InspectContent(request);

        Console.WriteLine($"Findings: {response.Result.Findings.Count}");
        foreach (var f in response.Result.Findings)
        {
            Console.WriteLine("\tQuote: " + f.Quote);
            Console.WriteLine("\tInfo type: " + f.InfoType.Name);
            Console.WriteLine("\tLikelihood: " + f.Likelihood);
        }

        return(response);
    }