Azure functions 从功能应用程序访问虚拟网络中的Cosmos DB

Azure functions 从功能应用程序访问虚拟网络中的Cosmos DB,azure-functions,azure-cosmosdb,azure-virtual-network,Azure Functions,Azure Cosmosdb,Azure Virtual Network,很抱歉写了这么长的帖子,但是我已经在这个问题上工作了好几天,似乎找不到解决办法 处境 我想通过将Azure中的Cosmos DB置于虚拟网络(VNet)中来限制对其的访问,并且只允许通过功能应用程序进行访问 方法 第一部分很简单:创建一个Cosmos DB和一个VNet,并按照描述配置数据库的服务端点 将函数应用程序连接到VNet可以通过两种方式完成 使用网关 根据指南,我已经用网关子网配置了我的VNet,并添加和配置了VPN网关,如前所述 之后,我将网关子网添加到Cosmos DB服务端点配置

很抱歉写了这么长的帖子,但是我已经在这个问题上工作了好几天,似乎找不到解决办法

处境 我想通过将Azure中的Cosmos DB置于虚拟网络(VNet)中来限制对其的访问,并且只允许通过功能应用程序进行访问

方法 第一部分很简单:创建一个Cosmos DB和一个VNet,并按照描述配置数据库的服务端点

将函数应用程序连接到VNet可以通过两种方式完成

使用网关 根据指南,我已经用网关子网配置了我的VNet,并添加和配置了VPN网关,如前所述

之后,我将网关子网添加到Cosmos DB服务端点配置中,并设置我的函数应用程序

使用VNet(预览)功能选择子网 此方法易于设置,但如中所述,它不适用于生产工作负载。
配置更简单,但由于结果相同,我在本文的其余部分省略了这个选项

配置 网络

网关

宇宙数据库

功能应用程序

问题 尝试从我的功能应用程序连接到Cosmos DB时,我不断收到连接错误:

{
    "Message": "An error has occurred.",
    "ExceptionMessage": "Unable to proceed with the request. Please check the authorization claims to ensure the required permissions to process the request.\r\nActivityId: f784890f-2e1a-4e36-bfb9-8f62ff32c034, Microsoft.Azure.Documents.Common/2.2.0.0, Windows/10.0.14393 documentdb-netcore-sdk/2.2.2",
    "ExceptionType": "Microsoft.Azure.Documents.DocumentClientException",
    "StackTrace": "   at Microsoft.Azure.Documents.Client.ClientExtensions.ParseResponseAsync(HttpResponseMessage responseMessage, JsonSerializerSettings serializerSettings, Boolean throwOnKnownClientErrorCodes)\r\n   at Microsoft.Azure.Documents.Client.GatewayServiceConfigurationReader.GetDatabaseAccountAsync(Uri serviceEndpoint)\r\n   at Microsoft.Azure.Documents.Routing.GlobalEndpointManager.GetDatabaseAccountFromAnyLocationsAsync(Uri defaultEndpoint, IList`1 locations, Func`2 getDatabaseAccountFn)\r\n   at Microsoft.Azure.Documents.Client.GatewayServiceConfigurationReader.InitializeReaderAsync()\r\n   at Microsoft.Azure.Documents.Client.DocumentClient.InitializeGatewayConfigurationReader()\r\n   at Microsoft.Azure.Documents.Client.DocumentClient.GetInitializationTask()\r\n   at Microsoft.Azure.Documents.Client.DocumentClient.EnsureValidClientAsync()\r\n   at Microsoft.Azure.Documents.Client.DocumentClient.ReadDatabasePrivateAsync(String databaseLink, RequestOptions options, IDocumentClientRetryPolicy retryPolicyInstance)\r\n   at Microsoft.Azure.Documents.BackoffRetryUtility`1.<>c__DisplayClass1_0.<<ExecuteAsync>b__0>d.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at Microsoft.Azure.Documents.BackoffRetryUtility`1.ExecuteRetryAsync(Func`1 callbackMethod, Func`3 callShouldRetry, Func`1 inBackoffAlternateCallbackMethod, TimeSpan minBackoffForInBackoffCallback, CancellationToken cancellationToken, Action`1 preRetryCallback)\r\n   at Microsoft.Azure.Documents.ShouldRetryResult.ThrowIfDoneTrying(ExceptionDispatchInfo capturedException)\r\n   at Microsoft.Azure.Documents.BackoffRetryUtility`1.ExecuteRetryAsync(Func`1 callbackMethod, Func`3 callShouldRetry, Func`1 inBackoffAlternateCallbackMethod, TimeSpan minBackoffForInBackoffCallback, CancellationToken cancellationToken, Action`1 preRetryCallback)\r\n   at Microsoft.Azure.Documents.BackoffRetryUtility`1.ExecuteAsync(Func`1 callbackMethod, IRetryPolicy retryPolicy, CancellationToken cancellationToken, Action`1 preRetryCallback)\r\n   at Microsoft.Azure.Documents.Client.DocumentClient.CreateDatabaseIfNotExistsPrivateAsync(Database database, RequestOptions options)\r\n   at VnetTestFunction.Function1.Run(HttpRequest req, ILogger log) in C:\\Development\\VnetTestFunction\\VnetTestFunction\\Function1.cs:line 39"
}
{
“消息”:“发生错误。”,
“ExceptionMessage”:“无法继续请求。请检查授权声明以确保处理请求所需的权限。\r\n活动ID:f784890f-2e1a-4e36-bfb9-8f62ff32c034,Microsoft.Azure.Documents.Common/2.2.0,Windows/10.0.14393 documentdb netcore sdk/2.2.2”,
“例外类型”:“Microsoft.Azure.Documents.DocumentClientException”,
“StackTrace”:“位于Microsoft.Azure.Documents.Client.ClientExtensions.ParseResponseAsync(HttpResponseMessage responseMessage,JsonSerializerSettings serializerSettings,Boolean throwOnKnownClientErrorCodes)\r\n位于Microsoft.Azure.Documents.Client.GatewayServiceConfigurationReader.GetDatabaseAccountAsync(Uri serviceEndpoint)\r\n位于Microsoft.Azure.Documents.Routing.GlobalEndpointManager.GetDatabaseAccountFromAnyLocationsAsync(Uri defaultEndpoint,IList`1个位置,Func`2 getDatabaseAccountFn)\r\n位于Microsoft.Azure.Documents.Client.GatewayServiceConfigurationReader.InitializeReaderAsync()\r\n位于Microsoft.Azure.Documents.Client.DocumentClient.InitializeGatewayConfigurationReader()\r\n位于Microsoft.Azure.Documents.Client.DocumentClient.GetInitializationTask()\r\n位于Microsoft.Azure.Documents.Client.DocumentClient.EnsureValidClientAsync()\r\n位于Microsoft.Azure.Documents.Client.DocumentClient.ReadDatabasePrivateAync(字符串databaseLink,RequestOptions options,IDocumentClientRetryPolicy retryPolicyInstance)\r\n位于Microsoft.Azure.Documents.BackoffRetryUtility`1.c\u DisplayClass1\u 0.d.MoveNext()\r\n---来自引发异常的上一个位置的堆栈结束跟踪---\r\n位于Microsoft.Azure.Documents.BackoffRetryUtility`1.ExecuteTryAsync(Func`1 callbackMethod、Func`3 callShouldRetry、Func`1 inBackoffAlternateCallbackMethod、TimeSpan minBackoffForInBackoffCallback、CancellationToken CancellationToken、Action`1 preRetryCallback)\r\n在Microsoft.Azure.Documents.ShouldRetryResult.ThrowIfDoneTrying上(DispatchInfo CaptureException除外)\r\n在Microsoft.Azure.Documents.BackoffRetryUtility`1.ExecuteTryAsync(Func`1 callbackMethod,Func`3 callShouldRetry,Func`1 inBackoffAlternateCallbackMethod,TimeSpan minBackoffForInBackoffCallback,CancellationToken CancellationToken,Action`1 preRetryCallback)\r\n在Microsoft.Azure.Documents.BackoffRetryUtility`1.ExecuteAsync(Microsoft.Azure.Documents.Client.DocumentClient.CreateDatabaseIfNoteExistsSprivateAsync(数据库,RequestOptions选项)上的Func`1 callbackMethod,IRetryPolicy,CancellationToken CancellationToken,Action`1 preRetryCallback)\r\n在C:\\Development\\VnetTestFunction\\VnetTestFunction\\Function1.cs中:第39行“
}
两种方法之间没有区别。在这两种情况下,我的函数应用程序和数据库之间没有连接

作为测试,我按照教程添加了一个虚拟机并设置了一个代理。这很有效,所以函数应用程序可以访问VNet

我的直觉告诉我,这是某个地方的配置问题,但我无法找到位置。我会继续搜索,但非常感谢任何帮助

使用的在线资源

我相信您的配置是正确的,因为这两种方法都不应该像您所希望的那样工作

例如,您可以使用此网关安全地访问VNet中的资源

VNet集成使您的web应用程序(或功能应用程序)能够访问您的应用程序中的资源 虚拟网络,但不从授予对您的web应用的私人访问权限 虚拟网络

此外,Cosmos DB不是Azure VNet内的资源,实际上您没有使用此方法限制Azure函数对Cosmos DB的访问。您的函数应用程序已连接到Internet和您的VNet。它仍然可以通过Internet从函数应用程序到Cosmos DB

此外,一旦您在特定子网中启用Cosmos DB服务端点,这确实会限制通过虚拟网络中此授权子网的连接访问Azure Cosmos DB帐户。如果您仅在Cosmos DB的防火墙中配置授权子网,我认为只有来自授权子网的请求才能绕过防火墙在这种情况下,Azure函数也不是授权子网中的资源

当您在Azure VNet中添加VM并按照的过程设置代理时。在此场景中,VM是部署在Azure VNet中的资源。因此,您可以使用VNet集成功能在专用VNet中访问它

据我所知,如果您希望在VNet中部署Azure Function应用程序,可以在部署Azure应用程序服务的中部署应用程序服务