Class: PgCanary::Rules::Base

Inherits:
Object
  • Object
show all
Includes:
PgQuerySupport
Defined in:
lib/pg_canary/rules/base.rb

Overview

Base class for detection rules.

A rule implements #check(query) and returns an array of Detection. Configuration flows in explicitly through the QueryContext (query.config); rules never reach for global state themselves.

Constant Summary

Constants included from PgQuerySupport

PgQuerySupport::COMPARISON_OPS

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.allObject



14
15
16
# File 'lib/pg_canary/rules/base.rb', line 14

def all
  subclasses.sort_by(&:name)
end

.optionsObject

Rule-specific options and their defaults, e.g. { threshold: 1000 }. Declared statically so RuleConfig can generate real accessors.



27
28
29
# File 'lib/pg_canary/rules/base.rb', line 27

def options
  {}
end

.rule_nameObject

:leading_wildcard_like for LeadingWildcardLike



19
20
21
22
23
# File 'lib/pg_canary/rules/base.rb', line 19

def rule_name
  @rule_name ||= name.split("::").last
                     .gsub(/([a-z\d])([A-Z])/, '\1_\2')
                     .downcase.to_sym
end

Instance Method Details

#check(_query) ⇒ Object

Raises:

  • (NotImplementedError)


32
33
34
# File 'lib/pg_canary/rules/base.rb', line 32

def check(_query)
  raise NotImplementedError, "#{self.class} must implement #check"
end

#default_enabledObject

Raises:

  • (NotImplementedError)


41
42
43
# File 'lib/pg_canary/rules/base.rb', line 41

def default_enabled
  raise NotImplementedError, "#{self.class} must declare #default_enabled (true for Tier 1, false for Tier 2)"
end

#enabled?(config) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
# File 'lib/pg_canary/rules/base.rb', line 36

def enabled?(config)
  setting = config.rules[self.class.rule_name].enabled
  setting.nil? ? default_enabled : setting
end