Avoid using typing annotations so tests run with Python 3.4.

This commit is contained in:
Todd C. Miller
2020-04-07 14:03:58 -06:00
parent ee868776de
commit 02a117f336
6 changed files with 20 additions and 29 deletions

View File

@@ -1,12 +1,11 @@
import sudo import sudo
from datetime import datetime from datetime import datetime
from typing import Tuple
class BusinessHoursApprovalPlugin(sudo.Plugin): class BusinessHoursApprovalPlugin(sudo.Plugin):
def check(self, command_info: Tuple[str, ...], run_argv: Tuple[str, ...], def check(self, command_info: tuple, run_argv: tuple,
run_env: Tuple[str, ...]) -> int: run_env: tuple) -> int:
error_msg = "" error_msg = ""
now = datetime.now() now = datetime.now()
if now.weekday() >= 5: if now.weekday() >= 5:

View File

@@ -1,7 +1,6 @@
import sudo import sudo
import os import os
from typing import Tuple
VERSION = 1.0 VERSION = 1.0
@@ -22,7 +21,7 @@ class SudoAuditPlugin(sudo.Plugin):
def __del__(self): def __del__(self):
self._log("-- Finished --") self._log("-- Finished --")
def open(self, submit_optind: int, submit_argv: Tuple[str, ...]) -> int: def open(self, submit_optind: int, submit_argv: tuple) -> int:
# To cut out the sudo options, use "submit_optind": # To cut out the sudo options, use "submit_optind":
program_args = submit_argv[submit_optind:] program_args = submit_argv[submit_optind:]
if program_args: if program_args:

View File

@@ -1,7 +1,5 @@
import sudo import sudo
from typing import Tuple
class SudoGroupPlugin(sudo.Plugin): class SudoGroupPlugin(sudo.Plugin):
"""Example sudo input/output plugin """Example sudo input/output plugin
@@ -32,7 +30,7 @@ class SudoGroupPlugin(sudo.Plugin):
""" """
# -- Plugin API functions -- # -- Plugin API functions --
def query(self, user: str, group: str, user_pwd: Tuple): def query(self, user: str, group: str, user_pwd: tuple):
"""Query if user is part of the specified group. """Query if user is part of the specified group.
Beware that user_pwd can be None if user is not present in the password Beware that user_pwd can be None if user is not present in the password

View File

@@ -5,7 +5,6 @@ import errno
import signal import signal
import sys import sys
import json import json
from typing import Tuple
VERSION = 1.0 VERSION = 1.0
@@ -15,9 +14,7 @@ class SudoIOPlugin(sudo.Plugin):
"""Example sudo input/output plugin """Example sudo input/output plugin
Demonstrates how to use the sudo IO plugin API. All functions are added as Demonstrates how to use the sudo IO plugin API. All functions are added as
an example on their syntax, but note that all of them are optional. Also an example on their syntax, but note that all of them are optional.
typing annotations are just here for the help on the syntax (requires
python >= 3.5).
On detailed description of the functions refer to sudo_plugin manual (man On detailed description of the functions refer to sudo_plugin manual (man
sudo_plugin). sudo_plugin).
@@ -44,13 +41,13 @@ class SudoIOPlugin(sudo.Plugin):
# -- Plugin API functions -- # -- Plugin API functions --
def __init__(self, version: str, def __init__(self, version: str,
plugin_options: Tuple[str, ...], **kwargs): plugin_options: tuple, **kwargs):
"""The constructor of the IO plugin. """The constructor of the IO plugin.
Other variables you can currently use as arguments are: Other variables you can currently use as arguments are:
user_env: Tuple[str, ...] user_env: tuple
settings: Tuple[str, ...] settings: tuple
user_info: Tuple[str, ...] user_info: tuple
For their detailed description, see the open() call of the C plugin API For their detailed description, see the open() call of the C plugin API
in the sudo manual ("man sudo"). in the sudo manual ("man sudo").
@@ -72,8 +69,8 @@ class SudoIOPlugin(sudo.Plugin):
self._log("", "-- Plugin DESTROYED --") self._log("", "-- Plugin DESTROYED --")
self._log_file.close() self._log_file.close()
def open(self, argv: Tuple[str, ...], def open(self, argv: tuple,
command_info: Tuple[str, ...]) -> int: command_info: tuple) -> int:
"""Receives the command the user wishes to run. """Receives the command the user wishes to run.
This function works the same as open() call of the C IO plugin API (see This function works the same as open() call of the C IO plugin API (see

View File

@@ -6,7 +6,6 @@ import os
import pwd import pwd
import grp import grp
import shutil import shutil
from typing import Tuple
VERSION = 1.0 VERSION = 1.0
@@ -17,8 +16,7 @@ class SudoPolicyPlugin(sudo.Plugin):
Demonstrates how to use the sudo policy plugin API. All functions are added Demonstrates how to use the sudo policy plugin API. All functions are added
as an example on their syntax, but note that most of them are optional as an example on their syntax, but note that most of them are optional
(except check_policy). Also typing annotations are just here for the help (except check_policy).
on the syntax (requires python >= 3.5).
On detailed description of the functions refer to sudo_plugin manual (man On detailed description of the functions refer to sudo_plugin manual (man
sudo_plugin). sudo_plugin).
@@ -47,13 +45,13 @@ class SudoPolicyPlugin(sudo.Plugin):
# -- Plugin API functions -- # -- Plugin API functions --
def __init__(self, user_env: Tuple[str, ...], settings: Tuple[str, ...], def __init__(self, user_env: tuple, settings: tuple,
version: str, **kwargs): version: str, **kwargs):
"""The constructor matches the C sudo plugin API open() call """The constructor matches the C sudo plugin API open() call
Other variables you can currently use as arguments are: Other variables you can currently use as arguments are:
user_info: Tuple[str, ...] user_info: tuple
plugin_options: Tuple[str, ...] plugin_options: tuple
For their detailed description, see the open() call of the C plugin API For their detailed description, see the open() call of the C plugin API
in the sudo manual ("man sudo"). in the sudo manual ("man sudo").
@@ -66,7 +64,7 @@ class SudoPolicyPlugin(sudo.Plugin):
self.user_env = sudo.options_as_dict(user_env) self.user_env = sudo.options_as_dict(user_env)
self.settings = sudo.options_as_dict(settings) self.settings = sudo.options_as_dict(settings)
def check_policy(self, argv: Tuple[str, ...], env_add: Tuple[str, ...]): def check_policy(self, argv: tuple, env_add: tuple):
cmd = argv[0] cmd = argv[0]
# Example for a simple reject: # Example for a simple reject:
if not self._is_command_allowed(cmd): if not self._is_command_allowed(cmd):
@@ -86,7 +84,7 @@ class SudoPolicyPlugin(sudo.Plugin):
return (sudo.RC.ACCEPT, command_info_out, argv, user_env_out) return (sudo.RC.ACCEPT, command_info_out, argv, user_env_out)
def init_session(self, user_pwd: Tuple, user_env: Tuple[str, ...]): def init_session(self, user_pwd: tuple, user_env: tuple):
"""Perform session setup """Perform session setup
Beware that user_pwd can be None if user is not present in the password Beware that user_pwd can be None if user is not present in the password
@@ -101,7 +99,7 @@ class SudoPolicyPlugin(sudo.Plugin):
# If you do not want to change user_env, you can just return (or None): # If you do not want to change user_env, you can just return (or None):
# return sudo.RC.OK # return sudo.RC.OK
def list(self, argv: Tuple[str, ...], is_verbose: int, user: str): def list(self, argv: tuple, is_verbose: int, user: str):
cmd = argv[0] if argv else None cmd = argv[0] if argv else None
as_user_text = "as user '{}'".format(user) if user else "" as_user_text = "as user '{}'".format(user) if user else ""

View File

@@ -1,7 +1,7 @@
Example sudo python plugin will log to /some/not/writable/directory/sudo.log Example sudo python plugin will log to /some/not/writable/directory/sudo.log
Traceback: Traceback:
File "SRC_DIR/example_io_plugin.py", line 67, in __init__ File "SRC_DIR/example_io_plugin.py", line 64, in __init__
self._open_log_file(path.join(log_path, "sudo.log")) self._open_log_file(path.join(log_path, "sudo.log"))
File "SRC_DIR/example_io_plugin.py", line 140, in _open_log_file File "SRC_DIR/example_io_plugin.py", line 137, in _open_log_file
self._log_file = open(log_path, "a") self._log_file = open(log_path, "a")