Skip to content

Commit

Permalink
Pass in shell command as list of arguments rather than string for bet…
Browse files Browse the repository at this point in the history
…ter compatibility between Windows and Mac/Linux
  • Loading branch information
paulfioravanti committed Oct 10, 2024
1 parent eac446f commit 08730e7
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/plover_1password/__version__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
Version attribute
"""

__version__ = "0.4.0"
__version__ = "0.4.1"
2 changes: 1 addition & 1 deletion src/plover_1password/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class OnePassword:
_client: Client
_engine: StenoEngine
_platform: str
_shell_command: Callable[[str], str]
_shell_command: Callable[[str], list[str]]

def __init__(self, engine: StenoEngine) -> None:
self._engine = engine
Expand Down
2 changes: 1 addition & 1 deletion src/plover_1password/secret_reference/expander.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
_ENV_VAR_SYNTAX: str = "$"

def expand_env_vars(
shell_command_resolver: Callable[[str], str],
shell_command_resolver: Callable[[str], list[str]],
secret_reference: str
) -> str:
"""
Expand Down
2 changes: 1 addition & 1 deletion src/plover_1password/service_account/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

def get_token(
platform: str,
shell_command_resolver: Callable[[str], str]
shell_command_resolver: Callable[[str], list[str]]
) -> str:
"""
Returns token from the local environment and errors if it is empty.
Expand Down
15 changes: 9 additions & 6 deletions src/plover_1password/shell_command/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,21 @@


_DEFAULT_SHELL: str = "bash"
_POWERSHELL_COMMAND: Callable[[str], str] = lambda env_var: (
"powershell -command "
f"\"$ExecutionContext.InvokeCommand.ExpandString({env_var})\""
_POWERSHELL_COMMAND: Callable[[str], list[str]] = lambda env_var: (
[
"powershell",
"-command",
f"$ExecutionContext.InvokeCommand.ExpandString(\"{env_var}\")"
]
)
# NOTE: Using an interactive mode command (bash/zsh/fish -ic) seemed to be
# the only way to access a user's env vars on a Mac outside Plover's
# environment.
_SHELL_COMMAND: Callable[[str], Callable[[str], str]] = lambda shell: (
lambda env_var: f"{shell} -ic 'echo {env_var}'"
_SHELL_COMMAND: Callable[[str], Callable[[str], list[str]]] = lambda shell: (
lambda env_var: [f"{shell}", "-ic", f"'echo {env_var}'"]
)

def resolve(platform: str) -> Callable[[str], str]:
def resolve(platform: str) -> Callable[[str], list[str]]:
"""
Resolves a shell command for a given platform.
"""
Expand Down
4 changes: 2 additions & 2 deletions src/plover_1password/shell_command/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
from typing import Callable


def run(shell_command_resolver: Callable[[str], str], target: str) -> str:
def run(shell_command_resolver: Callable[[str], list[str]], target: str) -> str:
"""
Runs a provided shell command against target in a subprocess.
"""
command: str = shell_command_resolver(target)
command: list[str] = shell_command_resolver(target)
result: str = subprocess.run(
command,
capture_output=True,
Expand Down
9 changes: 6 additions & 3 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@ def _method(return_value=None):

@pytest.fixture()
def bash_command():
return lambda env_var: f"bash -ic 'echo {env_var}'"
return lambda env_var: ["bash", "-ic", f"'echo {env_var}'"]

@pytest.fixture()
def powershell_command():
return lambda env_var: (
"powershell -command "
f"\"$ExecutionContext.InvokeCommand.ExpandString({env_var})\""
[
"powershell",
"-command",
f"$ExecutionContext.InvokeCommand.ExpandString(\"{env_var}\")"
]
)
15 changes: 11 additions & 4 deletions test/secret_reference/test_secret_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ def test_expand_secret_reference_using_mac_or_linux(
)

spy.assert_called_once_with(
"bash -ic 'echo op://$VAULT_NAME/$ITEM_NAME/$SECTION_NAME/Mobile'",
[
"bash",
"-ic",
"'echo op://$VAULT_NAME/$ITEM_NAME/$SECTION_NAME/Mobile'"
],
capture_output=True,
check=False,
encoding="utf-8",
Expand All @@ -51,9 +55,12 @@ def test_expand_secret_reference_using_windows(
) == "op://Plover/Personal/Phone/Mobile"

spy.assert_called_once_with(
"powershell -command "
"\"$ExecutionContext.InvokeCommand.ExpandString("
"op://$ENV:VAULT_NAME/$ENV:ITEM_NAME/$ENV:SECTION_NAME/Mobile)\"",
[
"powershell",
"-command",
"$ExecutionContext.InvokeCommand.ExpandString("
"\"op://$ENV:VAULT_NAME/$ENV:ITEM_NAME/$ENV:SECTION_NAME/Mobile\")"
],
capture_output=True,
check=False,
encoding="utf-8",
Expand Down
22 changes: 14 additions & 8 deletions test/service_account/test_service_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ def test_blank_token_env_var_value_on_windows(
service_account.get_token("Windows", powershell_command)

spy.assert_called_once_with(
"powershell -command "
"\"$ExecutionContext.InvokeCommand.ExpandString("
"$ENV:OP_SERVICE_ACCOUNT_TOKEN)\"",
[
"powershell",
"-command",
"$ExecutionContext.InvokeCommand.ExpandString("
"\"$ENV:OP_SERVICE_ACCOUNT_TOKEN\")"
],
capture_output=True,
check=False,
encoding="utf-8",
Expand All @@ -43,7 +46,7 @@ def test_blank_token_env_var_value_on_mac_or_linux(
service_account.get_token("Darwin", bash_command)

spy.assert_called_once_with(
"bash -ic 'echo $OP_SERVICE_ACCOUNT_TOKEN'",
["bash", "-ic", "'echo $OP_SERVICE_ACCOUNT_TOKEN'"],
capture_output=True,
check=False,
encoding="utf-8",
Expand All @@ -63,9 +66,12 @@ def test_get_token_using_windows(
== "windows token"
)
spy.assert_called_once_with(
"powershell -command "
"\"$ExecutionContext.InvokeCommand.ExpandString("
"$ENV:OP_SERVICE_ACCOUNT_TOKEN)\"",
[
"powershell",
"-command",
"$ExecutionContext.InvokeCommand.ExpandString("
"\"$ENV:OP_SERVICE_ACCOUNT_TOKEN\")"
],
capture_output=True,
check=False,
encoding="utf-8",
Expand All @@ -85,7 +91,7 @@ def test_get_token_using_mac_or_linux(
== "mac/linux token"
)
spy.assert_called_once_with(
"bash -ic 'echo $OP_SERVICE_ACCOUNT_TOKEN'",
["bash", "-ic", "'echo $OP_SERVICE_ACCOUNT_TOKEN'"],
capture_output=True,
check=False,
encoding="utf-8",
Expand Down
2 changes: 1 addition & 1 deletion test/shell_command/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
@pytest.fixture()
def bash_command():
def _method(shell):
return lambda env_var: f"{shell} -ic 'echo {env_var}'"
return lambda env_var: [f"{shell}", "-ic", f"'echo {env_var}'"]

return _method

0 comments on commit 08730e7

Please sign in to comment.