Skip to content

Commit

Permalink
Use in-memory account info since v4 only
Browse files Browse the repository at this point in the history
  • Loading branch information
agoncharov-reef committed Mar 25, 2024
1 parent e5215b5 commit 86d7574
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 13 deletions.
10 changes: 10 additions & 0 deletions b2/_internal/b2v3/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,22 @@

# ruff: noqa: F405
from b2._internal._b2v4.registry import * # noqa
from b2._internal._cli.b2api import _get_b2api_for_profile
from b2._internal.arg_parser import enable_camel_case_arguments
from .rm import Rm

enable_camel_case_arguments()


class ConsoleTool(ConsoleTool):
# same as original console tool, but does not use InMemoryAccountInfo and InMemoryCache
# when auth env vars are used

@classmethod
def _initialize_b2_api(cls, args: argparse.Namespace, kwargs: dict) -> B2Api:
return _get_b2api_for_profile(profile=args.profile, **kwargs)


class Ls(B2URIBucketNFolderNameArgMixin, BaseLs):
"""
{BASELS}
Expand Down
16 changes: 10 additions & 6 deletions b2/_internal/console_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -4224,12 +4224,7 @@ def run_command(self, argv):
with suppress(AttributeError):
kwargs['max_download_streams_per_file'] = args.max_download_streams_per_file

if all(get_keyid_and_key_from_env_vars()
) and args.command_class not in (AuthorizeAccount, ClearAccount):
# when user specifies keys via env variables, we switch to in-memory account info
self.api = _get_inmemory_b2api(**kwargs)
else:
self.api = _get_b2api_for_profile(profile=args.profile, **kwargs)
self.api = self._initialize_b2_api(args=args, kwargs=kwargs)

b2_command = B2(self)
command_class = b2_command.run(args)
Expand Down Expand Up @@ -4264,6 +4259,15 @@ def run_command(self, argv):
logger.exception('ConsoleTool unexpected exception')
raise

@classmethod
def _initialize_b2_api(cls, args: argparse.Namespace, kwargs: dict) -> B2Api:
if all(get_keyid_and_key_from_env_vars()
) and args.command_class not in (AuthorizeAccount, ClearAccount):
# when user specifies keys via env variables, we switch to in-memory account info
return _get_inmemory_b2api(**kwargs)

return _get_b2api_for_profile(profile=args.profile, **kwargs)

def authorize_from_env(self, command_class):
if not command_class.REQUIRES_AUTH:
return 0
Expand Down
34 changes: 34 additions & 0 deletions test/integration/test_b2_command_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,40 @@ def test_clear_account_with_env_vars(
account_info.get_application_key_id()


@pytest.mark.apiver(to_ver=3)
def test_command_with_env_vars_saving_credentials(
b2_tool,
application_key,
application_key_id,
account_info_file,
bucket_name,
b2_uri_args,
):
"""
When calling any command other then `authorize-account` and passing credentials
via env vars, we don't want them to be saved.
"""

b2_tool.should_succeed(['clear-account'])

assert B2_APPLICATION_KEY_ID_ENV_VAR not in os.environ
assert B2_APPLICATION_KEY_ENV_VAR not in os.environ

b2_tool.should_succeed(
['ls', '--long', *b2_uri_args(bucket_name)],
additional_env={
B2_APPLICATION_KEY_ID_ENV_VAR: application_key_id,
B2_APPLICATION_KEY_ENV_VAR: application_key,
}
)

assert account_info_file.exists()
account_info = SqliteAccountInfo()
assert account_info.get_application_key() == application_key
assert account_info.get_application_key_id() == application_key_id


@pytest.mark.apiver(from_ver=4)
def test_command_with_env_vars_not_saving_credentials(
b2_tool,
application_key,
Expand Down
8 changes: 1 addition & 7 deletions test/unit/test_console_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ def _get_b2api(**kwargs) -> B2Api:
**kwargs,
)

self.mp = pytest.MonkeyPatch()
self.mp.setattr('b2._internal.console_tool._get_b2api_for_profile', _get_b2api)
self.mp.setattr('b2._internal.console_tool._get_inmemory_b2api', _get_b2api)
self.console_tool_class._initialize_b2_api = lambda cls, args, kwargs: _get_b2api(**kwargs)

self.b2_api = _get_b2api()
self.raw_api = self.b2_api.session.raw_api
Expand All @@ -89,10 +87,6 @@ def _get_b2api(**kwargs) -> B2Api:
]:
os.environ.pop(env_var_name, None)

def tearDown(self) -> None:
super().tearDown()
self.mp.undo()

def _get_stdouterr(self):
stdout = StringIO()
stderr = StringIO()
Expand Down

0 comments on commit 86d7574

Please sign in to comment.