public ProcessResult Process(ElementNode node, ProcessContext context = null, Dictionary <string, object> settings = null) { EnsureArg.IsNotNull(node); EnsureArg.IsNotNull(context?.VisitedNodes); EnsureArg.IsNotNull(settings); var substituteSetting = SubstituteSetting.CreateFromRuleSettings(settings); ElementNode replacementNode; // Get replacementNode for substitution if (ModelInfo.IsPrimitive(node.InstanceType)) { // Handle replaceWith value of string replacementNode = GetPrimitiveNode(substituteSetting.ReplaceWith); } else { // Handle replaceWith value of json object var replacementNodeType = ModelInfo.GetTypeForFhirType(node.InstanceType); if (replacementNodeType == null) { // Shall never throws here throw new Exception($"Node type is invalid at path {node.GetFhirPath()}."); } // Convert null object to empty object var replaceWith = substituteSetting.ReplaceWith ?? "{}"; var replaceElement = _parser.Parse(replaceWith, replacementNodeType).ToTypedElement(); replacementNode = ElementNode.FromElement(replaceElement); } var keepNodes = new HashSet <ElementNode>(); // Retrieve all nodes that have been processed before to keep _ = GenerateKeepNodeSetForSubstitution(node, context.VisitedNodes, keepNodes); var processResult = SubstituteNode(node, replacementNode, context.VisitedNodes, keepNodes); MarkSubstitutedFragmentAsVisited(node, context.VisitedNodes); return(processResult); }
public void Validate(AnonymizerConfiguration config) { if (string.IsNullOrEmpty(config.FhirVersion)) { _logger.LogWarning($"Version is not specified in configuration file."); } else if (!string.Equals(Constants.SupportedVersion, config.FhirVersion, StringComparison.InvariantCultureIgnoreCase)) { throw new AnonymizerConfigurationErrorsException($"Configuration of fhirVersion {config.FhirVersion} is not supported. Expected fhirVersion: {Constants.SupportedVersion}"); } if (config.FhirPathRules == null) { throw new AnonymizerConfigurationErrorsException("The configuration is invalid, please specify any fhirPathRules"); } FhirPathCompiler compiler = new FhirPathCompiler(); var supportedMethods = Enum.GetNames(typeof(AnonymizerMethod)).ToHashSet(StringComparer.InvariantCultureIgnoreCase); foreach (var rule in config.FhirPathRules) { if (!rule.ContainsKey(Constants.PathKey) || !rule.ContainsKey(Constants.MethodKey)) { throw new AnonymizerConfigurationErrorsException("Missing path or method in Fhir path rule config."); } // Grammar check on FHIR path try { compiler.Compile(rule[Constants.PathKey].ToString()); } catch (Exception ex) { throw new AnonymizerConfigurationErrorsException($"Invalid FHIR path {rule[Constants.PathKey]}", ex); } // Method validate string method = rule[Constants.MethodKey].ToString(); if (!supportedMethods.Contains(method)) { throw new AnonymizerConfigurationErrorsException($"Anonymization method {method} not supported."); } // Should provide replacement value for substitute rule if (string.Equals(method, AnonymizerMethod.Substitute.ToString(), StringComparison.InvariantCultureIgnoreCase)) { SubstituteSetting.ValidateRuleSettings(rule); } if (string.Equals(method, AnonymizerMethod.Perturb.ToString(), StringComparison.InvariantCultureIgnoreCase)) { PerturbSetting.ValidateRuleSettings(rule); } if (string.Equals(method, AnonymizerMethod.Generalize.ToString(), StringComparison.InvariantCultureIgnoreCase)) { GeneralizeSetting.ValidateRuleSettings(rule); } } // Check AES key size is valid (16, 24 or 32 bytes). if (!string.IsNullOrEmpty(config.ParameterConfiguration?.EncryptKey)) { using Aes aes = Aes.Create(); var encryptKeySize = Encoding.UTF8.GetByteCount(config.ParameterConfiguration.EncryptKey) * 8; if (!IsValidKeySize(encryptKeySize, aes.LegalKeySizes)) { throw new AnonymizerConfigurationErrorsException($"Invalid encrypt key size : {encryptKeySize} bits! Please provide key sizes of 128, 192 or 256 bits."); } } }
public void GivenAInvalidSubstituteSetting_WhenValidate_ExceptionShouldBeThrown(Dictionary <string, object> config) { Assert.Throws <AnonymizerConfigurationException>(() => SubstituteSetting.ValidateRuleSettings(config)); }
public void GivenASubstituteSetting_WhenCreate_ReplacementValueShouldBeParsedCorrectly(Dictionary <string, object> config, string expectedValue) { var substituteSetting = SubstituteSetting.CreateFromRuleSettings(config); Assert.Equal(expectedValue, substituteSetting.ReplaceWith); }