Skip to content

Commit 671fd38

Browse files
review fixes
1 parent 9dbcd03 commit 671fd38

File tree

2 files changed

+30
-24
lines changed

2 files changed

+30
-24
lines changed

b2/console_tool.py

+10-13
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
EncryptionSetting,
5353
EncryptionKey,
5454
BasicSyncEncryptionSettingsProvider,
55+
SSE_C_KEY_ID_FILE_INFO_KEY_NAME,
5556
)
5657
from b2sdk.v1.exception import B2Error, BadFileInfo, MissingAccountData
5758
from b2.arg_parser import ArgumentParser, parse_comma_separated_list, \
@@ -94,7 +95,7 @@
9495
B2_DESTINATION_SSE_C_KEY_B64_ENV_VAR=B2_DESTINATION_SSE_C_KEY_B64_ENV_VAR,
9596
B2_DESTINATION_SSE_C_KEY_ID_ENV_VAR=B2_DESTINATION_SSE_C_KEY_ID_ENV_VAR,
9697
B2_SOURCE_SSE_C_KEY_B64_ENV_VAR=B2_SOURCE_SSE_C_KEY_B64_ENV_VAR,
97-
FILE_INFO_KEY_ID='sse_c_key_id',
98+
SSE_C_KEY_ID_FILE_INFO_KEY_NAME=SSE_C_KEY_ID_FILE_INFO_KEY_NAME,
9899
)
99100

100101

@@ -218,7 +219,7 @@ class DestinationSseMixin(Described):
218219
Using SSE-C requires providing ``{B2_DESTINATION_SSE_C_KEY_B64_ENV_VAR}`` environment variable,
219220
containing the base64 encoded encryption key.
220221
If ``{B2_DESTINATION_SSE_C_KEY_ID_ENV_VAR}`` environment variable is provided,
221-
it's value will be saved as ``{FILE_INFO_KEY_ID}`` in the
222+
it's value will be saved as ``{SSE_C_KEY_ID_FILE_INFO_KEY_NAME}`` in the
222223
uploaded file's fileInfo.
223224
"""
224225

@@ -1641,31 +1642,27 @@ def run(self, args):
16411642
)
16421643

16431644
kwargs = {}
1644-
encryption_settings = {}
1645+
read_encryption_settings = {}
1646+
write_encryption_settings = {}
16451647
source_bucket = destination_bucket = None
16461648
destination_sse = self._get_destination_sse_setting(args)
16471649
if destination.folder_type() == 'b2':
16481650
destination_bucket = destination.bucket_name
1649-
encryption_settings[destination_bucket] = destination_sse
1651+
write_encryption_settings[destination_bucket] = destination_sse
16501652
elif destination_sse is not None:
16511653
raise ValueError('server-side encryption cannot be set for a non-b2 sync destination')
16521654

16531655
source_sse = self._get_source_sse_setting(args)
16541656
if source.folder_type() == 'b2':
16551657
source_bucket = source.bucket_name
1656-
encryption_settings[source_bucket] = source_sse
1658+
read_encryption_settings[source_bucket] = source_sse
16571659
elif source_sse is not None:
16581660
raise ValueError('server-side encryption cannot be set for a non-b2 sync source')
16591661

1660-
if source_bucket == destination_bucket and source_sse != destination_sse:
1661-
raise ValueError(
1662-
'SOURCE and DESTINATION server side encyrption settings have to be equal when syncing '
1663-
'within one bucket'
1664-
)
1665-
1666-
if encryption_settings:
1662+
if read_encryption_settings or write_encryption_settings:
16671663
kwargs['encryption_settings_provider'] = BasicSyncEncryptionSettingsProvider(
1668-
encryption_settings
1664+
read_bucket_settings=read_encryption_settings,
1665+
write_bucket_settings=write_encryption_settings,
16691666
)
16701667

16711668
with SyncReport(self.stdout, args.noProgress) as reporter:

test/integration/test_b2_command_line.py

+20-11
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from typing import Optional
3131

3232
from b2sdk.v1 import B2Api, InMemoryAccountInfo, InMemoryCache, fix_windows_path_limit
33-
from b2sdk.v1 import EncryptionAlgorithm, EncryptionMode, EncryptionSetting, EncryptionKey
33+
from b2sdk.v1 import EncryptionAlgorithm, EncryptionMode, EncryptionSetting, EncryptionKey, SSE_C_KEY_ID_FILE_INFO_KEY_NAME
3434

3535
SSE_NONE = EncryptionSetting(mode=EncryptionMode.NONE,)
3636
SSE_B2_AES = EncryptionSetting(
@@ -138,7 +138,7 @@ def remove_warnings(text):
138138
)
139139

140140

141-
def run_command(cmd, args, additional_env: Optional[dict]):
141+
def run_command(cmd, args, additional_env: Optional[dict] = None):
142142
"""
143143
:param cmd: a command to run
144144
:param args: command's arguments
@@ -166,7 +166,7 @@ def run_command(cmd, args, additional_env: Optional[dict]):
166166
stdout=subprocess.PIPE,
167167
stderr=subprocess.PIPE,
168168
close_fds=platform.system() != 'Windows',
169-
env=env
169+
env=env,
170170
)
171171
p.stdin.close()
172172
reader1 = threading.Thread(target=stdout.read_from, args=[p.stdout])
@@ -665,12 +665,15 @@ def encryption_summary(sse_dict, file_info):
665665
if isinstance(sse_dict, EncryptionSetting):
666666
sse_dict = sse_dict.as_dict()
667667
encryption = sse_dict['mode']
668+
assert encryption in (
669+
EncryptionMode.NONE.value, EncryptionMode.SSE_B2.value, EncryptionMode.SSE_C.value
670+
)
668671
algorithm = sse_dict.get('algorithm')
669672
if algorithm is not None:
670673
encryption += ':' + algorithm
671674
if sse_dict['mode'] == 'SSE-C':
672-
sse_c_key_id = file_info.get('sse_c_key_id')
673-
encryption += '?' + 'sse_c_key_id=' + str(sse_c_key_id)
675+
sse_c_key_id = file_info.get(SSE_C_KEY_ID_FILE_INFO_KEY_NAME)
676+
encryption += '?%s=%s' % (SSE_C_KEY_ID_FILE_INFO_KEY_NAME, sse_c_key_id)
674677

675678
return encryption
676679

@@ -743,7 +746,8 @@ def sync_up_helper(b2_tool, bucket_name, dir_, encryption=None):
743746
'B2_DESTINATION_SSE_C_KEY_ID': SSE_C_AES.key.key_id,
744747
}
745748
expected_encryption_str = encryption_summary(
746-
expected_encryption.as_dict(), {'sse_c_key_id': SSE_C_AES.key.key_id}
749+
expected_encryption.as_dict(),
750+
{SSE_C_KEY_ID_FILE_INFO_KEY_NAME: SSE_C_AES.key.key_id}
747751
)
748752
else:
749753
raise NotImplementedError('unsupported encryption mode: %s' % encryption)
@@ -1161,7 +1165,8 @@ def sync_copy_helper(
11611165
}
11621166
)
11631167
expected_encryption_str = encryption_summary(
1164-
expected_encryption.as_dict(), {'sse_c_key_id': destination_encryption.key.key_id}
1168+
expected_encryption.as_dict(),
1169+
{SSE_C_KEY_ID_FILE_INFO_KEY_NAME: destination_encryption.key.key_id}
11651170
)
11661171

11671172
else:
@@ -1341,7 +1346,8 @@ def sse_c_test(b2_tool, bucket_name):
13411346
}, file_version_info['serverSideEncryption']
13421347
)
13431348
should_equal(
1344-
'user-generated-key-id \nąóźćż\nœøΩ≈ç\nßäöü', file_version_info['fileInfo']['sse_c_key_id']
1349+
'user-generated-key-id \nąóźćż\nœøΩ≈ç\nßäöü',
1350+
file_version_info['fileInfo'][SSE_C_KEY_ID_FILE_INFO_KEY_NAME]
13451351
)
13461352

13471353
b2_tool.should_fail(
@@ -1498,9 +1504,12 @@ def sse_c_test(b2_tool, bucket_name):
14981504
sorted(
14991505
[
15001506
{
1501-
'sse_c_key_id': f['fileInfo'].get('sse_c_key_id', 'missing_key'),
1502-
'serverSideEncryption': f['serverSideEncryption'],
1503-
'file_name': f['fileName']
1507+
'sse_c_key_id':
1508+
f['fileInfo'].get(SSE_C_KEY_ID_FILE_INFO_KEY_NAME, 'missing_key'),
1509+
'serverSideEncryption':
1510+
f['serverSideEncryption'],
1511+
'file_name':
1512+
f['fileName']
15041513
} for f in list_of_files
15051514
],
15061515
key=lambda r: r['file_name']

0 commit comments

Comments
 (0)