Skip to content

Commit

Permalink
Merge pull request #758 from Backblaze/all-capabilities
Browse files Browse the repository at this point in the history
Add --allCapabilities to create-key
  • Loading branch information
mlech-reef authored Oct 25, 2021
2 parents 0fa60d1 + f4fe5c5 commit a9fe66d
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 12 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added
* Add `--allCapabilities` to `create-key`

### Fixed
* Fix testing bundle in CI for a new `staticx` version

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Alternatively, you can install with:
b2 clear-account [-h]
b2 copy-file-by-id [-h] [--fetchMetadata] [--contentType CONTENTTYPE] [--range RANGE] [--info INFO | --noInfo] [--destinationServerSideEncryption {SSE-B2,SSE-C}] [--destinationServerSideEncryptionAlgorithm {AES256}] [--sourceServerSideEncryption {SSE-C}] [--sourceServerSideEncryptionAlgorithm {AES256}] [--fileRetentionMode {compliance,governance}] [--retainUntil TIMESTAMP] [--legalHold {on,off}] sourceFileId destinationBucketName b2FileName
b2 create-bucket [-h] [--bucketInfo BUCKETINFO] [--corsRules CORSRULES] [--lifecycleRules LIFECYCLERULES] [--fileLockEnabled] [--defaultServerSideEncryption {SSE-B2,none}] [--defaultServerSideEncryptionAlgorithm {AES256}] bucketName bucketType
b2 create-key [-h] [--bucket BUCKET] [--namePrefix NAMEPREFIX] [--duration DURATION] keyName capabilities
b2 create-key [-h] [--bucket BUCKET] [--namePrefix NAMEPREFIX] [--duration DURATION] [--allCapabilities] keyName [capabilities]
b2 delete-bucket [-h] bucketName
b2 delete-file-version [-h] [fileName] fileId
b2 delete-key [-h] applicationKeyId
Expand Down
10 changes: 9 additions & 1 deletion b2/console_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from typing import Optional, Tuple

from b2sdk.v2 import (
ALL_CAPABILITIES,
B2_ACCOUNT_INFO_DEFAULT_FILE,
B2_ACCOUNT_INFO_ENV_VAR,
DEFAULT_SCAN_MANAGER,
Expand Down Expand Up @@ -974,6 +975,7 @@ class CreateKey(Command):
their IDs, but not the secret keys.
The capabilities are passed in as a comma-separated list, like ``readFiles,writeFiles``.
Optionally, you can pass all capabilities known to this client with ``--allCapabilities``.
The ``duration`` is the length of time (in seconds) the new application key will exist.
When the time expires the key will disappear and will no longer be usable. If not
Expand All @@ -998,7 +1000,10 @@ def _setup_parser(cls, parser):
parser.add_argument('--namePrefix')
parser.add_argument('--duration', type=int)
parser.add_argument('keyName')
parser.add_argument('capabilities', type=parse_comma_separated_list)

capabilities = parser.add_mutually_exclusive_group(required=True)
capabilities.add_argument('capabilities', type=parse_comma_separated_list, nargs='?')
capabilities.add_argument('--allCapabilities', action='store_true')

def run(self, args):
# Translate the bucket name into a bucketId
Expand All @@ -1007,6 +1012,9 @@ def run(self, args):
else:
bucket_id_or_none = self.api.get_bucket_by_name(args.bucket).id_

if args.allCapabilities:
args.capabilities = ALL_CAPABILITIES

application_key = self.api.create_key(
capabilities=args.capabilities,
key_name=args.keyName,
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
arrow>=0.8.0,<1.0.0
b2sdk>=1.12.0,<2.0.0
b2sdk>=1.13.0,<2.0.0
docutils==0.16
importlib-metadata>=2.1.1,<3.0.0; python_version <= '3.5'
importlib-metadata>=3.3.0; python_version > '3.5' and python_version < '3.8'
Expand Down
20 changes: 11 additions & 9 deletions test/unit/test_console_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

from b2sdk import v1

from b2sdk.v2 import ALL_CAPABILITIES
from b2sdk.v2 import REALM_URLS
from b2sdk.v2 import StubAccountInfo
from b2sdk.v2 import B2Api
Expand Down Expand Up @@ -611,6 +612,12 @@ def test_keys(self):
'',
0,
)
self._run_command(
['create-key', '--allCapabilities', 'goodKeyName-Four'],
'appKeyId3 appKey3\n',
'',
0,
)

# Delete one key
self._run_command(['delete-key', 'appKeyId2'], 'appKeyId2\n', '', 0)
Expand All @@ -623,13 +630,15 @@ def test_keys(self):
appKeyId0 goodKeyName-One
appKeyId1 goodKeyName-Two
appKeyId2 goodKeyName-Three
appKeyId3 goodKeyName-Four
"""

expected_list_keys_out_long = """
appKeyId0 goodKeyName-One - - - '' readFiles,listBuckets
appKeyId1 goodKeyName-Two my-bucket-a - - '' readFiles,listBuckets,readBucketEncryption
appKeyId2 goodKeyName-Three id=bucket_1 - - '' readFiles,listBuckets
"""
appKeyId3 goodKeyName-Four - - - '' %s
""" % (','.join(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 @@ -1390,14 +1399,7 @@ def test_get_account_info(self):
{
"bucketId": None,
"bucketName": None,
"capabilities":
[
"listKeys", "writeKeys", "deleteKeys", "listBuckets", "writeBuckets",
"deleteBuckets", "readBucketEncryption", "writeBucketEncryption",
"readBucketRetentions", "writeBucketRetentions", "writeFileRetentions",
"writeFileLegalHolds", "readFileRetentions", "readFileLegalHolds",
"listFiles", "readFiles", "shareFiles", "writeFiles", "deleteFiles"
],
"capabilities": ALL_CAPABILITIES,
"namePrefix": None
},
"apiUrl": "http://api.example.com",
Expand Down

0 comments on commit a9fe66d

Please sign in to comment.