Skip to content

Commit

Permalink
[347] Container SAS token is generated for internal TES URLs (#348)
Browse files Browse the repository at this point in the history
  • Loading branch information
giventocode authored Aug 9, 2023
1 parent cc53a4e commit b0ae6e8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
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

0 comments on commit b0ae6e8

Please sign in to comment.