@@ -581,6 +581,62 @@ def test_create_bucket_key_and_authorize_with_it(self):
581581 0 ,
582582 )
583583
584+ def test_create_multi_bucket_key_and_authorize_with_it (self ):
585+ # Start with authorizing with the master key
586+ self ._authorize_account ()
587+
588+ # Make two buckets
589+ self ._run_command (['bucket' , 'create' , 'my-bucket-0' , 'allPrivate' ], 'bucket_0\n ' , '' , 0 )
590+ self ._run_command (['bucket' , 'create' , 'my-bucket-1' , 'allPrivate' ], 'bucket_1\n ' , '' , 0 )
591+
592+ # Create a key restricted to those buckets
593+ self ._run_command (
594+ [
595+ 'key' ,
596+ 'create' ,
597+ '--bucket' ,
598+ 'my-bucket-0' ,
599+ '--bucket' ,
600+ 'my-bucket-1' ,
601+ 'key1' ,
602+ 'listKeys,listBuckets' ,
603+ ],
604+ 'appKeyId0 appKey0\n ' ,
605+ '' ,
606+ 0 ,
607+ )
608+
609+ # test deprecated command
610+ self ._run_command (
611+ [
612+ 'create-key' ,
613+ '--bucket' ,
614+ 'my-bucket-0' ,
615+ '--bucket' ,
616+ 'my-bucket-1' ,
617+ 'key2' ,
618+ 'listKeys,listBuckets' ,
619+ ],
620+ 'appKeyId1 appKey1\n ' ,
621+ 'WARNING: `create-key` command is deprecated. Use `key create` instead.\n ' ,
622+ 0 ,
623+ )
624+
625+ # Authorize with the key
626+ self ._run_command (
627+ ['account' , 'authorize' , 'appKeyId0' , 'appKey0' ],
628+ None ,
629+ '' ,
630+ 0 ,
631+ )
632+
633+ self ._run_command (
634+ ['account' , 'authorize' , 'appKeyId1' , 'appKey1' ],
635+ None ,
636+ '' ,
637+ 0 ,
638+ )
639+
584640 def test_update_bucket_without_lifecycle (self ):
585641 # Start with authorizing with the master key
586642 self ._authorize_account ()
@@ -1028,6 +1084,109 @@ def test_keys(self):
10281084 1 ,
10291085 )
10301086
1087+ def test_multi_bucket_keys (self ):
1088+ self ._authorize_account ()
1089+
1090+ self ._run_command (['bucket' , 'create' , 'my-bucket-a' , 'allPublic' ], 'bucket_0\n ' , '' , 0 )
1091+ self ._run_command (['bucket' , 'create' , 'my-bucket-b' , 'allPublic' ], 'bucket_1\n ' , '' , 0 )
1092+ self ._run_command (['bucket' , 'create' , 'my-bucket-c' , 'allPublic' ], 'bucket_2\n ' , '' , 0 )
1093+
1094+ capabilities = ['readFiles' , 'listBuckets' ]
1095+ capabilities_with_commas = ',' .join (capabilities )
1096+
1097+ # Create a multi-bucket key with one of the buckets having invalid name
1098+ expected_stderr = 'Bucket not found: invalid. If you believe it exists, run `b2 bucket list` to reset cache, then try again.\n '
1099+
1100+ self ._run_command (
1101+ [
1102+ 'key' ,
1103+ 'create' ,
1104+ '--bucket' ,
1105+ 'my-bucket-a' ,
1106+ '--bucket' ,
1107+ 'invalid' ,
1108+ 'goodKeyName' ,
1109+ capabilities_with_commas ,
1110+ ],
1111+ '' ,
1112+ expected_stderr ,
1113+ 1 ,
1114+ )
1115+
1116+ # Create a multi-bucket key
1117+ self ._run_command (
1118+ [
1119+ 'key' ,
1120+ 'create' ,
1121+ '--bucket' ,
1122+ 'my-bucket-a' ,
1123+ '--bucket' ,
1124+ 'my-bucket-b' ,
1125+ 'goodKeyName' ,
1126+ capabilities_with_commas ,
1127+ ],
1128+ 'appKeyId0 appKey0\n ' ,
1129+ '' ,
1130+ 0 ,
1131+ )
1132+
1133+ # List keys
1134+ expected_list_keys_out = 'appKeyId0 goodKeyName\n '
1135+
1136+ expected_list_keys_out_long = """
1137+ appKeyId0 goodKeyName my-bucket-a, my-bucket-b - - '' readFiles,listBuckets
1138+ """
1139+
1140+ self ._run_command (['key' , 'list' ], expected_list_keys_out , '' , 0 )
1141+ self ._run_command (['key' , 'list' , '--long' ], expected_list_keys_out_long , '' , 0 )
1142+
1143+ # authorize and make calls using an application key with bucket restrictions
1144+ self ._run_command (['account' , 'authorize' , 'appKeyId0' , 'appKey0' ], None , '' , 0 )
1145+
1146+ self ._run_command (
1147+ ['bucket' , 'list' ],
1148+ '' ,
1149+ "ERROR: Application key is restricted to buckets: ['my-bucket-a', 'my-bucket-b']\n " ,
1150+ 1 ,
1151+ )
1152+ self ._run_command (
1153+ ['bucket' , 'get' , 'my-bucket-c' ],
1154+ '' ,
1155+ "ERROR: Application key is restricted to buckets: ['my-bucket-a', 'my-bucket-b']\n " ,
1156+ 1 ,
1157+ )
1158+
1159+ def _get_expected_json (bucket_id : str , bucket_name : str ):
1160+ return {
1161+ 'accountId' : self .account_id ,
1162+ 'bucketId' : bucket_id ,
1163+ 'bucketInfo' : {},
1164+ 'bucketName' : bucket_name ,
1165+ 'bucketType' : 'allPublic' ,
1166+ 'corsRules' : [],
1167+ 'defaultServerSideEncryption' : {'mode' : None },
1168+ 'lifecycleRules' : [],
1169+ 'options' : [],
1170+ 'revision' : 1 ,
1171+ }
1172+
1173+ self ._run_command (
1174+ ['bucket' , 'get' , 'my-bucket-a' ],
1175+ expected_json_in_stdout = _get_expected_json ('bucket_0' , 'my-bucket-a' ),
1176+ )
1177+
1178+ self ._run_command (
1179+ ['bucket' , 'get' , 'my-bucket-b' ],
1180+ expected_json_in_stdout = _get_expected_json ('bucket_1' , 'my-bucket-b' ),
1181+ )
1182+
1183+ self ._run_command (
1184+ ['ls' , '--json' , * self .b2_uri_args ('my-bucket-c' )],
1185+ '' ,
1186+ "ERROR: Application key is restricted to buckets: ['my-bucket-a', 'my-bucket-b']\n " ,
1187+ 1 ,
1188+ )
1189+
10311190 def test_bucket_info_from_json (self ):
10321191 self ._authorize_account ()
10331192 self ._run_command (['bucket' , 'create' , 'my-bucket' , 'allPublic' ], 'bucket_0\n ' , '' , 0 )
0 commit comments