Skip to content

Commit

Permalink
Merge pull request #24 from codectl/feat/#23
Browse files Browse the repository at this point in the history
ensure no error when OS is not supported by pwd
  • Loading branch information
codectl authored Mar 30, 2023
2 parents 6396ec8 + a240e47 commit b073a85
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
continue-on-error: ${{ matrix.experimental }}
strategy:
matrix:
os: [ ubuntu-latest, macos-latest ]
os: [ ubuntu-latest, macos-latest, windows-latest ]
python-version: [ "3.8", "3.9", "3.10", "3.11" ]
experimental: [ false ]

Expand Down
9 changes: 7 additions & 2 deletions src/impersonation/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import re
import sys
import warnings
from multiprocessing import Pipe, get_context

from impersonation import utils
Expand Down Expand Up @@ -55,8 +56,12 @@ def decorator(fn):

@functools.wraps(fn)
def wrapper(*args, **kwargs):
# call wrapped function if no user impersonation
if not username or str(username) == utils.system_username():
# call wrapped function if user cannot be impersonated
try:
if not username or str(username) == utils.pw_username():
return fn(*args, **kwargs)
except OSError:
warnings.warn("OS not supported: decorated fn cannot be impersonated.")
return fn(*args, **kwargs)

cls_name = fn.__qualname__.split(".")[0]
Expand Down
26 changes: 20 additions & 6 deletions src/impersonation/utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
import os
import pwd


def pw_pair(username):
pwnam = pwd.getpwnam(username)
return pwnam.pw_uid, pwnam.pw_gid
__all__ = (
"pw_username",
"pw_pair",
)


def pwd():
try:
import pwd

return pwd
except ImportError:
raise OSError


def system_username():
return pwd.getpwuid(os.getuid()).pw_name
def pw_username():
return pwd().getpwuid(os.getuid()).pw_name


def pw_pair(username):
pwnam = pwd().getpwnam(username)
return pwnam.pw_uid, pwnam.pw_gid

0 comments on commit b073a85

Please sign in to comment.