将 Open Policy Agent 与 ASP.Net Core web API 集成
Integrate Open Policy Agent with ASP.Net Core web API
我浏览了一些关于 OPA(开放策略代理)的视频和教程,发现使用它来跨多个服务/API 实现身份验证和授权真的很酷。但是,我无法深入了解如何在 windows 上安装它并将其与 ASP.Net 核心 Web API 集成以实施身份验证和授权。谁能帮我解决这个问题?
谢谢,
阿米特·阿南德
在不了解您的用例或您运行正在使用的平台的情况下,这里有一些一般性建议。
架构。决定是否要 运行 OPA 作为 sidecar 或独立服务。这是一个架构问题,取决于延迟、性能和策略所需的数据。 OPA 是一个设计为边车的构建块,但您可以通过旋转多个副本、在它们之间进行负载平衡、添加持久层等来围绕 OPA 构建服务。
管理。决定how to load/update policies and log decisions to OPA and if applicable, decide how to load data into OPA.
服务集成。如果您使用的网络代理拦截所有进入您的服务的网络流量(例如 Envoy、Linkerd、Kong 等),您可以 configure your network proxy to call out to OPA without modifying your .Net service. If you're not using a network proxy, modify your .Net services to make HTTP callouts when your services need policy decisions, using a library where possible to minimize the impact on individual services. The integration page 显示 Java Spring 和 PHP.
Tim Hinrichs 上面的回答是正确的。但是,这里要添加一些特定的解决方案。在下面的 2 个解决方案中,我建议使用 REST API 和 ASP.NET 中间件。此外,虽然 OPA 理论上可以用作身份验证工具,但我建议不要这样做。它的目的是授权。
使用ASP.NET授权中间件
首先,OPA 将 运行 作为它自己的服务,作为 k8 中的 sidecar,或者在 Docker 容器中。 OPA 的文档很好地展示了如何实现它的示例,所以我不会详细说明。
在这里您将创建一个查询 OPA's Rest API.
的 .NET 服务
这就是中间件的样子。
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
namespace Authz.Opa
{
public class OpaAuthzMiddleware
{
private const string ForbiddenMessage = "Forbidden";
private readonly RequestDelegate _next;
private readonly IOpaService _opaService;
public OpaAuthzMiddleware(RequestDelegate next, IOpaService service)
{
_next = next;
_opaService= service;
}
public async Task InvokeAsync(HttpContext context)
{
var enforceResult = await _opaService.RunAuthorizationAsync(context);
if (!enforceResult)
{
context.Response.StatusCode = (int)HttpStatusCode.Forbidden;
await context.Response.WriteAsync(ForbiddenMessage);
return;
}
await _next(context);
}
}
}
你会像这样在你的创业公司中实现它
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace Sample
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddRouting();
services.AddSingleton<IOpaService, OpaService>();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseMiddleware<OpaAuthzMiddleware>();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}
使用OPA的Wasm编译
OPA 拥有可以将 Rego 策略编译成可执行 Wasm 模块的工具。他们提供文档 here。
它目前正在开发中,但有一个在 .NET here 中使用它的示例。查看该回购协议的问题部分下的讨论,看起来他们仍在解决一些问题。
您需要使用可用的 .NET 库之一来读取编译后的 Wasm 文件,但这被认为是 OPA 提供的最快的评估方法。
我浏览了一些关于 OPA(开放策略代理)的视频和教程,发现使用它来跨多个服务/API 实现身份验证和授权真的很酷。但是,我无法深入了解如何在 windows 上安装它并将其与 ASP.Net 核心 Web API 集成以实施身份验证和授权。谁能帮我解决这个问题?
谢谢,
阿米特·阿南德
在不了解您的用例或您运行正在使用的平台的情况下,这里有一些一般性建议。
架构。决定是否要 运行 OPA 作为 sidecar 或独立服务。这是一个架构问题,取决于延迟、性能和策略所需的数据。 OPA 是一个设计为边车的构建块,但您可以通过旋转多个副本、在它们之间进行负载平衡、添加持久层等来围绕 OPA 构建服务。
管理。决定how to load/update policies and log decisions to OPA and if applicable, decide how to load data into OPA.
服务集成。如果您使用的网络代理拦截所有进入您的服务的网络流量(例如 Envoy、Linkerd、Kong 等),您可以 configure your network proxy to call out to OPA without modifying your .Net service. If you're not using a network proxy, modify your .Net services to make HTTP callouts when your services need policy decisions, using a library where possible to minimize the impact on individual services. The integration page 显示 Java Spring 和 PHP.
Tim Hinrichs 上面的回答是正确的。但是,这里要添加一些特定的解决方案。在下面的 2 个解决方案中,我建议使用 REST API 和 ASP.NET 中间件。此外,虽然 OPA 理论上可以用作身份验证工具,但我建议不要这样做。它的目的是授权。
使用ASP.NET授权中间件
首先,OPA 将 运行 作为它自己的服务,作为 k8 中的 sidecar,或者在 Docker 容器中。 OPA 的文档很好地展示了如何实现它的示例,所以我不会详细说明。
在这里您将创建一个查询 OPA's Rest API.
的 .NET 服务这就是中间件的样子。
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
namespace Authz.Opa
{
public class OpaAuthzMiddleware
{
private const string ForbiddenMessage = "Forbidden";
private readonly RequestDelegate _next;
private readonly IOpaService _opaService;
public OpaAuthzMiddleware(RequestDelegate next, IOpaService service)
{
_next = next;
_opaService= service;
}
public async Task InvokeAsync(HttpContext context)
{
var enforceResult = await _opaService.RunAuthorizationAsync(context);
if (!enforceResult)
{
context.Response.StatusCode = (int)HttpStatusCode.Forbidden;
await context.Response.WriteAsync(ForbiddenMessage);
return;
}
await _next(context);
}
}
}
你会像这样在你的创业公司中实现它
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace Sample
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddRouting();
services.AddSingleton<IOpaService, OpaService>();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseMiddleware<OpaAuthzMiddleware>();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}
使用OPA的Wasm编译
OPA 拥有可以将 Rego 策略编译成可执行 Wasm 模块的工具。他们提供文档 here。 它目前正在开发中,但有一个在 .NET here 中使用它的示例。查看该回购协议的问题部分下的讨论,看起来他们仍在解决一些问题。 您需要使用可用的 .NET 库之一来读取编译后的 Wasm 文件,但这被认为是 OPA 提供的最快的评估方法。