migrate timer_proxy part 2 (#1836)

This commit is contained in:
Cheick Keita
2022-04-25 10:14:15 -07:00
committed by GitHub
parent 66796148c5
commit 3a93de4801
20 changed files with 305 additions and 66 deletions

View File

@ -3,17 +3,20 @@ using Azure.ResourceManager;
using Azure.Storage.Blobs;
using Azure.Storage;
using Azure;
using Azure.Storage.Sas;
namespace Microsoft.OneFuzz.Service;
public interface IContainers
{
public Task<IEnumerable<byte>?> GetBlob(Container container, string name, StorageType storageType);
public Task<BinaryData?> GetBlob(Container container, string name, StorageType storageType);
public Async.Task<BlobContainerClient?> FindContainer(Container container, StorageType storageType);
public Uri GetFileSasUrl(Container container, string name, StorageType storageType, bool read = false, bool add = false, bool create = false, bool write = false, bool delete = false, bool delete_previous_version = false, bool tag = false, int days = 30, int hours = 0, int minutes = 0);
public Async.Task<Uri?> GetFileSasUrl(Container container, string name, StorageType storageType, BlobSasPermissions permissions, TimeSpan? duration = null);
Async.Task saveBlob(Container container, string v1, string v2, StorageType config);
Task<Guid> GetInstanceId();
}
public class Containers : IContainers
@ -29,7 +32,7 @@ public class Containers : IContainers
_creds = creds;
_armClient = creds.ArmClient;
}
public async Task<IEnumerable<byte>?> GetBlob(Container container, string name, StorageType storageType)
public async Task<BinaryData?> GetBlob(Container container, string name, StorageType storageType)
{
var client = await FindContainer(container, storageType);
@ -41,7 +44,7 @@ public class Containers : IContainers
try
{
return (await client.GetBlobClient(name).DownloadContentAsync())
.Value.Content.ToArray();
.Value.Content;
}
catch (RequestFailedException)
{
@ -85,9 +88,59 @@ public class Containers : IContainers
return new Uri($"https://{accountName}.blob.core.windows.net/");
}
public Uri GetFileSasUrl(Container container, string name, StorageType storageType, bool read = false, bool add = false, bool create = false, bool write = false, bool delete = false, bool delete_previous_version = false, bool tag = false, int days = 30, int hours = 0, int minutes = 0)
public async Async.Task<Uri?> GetFileSasUrl(Container container, string name, StorageType storageType, BlobSasPermissions permissions, TimeSpan? duration = null)
{
throw new NotImplementedException();
var client = await FindContainer(container, storageType) ?? throw new Exception($"unable to find container: {container.ContainerName} - {storageType}");
var (accountName, accountKey) = _storage.GetStorageAccountNameAndKey(client.AccountName);
var (startTime, endTime) = SasTimeWindow(duration ?? TimeSpan.FromDays(30));
var sasBuilder = new BlobSasBuilder(permissions, endTime)
{
StartsOn = startTime,
BlobContainerName = container.ContainerName,
BlobName = name
};
var sasUrl = client.GetBlobClient(name).GenerateSasUri(sasBuilder);
return sasUrl;
}
public (DateTimeOffset, DateTimeOffset) SasTimeWindow(TimeSpan timeSpan)
{
// SAS URLs are valid 6 hours earlier, primarily to work around dev
// workstations having out-of-sync time. Additionally, SAS URLs are stopped
// 15 minutes later than requested based on "Be careful with SAS start time"
// guidance.
// Ref: https://docs.microsoft.com/en-us/azure/storage/common/storage-sas-overview
var SAS_START_TIME_DELTA = TimeSpan.FromHours(6);
var SAS_END_TIME_DELTA = TimeSpan.FromMinutes(6);
// SAS_START_TIME_DELTA = datetime.timedelta(hours = 6)
//SAS_END_TIME_DELTA = datetime.timedelta(minutes = 15)
var now = DateTimeOffset.UtcNow;
var start = now - SAS_START_TIME_DELTA;
var expiry = now + timeSpan + SAS_END_TIME_DELTA;
return (start, expiry);
}
public async System.Threading.Tasks.Task saveBlob(Container container, string name, string data, StorageType storageType)
{
var client = await FindContainer(container, storageType) ?? throw new Exception($"unable to find container: {container.ContainerName} - {storageType}");
await client.UploadBlobAsync(name, new BinaryData(data));
}
public async Async.Task<Guid> GetInstanceId()
{
var blob = await GetBlob(new Container("base-config"), "instance_id", StorageType.Config);
if (blob == null)
{
throw new System.Exception("Blob Not Found");
}
return System.Guid.Parse(blob.ToString());
}
}