public ElementNode AnonymizeResourceNode(ElementNode root)
        {
            EnsureArg.IsNotNull(root, nameof(root));

            if (root.IsBundleNode())
            {
                var entryResources = root.GetEntryResourceChildren();
                AnonymizeInternalResourceNodes(entryResources);
            }

            if (root.HasContainedNode())
            {
                var containedResources = root.GetContainedChildren();
                AnonymizeInternalResourceNodes(containedResources);
            }

            var resourceContext = ResourceAnonymizerContext.Create(root, _configurationManger);
            var resourceId      = root.GetNodeId();

            foreach (var rule in resourceContext.RuleList)
            {
                var matchedNodes = root.Select(rule.Path).Cast <ElementNode>();

                _logger.LogDebug(rule.Type == AnonymizerRuleType.PathRule ?
                                 $"Path {rule.Source} matches {matchedNodes.Count()} nodes in resource ID {resourceId}." :
                                 $"Type {rule.Source} matches {matchedNodes.Count()} nodes with path {rule.Path} in resource ID {resourceId}.");

                foreach (var node in matchedNodes)
                {
                    AnonymizeChildNode(node, rule, resourceContext.PathSet, resourceId);
                }
            }

            return(root);
        }
        public ElementNode AnonymizeResourceNode(ElementNode root)
        {
            EnsureArg.IsNotNull(root, nameof(root));

            if (root.IsBundleNode())
            {
                var entryResources = root.GetEntryResourceChildren();
                AnonymizeInternalResourceNodes(entryResources);
            }

            if (root.HasContainedNode())
            {
                var containedResources = root.GetContainedChildren();
                AnonymizeInternalResourceNodes(containedResources);
            }

            var resourceContext = ResourceAnonymizerContext.Create(root, _configurationManger);

            foreach (var rule in resourceContext.RuleList)
            {
                var pathCompileExpression = new FhirPathCompiler().Compile($"{rule.Path}");
                var matchedNodes          = pathCompileExpression(root, EvaluationContext.CreateDefault())
                                            .Cast <ElementNode>();
                foreach (var node in matchedNodes)
                {
                    AnonymizeChildNode(node, rule.Method, resourceContext.PathSet);
                }
            }

            return(root);
        }
        public void GivenATypeRuleContainsAnother_WhenParseRule_NestedOneShouldOverwriteInheritedOne()
        {
            AnonymizerConfiguration configuration = new AnonymizerConfiguration()
            {
                PathRules = new Dictionary <string, string>(),
                ParameterConfiguration = new ParameterConfiguration(),
                TypeRules = new Dictionary <string, string>()
                {
                    { "Address", "redact" },
                    { "dateTime", "keep" }
                }
            };

            AnonymizerConfigurationManager configurationManager = new AnonymizerConfigurationManager(configuration);
            var context = ResourceAnonymizerContext.Create(TestPatientElementNode(), configurationManager);

            Assert.Contains("Patient.address", context.PathSet);
            Assert.Contains("Patient.address.period.start", context.PathSet);
            Assert.Equal("keep", context.RuleList.First(r => r.Path.Equals("Patient.address.period.start")).Method);
            Assert.Equal("redact", context.RuleList.First(r => r.Path.Equals("Patient.address")).Method);
        }
        public void GivenConflictTypeRuleAndPathRule_WhenParseRule_TypeRuleShouldBeIgnored()
        {
            AnonymizerConfiguration configuration = new AnonymizerConfiguration()
            {
                PathRules = new Dictionary <string, string>()
                {
                    { "Patient.address", "keep" }
                },
                ParameterConfiguration = new ParameterConfiguration(),
                TypeRules = new Dictionary <string, string>()
                {
                    { "Address", "redact" }
                }
            };

            AnonymizerConfigurationManager configurationManager = new AnonymizerConfigurationManager(configuration);
            var context = ResourceAnonymizerContext.Create(TestPatientElementNode(), configurationManager);

            Assert.Contains("Patient.address", context.PathSet);
            Assert.Equal("keep", context.RuleList.First().Method);
        }
Exemple #5
0
        public void GivenATypeRule_WhenParseRule_TransformedPathRuleShouldBeReturned()
        {
            AnonymizerConfiguration configuration = new AnonymizerConfiguration()
            {
                PathRules = new Dictionary <string, string>(),
                ParameterConfiguration = new ParameterConfiguration(),
                TypeRules = new Dictionary <string, string>()
                {
                    { "Address", "redact" },
                    { "HumanName", "redact" },
                    { "dateTime", "dateShift" }
                }
            };

            AnonymizerConfigurationManager configurationManager = new AnonymizerConfigurationManager(configuration);
            var context = ResourceAnonymizerContext.Create(TestPatientElementNode(), configurationManager);

            Assert.Contains("Patient.address", context.PathSet);
            Assert.Contains("Patient.name", context.PathSet);
            Assert.Contains("Patient.address.period.start", context.PathSet);
            Assert.Contains("Patient.identifier.period.start", context.PathSet);
        }