Example #1
0
        private static ControlResult ExtractMultiToken <TV, TM>(ResourceControl control, JObject resource,
                                                                out IEnumerable <TV> actual,
                                                                out TM match)
        {
            IEnumerable <JToken> tokens = null;

            foreach (var jsonPath in control.JsonPath)
            {
                tokens = resource.SelectTokens(jsonPath);
                if (tokens != null)
                {
                    break;
                }
            }
            var tokenNotFound = tokens == null;
            var result        = ControlResult.Build(control, resource, tokens, tokenNotFound, VerificationResult.Failed);

            if (tokenNotFound)
            {
                result.IsTokenNotValid = true;
            }
            try
            {
                actual = tokenNotFound ? default(IEnumerable <TV>) : tokens.Values <TV>();
            }
            catch (Exception)
            {
                actual = default(IEnumerable <TV>);
                result.IsTokenNotValid = true;
            }
            match = control.Data.ToObject <TM>();
            return(result);
        }
Example #2
0
        public static ControlResult Build(ResourceControl control, JObject resourceObject, IEnumerable <JToken> resultTokens,
                                          bool isTokenNotFound, VerificationResult verificationResult)
        {
            var resourceDataMarker = BuildDataMarker(resourceObject, 150);
            var result             = new ControlResult
            {
                Id                 = control.Id,
                ControlId          = control.ControlId,
                Description        = control.Description,
                Rationale          = control.Rationale,
                Recommendation     = control.Recommendation,
                Severity           = control.Severity,
                VerificationResult = verificationResult,
                ResourceDataMarker = resourceDataMarker,
                IsTokenNotFound    = isTokenNotFound,
                ResourceType       = resourceObject.GetValue("type").Value <string>(),
                FeatureName        = control.FeatureName,
                SupportedResources = control.SupportedResources,
                ResultDataMarkers  = isTokenNotFound
                    ? new List <ControlResultDataMarker>()
                    : resultTokens.Select(x => BuildDataMarker(x)).ToList()
            };

            return(result);
        }
Example #3
0
        private static ControlResult ExtractSingleToken <TV, TM>(ResourceControl control, JObject resource, out TV actual,
                                                                 out TM match)
        {
            JToken token = null;

            foreach (var jsonPath in control.JsonPath)
            {
                token = resource.SelectToken(jsonPath);
                if (token != null)
                {
                    break;
                }
            }
            var tokenNotFound = token == null;
            var result        = ControlResult.Build(control, resource, token, tokenNotFound, VerificationResult.Failed);

            if (tokenNotFound)
            {
                result.IsTokenNotValid = true;
            }
            try
            {
                actual = tokenNotFound ? default(TV) : token.Value <TV>();
            }
            catch (Exception)
            {
                actual = default(TV);
                result.IsTokenNotValid = true;
            }
            match = control.Data.ToObject <TM>();
            return(result);
        }
        public IList <ControlResult> Evaluate(IList <ResourceControlSet> resourceControlSets, List <ResourceModel> resources)
        {
            var featureName = resources?.First().FeatureName;
            var controlSet  = resourceControlSets?.FirstOrDefault(x => x.FeatureName.Equals(featureName, StringComparison.OrdinalIgnoreCase));
            var results     = new List <ControlResult>();

            foreach (var control in controlSet.Controls)
            {
                List <string> resourcePathList = new List <string>();
                ControlResult controlResult    = null;
                foreach (var resource in resources)
                {
                    controlResult = _controlEvaluator.Evaluate(control, resource.Resource);
                    resourcePathList.Add(controlResult.ResourceDataMarker.JsonPath);
                    if (!controlResult.IsTokenNotFound)
                    {
                        break;
                    }
                }
                if (controlResult != null)
                {
                    if (controlResult.IsTokenNotFound)
                    {
                        controlResult.ResourceDataMarker.JsonPath = resourcePathList.ToArray().ToSingleString(" , ");
                    }
                    results.Add(controlResult);
                }
            }
            foreach (var resource in resources)
            {
                EvaluateNestedResources(controlSet, resource.Resource, results);
            }

            return(results);
        }
        public IList <ControlResult> Evaluate(IList <ResourceControlSet> resourceControlSets, JObject resource)
        {
            var type = resource.GetValueCaseInsensitive <string>("type");
            //var apiVersion = resource.GetValueCaseInsensitive<string>("apiVersion");
            var controlSet = resourceControlSets?.FirstOrDefault(
                x => x.supportedResourceTypes.Any(y => y.Equals(type, StringComparison.OrdinalIgnoreCase)));

            if (controlSet == null)
            {
                return(new List <ControlResult> {
                    ControlResult.NotSupported(resource)
                });
            }
            var results = new List <ControlResult>();

            foreach (var control in controlSet.Controls)
            {
                control.FeatureName        = controlSet.FeatureName;
                control.SupportedResources = controlSet.supportedResourceTypes.ToArray().ToSingleString(" , ");
                var controlResult = _controlEvaluator.Evaluate(control, resource);
                results.Add(controlResult);
            }
            EvaluateNestedResources(controlSet, resource, results);
            return(results);
        }
        public ControlResult Evaluate(ResourceControl control, JObject resource)
        {
            switch (control.MatchType)
            {
            case ControlMatchType.Null:
                return(ControlResult.NotSupported(resource));

            case ControlMatchType.Boolean:
                return(EvaluateBoolean(control, resource));

            case ControlMatchType.IntegerValue:
                return(EvaluateIntegerValue(control, resource));

            case ControlMatchType.ItemCount:
                return(EvaluateItemCount(control, resource));

            case ControlMatchType.ItemProperties:
                return(EvaluateItemProperties(control, resource));

            case ControlMatchType.SecureParam:
                return(EvaluateSecureParam(control, resource));

            case ControlMatchType.StringLength:
                return(ControlResult.NotSupported(resource));

            case ControlMatchType.StringWhitespace:
                return(EvaluateStringWhitespace(control, resource));

            case ControlMatchType.StringSingleToken:
                return(EvaluateStringSingleToken(control, resource));

            case ControlMatchType.StringMultiToken:
                return(EvaluateStringMultiToken(control, resource));

            case ControlMatchType.RegExpressionSingleToken:
                return(EvaluateRegExpressionSingleToken(control, resource));

            case ControlMatchType.RegExpressionMultiToken:
                return(ControlResult.NotSupported(resource));

            case ControlMatchType.VerifiableSingleToken:
                return(EvaluateVerifiableSingleToken(control, resource));

            case ControlMatchType.VerifiableMultiToken:
                return(ControlResult.NotSupported(resource));

            case ControlMatchType.Custom:
                return(ControlResult.NotSupported(resource));

            case ControlMatchType.NullableSingleToken:
                return(EvaluateNullableSingleToken(control, resource));

            case ControlMatchType.VersionSingleToken:
                return(EvaluateSingleVersionToken(control, resource));

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Example #7
0
        public static ControlResult ResourceNotFound(ResourceControl resourceControl)
        {
            var result = new ControlResult
            {
                Id                 = resourceControl.Id,
                ControlId          = resourceControl.ControlId,
                Description        = resourceControl.Description,
                Rationale          = resourceControl.Rationale,
                Recommendation     = resourceControl.Recommendation,
                Severity           = resourceControl.Severity,
                VerificationResult = VerificationResult.Failed,
                ResourceType       = resourceControl.ResourceType,
                ResourceDataMarker = BuildDataMarker(resourceControl.JsonPath[0], 250)
            };

            return(result);
        }
Example #8
0
        public static ControlResult NotSupported(JObject resourceObject)
        {
            const string notSupported = "NotSupported";
            var          result       = new ControlResult
            {
                Id                 = notSupported,
                ControlId          = notSupported,
                Description        = notSupported,
                Rationale          = notSupported,
                Recommendation     = notSupported,
                Severity           = ControlSeverity.Low,
                VerificationResult = VerificationResult.NotSupported,
                ResourceType       = resourceObject.GetValue("type").Value <string>(),
                ResourceDataMarker = BuildDataMarker(resourceObject, 250)
            };

            return(result);
        }
Example #9
0
        private static ControlResult ExtractMultiToken <TV, TM>(ResourceControl control, JObject resource,
                                                                out IEnumerable <TV> actual,
                                                                out TM match)
        {
            IEnumerable <JToken> tokens = null;

            foreach (var jsonPath in control.JsonPath)
            {
                tokens = resource.SelectTokens(jsonPath);
                if (tokens != null)
                {
                    break;
                }
            }
            var tokenNotFound = tokens == null;
            var result        = ControlResult.Build(control, resource, tokens, tokenNotFound, VerificationResult.Failed);

            if (tokenNotFound)
            {
                result.IsTokenNotValid = true;
            }
            try
            {
                if (tokenNotFound)
                {
                    actual = default(IEnumerable <TV>);
                }
                else
                {
                    var tokenValues = default(IEnumerable <TV>);


                    string ARMtemplateFunctionType = null;
                    if (tokens.Values <TV>().FirstOrDefault() != null)
                    {
                        ARMtemplateFunctionType = tokens.Values <TV>().First().ToString().GetFunctionType();
                    }
                    switch (ARMtemplateFunctionType)
                    {
                    case "parameters":
                        tokenValues = functionMultiParameters(tokens, tokenValues);
                        break;

                    case "variables":
                        tokenValues = functionMultiVariables(tokens, tokenValues);
                        break;

                    default:
                        tokenValues = tokens.Values <TV>();
                        break;
                    }

                    // Check if current token is parameter

                    actual = tokenValues;
                }
                //actual = tokenNotFound ? default(IEnumerable<TV>) : tokens.Values<TV>();
            }
            catch (Exception)
            {
                actual = default(IEnumerable <TV>);
                result.IsTokenNotValid = true;
            }
            match = control.Data.ToObject <TM>();

            return(result);
        }
Example #10
0
        private static ControlResult ExtractSingleToken <TV, TM>(ResourceControl control, JObject resource, out TV actual,
                                                                 out TM match, bool validateParameters = true)
        {
            JToken token = null;

            foreach (var jsonPath in control.JsonPath)
            {
                token = resource.SelectToken(jsonPath);
                if (token != null)
                {
                    break;
                }
            }
            var tokenNotFound = token == null;
            var result        = ControlResult.Build(control, resource, token, tokenNotFound, VerificationResult.Failed);

            if (tokenNotFound)
            {
                result.IsTokenNotValid = true;
            }
            try
            {
                if (tokenNotFound)
                {
                    actual = default(TV);
                }
                else
                {
                    var    tokenValue = default(TV);
                    string ARMtemplateFunctionType = token.Value <String>().GetFunctionType();
                    // Switch Case to call particular function on the basis of its function type which may be parameters(), variables(),concat() and Substring().
                    switch (ARMtemplateFunctionType)
                    {
                    case "parameters":
                        tokenValue = functionParameters(validateParameters, token, tokenValue);
                        break;

                    case "variables":
                        tokenValue = functionVariables(validateParameters, token, tokenValue);
                        break;

                    case "concat":
                        tokenValue = functionConcat(validateParameters, token, tokenValue);
                        break;

                    case "substring":
                        tokenValue = functionSubString(validateParameters, token, tokenValue);
                        break;

                    default:
                        tokenValue = token.Value <TV>();
                        break;
                    }
                    actual = tokenValue;
                }
            }
            catch (Exception)
            {
                actual = default(TV);
                result.IsTokenNotValid = true;
            }
            match = control.Data.ToObject <TM>();
            return(result);
        }
Example #11
0
        private static ControlResult ExtractAllSingleToken <TV, TM>(ResourceControl control, JObject resource, out List <TV> actual,
                                                                    out TM match, bool validateParameters = true)
        {
            bool          tokenNotFound = false;
            List <JToken> tokens        = new List <JToken>();

            foreach (var jsonPath in control.JsonPath)
            {
                if (resource.SelectToken(jsonPath) != null)
                {
                    tokens.Add(resource.SelectToken(jsonPath));
                }
                else
                {
                    tokenNotFound = true;
                    break;
                }
            }

            var result = ControlResult.Build(control, resource, tokens, tokenNotFound, VerificationResult.Failed);

            if (tokenNotFound)
            {
                result.IsTokenNotValid = true;
            }
            actual = new List <TV>();
            try
            {
                if (tokenNotFound)
                {
                    actual = null;
                }
                else
                {
                    List <TV> tokenValues = new List <TV>();
                    for (int i = 0; i < tokens.Count; i++)
                    {
                        string ARMtemplateFunctionType = tokens[i].Value <String>().GetFunctionType();

                        switch (ARMtemplateFunctionType)
                        {
                        case "parameters":
                            tokenValues = functionParameters(validateParameters, tokens[i], tokenValues);
                            break;

                        case "variables":
                            tokenValues = functionVariables(validateParameters, tokens[i], tokenValues);
                            break;

                        case "concat":
                            tokenValues = functionConcat(validateParameters, tokens[i], tokenValues);
                            break;

                        case "substring":
                            tokenValues = functionSubString(validateParameters, tokens[i], tokenValues);
                            break;

                        default:
                            tokenValues.Add(tokens[i].Value <TV>());
                            break;
                        }
                        actual = tokenValues;
                    }
                }
            }
            catch (Exception)
            {
                actual = null;
                result.IsTokenNotValid = true;
            }
            match = control.Data.ToObject <TM>();

            return(result);
        }
Example #12
0
        private static ControlResult ExtractMultiToken <TV, TM>(ResourceControl control, JObject resource,
                                                                out IEnumerable <TV> actual,
                                                                out TM match)
        {
            IEnumerable <JToken> tokens = null;

            foreach (var jsonPath in control.JsonPath)
            {
                tokens = resource.SelectTokens(jsonPath);
                if (tokens != null)
                {
                    break;
                }
            }
            var tokenNotFound = tokens == null;
            var result        = ControlResult.Build(control, resource, tokens, tokenNotFound, VerificationResult.Failed);

            if (tokenNotFound)
            {
                result.IsTokenNotValid = true;
            }
            try
            {
                if (tokenNotFound)
                {
                    actual = default(IEnumerable <TV>);
                }
                else
                {
                    var  tokenValues        = default(IEnumerable <TV>);
                    bool paramterValueFound = false;
                    // Check if current token is parameter
                    if (tokens.Values <TV>().FirstOrDefault() != null && tokens.Values <TV>().First().ToString().CheckIsParameter())
                    {
                        var parameterKey = tokens.Values <String>().First().GetParameterKey();
                        if (parameterKey != null)
                        {
                            // Check if parameter value is present in external parameter file
                            if (_externalParametersDict.ContainsKey("parameters"))
                            {
                                JObject externalParameters = _externalParametersDict["parameters"].Value <JObject>();
                                var     externalParamValue = externalParameters.Properties().Where(p => p.Name == parameterKey).Select(p => p.Value["value"].Values <TV>());
                                if (externalParamValue != null && externalParamValue.Count() > 0)
                                {
                                    paramterValueFound = true;
                                    tokenValues        = externalParamValue.First();
                                }
                            }
                            // If parameter value is not present in external parameter file, check for default value
                            if (!paramterValueFound)
                            {
                                JObject innerParameters = _armTemplate["parameters"].Value <JObject>();
                                tokenValues = innerParameters.Properties().Where(p => p.Name == parameterKey).Select(p => p.Value["defaultValue"].Values <TV>()).FirstOrDefault();
                            }
                        }
                    }
                    else
                    {
                        tokenValues = tokens.Values <TV>();
                    }
                    actual = tokenValues;
                }
                //actual = tokenNotFound ? default(IEnumerable<TV>) : tokens.Values<TV>();
            }
            catch (Exception)
            {
                actual = default(IEnumerable <TV>);
                result.IsTokenNotValid = true;
            }
            match = control.Data.ToObject <TM>();
            return(result);
        }