Module: FiberAudit::CLI
- Defined in:
- lib/fiber_audit/cli.rb
Overview
rubocop:disable Metrics/ModuleLength
Class Method Summary collapse
- .analyze_project(options, cwd, stderr) ⇒ Object
- .color_enabled?(stdout, options) ⇒ Boolean
- .default_format(stdout, output_path) ⇒ Object
- .expand_target_map(targets, separator) ⇒ Object
- .explain_rule(argv, stdout:, stderr:) ⇒ Object
- .list_rules(stdout) ⇒ Object
- .load_configuration(project, override) ⇒ Object
- .parse_runtime_options(argv) ⇒ Object
- .parse_static_options(argv) ⇒ Object
- .print_help(output = $stdout) ⇒ Object
- .print_rule_remediations(rule_class, stdout) ⇒ Object
- .publish_report(report, output_path, stdout, cwd) ⇒ Object
- .reject_arguments!(argv) ⇒ Object
- .render_report(format, result, color:) ⇒ Object
- .reportable_findings?(findings, minimum) ⇒ Boolean
- .rule_targets(rule_class) ⇒ Object
- .rule_title(rule_class) ⇒ Object
- .run_runtime(argv, stdout:, cwd:) ⇒ Object
- .run_static(argv, stdout:, stderr:, cwd:) ⇒ Object
- .runtime_output_directory(override, project) ⇒ Object
- .runtime_policy_with_overrides(policy, options) ⇒ Object
- .start(argv, stdout: $stdout, stderr: $stderr, cwd: Dir.pwd) ⇒ Object
- .unknown_project_note(project) ⇒ Object
- .with_min_severity(configuration, severity) ⇒ Object
Class Method Details
.analyze_project(options, cwd, stderr) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/fiber_audit/cli.rb', line 163 def analyze_project(, cwd, stderr) project = Project.detect(start_path: cwd) stderr.puts unknown_project_note(project) unless project.known? config_path = project.config_path([:config]) if [:config] && !File.file?(config_path) raise ConfigurationError, "configuration file does not exist: #{config_path}" end configuration = Configuration.load(config_path) configuration = with_min_severity(configuration, [:min_severity]) if [:min_severity] result = Audit.new(configuration: configuration, root: project.root).call [configuration, result] end |
.color_enabled?(stdout, options) ⇒ Boolean
215 216 217 |
# File 'lib/fiber_audit/cli.rb', line 215 def color_enabled?(stdout, ) ![:no_color] && ![:out] && stdout.respond_to?(:tty?) && stdout.tty? end |
.default_format(stdout, output_path) ⇒ Object
209 210 211 212 213 |
# File 'lib/fiber_audit/cli.rb', line 209 def default_format(stdout, output_path) return 'json' if output_path stdout.respond_to?(:tty?) && stdout.tty? ? 'text' : 'json' end |
.expand_target_map(targets, separator) ⇒ Object
325 326 327 328 329 |
# File 'lib/fiber_audit/cli.rb', line 325 def (targets, separator) targets.flat_map do |constant, methods| Array(methods).map { |method| "#{constant}#{separator}#{method}" } end end |
.explain_rule(argv, stdout:, stderr:) ⇒ Object
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/fiber_audit/cli.rb', line 257 def explain_rule(argv, stdout:, stderr:) rule_id = argv.shift reject_arguments!(argv) unless rule_id stderr.puts 'Usage: fiber-audit explain <RULE_ID>' return 2 end rule_class = Static::Rules::BuiltIns.registry.find(rule_id) unless rule_class stderr.puts "Unknown rule: #{rule_id}" return 2 end stdout.puts "#{rule_class.id} — #{rule_title(rule_class)}" stdout.puts "Default severity: #{rule_class.default_severity}" stdout.puts "Default confidence: #{rule_class.default_confidence}" stdout.puts "Description: #{rule_class.description}" stdout.puts 'Targets:' rule_targets(rule_class).each { |target| stdout.puts " - #{target}" } print_rule_remediations(rule_class, stdout) 0 end |
.list_rules(stdout) ⇒ Object
247 248 249 250 251 252 253 254 255 |
# File 'lib/fiber_audit/cli.rb', line 247 def list_rules(stdout) Static::Rules::BuiltIns.registry.each do |rule_class| stdout.puts format('%<id>-7s %<severity>-8s %<description>s', id: rule_class.id, severity: rule_class.default_severity.to_s.upcase, description: rule_class.description) end 0 end |
.load_configuration(project, override) ⇒ Object
156 157 158 159 160 161 |
# File 'lib/fiber_audit/cli.rb', line 156 def load_configuration(project, override) config_path = project.config_path(override) raise ConfigurationError, "configuration file does not exist: #{config_path}" if override && !File.file?(config_path) Configuration.load(config_path) end |
.parse_runtime_options(argv) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/fiber_audit/cli.rb', line 113 def (argv) = { config: nil, out: nil, sampling_rate: nil, no_fail_open: false, help: false } parser = OptionParser.new do |opts| opts. = 'Usage: fiber-audit runtime [options] -- COMMAND [ARGUMENTS...]' opts.on('--config PATH', 'Path to configuration file') { |value| [:config] = value } opts.on('--out DIRECTORY', 'Directory for runtime JSONL sessions') { |value| [:out] = value } opts.on('--sampling-rate RATE', Float, 'Override runtime sampling rate') do |value| [:sampling_rate] = value end opts.on('--no-fail-open', 'Fail the Ruby child if runtime recording fails') { [:no_fail_open] = true } opts.on('-h', '--help', 'Show runtime options') { [:help] = true } end separator = argv.index('--') option_arguments = separator ? argv.take(separator) : argv.dup command = separator ? argv.drop(separator + 1) : [] parser.parse!(option_arguments) unless [:help] raise OptionParser::MissingArgument, 'runtime command separator -- is required' unless separator raise OptionParser::MissingArgument, 'runtime command is required after --' if command.empty? end reject_arguments!(option_arguments) [, command, parser] end |
.parse_static_options(argv) ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/fiber_audit/cli.rb', line 178 def (argv) = { format: nil, config: nil, out: nil, min_severity: nil, no_color: false, help: false } parser = OptionParser.new do |opts| opts. = 'Usage: fiber-audit static [options]' opts.on('--format FORMAT', %w[text json], 'Output format: text or json') { |value| [:format] = value } opts.on('--config PATH', 'Path to configuration file') { |value| [:config] = value } opts.on('--out PATH', 'Write report to a file') { |value| [:out] = value } opts.on('--min-severity SEVERITY', Severity::LEVELS.map(&:to_s), 'Minimum reported severity') do |value| [:min_severity] = value.to_sym end opts.on('--no-color', 'Disable ANSI colors') { [:no_color] = true } opts.on('-h', '--help', 'Show static options') { [:help] = true } end parser.parse!(argv) reject_arguments!(argv) [, parser] end |
.print_help(output = $stdout) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/fiber_audit/cli.rb', line 54 def print_help(output = $stdout) output.puts <<~HELP Usage: fiber-audit <command> [options] Commands: static Run static fiber-compatibility analysis runtime Observe an explicitly supplied Ruby command list-rules List all registered static rules explain ID Explain a specific rule version Print version help Show this help Run `fiber-audit static --help` or `fiber-audit runtime --help` for options. HELP end |
.print_rule_remediations(rule_class, stdout) ⇒ Object
281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/fiber_audit/cli.rb', line 281 def print_rule_remediations(rule_class, stdout) values = if rule_class.const_defined?(:REMEDIATION, false) [rule_class.const_get(:REMEDIATION)] elsif rule_class.const_defined?(:CATEGORY_METADATA, false) rule_class.const_get(:CATEGORY_METADATA).values.map { || .fetch(:remediation) }.uniq else [] end values.each_with_index do |remediation, index| stdout.puts "#{index.zero? ? 'Remediation:' : ' '} #{remediation}" end end |
.publish_report(report, output_path, stdout, cwd) ⇒ Object
230 231 232 233 234 235 236 237 238 239 |
# File 'lib/fiber_audit/cli.rb', line 230 def publish_report(report, output_path, stdout, cwd) unless output_path stdout.write(report) return end resolved_path = File.(output_path, cwd) File.write(resolved_path, report) stdout.puts "Report written to #{output_path}" end |
.reject_arguments!(argv) ⇒ Object
331 332 333 334 335 |
# File 'lib/fiber_audit/cli.rb', line 331 def reject_arguments!(argv) return if argv.empty? raise OptionParser::InvalidArgument, "unexpected arguments: #{argv.join(' ')}" end |
.render_report(format, result, color:) ⇒ Object
219 220 221 222 223 224 225 226 227 228 |
# File 'lib/fiber_audit/cli.rb', line 219 def render_report(format, result, color:) case format when 'text' Reporters::Text.new(color: color).render(result) when 'json' Reporters::JSON.new(pretty: true).render(result) else raise OptionParser::InvalidArgument, "unsupported format: #{format}" end end |
.reportable_findings?(findings, minimum) ⇒ Boolean
241 242 243 244 245 |
# File 'lib/fiber_audit/cli.rb', line 241 def reportable_findings?(findings, minimum) findings.any? do |finding| Severity.index(finding.severity) <= Severity.index(minimum) end end |
.rule_targets(rule_class) ⇒ Object
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
# File 'lib/fiber_audit/cli.rb', line 303 def rule_targets(rule_class) case rule_class.id when 'FA1001' (rule_class::TARGETS, '.') when 'FA1002' rule_class::TARGET_METHODS.map { |method| "Thread##{method}" } when 'FA1003' (rule_class::TARGETS, '#') when 'FA1004' rule_class::THREAD_VARIABLE_METHODS.map { |method| "Thread##{method}" } when 'FA1005' (rule_class::TARGETS.transform_values { |method| [method] }, '.') when 'FA1006' rule_class::EXACT.map { |constant| "#{constant}.new" } + ['IPSocket subclasses'] when 'FA1007' rule_class::NET_HTTP_METHODS.map { |method| "Net::HTTP.#{method}" } + rule_class::URI_METHODS.map { |constant, method| "#{constant}.#{method}" } else [] end end |
.rule_title(rule_class) ⇒ Object
294 295 296 297 298 299 300 301 |
# File 'lib/fiber_audit/cli.rb', line 294 def rule_title(rule_class) return 'Subprocess lifecycle operations' if rule_class.id == 'FA1001' %i[TITLE RULE_TITLE].each do |name| return rule_class.const_get(name) if rule_class.const_defined?(name, false) end rule_class.name.split('::').last end |
.run_runtime(argv, stdout:, cwd:) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/fiber_audit/cli.rb', line 85 def run_runtime(argv, stdout:, cwd:) , command, parser = (argv) if [:help] stdout.puts parser return 0 end project = Project.detect(start_path: cwd) configuration = load_configuration(project, [:config]) policy = runtime_policy_with_overrides(configuration.runtime_policy, ) output_directory = runtime_output_directory([:out], project) settings = Runtime::Environment.build( policy: policy, output_directory: output_directory, project_root: project.root ) environment = Runtime::Environment.child_environment( settings: settings, watchdog_policy: configuration.runtime_watchdog_policy, probes_enabled: true ) Runtime::Supervisor.new( command: command, environment: environment, cwd: project.invocation_path ).run end |
.run_static(argv, stdout:, stderr:, cwd:) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/fiber_audit/cli.rb', line 70 def run_static(argv, stdout:, stderr:, cwd:) , parser = (argv) if [:help] stdout.puts parser return 0 end configuration, result = analyze_project(, cwd, stderr) format = [:format] || default_format(stdout, [:out]) report = render_report(format, result, color: color_enabled?(stdout, )) publish_report(report, [:out], stdout, cwd) reportable_findings?(result.findings, configuration.min_severity) ? 1 : 0 end |
.runtime_output_directory(override, project) ⇒ Object
147 148 149 150 151 152 153 154 |
# File 'lib/fiber_audit/cli.rb', line 147 def runtime_output_directory(override, project) path = if override File.(override, project.invocation_path) else File.join(project.root, 'tmp', 'fiber-audit-runtime') end Runtime::Environment.prepare_output_directory(path) end |
.runtime_policy_with_overrides(policy, options) ⇒ Object
138 139 140 141 142 143 144 145 |
# File 'lib/fiber_audit/cli.rb', line 138 def runtime_policy_with_overrides(policy, ) values = policy.to_h values[:sampling_rate] = [:sampling_rate] unless [:sampling_rate].nil? values[:fail_open] = false if [:no_fail_open] Runtime::Policy.new(**values) rescue RuntimeContractError => e raise ConfigurationError, "runtime command policy is invalid: #{e.}" end |
.start(argv, stdout: $stdout, stderr: $stderr, cwd: Dir.pwd) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/fiber_audit/cli.rb', line 21 def start(argv, stdout: $stdout, stderr: $stderr, cwd: Dir.pwd) args = argv.dup command = args.shift case command when 'static' run_static(args, stdout: stdout, stderr: stderr, cwd: cwd) when 'runtime' run_runtime(args, stdout: stdout, cwd: cwd) when 'list-rules' reject_arguments!(args) list_rules(stdout) when 'explain' explain_rule(args, stdout: stdout, stderr: stderr) when 'version', '--version', '-v' reject_arguments!(args) stdout.puts "fiber-audit #{FiberAudit::VERSION}" 0 when nil print_help(stdout) 0 when 'help', '--help', '-h' reject_arguments!(args) print_help(stdout) 0 else raise OptionParser::InvalidArgument, "unknown command: #{command}" end rescue StandardError => e stderr.puts "fiber-audit: #{e.}" 2 end |
.unknown_project_note(project) ⇒ Object
337 338 339 |
# File 'lib/fiber_audit/cli.rb', line 337 def unknown_project_note(project) "Note: project root could not be detected; using #{project.root}" end |
.with_min_severity(configuration, severity) ⇒ Object
196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/fiber_audit/cli.rb', line 196 def with_min_severity(configuration, severity) Configuration.new( static_include: configuration.static_include, static_exclude: configuration.static_exclude, rules_config: configuration.rules_config, report_formats: configuration.report_formats, min_severity: severity, suppressions_path: configuration.suppressions_path, runtime_policy: configuration.runtime_policy, runtime_watchdog_policy: configuration.runtime_watchdog_policy ) end |