-
Notifications
You must be signed in to change notification settings - Fork 13
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
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1d8630d
feat: Add Windows session user
roblutt a977816
address comments
roblutt 8d6f5d0
formatting
roblutt f34d319
Skip windows session user test on linux
roblutt fb07f18
Improve WindowsSessionUser doscstring
roblutt b2c5a67
Add password to windows session user
roblutt dbcdc6b
Add missing password param and expand docstring
roblutt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | ||
|
@@ -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") | ||
"""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. | ||
""" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes