Exemple #1
0
        // [END dlp_deidentify_masking]

        // [START dlp_deidentify_fpe]
        public static object DeidFpe(
            string projectId,
            string dataValue,
            IEnumerable <InfoType> infoTypes,
            string keyName,
            string wrappedKey,
            string alphabet)
        {
            var deidentifyConfig = new DeidentifyConfig
            {
                InfoTypeTransformations = new InfoTypeTransformations
                {
                    Transformations =
                    {
                        new InfoTypeTransformations.Types.InfoTypeTransformation
                        {
                            PrimitiveTransformation = new PrimitiveTransformation
                            {
                                CryptoReplaceFfxFpeConfig = new CryptoReplaceFfxFpeConfig
                                {
                                    CommonAlphabet = (FfxCommonNativeAlphabet)Enum.Parse(typeof(FfxCommonNativeAlphabet), alphabet),
                                    CryptoKey      = new CryptoKey
                                    {
                                        KmsWrapped = new KmsWrappedCryptoKey
                                        {
                                            CryptoKeyName = keyName,
                                            WrappedKey    = ByteString.FromBase64(wrappedKey)
                                        }
                                    },
                                    SurrogateInfoType = new InfoType
                                    {
                                        Name = "TOKEN"
                                    }
                                }
                            }
                        }
                    }
                }
            };

            DlpServiceClient dlp = DlpServiceClient.Create();
            var response         = dlp.DeidentifyContent(
                new DeidentifyContentRequest
            {
                ParentAsProjectName = new ProjectName(projectId),
                InspectConfig       = new InspectConfig
                {
                    InfoTypes = { infoTypes }
                },
                DeidentifyConfig = deidentifyConfig,
                Item             = new ContentItem {
                    Value = dataValue
                }
            });

            Console.WriteLine($"Deidentified content: {response.Item.Value}");
            return(0);
        }
Exemple #2
0
 /// <summary>Snippet for DeidentifyContent</summary>
 public void DeidentifyContent()
 {
     // Snippet: DeidentifyContent(DeidentifyConfig,InspectConfig,IEnumerable<ContentItem>,CallSettings)
     // Create client
     DlpServiceClient dlpServiceClient = DlpServiceClient.Create();
     // Initialize request argument(s)
     DeidentifyConfig          deidentifyConfig = new DeidentifyConfig();
     InspectConfig             inspectConfig    = new InspectConfig();
     IEnumerable <ContentItem> items            = new List <ContentItem>();
     // Make the request
     DeidentifyContentResponse response = dlpServiceClient.DeidentifyContent(deidentifyConfig, inspectConfig, items);
     // End snippet
 }
Exemple #3
0
        /// <summary>Snippet for DeidentifyContentAsync</summary>
        public async Task DeidentifyContentAsync()
        {
            // Snippet: DeidentifyContentAsync(DeidentifyConfig,InspectConfig,IEnumerable<ContentItem>,CallSettings)
            // Additional: DeidentifyContentAsync(DeidentifyConfig,InspectConfig,IEnumerable<ContentItem>,CancellationToken)
            // Create client
            DlpServiceClient dlpServiceClient = await DlpServiceClient.CreateAsync();

            // Initialize request argument(s)
            DeidentifyConfig          deidentifyConfig = new DeidentifyConfig();
            InspectConfig             inspectConfig    = new InspectConfig();
            IEnumerable <ContentItem> items            = new List <ContentItem>();
            // Make the request
            DeidentifyContentResponse response = await dlpServiceClient.DeidentifyContentAsync(deidentifyConfig, inspectConfig, items);

            // End snippet
        }
    public static DeidentifyContentResponse Deidentify(
        string projectId,
        string inputCsvFilePath,
        int lowerBoundDays,
        int upperBoundDays,
        string dateFields,
        string contextField,
        string keyName,
        string wrappedKey)
    {
        var  hasKeyName    = !string.IsNullOrEmpty(keyName);
        var  hasWrappedKey = !string.IsNullOrEmpty(wrappedKey);
        var  hasContext    = !string.IsNullOrEmpty(contextField);
        bool allFieldsSet  = hasKeyName && hasWrappedKey && hasContext;
        bool noFieldsSet   = !hasKeyName && !hasWrappedKey && !hasContext;

        if (!(allFieldsSet || noFieldsSet))
        {
            throw new ArgumentException("Must specify ALL or NONE of: {contextFieldId, keyName, wrappedKey}!");
        }

        var dlp = DlpServiceClient.Create();

        // Read file
        var csvLines   = File.ReadAllLines(inputCsvFilePath);
        var csvHeaders = csvLines[0].Split(',');
        var csvRows    = csvLines.Skip(1).ToArray();

        // Convert dates to protobuf format, and everything else to a string
        var protoHeaders = csvHeaders.Select(header => new FieldId {
            Name = header
        });
        var protoRows = csvRows.Select(csvRow =>
        {
            var rowValues   = csvRow.Split(',');
            var protoValues = rowValues.Select(rowValue =>
                                               System.DateTime.TryParse(rowValue, out var parsedDate)
               ? new Value {
                DateValue = Google.Type.Date.FromDateTime(parsedDate)
            }
               : new Value {
                StringValue = rowValue
            });

            var rowObject = new Table.Types.Row();
            rowObject.Values.Add(protoValues);
            return(rowObject);
        });

        var dateFieldList = dateFields
                            .Split(',')
                            .Select(field => new FieldId {
            Name = field
        });

        // Construct + execute the request
        var dateShiftConfig = new DateShiftConfig
        {
            LowerBoundDays = lowerBoundDays,
            UpperBoundDays = upperBoundDays
        };

        dateShiftConfig.Context = new FieldId {
            Name = contextField
        };
        dateShiftConfig.CryptoKey = new CryptoKey
        {
            KmsWrapped = new KmsWrappedCryptoKey
            {
                WrappedKey    = ByteString.FromBase64(wrappedKey),
                CryptoKeyName = keyName
            }
        };

        var deidConfig = new DeidentifyConfig
        {
            RecordTransformations = new RecordTransformations
            {
                FieldTransformations =
                {
                    new FieldTransformation
                    {
                        PrimitiveTransformation = new PrimitiveTransformation
                        {
                            DateShiftConfig = dateShiftConfig
                        },
                        Fields ={ dateFieldList          }
                    }
                }
            }
        };

        var response = dlp.DeidentifyContent(
            new DeidentifyContentRequest
        {
            ParentAsProjectName = new ProjectName(projectId),
            DeidentifyConfig    = deidConfig,
            Item = new ContentItem
            {
                Table = new Table
                {
                    Headers = { protoHeaders },
                    Rows    = { protoRows }
                }
            }
        });

        return(response);
    }
Exemple #5
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);
    }
Exemple #6
0
        // [END dlp_reidentify_fpe]

        // [START dlp_deidentify_date_shift]
        public static object DeidDateShift(
            string projectId,
            string inputCsvFile,
            string outputCsvFile,
            int lowerBoundDays,
            int upperBoundDays,
            string dateFields,
            string contextField = "",
            string keyName      = "",
            string wrappedKey   = "")
        {
            var dlp = DlpServiceClient.Create();

            // Read file
            string[] csvLines   = File.ReadAllLines(inputCsvFile);
            string[] csvHeaders = csvLines[0].Split(',');
            string[] csvRows    = csvLines.Skip(1).ToArray();

            // Convert dates to protobuf format, and everything else to a string
            var protoHeaders = csvHeaders.Select(header => new FieldId {
                Name = header
            });
            var protoRows = csvRows.Select(CsvRow =>
            {
                var rowValues   = CsvRow.Split(',');
                var protoValues = rowValues.Select(RowValue =>
                {
                    System.DateTime parsedDate;
                    if (System.DateTime.TryParse(RowValue, out parsedDate))
                    {
                        return(new Value
                        {
                            DateValue = new Google.Type.Date
                            {
                                Year = parsedDate.Year,
                                Month = parsedDate.Month,
                                Day = parsedDate.Day
                            }
                        });
                    }
                    else
                    {
                        return(new Value
                        {
                            StringValue = RowValue
                        });
                    }
                });

                var rowObject = new Table.Types.Row();
                rowObject.Values.Add(protoValues);
                return(rowObject);
            });

            var dateFieldList = dateFields
                                .Split(',')
                                .Select(field => new FieldId {
                Name = field
            });

            // Construct + execute the request
            var dateShiftConfig = new DateShiftConfig
            {
                LowerBoundDays = lowerBoundDays,
                UpperBoundDays = upperBoundDays
            };
            bool hasKeyName    = !String.IsNullOrEmpty(keyName);
            bool hasWrappedKey = !String.IsNullOrEmpty(wrappedKey);
            bool hasContext    = !String.IsNullOrEmpty(contextField);

            if (hasKeyName && hasWrappedKey && hasContext)
            {
                dateShiftConfig.Context = new FieldId {
                    Name = contextField
                };
                dateShiftConfig.CryptoKey = new CryptoKey
                {
                    KmsWrapped = new KmsWrappedCryptoKey
                    {
                        WrappedKey    = ByteString.FromBase64(wrappedKey),
                        CryptoKeyName = keyName
                    }
                };
            }
            else if (hasKeyName || hasWrappedKey || hasContext)
            {
                throw new ArgumentException("Must specify ALL or NONE of: {contextFieldId, keyName, wrappedKey}!");
            }

            var deidConfig = new DeidentifyConfig
            {
                RecordTransformations = new RecordTransformations
                {
                    FieldTransformations =
                    {
                        new FieldTransformation
                        {
                            PrimitiveTransformation = new PrimitiveTransformation
                            {
                                DateShiftConfig = dateShiftConfig
                            },
                            Fields ={ dateFieldList          }
                        }
                    }
                }
            };

            DeidentifyContentResponse response = dlp.DeidentifyContent(
                new DeidentifyContentRequest
            {
                Parent           = $"projects/{projectId}",
                DeidentifyConfig = deidConfig,
                Item             = new ContentItem
                {
                    Table = new Table
                    {
                        Headers = { protoHeaders },
                        Rows    = { protoRows }
                    }
                }
            });

            // Save the results
            List <String> outputLines = new List <string>();

            outputLines.Add(csvLines[0]);

            outputLines.AddRange(response.Item.Table.Rows.Select(ProtoRow =>
            {
                var Values = ProtoRow.Values.Select(ProtoValue =>
                {
                    if (ProtoValue.DateValue != null)
                    {
                        var ProtoDate        = ProtoValue.DateValue;
                        System.DateTime Date = new System.DateTime(
                            ProtoDate.Year, ProtoDate.Month, ProtoDate.Day);
                        return(Date.ToShortDateString());
                    }
                    else
                    {
                        return(ProtoValue.StringValue);
                    }
                });
                return(String.Join(',', Values));
            }));

            File.WriteAllLines(outputCsvFile, outputLines);

            return(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[] { "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);
    }