-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cosmos DB - Add Support for AAD authentication (#736)
* adding dependency * Wiring connection resolution changes * Refactoring and adding new tests * styles * defaults * Correct initialization * preview version * preview1 * removing duplicate
- Loading branch information
Showing
18 changed files
with
393 additions
and
196 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 79 additions & 2 deletions
81
src/WebJobs.Extensions.CosmosDB/Config/DefaultCosmosDBServiceFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,92 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using Azure.Core; | ||
using Microsoft.Azure.Cosmos; | ||
using Microsoft.Extensions.Azure; | ||
using Microsoft.Extensions.Configuration; | ||
|
||
namespace Microsoft.Azure.WebJobs.Extensions.CosmosDB | ||
{ | ||
internal class DefaultCosmosDBServiceFactory : ICosmosDBServiceFactory | ||
{ | ||
public CosmosClient CreateService(string connectionString, CosmosClientOptions cosmosClientOptions) | ||
private readonly IConfiguration _configuration; | ||
private readonly AzureComponentFactory _componentFactory; | ||
|
||
public DefaultCosmosDBServiceFactory( | ||
IConfiguration configuration, | ||
AzureComponentFactory componentFactory) | ||
{ | ||
this._configuration = configuration; | ||
this._componentFactory = componentFactory; | ||
} | ||
|
||
public CosmosClient CreateService(string connectionName, CosmosClientOptions cosmosClientOptions) | ||
{ | ||
CosmosConnectionInformation cosmosConnectionInformation = this.ResolveConnectionInformation(connectionName); | ||
if (cosmosConnectionInformation.UsesConnectionString) | ||
{ | ||
// Connection string based auth | ||
return new CosmosClient(cosmosConnectionInformation.ConnectionString, cosmosClientOptions); | ||
} | ||
|
||
// AAD auth | ||
return new CosmosClient(cosmosConnectionInformation.AccountEndpoint, cosmosConnectionInformation.Credential, cosmosClientOptions); | ||
} | ||
|
||
private CosmosConnectionInformation ResolveConnectionInformation(string connection) | ||
{ | ||
var connectionSetting = connection ?? Constants.DefaultConnectionStringName; | ||
IConfigurationSection connectionSection = WebJobsConfigurationExtensions.GetWebJobsConnectionStringSection(this._configuration, connectionSetting); | ||
if (!connectionSection.Exists()) | ||
{ | ||
// Not found | ||
throw new InvalidOperationException($"Cosmos DB connection configuration '{connectionSetting}' does not exist. " + | ||
$"Make sure that it is a defined App Setting."); | ||
} | ||
|
||
if (!string.IsNullOrWhiteSpace(connectionSection.Value)) | ||
{ | ||
return new CosmosConnectionInformation(connectionSection.Value); | ||
} | ||
else | ||
{ | ||
string accountEndpoint = connectionSection["accountEndpoint"]; | ||
if (string.IsNullOrWhiteSpace(accountEndpoint)) | ||
{ | ||
// Not found | ||
throw new InvalidOperationException($"Connection should have an 'accountEndpoint' property or be a " + | ||
$"string representing a connection string."); | ||
} | ||
|
||
TokenCredential credential = _componentFactory.CreateTokenCredential(connectionSection); | ||
return new CosmosConnectionInformation(accountEndpoint, credential); | ||
} | ||
} | ||
|
||
private class CosmosConnectionInformation | ||
{ | ||
return new CosmosClient(connectionString, cosmosClientOptions); | ||
public CosmosConnectionInformation(string connectionString) | ||
{ | ||
this.ConnectionString = connectionString; | ||
this.UsesConnectionString = true; | ||
} | ||
|
||
public CosmosConnectionInformation(string accountEndpoint, TokenCredential tokenCredential) | ||
{ | ||
this.AccountEndpoint = accountEndpoint; | ||
this.Credential = tokenCredential; | ||
this.UsesConnectionString = false; | ||
} | ||
|
||
public bool UsesConnectionString { get; } | ||
|
||
public string ConnectionString { get; } | ||
|
||
public string AccountEndpoint { get; } | ||
|
||
public TokenCredential Credential { get; } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.