Joh*_*ane 4 security azure azure-active-directory azure-logic-apps azure-functions
I understand that Azure Functions are potentially open endpoints on the internet if I read Microsoft’s documentation correctly and per conversations with a friend who has some experience working with web development paradigms that Azure Functions leverages. A cursory reading of security forums and stack overflow questions on the topic leads me to understand at least a couple options of securing them namely
Context/ What does my Azure Function do? It manages a blob container related to an ETL of vendor data from a SFTP source to a SQL Endpoint which this ETL utilizes an intermediary blob container for file transfer and long term cold storage of source data. The Azure Function moves the blobs from one container to an archive container after they have been loaded to the SQL endpoint. Why Azure Function to manage the blob containers?
An example of one of the functions is shown here below:
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")
};
}
}
}
Run Code Online (Sandbox Code Playgroud)
首先,尽管使用键可能很方便,但我看到官方文档建议不要在生产场景中使用键来保护功能端点。
我建议使用Azure Active Directory以获得安全性是一个更好的选择。如此处所述,保护生产中的HTTP终结点
如何实施
我看到两种可能的方法:
1.简单方法:检查调用应用程序是否是您的Azure逻辑应用程序
为您的Azure Function应用启用Azure Active Directory身份验证。您可以简单地使用Express设置(通过创建新的Azure AD应用程序)
为您的逻辑应用程序启用托管服务身份。
找出与您的逻辑应用程序关联的托管服务标识的appid。.转到Azure门户> Azure Active Directory>企业应用程序>所有应用程序>相关服务主体(在此处的另一个SO帖子中详细说明了屏幕快照)
使用托管服务身份对逻辑应用程序进行身份验证,如此处所述。. 在逻辑应用程序中对托管应用程序身份进行身份验证 ..请注意,被访问的资源将是您的Azure函数。
在您的函数代码中,现在您可以检查appid访问令牌中的声明是否与appidfor逻辑应用完全匹配(即,逻辑应用是调用您的函数的那个)。否则,您可以拒绝带有未授权异常的调用。
2.一种更具声明性的方法:为Azure函数应用程序定义一个应用程序权限,并从客户端调用您的函数的auth令牌中检查此权限/角色是否存在
这种方法更具声明性,因为您定义了一个应用程序权限,该权限需要分配给可以调用您的Azure函数的任何应用程序。
为您的Azure Function应用启用Azure Active Directory身份验证。您可以简单地使用Express设置(通过创建新的Azure AD应用程序)
现在转到Azure Active Directory>应用程序注册>功能应用程序的应用程序注册>清单
使用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"
}]
Run Code Online (Sandbox Code Playgroud)
为您的逻辑应用程序启用托管服务身份。
找出与您的逻辑应用程序关联的托管服务标识的appid ..如上面方法1中所述
将应用权限分配给此托管服务标识。
New-AzureADServiceAppRoleAssignment -ObjectId <logicappmsi.ObjectId> -PrincipalId <logicappmsi.ObjectId> -Id "fc803414-3c61-4ebc-a5e5-cd1675c14bbb" -ResourceId <yourfunctionaadapp.ObjectId>
Run Code Online (Sandbox Code Playgroud)
如上文方法1中所述,使用托管服务身份对逻辑应用程序进行身份验证以使用Azure功能。
现在,在函数收到的auth令牌中,您可以检查roleClaims集合中是否必须包含一个名为的角色,"MyFunctionValidClient"否则您可以拒绝带有Unauthorized异常的调用。
| 归档时间: |
|
| 查看次数: |
1133 次 |
| 最近记录: |