def audit(self, file_path): self.auditor = PluginsManager(config=self.config) self.parser = NginxParser(file_path, allow_includes=self.config.allow_includes) self.root = self.parser.parse(file_path) push_context(self.root) self._audit_recursive(self.root.children)
def audit(self, file_path, file_data, is_stdin=False): LOG.debug("Audit config file: {fname}".format(fname=file_path)) parser = NginxParser( cwd=os.path.dirname(file_path) if not is_stdin else '', allow_includes=self.config.allow_includes) self.root = parser.parse(content=file_data.read(), path_info=file_path) push_context(self.root) self._audit_recursive(self.root.children)
class Manager(object): def __init__(self, config=None): self.root = None self.parser = None self.auditor = None self.config = config or Config() self.stats = { gixy.severity.UNSPECIFIED: 0, gixy.severity.LOW: 0, gixy.severity.MEDIUM: 0, gixy.severity.HIGH: 0 } def audit(self, file_path): self.auditor = PluginsManager(config=self.config) self.parser = NginxParser(file_path, allow_includes=self.config.allow_includes) self.root = self.parser.parse(file_path) push_context(self.root) self._audit_recursive(self.root.children) def get_results(self): for plugin in self.auditor.plugins: if plugin.issues: self.stats[plugin.severity] += len(plugin.issues) yield plugin def _audit_recursive(self, tree): for directive in tree: self._update_variables(directive) self.auditor.audit(directive) if directive.is_block: if directive.self_context: push_context(directive) self._audit_recursive(directive.children) if directive.self_context: pop_context() def _update_variables(self, directive): # TODO(buglloc): finish him! if not directive.provide_variables: return context = get_context() for var in directive.variables: if var.name == 0: # All regexps must clean indexed variables context.clear_index_vars() context.add_var(var.name, var) def __enter__(self): return self def __exit__(self, exc_type, exc_val, exc_tb): purge_context()
def _get_parsed(config): root = NginxParser(cwd='', allow_includes=False).parse(config) return root.children[0]
def _parse(config): return NginxParser(cwd='', allow_includes=False).parse(config)
def _get_parsed(config): with mock.patch('%s.open' % builtins.__name__) as mock_open: mock_open.return_value = StringIO(config) root = NginxParser('/foo/bar', allow_includes=False).parse('/foo/bar') return root.children[0]