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); }
// [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); }