google.cloud.forseti.scanner.audit.ke_rules_engine module

Rules engine for checking arbitrary properties ofKE clusters.

class KeRuleBook(rule_defs=None)[source]

Bases: google.cloud.forseti.scanner.audit.base_rules_engine.BaseRuleBook

The RuleBook for KE rules.

_abc_cache = <_weakrefset.WeakSet object>
_abc_negative_cache = <_weakrefset.WeakSet object>
_abc_negative_cache_version = 207
_abc_registry = <_weakrefset.WeakSet object>
add_rule(rule_def, rule_index)[source]

Add a rule to the rule book.

Parameters:
  • rule_def (dict) – A dictionary containing rule definition properties.
  • rule_index (int) – The index of the rule from the rule definitions. Assigned automatically when the rule book is built.
add_rules(rule_defs)[source]

Add rules to the rule book.

Parameters:rule_defs (dict) – rule definitions dictionary
find_violations(ke_cluster)[source]

Find violations in the rule book.

Parameters:ke_cluster (KeCluster) – KE Cluster and ServerConfig data.
Returns:RuleViolation
Return type:list
get_resource_rules(resource)[source]

Get all the resource rules for resource.

Parameters:resource (Resource) – The gcp_type Resource find in the map.
Returns:A ResourceRules object.
Return type:ResourceRules
class KeRulesEngine(rules_file_path, snapshot_timestamp=None)[source]

Bases: google.cloud.forseti.scanner.audit.base_rules_engine.BaseRulesEngine

Rules engine for KE scanner.

build_rule_book(global_configs=None)[source]

Build KeRuleBook from the rules definition file.

Parameters:global_configs (dict) – Global configurations.
find_violations(ke_cluster, force_rebuild=False)[source]

Check if KE cluster satisfies provided rules.

Parameters:
  • ke_cluster (KeCluster) – A KE Cluster object to check.
  • force_rebuild (bool) – If True, rebuilds the rule book. This will reload the rules definition file and add the rules to the book.
Returns:

A generator of rule violations.

Return type:

generator

class ResourceRules(resource=None, rules=None)[source]

Bases: object

An association of a resource to rules.

__eq__(other)[source]

Compare == with another object.

Parameters:other (ResourceRules) – object to compare with
Returns:comparison result
Return type:int
__ne__(other)[source]

Compare != with another object.

Parameters:other (object) – object to compare with
Returns:comparison result
Return type:int
__repr__()[source]

String representation of this node.

Returns:debug string
Return type:str
find_violations(ke_cluster)[source]

Determine if the policy binding matches this rule’s criteria.

Parameters:ke_cluster (KeCluster) – KE Cluster and ServerConfig data.
Returns:RuleViolation
Return type:list
class Rule(rule_name, rule_index, rule_mode, rule_key, rule_values)[source]

Bases: object

Rule properties from the rule definition file, also finds violations.

__eq__(other)[source]

Test whether Rule equals other Rule.

Parameters:other (Rule) – object to compare to
Returns:comparison result
Return type:int
__hash__()[source]

Make a hash of the rule index.

For now, this will suffice since the rule index is assigned automatically when the rules map is built, and the scanner only handles one rule file at a time. Later on, we’ll need to revisit this hash method when we process multiple rule files.

Returns:The hash of the rule index.
Return type:int
__ne__(other)[source]

Test whether Rule is not equal to another Rule.

Parameters:other (object) – object to compare to
Returns:comparison result
Return type:int
_make_violation(ke_cluster, violation_reason, actual)[source]

Build a RuleViolation for the cluster.

Parameters:
  • ke_cluster (KeCluster) – KE Cluster and ServerConfig data.
  • violation_reason (str) – The violation details.
  • actual (object) – The actual value of the jmespath expression.
Returns:

A new RuleViolation namedtuple.

Return type:

RuleViolation

find_violations(ke_cluster)[source]

Find KE violations in based on the rule.

Parameters:ke_cluster (KeCluster) – KE Cluster and ServerConfig data.
Returns:Returns a list of RuleViolation named tuples
Return type:list
class RuleViolation(resource_type, resource_id, full_name, rule_name, rule_index, rule_mode, rule_values, actual_value, violation_type, violation_reason, project_id, cluster_name, resource_data, resource_name)

Bases: tuple

__getnewargs__()

Return self as a plain tuple. Used by copy and pickle.

__getstate__()

Exclude the OrderedDict from pickling

__repr__()

Return a nicely formatted representation string

_asdict()

Return a new OrderedDict which maps field names to their values

_fields = ('resource_type', 'resource_id', 'full_name', 'rule_name', 'rule_index', 'rule_mode', 'rule_values', 'actual_value', 'violation_type', 'violation_reason', 'project_id', 'cluster_name', 'resource_data', 'resource_name')
classmethod _make(iterable, new=<built-in method __new__ of type object>, len=<built-in function len>)

Make a new RuleViolation object from a sequence or iterable

_replace(**kwds)

Return a new RuleViolation object replacing specified fields with new values

actual_value
cluster_name
full_name
project_id
resource_data
resource_id
resource_name
resource_type
rule_index
rule_mode
rule_name
rule_values
violation_reason
violation_type