【问题标题】:Bypassing Ocelot API Gateway绕过 Ocelot API 网关
【发布时间】:2019-09-30 21:50:48
【问题描述】:

我有一个 API 网关,在本例中称为 Gateway.Api。在Startup 类中,我有以下内容:

public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddOcelot(Configuration);
        services.AddMvc();

        var appSettingSection = Configuration.GetSection("AppSettings");
        services.Configure<AppSettings>(appSettingSection);

        var appSettings = appSettingSection.Get<AppSettings>();
        var key = Encoding.ASCII.GetBytes(appSettings.Secret);

        services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(options =>
            {
                options.SaveToken = true;
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey = new SymmetricSecurityKey(key),
                    ValidateIssuer = false,
                    ValidateAudience = false
                };
        });
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseAuthentication();
        app.UseOcelot().Wait();
        app.UseMvc();
    }

如您所见,它定义了身份验证方案。

使用Ocelot 我的Gateway.Api 有以下配置文件:

{
  "ReRoutes": [
    {
      "DownstreamPathTemplate": "/api/customer",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 50366
        }
      ],
      "UpstreamPathTemplate": "/api/customer",
      "UpstreamHttpMethod": [ "Get" ],
      "AuthenticationOptions": {
        "AuthenticationProviderKey": "Bearer",
        "AllowedScopes": []
      }
    },
    {
      "DownstreamPathTemplate": "/api/user/authenticate",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 50353
        }
      ],
      "UpstreamPathTemplate": "/api/user/authenticate",
      "UpstreamHttpMethod": [ "Post" ]
    }
  ],
  "GlobalConfiguration": {
    "UseServiceDiscovery": false
  }
}

当我尝试在没有令牌的情况下访问 http://localhost:50333/api/customer(Gateway.Api 的端口为 50333)时,我收到 401 响应,证明配置文件和身份验证工作正常。

除了客户微服务,我还有一个身份微服务,它允许用户使用有效的用户名和密码进行身份验证,然后发出一个令牌。然后使用此令牌调用客户服务,我得到了成功的响应(200 OK)。

现在由于某种原因,如果我直接访问客户服务而不使用网关(所以http://localhost:50366/api/customer),我可以在没有令牌的情况下获得成功的响应。

下面是客户微服务:

[Route("api/[controller]")]
public class CustomerController : Controller
{
    [HttpGet]
    public IEnumerable<string> Get()
    {
        var customers = new string[] {
            "test",
            "test"
        };

        return customers;
    }
}

这是否意味着我必须为每个微服务Startup 类添加一个身份验证方案?如果是这样,这不是矫枉过正吗?

我所做的尝试是在 Customer 微服务中的操作上使用 [Authorize] 属性,但这会引发异常,即它们不是默认身份验证方案。

【问题讨论】:

    标签: c# api jwt microservices ocelot


    【解决方案1】:

    这是开发环境,所以你可以直接访问 url。您的客户服务不知道网关。在实际生产环境中,您通常只会暴露 API 网关,其余服务位于防火墙(私有子网)后面。只有 API 网关可以访问它们。访问服务的唯一方法是通过网关。但是,如果您想公开服务,则必须进行单独的服务身份验证。

    无论如何 向您想要保护的服务添加身份验证总是一个好主意。

    【讨论】:

    • 我明白,但是用户不能从 api 端点嗅出下一个调用吗?尽管客户服务是隐藏的,但用户是否无法通过用户 fiddler 找到它最终拨打的电话并看到客户服务没有授权?
    • 这是不可能的。如果您的客户服务有公共端点,这是可能的唯一方法。如果网关是唯一的公共端点,那么请求就无法通过。但是,如果团队中的某些人不小心打开了 api 或弄乱了配置,那么进行身份验证是个好主意。在我看来,只有当您坐在环境或数据中心内时,才有可能进行嗅探。但是,如果它位于私有子网之后,它也无法访问该服务。
    【解决方案2】:

    我们这样理解,为什么要使用API​​ Gateway?

    使用 API Gateway 的原因有很多,其中之一是:

    这样我们就可以在API网关添加认证,而不是在很多微服务中添加认证代码。

    在生产服务器机器中,我们只为最终用户开放 API Gateway 端口,用户不知道其他微服务托管在哪里,也无法通过尝试其他端口来访问,因为其他端口未打开!

    我们还可以将 micros 服务放在另一台机器上,通过将其 IP 地址列入白名单,该机器只能由托管 API 网关的机器访问。

    但是在这种情况下,您信任您的开发人员和 DevOps 团队,否则您可以对高价值微服务进行进一步的身份验证,并且此身份验证与最终用户使用的身份验证没有什么不同,在这里您将对 API 进行身份验证网关。

    【讨论】:

      猜你喜欢
      • 2020-10-20
      • 2021-12-11
      • 2018-12-29
      • 2021-02-25
      • 1970-01-01
      • 2019-04-24
      • 2021-05-09
      • 2022-01-18
      • 2020-12-16
      相关资源
      最近更新 更多