Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.1.0
rev: v4.2.0
hooks:
- id: check-added-large-files
- id: check-ast
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ As user specifications are the most complicated, they are most likely the area t
* Run As notations
* Commands

One caveat to add is, this module currently does not do anything with `#include`, `#includedir`, `@include` and `@includedir` lines, but simply ignores them. You can, however, parse any included files individually if needed, but any interdependencies between the files will not be resolved.

## Installing

You can use pip to install pysudoers:
Expand Down
6 changes: 5 additions & 1 deletion pysudoers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-
"""Manage a sudoers file."""

import logging
import re

Expand Down Expand Up @@ -217,8 +216,13 @@ def parse_rule(self, line):
:rtype: dict
"""
rule_re = re.compile(r"([\S\s]*)=([\S\s]*)")
include_re = re.compile(r"^(@|#)include(dir)?\s+")
rule = {}

# Ignore includes for now
if include_re.search(line):
return []

# Do a basic check for rule syntax
match = rule_re.search(line)
if not match:
Expand Down
32 changes: 32 additions & 0 deletions tests/test_sudoers.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,38 @@ def test_escaped_split(self):
sudoobj = Sudoers(path=self.fake_path)
self.assertEqual(sudoobj.cmnd_aliases, result)

def test_hash_include(self):
"""An include with a hash will not cause an exception."""
# Find the path to the test sudoers file
data = "#include /test/dir/file\n"
mopen = self.get_mock_open(data)
with mock.patch(self.open_patch_id, mopen) as _:
_ = Sudoers(path=self.fake_path)

def test_at_include(self):
"""An include with an @ will not cause an exception."""
# Find the path to the test sudoers file
data = "@include /test/dir/file\n"
mopen = self.get_mock_open(data)
with mock.patch(self.open_patch_id, mopen) as _:
_ = Sudoers(path=self.fake_path)

def test_hash_includedir(self):
"""An includedir with a hash will not cause an exception."""
# Find the path to the test sudoers file
data = "#includedir /test/dir/file\n"
mopen = self.get_mock_open(data)
with mock.patch(self.open_patch_id, mopen) as _:
_ = Sudoers(path=self.fake_path)

def test_at_includedir(self):
"""An includedir with an @ will not cause an exception."""
# Find the path to the test sudoers file
data = "@includedir /test/dir/file\n"
mopen = self.get_mock_open(data)
with mock.patch(self.open_patch_id, mopen) as _:
_ = Sudoers(path=self.fake_path)


class TestResolution(TestSudoers):
"""Test the alias resolution methods."""
Expand Down