【问题标题】:How can I limit access to Azure Function App Proxy URLs?如何限制对 Azure Function App 代理 URL 的访问?
【发布时间】:2018-02-05 07:56:51
【问题描述】:

我有一个使用 OAuth2 (Google) 身份验证设置的 Azure Function App 代理,它代理对 blob 存储帐户的请求。我的想法是将代理用作存储在 blob 存储中的静态 HTML 站点的身份验证/授权层。

好的,因此身份验证工作正常,但现在任何拥有 Google 帐户的人都可以看到内容。如何控制访问权限,比如限制一组可配置的帐户?

【问题讨论】:

    标签: azure-functions azure-function-app-proxy


    【解决方案1】:

    嗯,我没有找到使用函数应用代理的快速方法,而且 API 管理似乎只是使事情变得复杂(授权服务器?对于一个带有一些自动发布的小型团队静态站点,我需要多少台服务器统计我们做什么?),所以我选择了代理功能应用程序,这里是:

    using System.Net;
    
    public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
    {
    
        var authURI = new Uri(req.RequestUri.GetLeftPart(UriPartial.Authority));
        var path = authURI.MakeRelativeUri(req.RequestUri);
        var prefix = "api/myfunction";
        var fileName = path.ToString().Remove(0,prefix.Length);
    
        // Get request body
        dynamic data = await req.Content.ReadAsAsync<object>();
    
        // Authorization - allow only @myorg.com users
        IEnumerable<string> headerValues = req.Headers.GetValues("X-MS-CLIENT-PRINCIPAL-NAME");
        var user = headerValues.FirstOrDefault();
        if (!user.EndsWith("@myorg.com", true, null))
        {
            return req.CreateResponse(HttpStatusCode.Forbidden, "Unauthorized");
        }
    
        var blobStorageURI = System.Configuration.ConfigurationManager              
             .ConnectionStrings["BLOB_SERVICE_ENDPOINT"].ConnectionString;
        var container = System.Configuration.ConfigurationManager
             .ConnectionStrings["BLOB_CONTAINER"].ConnectionString;
        var authKey = System.Configuration.ConfigurationManager
             .ConnectionStrings["BLOB_ACCESS_STRING"].ConnectionString;
        using(var client = new HttpClient())
        {
            client.BaseAddress = new Uri(blobStorageURI);
            var storagePath = "/" + container + fileName + authKey;
            return await client.GetAsync(storagePath);
        }
    }
    

    这是function.json:

    {
      "bindings": [
        {
          "authLevel": "anonymous",
          "name": "req",
          "type": "httpTrigger",
          "direction": "in",
          "route": "myfunction/{*path}",
          "methods": [
            "get",
            "head"
          ]
        },
        {
          "name": "$return",
          "type": "http",
          "direction": "out"
        }
      ],
      "disabled": false
    }
    
    • 注意路由以及路由与硬编码“myfunction”之间的关系。
    • 也可以使用 host.json 文件去掉“api”前缀。
    • 还要注意定义对 Blob 存储的访问的连接字符串。我为此创建了一个 SAS(共享访问签名)。

    【讨论】:

      猜你喜欢
      • 2016-01-04
      • 2018-08-28
      • 2020-05-06
      • 1970-01-01
      • 2018-06-11
      • 1970-01-01
      • 1970-01-01
      • 2019-03-09
      相关资源
      最近更新 更多