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

fix create-key --all-capabilities when account doesnt have access t… #277

Merged
merged 2 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 11 additions & 2 deletions b2/_internal/console_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,14 +291,16 @@ def apply_or_none(fcn, value):


def format_account_info(account_info: AbstractAccountInfo) -> dict:
allowed = account_info.get_allowed()
allowed['capabilities'] = sorted(allowed['capabilities'])
return dict(
accountId=account_info.get_account_id(),
accountFilePath=getattr(
account_info,
'filename',
None,
), # missing in StubAccountInfo in tests
allowed=account_info.get_allowed(),
allowed=allowed,
applicationKeyId=account_info.get_application_key_id(),
applicationKey=account_info.get_application_key(),
isMasterKey=account_info.is_master_key(),
Expand Down Expand Up @@ -1643,7 +1645,14 @@ def _run(self, args):
bucket_id_or_none = self.api.get_bucket_by_name(args.bucket).id_

if args.all_capabilities:
args.capabilities = ALL_CAPABILITIES
current_key_caps = set(self.api.account_info.get_allowed()['capabilities'])
preview_feature_caps = {
'readBucketNotifications',
'writeBucketNotifications',
}
args.capabilities = sorted(
set(ALL_CAPABILITIES) - preview_feature_caps | current_key_caps
)

application_key = self.api.create_key(
capabilities=args.capabilities,
Expand Down
1 change: 1 addition & 0 deletions changelog.d/+create_key.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix `create-key --all-capabilities` erroring out when new b2sdk is installed.
2 changes: 1 addition & 1 deletion test/integration/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ def should_succeed(
assert re.search(expected_pattern, stdout), \
f'did not match pattern="{expected_pattern}", stdout="{stdout}"'

return stdout
return stdout.replace(os.linesep, '\n')

@classmethod
def prepare_env(self, additional_env: dict | None = None):
Expand Down
10 changes: 5 additions & 5 deletions test/integration/test_b2_command_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -2954,10 +2954,10 @@ def test_download_file_stdout(
):
assert b2_tool.should_succeed(
['download-file', '--quiet', f"b2://{bucket_name}/{uploaded_sample_file['fileName']}", '-'],
).replace("\r", "") == sample_filepath.read_text()
) == sample_filepath.read_text()
assert b2_tool.should_succeed(
['download-file', '--quiet', f"b2id://{uploaded_sample_file['fileId']}", '-'],
).replace("\r", "") == sample_filepath.read_text()
) == sample_filepath.read_text()


def test_download_file_to_directory(
Expand Down Expand Up @@ -3003,9 +3003,9 @@ def test_download_file_to_directory(
def test_cat(b2_tool, bucket_name, sample_filepath, tmp_path, uploaded_sample_file):
assert b2_tool.should_succeed(
['cat', f"b2://{bucket_name}/{uploaded_sample_file['fileName']}"],
).replace("\r", "") == sample_filepath.read_text()
assert b2_tool.should_succeed(['cat', f"b2id://{uploaded_sample_file['fileId']}"
],).replace("\r", "") == sample_filepath.read_text()
) == sample_filepath.read_text()
assert b2_tool.should_succeed(['cat', f"b2id://{uploaded_sample_file['fileId']}"]
) == sample_filepath.read_text()


def test_header_arguments(b2_tool, bucket_name, sample_filepath, tmp_path):
Expand Down
31 changes: 2 additions & 29 deletions test/unit/console_tool/test_authorize_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from unittest import mock

import pytest
from b2sdk.v2 import ALL_CAPABILITIES

from b2._internal._cli.const import (
B2_APPLICATION_KEY_ENV_VAR,
Expand Down Expand Up @@ -133,35 +134,7 @@ def test_authorize_account_prints_account_info(b2_cli):
{
'bucketId': None,
'bucketName': None,
'capabilities':
[
'listKeys',
'writeKeys',
'deleteKeys',
'listBuckets',
'listAllBucketNames',
'readBuckets',
'writeBuckets',
'deleteBuckets',
'readBucketEncryption',
'writeBucketEncryption',
'readBucketRetentions',
'writeBucketRetentions',
'readFileRetentions',
'writeFileRetentions',
'readFileLegalHolds',
'writeFileLegalHolds',
'readBucketReplications',
'writeBucketReplications',
'bypassGovernance',
'listFiles',
'readFiles',
'shareFiles',
'writeFiles',
'deleteFiles',
'readBucketNotifications',
'writeBucketNotifications',
],
'capabilities': sorted(ALL_CAPABILITIES),
'namePrefix': None,
},
'apiUrl': 'http://api.example.com',
Expand Down
4 changes: 2 additions & 2 deletions test/unit/test_console_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ def test_keys(self):
appKeyId1 goodKeyName-Two my-bucket-a - - '' readFiles,listBuckets,readBucketEncryption
appKeyId3 goodKeyName-Four - - - '' {}
appKeyId4 goodKeyName-Five id=bucket_1 - - '' readFiles,listBuckets
""".format(','.join(ALL_CAPABILITIES))
""".format(','.join(sorted(ALL_CAPABILITIES)))

self._run_command(['list-keys'], expected_list_keys_out, '', 0)
self._run_command(['list-keys', '--long'], expected_list_keys_out_long, '', 0)
Expand Down Expand Up @@ -1584,7 +1584,7 @@ def test_get_account_info(self):
{
"bucketId": None,
"bucketName": None,
"capabilities": ALL_CAPABILITIES,
"capabilities": sorted(ALL_CAPABILITIES),
"namePrefix": None
},
"apiUrl": "http://api.example.com",
Expand Down