Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[347] Container SAS token is generated for internal TES URLs #348

Merged
merged 2 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/TesApi.Tests/TerraStorageAccessProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,25 +146,27 @@ public async Task GetInternalTesBlobUrlAsync_BlobPathIsProvided_ReturnsValidURLW
}

[TestMethod]
[DataRow("script/foo.sh")]
[DataRow("/script/foo.sh")]
[DataRow("script/foo.sh", "/script/foo.sh")]
[DataRow("/script/foo.sh", "/script/foo.sh")]
[DataRow("", "")]
public async Task GetInternalTesTaskBlobUrlAsync_BlobPathIsProvided_ReturnsValidURLWithWsmContainerTaskIdAndTesPrefixAppended(
string blobName)
string blobName, string expectedBlobName)
{
SetUpTerraApiClient();
var task = new TesTask { Name = "taskName", Id = Guid.NewGuid().ToString() };
var url = await terraStorageAccessProvider.GetInternalTesTaskBlobUrlAsync(task, blobName, CancellationToken.None);

Assert.IsNotNull(url);
var uri = new Uri(url);
Assert.AreEqual($"/{TerraApiStubData.WorkspaceStorageContainerName}/{batchSchedulingOptions.Prefix}{StorageAccessProvider.TesExecutionsPathPrefix}/{task.Id}/{blobName.TrimStart('/')}", uri.AbsolutePath);
Assert.AreEqual($"/{TerraApiStubData.WorkspaceStorageContainerName}/{batchSchedulingOptions.Prefix}{StorageAccessProvider.TesExecutionsPathPrefix}/{task.Id}{expectedBlobName}", uri.AbsolutePath);
}

[TestMethod]
[DataRow("script/foo.sh")]
[DataRow("/script/foo.sh")]
[DataRow("script/foo.sh", "/script/foo.sh")]
[DataRow("/script/foo.sh", "/script/foo.sh")]
[DataRow("", "")]
public async Task GetInternalTesTaskBlobUrlAsync_BlobPathAndInternalPathPrefixIsProvided_ReturnsValidURLWithWsmContainerTaskIdAndInternalPathPrefixAppended(
string blobName)
string blobName, string expectedBlobName)
{
var internalPathPrefix = "internalPathPrefix";

Expand All @@ -179,7 +181,7 @@ public async Task GetInternalTesTaskBlobUrlAsync_BlobPathAndInternalPathPrefixIs

Assert.IsNotNull(url);
var uri = new Uri(url);
Assert.AreEqual($"/{TerraApiStubData.WorkspaceStorageContainerName}/{internalPathPrefix}/{blobName.TrimStart('/')}", uri.AbsolutePath);
Assert.AreEqual($"/{TerraApiStubData.WorkspaceStorageContainerName}/{internalPathPrefix}{expectedBlobName}", uri.AbsolutePath);
}
}
}
32 changes: 28 additions & 4 deletions src/TesApi.Web/Storage/TerraStorageAccessProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,44 @@ public override async Task<string> MapLocalPathToSasUrlAsync(string path, Cancel
return await GetMappedSasUrlFromWsmAsync(terraBlobInfo, cancellationToken);
}

/// <inheritdoc />
/// <summary>
/// Returns a URL with a SAS token for the provided blobPath.The resulting URL contains the TES internal segments as a prefix to the blobPath.
/// If the blobPath is not provided(empty), a container SAS token is generated.
/// If the blobPath is provided, a SAS token to the blobPath prefixed with the TES internal segments is generated.
/// </summary>
/// <param name="blobPath"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public override async Task<string> GetInternalTesBlobUrlAsync(string blobPath, CancellationToken cancellationToken)
{
var blobInfo = GetTerraBlobInfoForInternalTes(blobPath);

if (string.IsNullOrEmpty(blobPath))
{
return await GetMappedSasContainerUrlFromWsmAsync(blobInfo, cancellationToken);
}

return await GetMappedSasUrlFromWsmAsync(blobInfo, cancellationToken);
}

/// <inheritdoc />
/// <summary>
/// Returns a URL with a SAS token for the provided blobPath.The resulting URL contains the TES task internal segments as a prefix to the blobPath.
/// If the blobPath is not provided(empty), a container SAS token is generated.
/// If the blobPath is provided, a SAS token to the blobPath prefixed with the TES task internal segments is generated.
/// </summary>
/// <param name="task"></param>
/// <param name="blobPath"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public override async Task<string> GetInternalTesTaskBlobUrlAsync(TesTask task, string blobPath, CancellationToken cancellationToken)
{
var blobInfo = GetTerraBlobInfoForInternalTesTask(task, blobPath);

if (string.IsNullOrEmpty(blobPath))
{
return await GetMappedSasContainerUrlFromWsmAsync(blobInfo, cancellationToken);
}

return await GetMappedSasUrlFromWsmAsync(blobInfo, cancellationToken);
}

Expand Down Expand Up @@ -225,14 +250,13 @@ private Guid ToWorkspaceId(string segmentsContainerName)

private async Task<string> GetMappedSasContainerUrlFromWsmAsync(TerraBlobInfo blobInfo, CancellationToken cancellationToken)
{
//an empty blob name gets a container Sas token
var tokenInfo = await GetWorkspaceContainerSasTokenFromWsmAsync(blobInfo, cancellationToken);

var urlBuilder = new UriBuilder(tokenInfo.Url);

if (!string.IsNullOrEmpty(blobInfo.BlobName))
{
urlBuilder.Path += $"/{blobInfo.BlobName}";
urlBuilder.Path += $"/{blobInfo.BlobName.TrimStart('/')}";
}

return urlBuilder.Uri.ToString();
Expand Down