@@ -12,6 +12,7 @@ import (
12
12
"crypto/md5"
13
13
"encoding/binary"
14
14
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
15
+ "github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/service"
15
16
"hash/crc64"
16
17
"io"
17
18
"math/rand"
@@ -371,6 +372,149 @@ func (s *AppendBlobUnrecordedTestsSuite) TestAppendBlockFromURL() {
371
372
_require .Equal (destBuffer , sourceData )
372
373
}
373
374
375
+ func (s * AppendBlobUnrecordedTestsSuite ) TestBlobEncryptionScopeSAS () {
376
+ _require := require .New (s .T ())
377
+ testName := s .T ().Name ()
378
+ svcClient , err := testcommon .GetServiceClient (s .T (), testcommon .TestAccountDefault , nil )
379
+ _require .NoError (err )
380
+
381
+ containerName := testcommon .GenerateContainerName (testName )
382
+ containerClient := testcommon .CreateNewContainer (context .Background (), _require , containerName , svcClient )
383
+ defer testcommon .DeleteContainer (context .Background (), _require , containerClient )
384
+
385
+ blobClient := containerClient .NewAppendBlobClient (testcommon .GenerateBlobName ("appendsrc" ))
386
+
387
+ // Get source abClient URL with SAS for AppendBlockFromURL.
388
+ blobParts , _ := blob .ParseURL (blobClient .URL ())
389
+
390
+ encryptionScope , err := testcommon .GetRequiredEnv (testcommon .EncryptionScopeEnvVar )
391
+ _require .Nil (err )
392
+ credential , err := testcommon .GetGenericSharedKeyCredential (testcommon .TestAccountDefault )
393
+ _require .Nil (err )
394
+ perms := sas.BlobPermissions {Read : true , Create : true , Write : true , Delete : true }
395
+
396
+ blobParts .SAS , err = sas.BlobSignatureValues {
397
+ Protocol : sas .ProtocolHTTPS , // Users MUST use HTTPS (not HTTP)
398
+ ExpiryTime : time .Now ().UTC ().Add (48 * time .Hour ), // 48-hours before expiration
399
+ ContainerName : blobParts .ContainerName ,
400
+ BlobName : blobParts .BlobName ,
401
+ Permissions : perms .String (),
402
+ EncryptionScope : encryptionScope ,
403
+ }.SignWithSharedKey (credential )
404
+ _require .NoError (err )
405
+
406
+ blobURLWithSAS := blobParts .String ()
407
+
408
+ // create new client with sas url
409
+ blobClient , err = appendblob .NewClientWithNoCredential (blobURLWithSAS , nil )
410
+ _require .Nil (err )
411
+
412
+ createResponse , err := blobClient .Create (context .Background (), nil )
413
+ _require .NoError (err )
414
+ _require .Equal (* createResponse .EncryptionScope , encryptionScope )
415
+ }
416
+
417
+ func (s * AppendBlobUnrecordedTestsSuite ) TestAccountEncryptionScopeSAS () {
418
+ _require := require .New (s .T ())
419
+ testName := s .T ().Name ()
420
+ svcClient , err := testcommon .GetServiceClient (s .T (), testcommon .TestAccountDefault , nil )
421
+ _require .NoError (err )
422
+
423
+ containerName := testcommon .GenerateContainerName (testName )
424
+ containerClient := testcommon .CreateNewContainer (context .Background (), _require , containerName , svcClient )
425
+ defer testcommon .DeleteContainer (context .Background (), _require , containerClient )
426
+
427
+ blobName := testcommon .GenerateBlobName ("appendsrc" )
428
+ blobClient := containerClient .NewAppendBlobClient (blobName )
429
+
430
+ // Get blob URL with SAS for AppendBlockFromURL.
431
+ blobParts , _ := blob .ParseURL (blobClient .URL ())
432
+
433
+ encryptionScope , err := testcommon .GetRequiredEnv (testcommon .EncryptionScopeEnvVar )
434
+ _require .Nil (err )
435
+
436
+ credential , err := testcommon .GetGenericSharedKeyCredential (testcommon .TestAccountDefault )
437
+ _require .Nil (err )
438
+
439
+ blobParts .SAS , err = sas.AccountSignatureValues {
440
+ Protocol : sas .ProtocolHTTPS , // Users MUST use HTTPS (not HTTP)
441
+ ExpiryTime : time .Now ().UTC ().Add (48 * time .Hour ), // 48-hours before expiration
442
+ Permissions : to .Ptr (sas.AccountPermissions {Read : true , Create : true , Write : true , Delete : true }).String (),
443
+ ResourceTypes : to .Ptr (sas.AccountResourceTypes {Service : true , Container : true , Object : true }).String (),
444
+ EncryptionScope : encryptionScope ,
445
+ }.SignWithSharedKey (credential )
446
+ _require .NoError (err )
447
+
448
+ blobURLWithSAS := blobParts .String ()
449
+ blobClient , err = appendblob .NewClientWithNoCredential (blobURLWithSAS , nil )
450
+ _require .NoError (err )
451
+
452
+ createResp , err := blobClient .Create (context .Background (), nil )
453
+ _require .NoError (err )
454
+ _require .NotNil (createResp )
455
+ _require .Equal (* createResp .EncryptionScope , encryptionScope )
456
+ }
457
+
458
+ func (s * AppendBlobUnrecordedTestsSuite ) TestGetUserDelegationEncryptionScopeSAS () {
459
+ _require := require .New (s .T ())
460
+ testName := s .T ().Name ()
461
+ accountName , _ := testcommon .GetGenericAccountInfo (testcommon .TestAccountDefault )
462
+ _require .Greater (len (accountName ), 0 )
463
+
464
+ cred , err := testcommon .GetGenericTokenCredential ()
465
+ _require .NoError (err )
466
+
467
+ svcClient , err := service .NewClient ("https://" + accountName + ".blob.core.windows.net/" , cred , nil )
468
+ _require .NoError (err )
469
+
470
+ containerName := testcommon .GenerateContainerName (testName )
471
+ cntClientTokenCred := testcommon .CreateNewContainer (context .Background (), _require , containerName , svcClient )
472
+ defer testcommon .DeleteContainer (context .Background (), _require , cntClientTokenCred )
473
+
474
+ blobName := testcommon .GenerateBlobName ("appendsrc" )
475
+ blobClient := cntClientTokenCred .NewAppendBlobClient (blobName )
476
+
477
+ // Set current and past time and create key
478
+ now := time .Now ().UTC ().Add (- 10 * time .Second )
479
+ expiry := now .Add (2 * time .Hour )
480
+ info := service.KeyInfo {
481
+ Start : to .Ptr (now .UTC ().Format (sas .TimeFormat )),
482
+ Expiry : to .Ptr (expiry .UTC ().Format (sas .TimeFormat )),
483
+ }
484
+
485
+ udc , err := svcClient .GetUserDelegationCredential (context .Background (), info , nil )
486
+ _require .NoError (err )
487
+
488
+ // get permissions and details for sas
489
+ encryptionScope , err := testcommon .GetRequiredEnv (testcommon .EncryptionScopeEnvVar )
490
+ _require .Nil (err )
491
+
492
+ permissions := sas.BlobPermissions {Read : true , Create : true , Write : true , List : true , Add : true , Delete : true }
493
+
494
+ blobParts , _ := blob .ParseURL (blobClient .URL ())
495
+
496
+ // Create Blob Signature Values with desired permissions and sign with user delegation credential
497
+ blobParts .SAS , err = sas.BlobSignatureValues {
498
+ Protocol : sas .ProtocolHTTPS ,
499
+ StartTime : time .Now ().UTC ().Add (time .Second * - 10 ),
500
+ ExpiryTime : time .Now ().UTC ().Add (15 * time .Minute ),
501
+ Permissions : permissions .String (),
502
+ ContainerName : containerName ,
503
+ EncryptionScope : encryptionScope ,
504
+ }.SignWithUserDelegation (udc )
505
+ _require .NoError (err )
506
+
507
+ blobURLWithSAS := blobParts .String ()
508
+ blobClient , err = appendblob .NewClientWithNoCredential (blobURLWithSAS , nil )
509
+ _require .NoError (err )
510
+
511
+ createResp , err := blobClient .Create (context .Background (), nil )
512
+ _require .NoError (err )
513
+ _require .NotNil (createResp )
514
+ _require .Equal (* createResp .EncryptionScope , encryptionScope )
515
+
516
+ }
517
+
374
518
func (s * AppendBlobUnrecordedTestsSuite ) TestAppendBlockFromURLWithMD5 () {
375
519
_require := require .New (s .T ())
376
520
testName := s .T ().Name ()
0 commit comments