Skip to content

Commit

Permalink
fix create-key --all-capabilities when account doesnt have access t…
Browse files Browse the repository at this point in the history
…o notification-rules cap
  • Loading branch information
mjurbanski-reef committed Apr 23, 2024
1 parent 6a78575 commit f1aee38
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 33 deletions.
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.
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

0 comments on commit f1aee38

Please sign in to comment.