Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Windows session user #16

Merged
merged 7 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
1 change: 1 addition & 0 deletions requirements-testing.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ black >= 23.7, == 23.*
ruff >= 0.0.286, == 0.0.*
mypy >= 1.5.1, == 1.5.*
psutil >= 5.9.5, == 5.9.*
pywin32 == 306; platform_system == "Windows"
68 changes: 65 additions & 3 deletions src/openjd/sessions/_session_user.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.

import os
from ._os_checker import is_posix
from ._os_checker import is_posix, is_windows

if is_posix():
import grp

if is_windows():
import win32api
import win32security
import win32net
import win32netcon

from typing import Optional

__all__ = ("PosixSessionUser", "SessionUser")
__all__ = ("PosixSessionUser", "SessionUser", "WindowsSessionUser")


class SessionUser:
Expand Down Expand Up @@ -38,7 +44,63 @@ def __init__(self, user: str, *, group: Optional[str] = None) -> None:
group (Optional[str]): The group. Defaults to the name of this
process' effective group.
"""
if os.name != "posix":
if not is_posix():
raise RuntimeError("Only available on posix systems.")
self.user = user
self.group = group if group else grp.getgrgid(os.getegid()).gr_name # type: ignore


class WindowsSessionUser(SessionUser):
__slots__ = ("user", "group")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add "password" here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

"""Specific os-user identity to run a Session as under Windows."""

user: str
"""
User name of the identity to run the Session's subprocesses under.
This can be either a plain username for a local user or a domain username in down-level logon form
ex: localUser, domain\\domainUser
"""

group: str
"""
Group name of the identity to run the Session's subprocesses under.
This can be just a group name for a local group, or a domain group in down-level logon form.
ex: localGroup, domain\\domainGroup
"""

password: str
"""
Password of the identity to run the Session's subprocess under.
"""

@staticmethod
def is_domain_joined() -> bool:
"""
Returns true if the machine is joined to a domain, otherwise False.
"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the docstring, I think we should also include doc for the return value as well. I am not quite sure which standard we are using in the github packages. Need some clarification from @ddneilson. I see different formats of docstring in different files.

_, join_status = win32net.NetGetJoinInformation()
return join_status != win32netcon.NetSetupUnjoined

def __init__(self, user: str, password: str, *, group: str) -> None:
"""
Arguments:
anandgo1 marked this conversation as resolved.
Show resolved Hide resolved
user (str): User name of the identity to run the Session's subprocesses under.
This can be either a plain username for a local user or a domain username in down-level logon form
ex: localUser, domain\\domainUser, [email protected]
group (str): Group name of the identity to run the Session's subprocesses under.
This can be just a group name for a local group, or a domain group in down-level format.
ex: localGroup, domain\\domainGroup
password (str): Password of the identity to run the Session's subprocess under.
"""
if not is_windows():
raise RuntimeError("Only available on Windows systems.")

self.group = group
self.password = password

if "@" in user and self.is_domain_joined():
anandgo1 marked this conversation as resolved.
Show resolved Hide resolved
user = win32security.TranslateName(
user, win32api.NameUserPrincipal, win32api.NameSamCompatible
)

self.user = user
18 changes: 18 additions & 0 deletions test/openjd/sessions/test_session_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.

from openjd.sessions._session_user import WindowsSessionUser
from openjd.sessions._os_checker import is_windows

import pytest


@pytest.mark.skipif(not is_windows(), reason="Windows-specific tests")
class TestWindowsSessionUser:
@pytest.mark.parametrize(
"user",
["userA", "domain\\userA"],
)
def test_user_not_converted(self, user):
windows_session_user = WindowsSessionUser(user, group="test_group")

assert windows_session_user.user == user