예제 #1
0
        private List <string> GetInvalidMethods(AnonymizerConfiguration config)
        {
            var invalidMethods = new List <string>();

            if (config.PathRules != null)
            {
                foreach (var method in config.PathRules.Values)
                {
                    if (!Enum.TryParse <AnonymizerMethods>(method, true, out _))
                    {
                        invalidMethods.Add(method);
                    }
                }
            }

            if (config.TypeRules != null)
            {
                foreach (var method in config.TypeRules.Values)
                {
                    if (!Enum.TryParse <AnonymizerMethods>(method, true, out _))
                    {
                        invalidMethods.Add(method);
                    }
                }
            }

            return(invalidMethods);
        }
예제 #2
0
        public void Validate(AnonymizerConfiguration config)
        {
            if (config.FhirPathRules == null)
            {
                throw new AnonymizerConfigurationErrorsException("The configuration is invalid, please specify any fhirPathRules");
            }

            FhirPathCompiler compiler = new FhirPathCompiler();

            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]);
                }
                catch (Exception ex)
                {
                    throw new AnonymizerConfigurationErrorsException($"Invalid FHIR path {rule[Constants.PathKey]}", ex);
                }

                // Method validate
                string method = rule[Constants.MethodKey];
                if (!Enum.TryParse <AnonymizerMethod>(method, true, out _))
                {
                    throw new AnonymizerConfigurationErrorsException($"{method} not support.");
                }
            }
        }
예제 #3
0
        private List <string> GetInvalidTypes(AnonymizerConfiguration config)
        {
            var invalidTypes = new List <string>();

            if (config.TypeRules != null)
            {
                foreach (var type in config.TypeRules.Keys)
                {
                    if (!Enum.TryParse <FHIRAllTypes>(type, true, out _))
                    {
                        invalidTypes.Add(type);
                    }
                }
            }

            return(invalidTypes);
        }
예제 #4
0
        private List <string> GetInvalidPaths(AnonymizerConfiguration config)
        {
            var invalidPaths = new List <string>();

            if (config.PathRules != null)
            {
                foreach (var path in config.PathRules.Keys)
                {
                    if (!s_fhirPathRegex.IsMatch(path))
                    {
                        invalidPaths.Add(path);
                    }
                }
            }

            return(invalidPaths);
        }
예제 #5
0
        private List <string> GetInvalidDateShiftTypes(AnonymizerConfiguration config)
        {
            var invalidTypes = new List <string>();

            if (config.TypeRules != null)
            {
                foreach (var rule in config.TypeRules)
                {
                    if (Enum.TryParse <AnonymizerMethods>(rule.Value, true, out AnonymizerMethods method))
                    {
                        if (method == AnonymizerMethods.DateShift && !IsValidDateShiftType(rule.Key))
                        {
                            invalidTypes.Add(rule.Key);
                        }
                    }
                }
            }

            return(invalidTypes);
        }
예제 #6
0
        public void Validate(AnonymizerConfiguration config)
        {
            if (config.TypeRules == null && config.PathRules == null)
            {
                throw new AnonymizerConfigurationErrorsException("The configuration is invalid, please specify any pathRules or typeRules");
            }

            var invalidPaths          = GetInvalidPaths(config);
            var invalidTypes          = GetInvalidTypes(config);
            var invalidMethods        = GetInvalidMethods(config);
            var invalidDateShiftTypes = GetInvalidDateShiftTypes(config);

            StringBuilder builder = new StringBuilder(string.Empty);

            if (invalidPaths.Count > 0)
            {
                builder.Append($"The specified path is unsupported: {String.Join(", ", invalidPaths)}. ");
            }
            if (invalidTypes.Count > 0)
            {
                builder.Append($"The specified type is unsupported: {String.Join(", ", invalidTypes)}. ");
            }
            if (invalidMethods.Count > 0)
            {
                builder.Append($"The specified method is unsupported: {String.Join(", ", invalidMethods)}. ");
            }
            if (invalidDateShiftTypes.Count > 0)
            {
                builder.Append($"Date shift will not take effect on types: {String.Join(", ", invalidDateShiftTypes)}");
            }

            var exceptionMessage = builder.ToString();

            if (!string.IsNullOrEmpty(exceptionMessage))
            {
                throw new AnonymizerConfigurationErrorsException(exceptionMessage);
            }
        }
예제 #7
0
        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);
                }
            }

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