From 5efd272cbbaddee212a6e392b5f2beff186b951d Mon Sep 17 00:00:00 2001 From: Cheick Keita Date: Tue, 5 Jul 2022 10:35:17 -0700 Subject: [PATCH] Porting divergent code introduced in #2116 Make the log sas url last as long as the job duration --- src/ApiService/ApiService/onefuzzlib/Containers.cs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/ApiService/ApiService/onefuzzlib/Containers.cs b/src/ApiService/ApiService/onefuzzlib/Containers.cs index e58c89bafc..0257146333 100644 --- a/src/ApiService/ApiService/onefuzzlib/Containers.cs +++ b/src/ApiService/ApiService/onefuzzlib/Containers.cs @@ -23,7 +23,7 @@ public interface IContainers { public Async.Task BlobExists(Container container, string name, StorageType storageType); - public Async.Task AddContainerSasUrl(Uri uri); + public Async.Task AddContainerSasUrl(Uri uri, TimeSpan? duration = null); } @@ -34,6 +34,8 @@ public class Containers : IContainers { private ArmClient _armClient; private readonly IServiceConfig _config; + static TimeSpan CONTAINER_SAS_DEFAULT_DURATION = TimeSpan.FromDays(30); + public Containers(ILogTracer log, IStorage storage, ICreds creds, IServiceConfig config) { _log = log; _storage = storage; @@ -160,16 +162,19 @@ public async Async.Task SaveBlob(Container container, string name, string data, return sas; } - public async Async.Task AddContainerSasUrl(Uri uri) { + public async Async.Task AddContainerSasUrl(Uri uri, TimeSpan? duration = null) { if (uri.Query.Contains("sig")) { return uri; } + + var (startTime, endTime) = SasTimeWindow(duration ?? CONTAINER_SAS_DEFAULT_DURATION); var blobUriBuilder = new BlobUriBuilder(uri); var accountKey = await _storage.GetStorageAccountNameKeyByName(blobUriBuilder.AccountName); var sasBuilder = new BlobSasBuilder( BlobContainerSasPermissions.Read | BlobContainerSasPermissions.Write | BlobContainerSasPermissions.Delete | BlobContainerSasPermissions.List, - DateTimeOffset.UtcNow + TimeSpan.FromHours(1)) { + endTime) { BlobContainerName = blobUriBuilder.BlobContainerName, + StartsOn = startTime }; var sas = sasBuilder.ToSasQueryParameters(new StorageSharedKeyCredential(blobUriBuilder.AccountName, accountKey)).ToString(); @@ -180,7 +185,7 @@ public async Async.Task AddContainerSasUrl(Uri uri) { public async Async.Task GetContainerSasUrl(Container container, StorageType storageType, BlobContainerSasPermissions permissions, TimeSpan? duration = null) { var client = await FindContainer(container, storageType) ?? throw new Exception($"unable to find container: {container.ContainerName} - {storageType}"); - var (startTime, endTime) = SasTimeWindow(duration ?? TimeSpan.FromDays(30)); + var (startTime, endTime) = SasTimeWindow(duration ?? CONTAINER_SAS_DEFAULT_DURATION); var sasBuilder = new BlobSasBuilder(permissions, endTime) { StartsOn = startTime, BlobContainerName = container.ContainerName