public void HandleObfuscation(Entity entity, FieldToBeObfuscated field, IEntityMetadataCache metaData) { entity.ThrowArgumentNullExceptionIfNull(nameof(entity)); field.ThrowArgumentNullExceptionIfNull(nameof(field)); metaData.ThrowArgumentNullExceptionIfNull(nameof(metaData)); // Get the min and maximum values for the field using the meta data cache DoubleAttributeMetadata doubleMetaData = (DoubleAttributeMetadata)metaData.GetAttribute(entity.LogicalName, field.FieldName); int min = (int)doubleMetaData.MinValue.GetValueOrDefault(0); int max = (int)doubleMetaData.MaxValue.GetValueOrDefault(10); if (field.CanBeFormatted) { Dictionary <string, object> metadataParameters = new Dictionary <string, object> { { "min", min }, { "max", max } }; entity[field.FieldName] = formattingClient.CreateFormattedValue((double)entity[field.FieldName], field, metadataParameters); return; } entity[field.FieldName] = doubleScramblerClient.ExecuteScramble((double)entity[field.FieldName], min, max); }
public void CanBeFormattedReturnsFalseIfFormattingArgsAreMissing() { FieldToBeObfuscated testObject = new FieldToBeObfuscated() { FieldName = "test" }; testObject.CanBeFormatted.Should().BeFalse(); }
public void HandleObfuscation(Entity entity, FieldToBeObfuscated field, IEntityMetadataCache metaData) { // Get the min and maximum values for the field using the meta data cache IntegerAttributeMetadata intMetaData = (IntegerAttributeMetadata)metaData.GetAttribute(entity.LogicalName, field.FieldName); int min = intMetaData.MinValue.GetValueOrDefault(0); int max = intMetaData.MaxValue.GetValueOrDefault(10); entity[field.FieldName] = intScramblerClient.ExecuteScramble((int)entity[field.FieldName], min, max); }
private void ObfuscateField(Entity entity, FieldToBeObfuscated field) { Type fieldType = entity[field.FieldName].GetType(); ICrmObfuscateHandler handler = crmObfuscateHandlers .FirstOrDefault(currentHandler => currentHandler.CanHandle(fieldType)); if (handler != null) { handler.HandleObfuscation(entity, field, metaDataCache); } }
public void HandleObfuscation(Entity entity, FieldToBeObfuscated field, IEntityMetadataCache metaData) { entity.ThrowArgumentNullExceptionIfNull(nameof(entity)); field.ThrowArgumentNullExceptionIfNull(nameof(field)); metaData.ThrowArgumentNullExceptionIfNull(nameof(metaData)); DecimalAttributeMetadata decimalMetaData = (DecimalAttributeMetadata)metaData.GetAttribute(entity.LogicalName, field.FieldName); int min = (int)decimalMetaData.MinValue.GetValueOrDefault(0); int max = (int)decimalMetaData.MaxValue.GetValueOrDefault(10); entity[field.FieldName] = decimalScramblerClient.ExecuteScramble((decimal)entity[field.FieldName], min, max); }
public string CreateFormattedValue(string originalValue, FieldToBeObfuscated field, Dictionary <string, object> metadataParameters) { field.ThrowArgumentNullExceptionIfNull(nameof(field)); metadataParameters.ThrowArgumentNullExceptionIfNull(nameof(metadataParameters)); string replacementString = Format(originalValue, field, metadataParameters); while (replacementString == originalValue) { replacementString = Format(originalValue, field, metadataParameters); } return(replacementString); }
public double CreateFormattedValue(double originalValue, FieldToBeObfuscated field, Dictionary <string, object> metadataParameters) { metadataParameters.ThrowArgumentNullExceptionIfNull(nameof(metadataParameters)); field.ThrowArgumentNullExceptionIfNull(nameof(field)); var replacementValue = Format(originalValue, field); while (replacementValue == originalValue || !ReplacementIsValid(replacementValue, metadataParameters)) { replacementValue = Format(originalValue, field); } return(replacementValue); }
public void ReturnAFormattedValue() { // Arrange var mockFormattingClient = new Mock <IObfuscationFormattingType <string> >(); mockFormattingClient.Setup(a => a.CreateFormattedValue(It.IsAny <string>(), It.IsAny <FieldToBeObfuscated>(), It.IsAny <Dictionary <string, object> >())) .Returns("126 New Close"); var systemUnderTest = new CrmObfuscateStringHandler(mockFormattingClient.Object); InitializeEntityMetadata(); MockEntityMetadataCache .Setup(cache => cache.GetAttribute("contact", "address1_line1")) .Returns(new StringAttributeMetadata() { MaxLength = 200 }); string originalValue = "1 Main Road"; Entity entity = new Entity("contact"); entity.Attributes.Add("address1_line1", originalValue); List <ObfuscationFormatOption> arguments = new List <ObfuscationFormatOption>(); Dictionary <string, string> argumentsParams = new Dictionary <string, string> { { "filename", "test.csv" }, { "columnname", "street" } }; arguments.Add(new ObfuscationFormatOption(ObfuscationFormatType.Lookup, argumentsParams)); var fieldToBeObfuscated = new FieldToBeObfuscated() { FieldName = "address1_line1", ObfuscationFormat = "{0}" }; fieldToBeObfuscated.ObfuscationFormatArgs.AddRange(arguments); // Act systemUnderTest.HandleObfuscation(entity, fieldToBeObfuscated, MockEntityMetadataCache.Object); string newValue = entity["address1_line1"].ToString(); newValue.Should().NotBe(originalValue); }
public void CanBeFormattedReturnsTrueIfFormattingArgsAreAvailable() { Dictionary <string, string> args = new Dictionary <string, string>(); args.Add("length", "10"); List <ObfuscationFormatOption> formatOptions = new List <ObfuscationFormatOption>(); formatOptions.Add(new ObfuscationFormatOption(ObfuscationFormatType.RandomString, args)); FieldToBeObfuscated testObject = new FieldToBeObfuscated() { FieldName = "test", ObfuscationFormat = "{0}", ObfuscationFormatArgs = formatOptions }; testObject.CanBeFormatted.Should().BeTrue(); }
public void ReturnAFormattedValue() { // Arrange var mockFormattingClient = new Mock <IObfuscationFormattingType <double> >(); mockFormattingClient.Setup(a => a.CreateFormattedValue(It.IsAny <double>(), It.IsAny <FieldToBeObfuscated>(), It.IsAny <Dictionary <string, object> >())) .Returns(49.123); CrmObfuscateDoubleHandler systemUnderTest = new CrmObfuscateDoubleHandler(mockFormattingClient.Object); InitializeEntityMetadata(); MockEntityMetadataCache .Setup(cache => cache.GetAttribute("contact", "address1_latitude")) .Returns(new DoubleAttributeMetadata()); double latitudeBefore = 51.5178737; Entity entity = new Entity("contact"); entity.Attributes.Add("address1_latitude", latitudeBefore); List <ObfuscationFormatOption> arguments = new List <ObfuscationFormatOption>(); Dictionary <string, string> argumentsParams = new Dictionary <string, string> { { "filename", "FirstnameAndSurnames.csv" }, { "columnname", "latitude" } }; arguments.Add(new ObfuscationFormatOption(ObfuscationFormatType.Lookup, argumentsParams)); var fieldToBeObfuscated = new FieldToBeObfuscated() { FieldName = "address1_latitude", ObfuscationFormat = "{0}" }; fieldToBeObfuscated.ObfuscationFormatArgs.AddRange(arguments); // Act systemUnderTest.HandleObfuscation(entity, fieldToBeObfuscated, MockEntityMetadataCache.Object); double latitudeAfter = (double)entity["address1_latitude"]; latitudeAfter.Should().NotBe(latitudeBefore); }
public void CanBeFormattedReturnsTrueIfFormattingArgsAreAvailable() { Dictionary <string, string> args = new Dictionary <string, string> { { "length", "10" } }; List <ObfuscationFormatOption> formatOptions = new List <ObfuscationFormatOption> { new ObfuscationFormatOption(ObfuscationFormatType.RandomString, args) }; var testObject = new FieldToBeObfuscated() { FieldName = "test", ObfuscationFormat = "{0}" }; testObject.ObfuscationFormatArgs.AddRange(formatOptions); testObject.CanBeFormatted.Should().BeTrue(); }
private double Format(double originalValue, FieldToBeObfuscated field) { if (field.ObfuscationFormatArgs.Count > 1) { throw new ValidationException($"Only a single ObfuscationFormatOption of type {ObfuscationFormatType.Lookup} can be applied to a field of type double."); } if (!field.ObfuscationFormatArgs[0].FormatType.Equals(ObfuscationFormatType.Lookup)) { throw new NotImplementedException($"The ObfuscationFormatType({field.ObfuscationFormatArgs[0].FormatType}) is not implemented for fields of type double."); } string lookupValue = optionProcessor.GenerateFromLookup(originalValue.ToString("G", CultureInfo.InvariantCulture), field.ObfuscationFormatArgs[0]); if (double.TryParse(lookupValue, out double replacementValue)) { return(replacementValue); } throw new InvalidCastException($"The value ({lookupValue}) read from the lookup source ({field.ObfuscationFormatArgs[0].Arguments["filename"]}) field ({field.ObfuscationFormatArgs[0].Arguments["columnname"]}) could not be converted to a double."); }
private static FieldToBeObfuscated CreateFieldToBeObfuscatedInvalidObject() { List <ObfuscationFormatOption> arguments = new List <ObfuscationFormatOption>(); Dictionary <string, string> argumentsParams = new Dictionary <string, string> { { "min", "-10.000" }, { "max", "60.000" } }; arguments.Add(new ObfuscationFormatOption(ObfuscationFormatType.RandomNumber, argumentsParams)); var fieldToBeObfuscated = new FieldToBeObfuscated() { FieldName = "address1_latitude", ObfuscationFormat = "{0}" }; fieldToBeObfuscated.ObfuscationFormatArgs.AddRange(arguments); return(fieldToBeObfuscated); }
private static FieldToBeObfuscated CreateFieldToBeObfuscatedValidObject() { List <ObfuscationFormatOption> arguments = new List <ObfuscationFormatOption>(); Dictionary <string, string> argumentsParams = new Dictionary <string, string> { { "filename", "FirstnameAndSurnames.csv" }, { "columnname", "latitude" } }; arguments.Add(new ObfuscationFormatOption(ObfuscationFormatType.Lookup, argumentsParams)); var fieldToBeObfuscated = new FieldToBeObfuscated() { FieldName = "address1_latitude", ObfuscationFormat = "{0}" }; fieldToBeObfuscated.ObfuscationFormatArgs.AddRange(arguments); return(fieldToBeObfuscated); }
public void HandleObfuscation(Entity entity, FieldToBeObfuscated field, IEntityMetadataCache metaData) { entity.ThrowArgumentNullExceptionIfNull(nameof(entity)); field.ThrowArgumentNullExceptionIfNull(nameof(field)); metaData.ThrowArgumentNullExceptionIfNull(nameof(metaData)); StringAttributeMetadata stringMetaData = (StringAttributeMetadata)metaData.GetAttribute(entity.LogicalName, field.FieldName); if (field.CanBeFormatted) { Dictionary <string, object> metadataParams = new Dictionary <string, object>(); if (stringMetaData.MaxLength != null) { metadataParams.Add("maxlength", stringMetaData.MaxLength); } entity[field.FieldName] = formattingClient.CreateFormattedValue((string)entity[field.FieldName], field, metadataParams); return; } entity[field.FieldName] = strScramblerClient.ExecuteScramble((string)entity[field.FieldName]); }
private string Format(string originalValue, FieldToBeObfuscated field, Dictionary <string, object> metadataParameters) { List <string> obfuscatedStrings = new List <string>(); foreach (var arg in field.ObfuscationFormatArgs) { switch (arg.FormatType) { case ObfuscationFormatType.RandomString: obfuscatedStrings.Add(optionProcessor.GenerateRandomString(originalValue, arg)); break; case ObfuscationFormatType.RandomNumber: obfuscatedStrings.Add(optionProcessor.GenerateRandomNumber(originalValue, arg).ToString(CultureInfo.InvariantCulture)); break; case ObfuscationFormatType.Lookup: obfuscatedStrings.Add(optionProcessor.GenerateFromLookup(originalValue, arg)); break; } } string replacementString = string.Format(CultureInfo.InvariantCulture, field.ObfuscationFormat, obfuscatedStrings.ToArray()); if (metadataParameters != null && metadataParameters.ContainsKey("maxlength")) { var maxLength = (int)metadataParameters["maxlength"]; if (replacementString.Length > maxLength) { replacementString = replacementString.Substring(0, maxLength); } } return(replacementString); }
private static EntityToBeObfuscated CreateObfuscationConfigForContact() { List <FieldToBeObfuscated> fieldsToBeObfuscated = new List <FieldToBeObfuscated>(); // Firstname List <ObfuscationFormatOption> firstnameArguments = new List <ObfuscationFormatOption>(); Dictionary <string, string> firstnameArgumentParams = new Dictionary <string, string> { { "filename", "FirstnameAndSurnames.csv" }, { "columnname", "firstname" } }; firstnameArguments.Add(new ObfuscationFormatOption(ObfuscationFormatType.Lookup, firstnameArgumentParams)); var firstnameFieldToBeObfuscated = new FieldToBeObfuscated() { FieldName = "firstname", ObfuscationFormat = "OB-{0}" }; firstnameFieldToBeObfuscated.ObfuscationFormatArgs.AddRange(firstnameArguments); fieldsToBeObfuscated.Add(firstnameFieldToBeObfuscated); // Lastname List <ObfuscationFormatOption> lastnameArguments = new List <ObfuscationFormatOption>(); Dictionary <string, string> lastnameArgumentsParams = new Dictionary <string, string> { { "filename", "FirstnameAndSurnames.csv" }, { "columnname", "surname" } }; lastnameArguments.Add(new ObfuscationFormatOption(ObfuscationFormatType.Lookup, lastnameArgumentsParams)); var lastnameFieldToBeObfuscated = new FieldToBeObfuscated() { FieldName = "lastname", ObfuscationFormat = "OB-{0}" }; lastnameFieldToBeObfuscated.ObfuscationFormatArgs.AddRange(lastnameArguments); fieldsToBeObfuscated.Add(lastnameFieldToBeObfuscated); // Email Address List <ObfuscationFormatOption> emailAddressArguments = new List <ObfuscationFormatOption>(); Dictionary <string, string> emailAddressArgumentsParams1 = new Dictionary <string, string>(); Dictionary <string, string> emailAddressArgumentsParams2 = new Dictionary <string, string>(); emailAddressArgumentsParams1.Add("filename", "FirstnameAndSurnames.csv"); emailAddressArgumentsParams1.Add("columnname", "firstname"); emailAddressArguments.Add(new ObfuscationFormatOption(ObfuscationFormatType.Lookup, emailAddressArgumentsParams1)); emailAddressArgumentsParams2.Add("length", "10"); emailAddressArguments.Add(new ObfuscationFormatOption(ObfuscationFormatType.RandomString, emailAddressArgumentsParams2)); var emailaddress1FieldToBeObfuscated = new FieldToBeObfuscated() { FieldName = "emailaddress1", ObfuscationFormat = "OB-{0}.{1}@email.com" }; emailaddress1FieldToBeObfuscated.ObfuscationFormatArgs.AddRange(emailAddressArguments); fieldsToBeObfuscated.Add(emailaddress1FieldToBeObfuscated); // Postcode List <ObfuscationFormatOption> postcodeArguments = new List <ObfuscationFormatOption>(); Dictionary <string, string> postcodeArgumentParams = new Dictionary <string, string> { { "filename", "ukpostcodes.csv" }, { "columnname", "postcode" } }; postcodeArguments.Add(new ObfuscationFormatOption(ObfuscationFormatType.Lookup, postcodeArgumentParams)); var address1PostalcodeFieldToBeObfuscated = new FieldToBeObfuscated() { FieldName = "address1_postalcode", ObfuscationFormat = "OB-{0}" }; address1PostalcodeFieldToBeObfuscated.ObfuscationFormatArgs.AddRange(postcodeArguments); fieldsToBeObfuscated.Add(address1PostalcodeFieldToBeObfuscated); // Address Latitude List <ObfuscationFormatOption> latitudeArguments = new List <ObfuscationFormatOption>(); Dictionary <string, string> latitudeArgumentParams = new Dictionary <string, string> { { "filename", "ukpostcodes.csv" }, { "columnname", "latitude" } }; latitudeArguments.Add(new ObfuscationFormatOption(ObfuscationFormatType.Lookup, latitudeArgumentParams)); var address1latitudeFieldToBeObfuscated = new FieldToBeObfuscated() { FieldName = "address1_latitude", ObfuscationFormat = "{0}" }; address1latitudeFieldToBeObfuscated.ObfuscationFormatArgs.AddRange(latitudeArguments); fieldsToBeObfuscated.Add(address1latitudeFieldToBeObfuscated); // Address Longitude List <ObfuscationFormatOption> longitudeArguments = new List <ObfuscationFormatOption>(); Dictionary <string, string> longitudeArgumentsParams = new Dictionary <string, string> { { "filename", "ukpostcodes.csv" }, { "columnname", "longitude" } }; longitudeArguments.Add(new ObfuscationFormatOption(ObfuscationFormatType.Lookup, longitudeArgumentsParams)); var address1LongitudeFieldToBeObfuscated = new FieldToBeObfuscated() { FieldName = "address1_longitude", ObfuscationFormat = "{0}" }; address1LongitudeFieldToBeObfuscated.ObfuscationFormatArgs.AddRange(longitudeArguments); fieldsToBeObfuscated.Add(address1LongitudeFieldToBeObfuscated); // Address 1 using default fieldsToBeObfuscated.Add(new FieldToBeObfuscated() { FieldName = "address1_city" }); fieldsToBeObfuscated.Add(new FieldToBeObfuscated() { FieldName = "address1_country" }); fieldsToBeObfuscated.Add(new FieldToBeObfuscated() { FieldName = "address1_county" }); // Address line 1 List <ObfuscationFormatOption> address_line1Arguments = new List <ObfuscationFormatOption>(); Dictionary <string, string> address_line1ArgumentParams = new Dictionary <string, string> { { "min", "1" }, { "max", "1234" } }; address_line1Arguments.Add(new ObfuscationFormatOption(ObfuscationFormatType.RandomNumber, address_line1ArgumentParams)); Dictionary <string, string> argumentsParams11_2 = new Dictionary <string, string> { { "length", "8" } }; address_line1Arguments.Add(new ObfuscationFormatOption(ObfuscationFormatType.RandomString, argumentsParams11_2)); var address1Line1FieldToBeObfuscated = new FieldToBeObfuscated() { FieldName = "address1_line1", ObfuscationFormat = "OB-{0} {1} Close", }; address1Line1FieldToBeObfuscated.ObfuscationFormatArgs.AddRange(address_line1Arguments); fieldsToBeObfuscated.Add(address1Line1FieldToBeObfuscated); // Address 1 using defaults fieldsToBeObfuscated.Add(new FieldToBeObfuscated() { FieldName = "address1_line2" }); fieldsToBeObfuscated.Add(new FieldToBeObfuscated() { FieldName = "address1_line3" }); // Address 1 Name List <ObfuscationFormatOption> address1_nameArguments = new List <ObfuscationFormatOption>(); Dictionary <string, string> address1_NameArgumentParams = new Dictionary <string, string> { { "length", "20" } }; address1_nameArguments.Add(new ObfuscationFormatOption(ObfuscationFormatType.RandomString, address1_NameArgumentParams)); var address1NameFieldToBeObfuscated = new FieldToBeObfuscated() { FieldName = "address1_name", ObfuscationFormat = "{0}" }; address1NameFieldToBeObfuscated.ObfuscationFormatArgs.AddRange(address1_nameArguments); fieldsToBeObfuscated.Add(address1NameFieldToBeObfuscated); // Mobile Telephone List <ObfuscationFormatOption> mobilePhoneArguments = new List <ObfuscationFormatOption>(); Dictionary <string, string> mobilephoneArgumentParams1 = new Dictionary <string, string>(); Dictionary <string, string> mobilephoneArgumentParams2 = new Dictionary <string, string>(); mobilephoneArgumentParams1.Add("min", "7000"); mobilephoneArgumentParams1.Add("max", "7999"); mobilePhoneArguments.Add(new ObfuscationFormatOption(ObfuscationFormatType.RandomNumber, mobilephoneArgumentParams1)); mobilephoneArgumentParams2.Add("min", "123456"); mobilephoneArgumentParams2.Add("max", "987654"); mobilePhoneArguments.Add(new ObfuscationFormatOption(ObfuscationFormatType.RandomNumber, mobilephoneArgumentParams2)); var address1MobilephoneFieldToBeObfuscated = new FieldToBeObfuscated() { FieldName = "address1_mobilephone", ObfuscationFormat = "OB-0{0} {1}" }; address1MobilephoneFieldToBeObfuscated.ObfuscationFormatArgs.AddRange(mobilePhoneArguments); fieldsToBeObfuscated.Add(address1MobilephoneFieldToBeObfuscated); // Address 1 Telephone1 List <ObfuscationFormatOption> telephoneArguments = new List <ObfuscationFormatOption>(); Dictionary <string, string> telephoneArgumentParams = new Dictionary <string, string> { { "min", "7000" }, { "max", "7999" } }; telephoneArguments.Add(new ObfuscationFormatOption(ObfuscationFormatType.RandomNumber, telephoneArgumentParams)); var address1Telephone1FieldToBeObfuscated = new FieldToBeObfuscated() { FieldName = "address1_telephone1", ObfuscationFormat = "OB-0{0}" }; address1Telephone1FieldToBeObfuscated.ObfuscationFormatArgs.AddRange(telephoneArguments); fieldsToBeObfuscated.Add(address1Telephone1FieldToBeObfuscated); EntityToBeObfuscated entityToBeObfuscated = new EntityToBeObfuscated() { EntityName = "contact" }; entityToBeObfuscated.FieldsToBeObfuscated.AddRange(fieldsToBeObfuscated); return(entityToBeObfuscated); }