Skip to content

Commit 7510abd

Browse files
Use TokenCredential in search clients (Azure#22726)
* Use TokenCredential in search clients * Update public API file
1 parent b1cf60e commit 7510abd

File tree

9 files changed

+218
-6
lines changed

9 files changed

+218
-6
lines changed

sdk/search/Azure.Search.Documents/api/Azure.Search.Documents.netstandard2.0.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ public partial class SearchClient
2727
protected SearchClient() { }
2828
public SearchClient(System.Uri endpoint, string indexName, Azure.AzureKeyCredential credential) { }
2929
public SearchClient(System.Uri endpoint, string indexName, Azure.AzureKeyCredential credential, Azure.Search.Documents.SearchClientOptions options) { }
30+
public SearchClient(System.Uri endpoint, string indexName, Azure.Core.TokenCredential tokenCredential) { }
31+
public SearchClient(System.Uri endpoint, string indexName, Azure.Core.TokenCredential tokenCredential, Azure.Search.Documents.SearchClientOptions options) { }
3032
public virtual System.Uri Endpoint { get { throw null; } }
3133
public virtual string IndexName { get { throw null; } }
3234
public virtual string ServiceName { get { throw null; } }
@@ -187,6 +189,8 @@ public partial class SearchIndexClient
187189
protected SearchIndexClient() { }
188190
public SearchIndexClient(System.Uri endpoint, Azure.AzureKeyCredential credential) { }
189191
public SearchIndexClient(System.Uri endpoint, Azure.AzureKeyCredential credential, Azure.Search.Documents.SearchClientOptions options) { }
192+
public SearchIndexClient(System.Uri endpoint, Azure.Core.TokenCredential tokenCredential) { }
193+
public SearchIndexClient(System.Uri endpoint, Azure.Core.TokenCredential tokenCredential, Azure.Search.Documents.SearchClientOptions options) { }
190194
public virtual System.Uri Endpoint { get { throw null; } }
191195
public virtual string ServiceName { get { throw null; } }
192196
public virtual Azure.Response<System.Collections.Generic.IReadOnlyList<Azure.Search.Documents.Indexes.Models.AnalyzedTokenInfo>> AnalyzeText(string indexName, Azure.Search.Documents.Indexes.Models.AnalyzeTextOptions options, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
@@ -230,6 +234,8 @@ public partial class SearchIndexerClient
230234
protected SearchIndexerClient() { }
231235
public SearchIndexerClient(System.Uri endpoint, Azure.AzureKeyCredential credential) { }
232236
public SearchIndexerClient(System.Uri endpoint, Azure.AzureKeyCredential credential, Azure.Search.Documents.SearchClientOptions options) { }
237+
public SearchIndexerClient(System.Uri endpoint, Azure.Core.TokenCredential tokenCredential) { }
238+
public SearchIndexerClient(System.Uri endpoint, Azure.Core.TokenCredential tokenCredential, Azure.Search.Documents.SearchClientOptions options) { }
233239
public virtual System.Uri Endpoint { get { throw null; } }
234240
public virtual string ServiceName { get { throw null; } }
235241
public virtual Azure.Response<Azure.Search.Documents.Indexes.Models.SearchIndexerDataSourceConnection> CreateDataSourceConnection(Azure.Search.Documents.Indexes.Models.SearchIndexerDataSourceConnection dataSourceConnection, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }

sdk/search/Azure.Search.Documents/src/Indexes/SearchIndexClient.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,21 @@ public SearchIndexClient(Uri endpoint, AzureKeyCredential credential) :
5050
{
5151
}
5252

53+
/// <summary>
54+
/// Initializes a new instance of the <see cref="SearchIndexClient"/> class.
55+
/// </summary>
56+
/// <param name="endpoint">Required. The URI endpoint of the Search service. This is likely to be similar to "https://{search_service}.search.windows.net". The URI must use HTTPS.</param>
57+
/// <param name="tokenCredential">
58+
/// Required. The token credential used to authenticate requests against the Search service.
59+
/// See <see href="https://docs.microsoft.com/azure/search/search-security-rbac">Use role-based authorization in Azure Cognitive Search</see> for more information about role-based authorization in Azure Cognitive Search.
60+
/// </param>
61+
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="endpoint"/> or <paramref name="tokenCredential"/> is null.</exception>
62+
/// <exception cref="ArgumentException">Thrown when the <paramref name="endpoint"/> is not using HTTPS.</exception>
63+
public SearchIndexClient(Uri endpoint, TokenCredential tokenCredential) :
64+
this(endpoint, tokenCredential, null)
65+
{
66+
}
67+
5368
/// <summary>
5469
/// Initializes a new instance of the <see cref="SearchIndexClient"/> class.
5570
/// </summary>
@@ -79,6 +94,35 @@ public SearchIndexClient(
7994
_version = options.Version;
8095
}
8196

97+
/// <summary>
98+
/// Initializes a new instance of the <see cref="SearchIndexClient"/> class.
99+
/// </summary>
100+
/// <param name="endpoint">Required. The URI endpoint of the Search service. This is likely to be similar to "https://{search_service}.search.windows.net". The URI must use HTTPS.</param>
101+
/// <param name="tokenCredential">
102+
/// Required. The token credential used to authenticate requests against the Search service.
103+
/// See <see href="https://docs.microsoft.com/azure/search/search-security-rbac">Use role-based authorization in Azure Cognitive Search</see> for more information about role-based authorization in Azure Cognitive Search.
104+
/// </param>
105+
/// <param name="options">Client configuration options for connecting to Azure Cognitive Search.</param>
106+
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="endpoint"/> or <paramref name="tokenCredential"/> is null.</exception>
107+
/// <exception cref="ArgumentException">Thrown when the <paramref name="endpoint"/> is not using HTTPS.</exception>
108+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "AZC0006:DO provide constructor overloads that allow specifying additional options.", Justification = "Avoid ambiguous method definition")]
109+
public SearchIndexClient(
110+
Uri endpoint,
111+
TokenCredential tokenCredential,
112+
SearchClientOptions options)
113+
{
114+
Argument.AssertNotNull(endpoint, nameof(endpoint));
115+
endpoint.AssertHttpsScheme(nameof(endpoint));
116+
Argument.AssertNotNull(tokenCredential, nameof(tokenCredential));
117+
118+
options ??= new SearchClientOptions();
119+
Endpoint = endpoint;
120+
_serializer = options.Serializer;
121+
_clientDiagnostics = new ClientDiagnostics(options);
122+
_pipeline = options.Build(tokenCredential);
123+
_version = options.Version;
124+
}
125+
82126
/// <summary>
83127
/// Initializes a new instance of the <see cref="SearchIndexClient"/> class.
84128
/// </summary>

sdk/search/Azure.Search.Documents/src/Indexes/SearchIndexerClient.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,21 @@ public SearchIndexerClient(Uri endpoint, AzureKeyCredential credential) :
4949
{
5050
}
5151

52+
/// <summary>
53+
/// Initializes a new instance of the <see cref="SearchIndexerClient"/> class.
54+
/// </summary>
55+
/// <param name="endpoint">Required. The URI endpoint of the Search service. This is likely to be similar to "https://{search_service}.search.windows.net". The URI must use HTTPS.</param>
56+
/// <param name="tokenCredential">
57+
/// Required.The token credential used to authenticate requests against the Search service.
58+
/// See <see href="https://docs.microsoft.com/azure/search/search-security-rbac">Use role-based authorization in Azure Cognitive Search</see> for more information about role-based authorization in Azure Cognitive Search.
59+
/// </param>
60+
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="endpoint"/> or <paramref name="tokenCredential"/> is null.</exception>
61+
/// <exception cref="ArgumentException">Thrown when the <paramref name="endpoint"/> is not using HTTPS.</exception>
62+
public SearchIndexerClient(Uri endpoint, TokenCredential tokenCredential) :
63+
this(endpoint, tokenCredential, null)
64+
{
65+
}
66+
5267
/// <summary>
5368
/// Initializes a new instance of the <see cref="SearchIndexerClient"/> class.
5469
/// </summary>
@@ -77,6 +92,34 @@ public SearchIndexerClient(
7792
_version = options.Version;
7893
}
7994

95+
/// <summary>
96+
/// Initializes a new instance of the <see cref="SearchIndexerClient"/> class.
97+
/// </summary>
98+
/// <param name="endpoint">Required. The URI endpoint of the Search service. This is likely to be similar to "https://{search_service}.search.windows.net". The URI must use HTTPS.</param>
99+
/// <param name="tokenCredential">
100+
/// Required. The token credential used to authenticate requests against the Search service.
101+
/// See <see href="https://docs.microsoft.com/azure/search/search-security-rbac">Use role-based authorization in Azure Cognitive Search</see> for more information about role-based authorization in Azure Cognitive Search.
102+
/// </param>
103+
/// <param name="options">Client configuration options for connecting to Azure Cognitive Search.</param>
104+
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="endpoint"/> or <paramref name="tokenCredential"/> is null.</exception>
105+
/// <exception cref="ArgumentException">Thrown when the <paramref name="endpoint"/> is not using HTTPS.</exception>
106+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "AZC0006:DO provide constructor overloads that allow specifying additional options.", Justification = "Avoid ambiguous method definition")]
107+
public SearchIndexerClient(
108+
Uri endpoint,
109+
TokenCredential tokenCredential,
110+
SearchClientOptions options)
111+
{
112+
Argument.AssertNotNull(endpoint, nameof(endpoint));
113+
endpoint.AssertHttpsScheme(nameof(endpoint));
114+
Argument.AssertNotNull(tokenCredential, nameof(tokenCredential));
115+
116+
options ??= new SearchClientOptions();
117+
Endpoint = endpoint;
118+
_clientDiagnostics = new ClientDiagnostics(options);
119+
_pipeline = options.Build(tokenCredential);
120+
_version = options.Version;
121+
}
122+
80123
/// <summary>
81124
/// Gets the URI endpoint of the Search service. This is likely
82125
/// to be similar to "https://{search_service}.search.windows.net".

sdk/search/Azure.Search.Documents/src/SearchClient.cs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,40 @@ public SearchClient(
120120
{
121121
}
122122

123+
/// <summary>
124+
/// Initializes a new instance of the SearchClient class for
125+
/// querying an index and uploading, merging, or deleting documents.
126+
/// </summary>
127+
/// <param name="endpoint">
128+
/// Required. The URI endpoint of the Search Service. This is likely
129+
/// to be similar to "https://{search_service}.search.windows.net".
130+
/// The URI must use HTTPS.
131+
/// </param>
132+
/// <param name="indexName">
133+
/// Required. The name of the Search Index.
134+
/// </param>
135+
/// <param name="tokenCredential">
136+
/// Required. The token credential used to authenticate requests against the Search service.
137+
/// See <see href="https://docs.microsoft.com/azure/search/search-security-rbac">Use role-based authorization in Azure Cognitive Search</see> for
138+
/// more information about role-based authorization in Azure Cognitive Search.
139+
/// </param>
140+
/// <exception cref="ArgumentNullException">
141+
/// Thrown when the <paramref name="endpoint"/>,
142+
/// <paramref name="indexName"/>, or <paramref name="tokenCredential"/> is
143+
/// null.
144+
/// </exception>
145+
/// <exception cref="ArgumentException">
146+
/// Thrown when the <paramref name="endpoint"/> is not using HTTPS or
147+
/// the <paramref name="indexName"/> is empty.
148+
/// </exception>
149+
public SearchClient(
150+
Uri endpoint,
151+
string indexName,
152+
TokenCredential tokenCredential) :
153+
this(endpoint, indexName, tokenCredential, null)
154+
{
155+
}
156+
123157
/// <summary>
124158
/// Initializes a new instance of the SearchClient class for
125159
/// querying an index and uploading, merging, or deleting documents.
@@ -180,6 +214,65 @@ public SearchClient(
180214
Version.ToVersionString());
181215
}
182216

217+
/// <summary>
218+
/// Initializes a new instance of the SearchClient class for
219+
/// querying an index and uploading, merging, or deleting documents.
220+
/// </summary>
221+
/// <param name="endpoint">
222+
/// Required. The URI endpoint of the Search Service. This is likely
223+
/// to be similar to "https://{search_service}.search.windows.net".
224+
/// The URI must use HTTPS.
225+
/// </param>
226+
/// <param name="indexName">
227+
/// Required. The name of the Search Index.
228+
/// </param>
229+
/// <param name="tokenCredential">
230+
/// Required. The token credential used to authenticate requests against the Search service.
231+
/// See <see href="https://docs.microsoft.com/azure/search/search-security-rbac">Use role-based authorization in Azure Cognitive Search</see> for
232+
/// more information about role-based authorization in Azure Cognitive Search.
233+
/// </param>
234+
/// <param name="options">
235+
/// Client configuration options for connecting to Azure Cognitive
236+
/// Search.
237+
/// </param>
238+
/// <exception cref="ArgumentNullException">
239+
/// Thrown when the <paramref name="endpoint"/>,
240+
/// <paramref name="indexName"/>, or <paramref name="tokenCredential"/> is
241+
/// null.
242+
/// </exception>
243+
/// <exception cref="ArgumentException">
244+
/// Thrown when the <paramref name="endpoint"/> is not using HTTPS or
245+
/// the <paramref name="indexName"/> is empty.
246+
/// </exception>
247+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "AZC0006:DO provide constructor overloads that allow specifying additional options.", Justification = "Avoid ambiguous method definition")]
248+
public SearchClient(
249+
Uri endpoint,
250+
string indexName,
251+
TokenCredential tokenCredential,
252+
SearchClientOptions options)
253+
{
254+
Argument.AssertNotNull(endpoint, nameof(endpoint));
255+
endpoint.AssertHttpsScheme(nameof(endpoint));
256+
Argument.AssertNotNullOrEmpty(indexName, nameof(indexName));
257+
Argument.AssertNotNull(tokenCredential, nameof(tokenCredential));
258+
259+
options ??= new SearchClientOptions();
260+
Endpoint = endpoint;
261+
IndexName = indexName;
262+
Serializer = options.Serializer;
263+
ClientDiagnostics = new ClientDiagnostics(options);
264+
Pipeline = options.Build(tokenCredential);
265+
Version = options.Version;
266+
267+
Protocol = new DocumentsRestClient(
268+
ClientDiagnostics,
269+
Pipeline,
270+
endpoint.AbsoluteUri,
271+
indexName,
272+
null,
273+
Version.ToVersionString());
274+
}
275+
183276
/// <summary>
184277
/// Initializes a new instance of the SearchClient class from a
185278
/// <see cref="SearchIndexClient"/>.

sdk/search/Azure.Search.Documents/src/SearchClientOptions.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,23 @@ internal HttpPipeline Build(AzureKeyCredential credential)
110110
responseClassifier: null);
111111
}
112112

113+
/// <summary>
114+
/// Create an <see cref="HttpPipeline"/> to send requests to the Search service.
115+
/// </summary>
116+
/// <param name="credential">
117+
/// The <see cref="TokenCredential"/> to authenticate requests.
118+
/// </param>
119+
/// <returns>An <see cref="HttpPipeline"/> to send requests.</returns>
120+
internal HttpPipeline Build(TokenCredential credential)
121+
{
122+
Debug.Assert(credential != null);
123+
return HttpPipelineBuilder.Build(
124+
options: this,
125+
perCallPolicies: new[] { new BearerTokenAuthenticationPolicy(credential, Constants.CredentialScopeName) },
126+
perRetryPolicies: Array.Empty<HttpPipelinePolicy>(),
127+
responseClassifier: null);
128+
}
129+
113130
/// <summary>
114131
/// Add the allow list headers to the <see cref="DiagnosticsOptions"/>
115132
/// that are considered safe for logging/exceptions by default.

sdk/search/Azure.Search.Documents/src/Utilities/Constants.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ internal static class Constants
2121
/// </summary>
2222
public const string ApiKeyHeaderName = "api-key";
2323

24+
/// <summary>
25+
/// The name of the scope to authenticate for when creating a <see cref="Azure.Core.Pipeline.BearerTokenAuthenticationPolicy"/>
26+
/// </summary>
27+
public const string CredentialScopeName = "https://search.azure.com/.default";
28+
2429
/// <summary>
2530
/// Gets the representation of a NaN value.
2631
/// </summary>

sdk/search/Azure.Search.Documents/tests/SearchClientTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ public void Constructor()
2929
Assert.Throws<ArgumentNullException>(() => new SearchClient(null, indexName, new AzureKeyCredential("fake")));
3030
Assert.Throws<ArgumentNullException>(() => new SearchClient(endpoint, null, new AzureKeyCredential("fake")));
3131
Assert.Throws<ArgumentException>(() => new SearchClient(endpoint, string.Empty, new AzureKeyCredential("fake")));
32-
Assert.Throws<ArgumentNullException>(() => new SearchClient(endpoint, indexName, null));
33-
Assert.Throws<ArgumentException>(() => new SearchClient(new Uri("http://bing.com"), indexName, null));
32+
Assert.Throws<ArgumentNullException>(() => new SearchClient(endpoint, indexName, credential: null));
33+
Assert.Throws<ArgumentException>(() => new SearchClient(new Uri("http://bing.com"), indexName, credential: null));
34+
35+
Assert.Throws<ArgumentNullException>(() => new SearchClient(endpoint, indexName, tokenCredential: null));
3436
}
3537

3638
[Test]

sdk/search/Azure.Search.Documents/tests/SearchIndexClientTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ public void Constructor()
3333
Assert.AreEqual(serviceName, service.ServiceName);
3434

3535
Assert.Throws<ArgumentNullException>(() => new SearchIndexClient(null, new AzureKeyCredential("fake")));
36-
Assert.Throws<ArgumentNullException>(() => new SearchIndexClient(endpoint, null));
37-
Assert.Throws<ArgumentException>(() => new SearchIndexClient(new Uri("http://bing.com"), null));
36+
Assert.Throws<ArgumentNullException>(() => new SearchIndexClient(endpoint, credential: null));
37+
Assert.Throws<ArgumentNullException>(() => new SearchIndexClient(endpoint, tokenCredential: null));
38+
Assert.Throws<ArgumentException>(() => new SearchIndexClient(new Uri("http://bing.com"), credential: null));
3839
}
3940

4041
[Test]

sdk/search/Azure.Search.Documents/tests/SearchIndexerClientTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ public void Constructor()
3232
Assert.AreEqual(serviceName, service.ServiceName);
3333

3434
Assert.Throws<ArgumentNullException>(() => new SearchIndexerClient(null, new AzureKeyCredential("fake")));
35-
Assert.Throws<ArgumentNullException>(() => new SearchIndexerClient(endpoint, null));
36-
Assert.Throws<ArgumentException>(() => new SearchIndexerClient(new Uri("http://bing.com"), null));
35+
Assert.Throws<ArgumentNullException>(() => new SearchIndexerClient(endpoint, credential: null));
36+
Assert.Throws<ArgumentNullException>(() => new SearchIndexerClient(endpoint, tokenCredential: null));
37+
Assert.Throws<ArgumentException>(() => new SearchIndexerClient(new Uri("http://bing.com"), credential: null));
3738
}
3839

3940
[Test]

0 commit comments

Comments
 (0)