|
7 | 7 | using System.IO;
|
8 | 8 | using System.Linq;
|
9 | 9 | using System.Threading.Tasks;
|
| 10 | +using Azure; |
10 | 11 | using Microsoft.Azure.KeyVault;
|
11 | 12 | using Microsoft.Azure.KeyVault.Models;
|
12 | 13 | using Microsoft.Azure.Services.AppAuthentication;
|
@@ -369,6 +370,133 @@ public async Task GetSecretSnapshots_ReturnsExpected(SecretsRepositoryType repos
|
369 | 370 | }
|
370 | 371 | }
|
371 | 372 |
|
| 373 | + [Theory] |
| 374 | + [InlineData(SecretsRepositoryType.BlobStorage)] |
| 375 | + [InlineData(SecretsRepositoryType.BlobStorageSas)] |
| 376 | + public async Task BlobRepository_WriteAsync_DoesNot_ClearBlobContents(SecretsRepositoryType repositoryType) |
| 377 | + { |
| 378 | + using (var directory = new TempDirectory()) |
| 379 | + { |
| 380 | + await _fixture.TestInitialize(repositoryType, directory.Path); |
| 381 | + |
| 382 | + ScriptSecrets testSecrets = new HostSecrets() |
| 383 | + { |
| 384 | + MasterKey = new Key("master", "test"), |
| 385 | + FunctionKeys = new List<Key>() { new Key(KeyName, "test") }, |
| 386 | + SystemKeys = new List<Key>() { new Key(KeyName, "test") } |
| 387 | + }; |
| 388 | + |
| 389 | + string testFunctionName = "host"; |
| 390 | + |
| 391 | + var target = _fixture.GetNewSecretRepository(); |
| 392 | + |
| 393 | + // Set up initial secrets. |
| 394 | + await _fixture.WriteSecret(testFunctionName, testSecrets); |
| 395 | + |
| 396 | + // Perform a write and read similtaneously. Previously, our usage of OpenWriteAsync |
| 397 | + // would erase the content of the blob while writing, resulting in null secrets from the |
| 398 | + // read. |
| 399 | + Task writeTask = target.WriteAsync(ScriptSecretsType.Host, testFunctionName, testSecrets); |
| 400 | + HostSecrets secretsContent = await target.ReadAsync(ScriptSecretsType.Host, testFunctionName) as HostSecrets; |
| 401 | + |
| 402 | + await writeTask; |
| 403 | + |
| 404 | + Assert.Equal(secretsContent.MasterKey.Name, "master"); |
| 405 | + Assert.Equal(secretsContent.MasterKey.Value, "test"); |
| 406 | + Assert.Equal(secretsContent.FunctionKeys[0].Name, KeyName); |
| 407 | + Assert.Equal(secretsContent.FunctionKeys[0].Value, "test"); |
| 408 | + Assert.Equal(secretsContent.SystemKeys[0].Name, KeyName); |
| 409 | + Assert.Equal(secretsContent.SystemKeys[0].Value, "test"); |
| 410 | + } |
| 411 | + } |
| 412 | + |
| 413 | + [Theory] |
| 414 | + [InlineData(SecretsRepositoryType.BlobStorage)] |
| 415 | + [InlineData(SecretsRepositoryType.BlobStorageSas)] |
| 416 | + public async Task BlobRepository_SimultaneousWrites_Throws_PreconditionFailed(SecretsRepositoryType repositoryType) |
| 417 | + { |
| 418 | + using (var directory = new TempDirectory()) |
| 419 | + { |
| 420 | + await _fixture.TestInitialize(repositoryType, directory.Path); |
| 421 | + |
| 422 | + HostSecrets testSecrets = new HostSecrets() |
| 423 | + { |
| 424 | + MasterKey = new Key("master", "test"), |
| 425 | + FunctionKeys = new List<Key>() { new Key(KeyName, "test") }, |
| 426 | + SystemKeys = new List<Key>() { new Key(KeyName, "test") } |
| 427 | + }; |
| 428 | + |
| 429 | + string testFunctionName = "host"; |
| 430 | + |
| 431 | + var target = _fixture.GetNewSecretRepository(); |
| 432 | + |
| 433 | + // Set up initial secrets. |
| 434 | + await _fixture.WriteSecret(testFunctionName, testSecrets); |
| 435 | + HostSecrets secretsContent = await target.ReadAsync(ScriptSecretsType.Host, testFunctionName) as HostSecrets; |
| 436 | + Assert.Equal("test", secretsContent.FunctionKeys.Single().Value); |
| 437 | + |
| 438 | + testSecrets.FunctionKeys.Single().Value = "changed"; |
| 439 | + |
| 440 | + // Simultaneous writes will result in one of the writes being discarded due to |
| 441 | + // non-matching ETag. |
| 442 | + Task writeTask1 = target.WriteAsync(ScriptSecretsType.Host, testFunctionName, testSecrets); |
| 443 | + Task writeTask2 = target.WriteAsync(ScriptSecretsType.Host, testFunctionName, testSecrets); |
| 444 | + |
| 445 | + var ex = await Assert.ThrowsAsync<RequestFailedException>(() => Task.WhenAll(writeTask1, writeTask2)); |
| 446 | + |
| 447 | + // Ensure the write went through. |
| 448 | + secretsContent = await target.ReadAsync(ScriptSecretsType.Host, testFunctionName) as HostSecrets; |
| 449 | + Assert.Equal("changed", secretsContent.FunctionKeys.Single().Value); |
| 450 | + |
| 451 | + Assert.Equal("ConditionNotMet", ex.ErrorCode); |
| 452 | + Assert.Equal(412, ex.Status); |
| 453 | + Assert.True(writeTask1.IsCompletedSuccessfully || writeTask2.IsCompletedSuccessfully, |
| 454 | + "One of the write operations should have completed successfully."); |
| 455 | + } |
| 456 | + } |
| 457 | + |
| 458 | + [Theory] |
| 459 | + [InlineData(SecretsRepositoryType.BlobStorage)] |
| 460 | + [InlineData(SecretsRepositoryType.BlobStorageSas)] |
| 461 | + public async Task BlobRepository_SimultaneousCreates_Throws_Conflict(SecretsRepositoryType repositoryType) |
| 462 | + { |
| 463 | + using (var directory = new TempDirectory()) |
| 464 | + { |
| 465 | + await _fixture.TestInitialize(repositoryType, directory.Path); |
| 466 | + |
| 467 | + HostSecrets testSecrets = new HostSecrets() |
| 468 | + { |
| 469 | + MasterKey = new Key("master", "test"), |
| 470 | + FunctionKeys = new List<Key>() { new Key(KeyName, "test") }, |
| 471 | + SystemKeys = new List<Key>() { new Key(KeyName, "test") } |
| 472 | + }; |
| 473 | + |
| 474 | + string testFunctionName = "host"; |
| 475 | + |
| 476 | + var target = _fixture.GetNewSecretRepository(); |
| 477 | + |
| 478 | + // Ensure nothing is there. |
| 479 | + HostSecrets secretsContent = await target.ReadAsync(ScriptSecretsType.Host, testFunctionName) as HostSecrets; |
| 480 | + Assert.Null(secretsContent); |
| 481 | + |
| 482 | + // Simultaneous creates will result in one of the writes being discarded due to |
| 483 | + // non-matching ETag. |
| 484 | + Task writeTask1 = target.WriteAsync(ScriptSecretsType.Host, testFunctionName, testSecrets); |
| 485 | + Task writeTask2 = target.WriteAsync(ScriptSecretsType.Host, testFunctionName, testSecrets); |
| 486 | + |
| 487 | + var ex = await Assert.ThrowsAsync<RequestFailedException>(() => Task.WhenAll(writeTask1, writeTask2)); |
| 488 | + |
| 489 | + // Ensure the write went through. |
| 490 | + secretsContent = await target.ReadAsync(ScriptSecretsType.Host, testFunctionName) as HostSecrets; |
| 491 | + Assert.Equal("test", secretsContent.FunctionKeys.Single().Value); |
| 492 | + |
| 493 | + Assert.Equal("BlobAlreadyExists", ex.ErrorCode); |
| 494 | + Assert.Equal(409, ex.Status); |
| 495 | + Assert.True(writeTask1.IsCompletedSuccessfully || writeTask2.IsCompletedSuccessfully, |
| 496 | + "One of the write operations should have completed successfully."); |
| 497 | + } |
| 498 | + } |
| 499 | + |
372 | 500 | public class Fixture : IDisposable
|
373 | 501 | {
|
374 | 502 | public Fixture()
|
|
0 commit comments