Skip to content
This repository has been archived by the owner on Nov 1, 2023. It is now read-only.

Commit

Permalink
Porting divergent code introduced in #2116 (#2121)
Browse files Browse the repository at this point in the history
Make the log sas url last as long as the job duration
  • Loading branch information
chkeita authored Jul 5, 2022
1 parent ab0b974 commit 52b760b
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/ApiService/ApiService/onefuzzlib/Containers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public interface IContainers {

public Async.Task<bool> BlobExists(Container container, string name, StorageType storageType);

public Async.Task<Uri> AddContainerSasUrl(Uri uri);
public Async.Task<Uri> AddContainerSasUrl(Uri uri, TimeSpan? duration = null);
}


Expand All @@ -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;
Expand Down Expand Up @@ -160,16 +162,19 @@ public async Async.Task SaveBlob(Container container, string name, string data,
return sas;
}

public async Async.Task<Uri> AddContainerSasUrl(Uri uri) {
public async Async.Task<Uri> 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();
Expand All @@ -180,7 +185,7 @@ public async Async.Task<Uri> AddContainerSasUrl(Uri uri) {

public async Async.Task<Uri> 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
Expand Down

0 comments on commit 52b760b

Please sign in to comment.