- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如果我正确阅读了 Microsoft 的文档,并且根据与一位在 Azure Functions 使用的 Web 开发范例方面有一定经验的 friend 的对话,我知道 Azure Functions 可能是互联网上的开放端点。粗略阅读有关该主题的安全论坛和堆栈溢出问题使我至少了解了几种保护它们的选项,即
上下文/我的 Azure 函数有什么作用?它管理与从 SFTP 源到 SQL 端点的供应商数据 ETL 相关的 Blob 容器,该 ETL 利用中间 Blob 容器进行文件传输和源数据的长期冷存储。在将 Blob 加载到 SQL 终结点后,Azure Function 将它们从一个容器移动到存档容器。为什么使用 Azure Function 来管理 Blob 容器?
下面显示了其中一个函数的示例:
using System.IO;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.Net.Http;
using System.Net;
using Microsoft.WindowsAzure.Storage.Blob;
using System.Collections.Generic;
using System.Text;
namespace AFA_ArchiveBlob
{
public static class HttpTrigger_BlobInput
{
[FunctionName("HttpTrigger_BlobInput")]
public static async Task<HttpResponseMessage> Run(
//public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = "{name}")] HttpRequest req,
string name,
ILogger log,
[Blob("{name}/blobname",FileAccess.ReadWrite,Connection = "AzureWebJobsStorage")] CloudBlobContainer myCloudBlobContainer
)
{
//Execution Logged.
log.LogInformation($"HttpTrigger_BlobInput - C# HTTP trigger function processed a request.");
//Run the query against the blob to list the contents.
BlobContinuationToken continuationToken = null;
List<IListBlobItem> results = new List<IListBlobItem>();
do
{
var response = await myCloudBlobContainer.ListBlobsSegmentedAsync(continuationToken);
continuationToken = response.ContinuationToken;
results.AddRange(response.Results);
}
while (continuationToken != null);
//Query the names of the blobs. Todo: can this be a single line linq query select instead?
List<string> listBlobNames = new List<string>();
foreach (CloudBlockBlob b in results)
{
listBlobNames.Add(b.Name);
}
//Serialize the list of blob names to json for passing to function caller via return statement
var jsonReturn = JsonConvert.SerializeObject(listBlobNames);
log.LogInformation("Returning the following JSON");
log.LogInformation(jsonReturn);
return new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(jsonReturn, Encoding.UTF8, "application/json")
};
}
}
}
最佳答案
首先,尽管使用按键可能很方便,但我看到 official documentation建议不要在生产场景中使用 key 来保护函数端点。
为了安全起见,我建议使用 Azure Active Directory 是更好的选择。如此处所述 Secure an HTTP endpoint in production
如何实现
我看到两种可能的方法:
<强>1。简单方法:检查调用应用程序是否专门是您的 Azure 逻辑应用
为您的 Azure Function 应用启用 Azure Active Directory 身份验证。您可以简单地使用 Express 设置(通过创建新的 Azure AD 应用程序)
为您的逻辑应用启用托管服务身份。
找出与逻辑应用关联的托管服务标识的 appid。转到 Azure 门户 > Azure Active Directory > 企业应用程序 > 所有应用程序 > 相关服务主体(在 another SO post here 中通过屏幕截图进行了更详细的说明)
使用托管服务标识对您的逻辑应用进行 Azure 功能身份验证,如下所述。 Authenticate with managed identity in logic app ..请注意,正在访问的资源将是您的 Azure 函数。
在您的函数代码中,现在您可以检查访问 token 中的 appid
声明是否应与逻辑应用的 appid
完全匹配(即逻辑应用是调用您的函数的应用) )..否则您可以拒绝调用并显示未经授权的异常(exception)。
<强>2。更具声明性的方法:为 Azure 函数应用程序定义应用程序权限,并检查调用函数的客户端的身份验证 token 中是否存在此权限/角色
此方法更具声明性,因为您定义了一个应用程序权限,需要将该权限分配给可以调用您的 Azure 函数的任何应用程序。
为您的 Azure Function 应用启用 Azure Active Directory 身份验证。您可以简单地使用 Express 设置(通过创建新的 Azure AD 应用程序)
现在转到 Azure Active Directory > 应用程序注册 > 函数应用的应用程序注册 > list
添加一个新的应用程序角色..使用 json,如下所示:
"appRoles": [
{
"allowedMemberTypes": [
"Application"
],
"displayName": "Can invoke my function",
"id": "fc803414-3c61-4ebc-a5e5-cd1675c14bbb",
"isEnabled": true,
"description": "Apps that have this role have the ability to invoke my Azure function",
"value": "MyFunctionValidClient"
}]
为您的逻辑应用启用托管服务身份。
找出与您的逻辑应用关联的托管服务标识的 appid。如上面方法 1 中已说明的那样
将应用程序权限分配给此托管服务身份。
New-AzureADServiceAppRoleAssignment -ObjectId <logicappmsi.ObjectId> -PrincipalId <logicappmsi.ObjectId> -Id "fc803414-3c61-4ebc-a5e5-cd1675c14bbb" -ResourceId <yourfunctionaadapp.ObjectId>
使用托管服务标识向 Azure 功能验证您的逻辑应用..如上面方法 1 中所述
现在,在您的函数收到的身份验证 token 中,您可以检查 role
声明集合必须包含名为 "MyFunctionValidClient"
的角色,否则您可以拒绝调用未经授权的异常(exception)。
关于azure - 有没有办法保护只能从特定 Azure 逻辑应用程序调用的 Azure 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55407966/
我是一名优秀的程序员,十分优秀!