Exemple #1
0
        public void TestInspectString_CustomDictionary()
        {
            string testData = "My name is Robert and my phone number is (425) 634-9233.";
            var infoTypes = new InfoType[] { };
            var customDictionary = new CustomInfoType[]
            {
                new CustomInfoType
                {
                    InfoType = new InfoType
                    {
                        Name = $"CUSTOM_DICTIONARY_{Guid.NewGuid()}"
                    },
                    Dictionary = new CustomInfoType.Types.Dictionary
                    {
                        WordList = new CustomInfoType.Types.Dictionary.Types.WordList
                        {
                            Words = { new string[] { "Robert" } }
                        }
                    }
                }
            };
            var response = InspectString.Inspect(Fixture.ProjectId, testData, Likelihood.Possible.ToString(), 5, true, infoTypes, customDictionary);

            Assert.Contains(response.Result.Findings, f => f.Quote == "Robert");
        }
Exemple #2
0
        public void TestInspectString_CustomInfoType_NoResults()
        {
            string testData = "She sells sea shells by the sea shore.";
            var infoTypes = new InfoType[] { };
            var customInfoType = new CustomInfoType[]
            {
                new CustomInfoType
                {
                    InfoType = new InfoType
                    {
                        Name = $"CUSTOM_REGEX_{Guid.NewGuid()}"
                    },
                    Regex = new CustomInfoType.Types.Regex
                    {
                        Pattern = "\\(\\d{3}\\) \\d{3}-\\d{4}"
                    },
                    Dictionary = new CustomInfoType.Types.Dictionary
                    {
                        WordList = new CustomInfoType.Types.Dictionary.Types.WordList
                        {
                            Words = { new string[] { "Robert" } }
                        }
                    }
                }
            };
            var response = InspectString.Inspect(Fixture.ProjectId, testData, Likelihood.Possible.ToString(), 5, true, infoTypes, customInfoType);

            Assert.True(!response.Result.Findings.Any());
        }
Exemple #3
0
        public void TestInspectString_NoResults()
        {
            string testData = "She sells sea shells by the sea shore.";
            var infoTypes = new InfoType[] { new InfoType { Name = "PERSON_NAME" } };
            var customInfoTypes = new CustomInfoType[] { };
            var response = InspectString.Inspect(Fixture.ProjectId, testData, Likelihood.Possible.ToString(), 5, true, infoTypes, customInfoTypes);

            Assert.True(!response.Result.Findings.Any());
        }
Exemple #4
0
        public void TestInspectString_PersonName()
        {
            string testData = "The name Robert is very common.";
            var infoTypes = new InfoType[] { new InfoType { Name = "PERSON_NAME" } };
            var customInfoTypes = new CustomInfoType[] { };
            var response = InspectString.Inspect(Fixture.ProjectId, testData, Likelihood.Possible.ToString(), 5, true, infoTypes, customInfoTypes);

            Assert.Contains(response.Result.Findings, f => f.Quote == "Robert");
        }
Exemple #5
0
        public void TestInspectString_CustomRegex()
        {
            string testData = "My name is Robert and my phone number is (425) 634-9233.";
            var infoTypes = new InfoType[] { };
            var customRegex = new CustomInfoType[]
            {
                new CustomInfoType
                {
                    InfoType = new InfoType
                    {
                        Name = $"CUSTOM_REGEX_{Guid.NewGuid()}"
                    },
                    Regex = new CustomInfoType.Types.Regex
                    {
                        Pattern = "\\(\\d{3}\\) \\d{3}-\\d{4}"
                    }
                }
            };
            var response = InspectString.Inspect(Fixture.ProjectId, testData, Likelihood.Possible.ToString(), 5, true, infoTypes, customRegex);

            Assert.Contains(response.Result.Findings, f => f.Quote == "(425) 634-9233");
        }
Exemple #6
0
        /// <summary>
        /// Split and parse a string representation of a custom dictionary and custom regexes.
        /// </summary>
        /// <param name="customDictionariesStr">Comma (default)-separated list of dictionary words.</param>
        /// <param name="customRegexesStr">Comma (default)-separated list of regexes.</param>
        /// <returns>IEnumerable of CustomInfoType items.</returns>
        public static IEnumerable <CustomInfoType> ParseCustomInfoTypes(
            string customDictionariesStr,
            string customRegexesStr,
            char separator = ',')
        {
            IEnumerable <CustomInfoType> dictionary = new CustomInfoType[] { };

            if (!String.IsNullOrEmpty(customDictionariesStr))
            {
                try
                {
                    dictionary = new[] {
                        new CustomInfoType
                        {
                            InfoType = new InfoType
                            {
                                Name = "CUSTOM_DICTIONARY"
                            },
                            Dictionary = new CustomInfoType.Types.Dictionary
                            {
                                WordList = new CustomInfoType.Types.Dictionary.Types.WordList
                                {
                                    Words = { customDictionariesStr.Split(new char[] { separator }) }
                                }
                            }
                        }
                    };
                }
                catch (Exception e)
                {
                    Console.WriteLine($"Failed to parse dictionary {customDictionariesStr}: {e}");
                    return(null);
                }
            }
            IEnumerable <CustomInfoType> regexTypes = new CustomInfoType[] { };

            if (!String.IsNullOrEmpty(customRegexesStr))
            {
                string[] regexes = customRegexesStr.Split(new char[] { separator });
                regexTypes = Enumerable.Range(0, regexes.Length).Select(idx =>
                {
                    try
                    {
                        return(new CustomInfoType
                        {
                            InfoType = new InfoType
                            {
                                Name = String.Format("CUSTOM_REGEX_{0}", idx)
                            },
                            Regex = new CustomInfoType.Types.Regex
                            {
                                Pattern = regexes[idx]
                            }
                        });
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine($"Failed to parse regexes {customRegexesStr}: {e}");
                        return(null);
                    }
                }).Where(it => it != null);
            }
            return(Enumerable.Concat(dictionary, regexTypes));
        }
    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 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[] { "RM-GREEN", "RM-YELLOW", "RM-ORANGE" } }
        };

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

        var customInfoType = new CustomInfoType
        {
            InfoType   = infoType,
            Dictionary = new CustomInfoType.Types.Dictionary
            {
                WordList = wordList
            }
        };

        var inspectConfig = new InspectConfig
        {
            CustomInfoTypes =
            {
                customInfoType,
            }
        };
        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);
    }
Exemple #9
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);
    }
Exemple #10
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);
    }