Skip to content

Commit 280eec8

Browse files
authored
Produce the correct Uri for TableClient.GenerateSasUri (Azure#26191)
* Produce the correct Uri for TableClient.GenerateSasUri
1 parent d1623b7 commit 280eec8

File tree

12 files changed

+732
-6
lines changed

12 files changed

+732
-6
lines changed

sdk/core/Azure.Core.TestFramework/src/TestRecording.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,10 @@ public string GetVariable(string variableName, string defaultValue, Func<string,
406406
case RecordedTestMode.Live:
407407
return defaultValue;
408408
case RecordedTestMode.Playback:
409+
if (Variables.Count == 0)
410+
{
411+
throw new TestRecordingMismatchException("The recording contains no variables.");
412+
}
409413
Variables.TryGetValue(variableName, out string value);
410414
return value;
411415
default:

sdk/tables/Azure.Data.Tables/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
### Bugs Fixed
1010
- Fixed a an issue when using `TableEntity.GetDateTime` that resulted in an `InvalidOperationException` exception. ([#25323](https://github.com/Azure/azure-sdk-for-net/issues/25323))
1111
- `TableClient.GenerateSasUri(...)` does not throw if the client was constructed via `TableServiceClient.GetTableClient(string tableName)` ([#25881](https://github.com/Azure/azure-sdk-for-net/issues/25881))
12+
- `TableClient.GenerateSasUri(...)` now generates a Uri with the table name in the path. ([#26155](https://github.com/Azure/azure-sdk-for-net/issues/26155))
1213
### Other Changes
1314

1415
## 12.3.0 (2021-11-09)

sdk/tables/Azure.Data.Tables/src/TableClient.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,11 @@ internal TableClient(Uri endpoint, string tableName, TableSharedKeyPipelinePolic
295295
var perCallPolicies = _isCosmosEndpoint ? new[] { new CosmosPatchTransformPolicy() } : Array.Empty<HttpPipelinePolicy>();
296296
HttpPipelinePolicy authPolicy = sasCredential switch
297297
{
298-
null => policy,
298+
// We were not passed an explicit SasCredential nor does one exist in the query string, default to policy
299+
null when string.IsNullOrWhiteSpace(_endpoint.Query) => policy,
300+
// The endpoint has a query string, so assume it is a SAS token
301+
null => new AzureSasCredentialSynchronousPolicy(new AzureSasCredential(_endpoint.Query)),
302+
// We were passed an explicit SasCredential, use that
299303
_ => new AzureSasCredentialSynchronousPolicy(sasCredential)
300304
};
301305
_pipeline = HttpPipelineBuilder.Build(
@@ -1425,6 +1429,13 @@ public virtual Uri GenerateSasUri(
14251429
throw new ArgumentException($"The {nameof(builder.TableName)} must match the table name used to initialize this instance of the client");
14261430
}
14271431
TableUriBuilder sasUri = new(_endpoint);
1432+
if (string.IsNullOrEmpty(sasUri.Tablename))
1433+
{
1434+
// The table name is not included in the URI, so add it while preserving the trailing slash, if it exists.
1435+
var sasUrlbuilder = new UriBuilder(_endpoint);
1436+
sasUrlbuilder.Path += sasUrlbuilder.Path.EndsWith("/", StringComparison.Ordinal) ? $"{Name}/" : $"/{Name}";
1437+
sasUri = new(sasUrlbuilder.Uri);
1438+
}
14281439
sasUri.Query = builder.ToSasQueryParameters(SharedKeyCredential).ToString();
14291440
return sasUri.ToUri();
14301441
}

sdk/tables/Azure.Data.Tables/src/TableConnectionString.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,9 +291,9 @@ internal static string GetTableNameFromUri(Uri uri)
291291
UriHostNameType hostNameType = Uri.CheckHostName(uri.Host);
292292
int expectedSegments = hostNameType switch
293293
{
294-
// This is a dns name such as contoso.core.windows.net/tableName
294+
// This is a dns name such as accountName.core.windows.net/tableName
295295
UriHostNameType.Dns => 2,
296-
// This is most likely Azurite, which looks like this: https://126.0.0.1:10002/contoso/tableName
296+
// This is most likely Azurite, which looks like this: https://127.0.0.1:10002/accountName/tableName
297297
UriHostNameType.IPv4 => 3,
298298
_ when uri.IsLoopback => 3,
299299
_ => 0

sdk/tables/Azure.Data.Tables/tests/SessionRecords/TableClientLiveTests(Storage)/ValidateSasCredentialsWithGenerateSasUri.json

Lines changed: 170 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sdk/tables/Azure.Data.Tables/tests/SessionRecords/TableClientLiveTests(Storage)/ValidateSasCredentialsWithGenerateSasUriAsync.json

Lines changed: 170 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)