/// <summary>
        /// Intentional as no identified value in calling from DLL at this time
        /// </summary>
        /// <returns></returns>
        public PackRulesResult GetResult()
        {
            WriteOnce.SafeLog("PackRules::Run", LogLevel.Trace);
            WriteOnce.Operation(MsgHelp.FormatString(MsgHelp.ID.CMD_RUNNING, "Pack Rules"));

            PackRulesResult packRulesResult = new()
            {
                AppVersion = Common.Utils.GetVersionString()
            };

            try
            {
                RulesVerifier verifier = new(_rules_path, _options?.Log);
                if (_options?.PackEmbeddedRules ?? false)
                {
                    verifier.LoadRuleSet(RuleSetUtils.GetDefaultRuleSet());
                }
                verifier.Verify();
                if (!verifier.IsVerified)
                {
                    throw new OpException(MsgHelp.GetString(MsgHelp.ID.VERIFY_RULES_RESULTS_FAIL));
                }
                packRulesResult.Rules      = verifier.CompiledRuleset?.GetAppInspectorRules().ToList() ?? new List <Rule>();
                packRulesResult.ResultCode = PackRulesResult.ExitCode.Success;
            }
            catch (OpException e)
            {
                WriteOnce.Error(e.Message);
                //caught for CLI callers with final exit msg about checking log or throws for DLL callers
                throw;
            }

            return(packRulesResult);
        }
    }
Ejemplo n.º 2
0
        private void ConfigRules()
        {
            _logger.LogTrace("ExportTagsCommand::ConfigRules");
            _rules = new RuleSet(_loggerFactory);
            if (!_options.IgnoreDefaultRules)
            {
                _rules = RuleSetUtils.GetDefaultRuleSet(_loggerFactory);
            }

            if (!string.IsNullOrEmpty(_options?.CustomRulesPath))
            {
                if (Directory.Exists(_options.CustomRulesPath))
                {
                    _rules.AddDirectory(_options.CustomRulesPath);
                }
                else if (File.Exists(_options.CustomRulesPath))
                {
                    _rules.AddFile(_options.CustomRulesPath);
                }
                else
                {
                    throw new OpException(MsgHelp.FormatString(MsgHelp.ID.CMD_INVALID_RULE_PATH, _options.CustomRulesPath));
                }
            }

            //error check based on ruleset not path enumeration
            if (_rules == null || !_rules.Any())
            {
                throw new OpException(MsgHelp.GetString(MsgHelp.ID.CMD_NORULES_SPECIFIED));
            }
        }
        /// <summary>
        /// Option for DLL use as alternate to Run which only outputs a file to return results as string
        /// CommandOption defaults will not have been set when used as DLL via CLI processing so some checks added
        /// </summary>
        /// <returns>output results</returns>
        public VerifyRulesResult GetResult()
        {
            _logger.LogTrace("VerifyRulesCommand::Run");
            _logger.LogInformation(MsgHelp.GetString(MsgHelp.ID.CMD_RUNNING), "Verify Rules");

            VerifyRulesResult verifyRulesResult = new() { AppVersion = Utils.GetVersionString() };

            try
            {
                var analyzer = new ApplicationInspectorAnalyzer();
                RulesVerifierOptions options = new()
                {
                    Analyzer      = analyzer,
                    FailFast      = false,
                    LoggerFactory = _loggerFactory,
                    LanguageSpecs = Languages.FromConfigurationFiles(_loggerFactory, _options.CustomCommentsPath, _options.CustomLanguagesPath)
                };
                RulesVerifier verifier = new(options);
                verifyRulesResult.ResultCode = VerifyRulesResult.ExitCode.Verified;
                var stati = new List <RuleStatus>();

                RuleSet?ruleSet = new(_loggerFactory);
                if (_options.VerifyDefaultRules)
                {
                    ruleSet = RuleSetUtils.GetDefaultRuleSet(_loggerFactory);
                }
                try
                {
                    if (_options.CustomRulesPath != null)
                    {
                        if (Directory.Exists(_options.CustomRulesPath))
                        {
                            ruleSet.AddDirectory(_options.CustomRulesPath);
                        }
                        else if (File.Exists(_options.CustomRulesPath))
                        {
                            ruleSet.AddFile(_options.CustomRulesPath);
                        }
                    }
                }
                catch (JsonException e)
                {
                    _logger.LogError(e.Message);
                    verifyRulesResult.ResultCode = VerifyRulesResult.ExitCode.CriticalError;
                    return(verifyRulesResult);
                }
                var verifyResult = verifier.Verify(ruleSet);
                verifyRulesResult.RuleStatusList = verifyResult.RuleStatuses;
                verifyRulesResult.ResultCode     = verifyResult.Verified ? VerifyRulesResult.ExitCode.Verified : VerifyRulesResult.ExitCode.NotVerified;
            }
            catch (OpException e)
            {
                _logger.LogTrace(e.Message);
                //caught for CLI callers with final exit msg about checking log or throws for DLL callers
                throw;
            }
            return(verifyRulesResult);
        }
    }
Ejemplo n.º 4
0
        /// <summary>
        /// Intentional as no identified value in calling from DLL at this time
        /// </summary>
        /// <returns></returns>
        public PackRulesResult GetResult()
        {
            _logger.LogTrace("PackRulesCommand::ConfigRules");
            _logger.LogInformation(MsgHelp.GetString(MsgHelp.ID.CMD_RUNNING), "Pack Rules");

            PackRulesResult packRulesResult = new()
            {
                AppVersion = Common.Utils.GetVersionString()
            };

            try
            {
                RulesVerifierOptions options = new()
                {
                    FailFast      = false,
                    LoggerFactory = _loggerFactory,
                    LanguageSpecs = Languages.FromConfigurationFiles(_loggerFactory, _options.CustomCommentsPath, _options.CustomLanguagesPath)
                };
                RulesVerifier verifier = new(options);
                RuleSet?      ruleSet  = _options.PackEmbeddedRules ? RuleSetUtils.GetDefaultRuleSet() : new RuleSet();
                if (!string.IsNullOrEmpty(_options.CustomRulesPath))
                {
                    ruleSet.AddDirectory(_options.CustomRulesPath);
                }
                RulesVerifierResult result = verifier.Verify(ruleSet);
                if (!result.Verified)
                {
                    throw new OpException(MsgHelp.GetString(MsgHelp.ID.VERIFY_RULES_RESULTS_FAIL));
                }
                packRulesResult.Rules      = result.CompiledRuleSet.GetAppInspectorRules().ToList();
                packRulesResult.ResultCode = PackRulesResult.ExitCode.Success;
            }
            catch (OpException e)
            {
                _logger.LogError(e.Message);
                //caught for CLI callers with final exit msg about checking log or throws for DLL callers
                throw;
            }

            return(packRulesResult);
        }
    }
        /// <summary>
        /// Option for DLL use as alternate to Run which only outputs a file to return results as string
        /// CommandOption defaults will not have been set when used as DLL via CLI processing so some checks added
        /// </summary>
        /// <returns>output results</returns>
        public VerifyRulesResult GetResult()
        {
            WriteOnce.SafeLog("VerifyRulesCommand::Run", LogLevel.Trace);
            WriteOnce.Operation(MsgHelp.FormatString(MsgHelp.ID.CMD_RUNNING, "Verify Rules"));

            VerifyRulesResult verifyRulesResult = new() { AppVersion = Utils.GetVersionString() };

            try
            {
                RulesVerifier verifier = new(null, _options.Log);
                verifyRulesResult.ResultCode = VerifyRulesResult.ExitCode.Verified;
                var stati    = new List <RuleStatus>();
                var analyzer = new Analyzer();
                analyzer.SetOperation(new WithinOperation(analyzer));
                analyzer.SetOperation(new OATRegexWithIndexOperation(analyzer));
                analyzer.SetOperation(new OATSubstringIndexOperation(analyzer));

                RuleSet?ruleSet = new(_options.Log);
                if (_options.VerifyDefaultRules)
                {
                    ruleSet = RuleSetUtils.GetDefaultRuleSet(_options.Log);
                }
                try
                {
                    if (_options.CustomRulesPath != null)
                    {
                        if (Directory.Exists(_options.CustomRulesPath))
                        {
                            ruleSet.AddDirectory(_options.CustomRulesPath);
                        }
                        else if (File.Exists(_options.CustomRulesPath))
                        {
                            ruleSet.AddFile(_options.CustomRulesPath);
                        }
                    }
                }
                catch (JsonSerializationException e)
                {
                    WriteOnce.Error(e.Message);
                    verifyRulesResult.ResultCode = VerifyRulesResult.ExitCode.CriticalError;
                    return(verifyRulesResult);
                }
                foreach (var rule in ruleSet.GetOatRules())
                {
                    stati.Add(new RuleStatus()
                    {
                        RulesId   = rule.AppInspectorRule.Id,
                        RulesName = rule.Name,
                        Verified  = verifier.Verify(rule.AppInspectorRule),
                        OatIssues = analyzer.EnumerateRuleIssues(rule)
                    });
                }
                verifyRulesResult.RuleStatusList = stati;
                verifyRulesResult.ResultCode     = stati.All(x => x.Verified && !x.OatIssues.Any()) ? VerifyRulesResult.ExitCode.Verified : VerifyRulesResult.ExitCode.NotVerified;
            }
            catch (OpException e)
            {
                WriteOnce.Error(e.Message);
                //caught for CLI callers with final exit msg about checking log or throws for DLL callers
                throw;
            }
            return(verifyRulesResult);
        }
    }