Class: Policy

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#configObject (readonly)

Returns the value of attribute config.



7
8
9
# File 'lib/policy.rb', line 7

def config
  @config
end

#errorsObject (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?

Returns:

  • (Boolean)


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?

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


16
# File 'lib/policy.rb', line 16

def loaded? = !@config.empty?

#min_severityObject

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


52
# File 'lib/policy.rb', line 52

def recommended_policies = (@config.dig("policy", "recommend") || [])

#required_policiesObject

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