Class: Policy
- Inherits:
-
Object
- Object
- Policy
- Defined in:
- lib/policy.rb
Constant Summary collapse
- KNOWN_TOP_KEYS =
%w[severity rules policy ignore exceptions].freeze
- KNOWN_POLICY_KEYS =
%w[require recommend].freeze
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
Instance Method Summary collapse
-
#excepted?(finding) ⇒ Boolean
Is this finding excepted?.
-
#ignored?(filename) ⇒ Boolean
Should this file be ignored?.
-
#initialize(path = nil) ⇒ Policy
constructor
A new instance of Policy.
- #loaded? ⇒ Boolean
-
#min_severity ⇒ Object
Severity override — returns the configured minimum severity or default.
- #recommended_policies ⇒ Object
-
#required_policies ⇒ Object
Policy requirements.
-
#rule_severity(rule_name) ⇒ Object
Rule severity override or :off.
Constructor Details
#initialize(path = nil) ⇒ Policy
Returns a new instance of Policy.
9 10 11 12 13 14 |
# File 'lib/policy.rb', line 9 def initialize(path = nil) @path = path @config = {} @errors = [] load_config if @path && File.exist?(@path) end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
7 8 9 |
# File 'lib/policy.rb', line 7 def config @config end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
7 8 9 |
# File 'lib/policy.rb', line 7 def errors @errors end |
Instance Method Details
#excepted?(finding) ⇒ Boolean
Is this finding excepted?
42 43 44 45 46 47 48 |
# File 'lib/policy.rb', line 42 def excepted?(finding) exceptions = @config["exceptions"] || [] exceptions.any? { |ex| ex["rule"] == finding.rule && (ex["file"].nil? || ex["file"] == finding.file) } end |
#ignored?(filename) ⇒ Boolean
Should this file be ignored?
36 37 38 39 |
# File 'lib/policy.rb', line 36 def ignored?(filename) patterns = @config["ignore"] || [] patterns.any? { |pat| File.fnmatch(pat, filename, File::FNM_PATHNAME) } end |
#loaded? ⇒ Boolean
16 |
# File 'lib/policy.rb', line 16 def loaded? = !@config.empty? |
#min_severity ⇒ Object
Severity override — returns the configured minimum severity or default
19 20 21 22 23 |
# File 'lib/policy.rb', line 19 def min_severity sev = @config["severity"] return :low unless sev sev.to_sym end |
#recommended_policies ⇒ Object
52 |
# File 'lib/policy.rb', line 52 def recommended_policies = (@config.dig("policy", "recommend") || []) |
#required_policies ⇒ Object
Policy requirements
51 |
# File 'lib/policy.rb', line 51 def required_policies = (@config.dig("policy", "require") || []) |
#rule_severity(rule_name) ⇒ Object
Rule severity override or :off
26 27 28 29 30 31 32 33 |
# File 'lib/policy.rb', line 26 def rule_severity(rule_name) rules = @config["rules"] || {} return nil unless rules.key?(rule_name) override = rules[rule_name] # YAML parses "off" as boolean false return :off if override == false || override.to_s == "off" override.to_sym end |