简体   繁体   English

如何将API网关与微服务和JWT结合使用?

[英]How do I use an API Gateway in conjunction with microservices and JWTs?

Afternoon y'all, 下午你们都

Just looking for someone to double check my work. 只是想找人来仔细检查我的工作。 Is the below an effective way to secure microservices? 以下是保护微服务的有效方法吗?

Premise 前提

Breaking up our monolithic application and monolithic Partner API into microservices oriented around specific business functions. 将我们的单片应用程序和单一的Partner API分解为面向特定业务功能的微服务。 They'll most likely be small expressjs applications running in a docker container, on elastic beanstalk, who knows. 它们很可能是在docker容器中运行的小型expressjs应用程序,在弹性beanstalk上,谁知道。 They'll live somewhere :) 他们会住在某个地方:)

I'm looking into either standing up Kong as my API Gateway or using AWS API Gateway to encapsulate the details of my microservices. 我正在研究将Kong作为我的API网关或使用AWS API Gateway来封装我的微服务的细节。 Also, it just feels good. 而且,它感觉很好。

The JWT plugin for Kong will verify the signature of the JWT and then pass the customer_id along in the header to the microservice. Kong的JWT插件将验证JWT的签名,然后将header_中的customer_id传递给微服务。 I should also mention that we have 3rd party developers that will be partaking in the integration fun as well. 我还要提一下,我们的第三方开发人员也将参与集成乐趣。 Here's a basic sketch of what I see happening: 这是我看到的事情的基本草图:

Implementation 履行

  1. Generate "consumers" for each platform and 3rd party developer we have. 为我们拥有的每个平台和第三方开发人员生成“消费者”。 (Web app, mobile app, and the current integration partners we have. Note: I'm not looking to create consumers for every user that logs in. While certainly more secure, this adds a lot of work. Also, if you figure out how to get the secret out of my API Gateway I clearly have other issues) (Web应用程序,移动应用程序以及我们当前的集成合作伙伴。注意:我不打算为每个登录用户创建消费者。虽然肯定更安全,但这会增加很多工作。另外,如果你弄清楚了如何从我的API网关中获取秘密我明显有其他问题)
  2. Let Kong verify the request for me. 让孔验证对我的要求。 Kind of like a bouncer at the door, there's no authorization, just authentication. 有点像门口的保镖,没有授权,只是认证。
  3. I don't need to know that the token is valid once it gets to the microservice, I can just use some middleware to decode it and use custom logic to decide if this user really should be doing whatever is they're trying to do. 我不需要知道令牌在进入微服务后是有效的,我可以使用一些中间件对其进行解码并使用自定义逻辑来确定该用户是否真的应该做他们正在做的任何事情。

Extra Stuff 额外的东西

  • There's a nice access control plugin for Kong. Kong有一个很好的访问控制插件。 Our application and mobile app would run with "God" privileges, but I could definitely lock down the developers to specific routes and methods. 我们的应用程序和移动应用程序将以“上帝”权限运行,但我绝对可以将开发人员锁定到特定的路由和方法。

  • Revoking 3rd party access will be easy, revoking end users access won't be so simple unless I'm willing to invalidate all JWTs at once by generating a new secret. 撤销第三方访问将很容易,撤销最终用户访问将不会那么简单,除非我愿意通过生成新秘密立即使所有JWT无效。 Perhaps I can limit token time to 10 minutes or so and make our applications check if they're expired, get a new token, and then get on with the original request. 也许我可以将令牌时间限制在10分钟左右,并让我们的应用程序检查它们是否已过期,获取新令牌,然后继续处理原始请求。 This way I can "flag" them in the database or something and not let the JWT be generated. 这样我就可以在数据库或其他东西中“标记”它们,而不是让JWT生成。

  • SSL used everywhere, JWT is stored in an SSL only cookie in the web browser and there's no sensitive information stored in any of the claims. 在任何地方都使用SSL,JWT存储在Web浏览器中仅限SSL的cookie中,并且在任何声明中都没有存储敏感信息。

Thanks guys. 多谢你们。

I recently worked on a solution to this very question and premise, refactoring a large monolith into multiple services in an AWS architecture. 我最近致力于解决这个问题和前提,在AWS架构中将大型整体重构为多个服务。

There is no right, wrong or definitive how to this question. 对于这个问题,没有正确,错误或明确的方法
However, we did implement a solution very similar to the one described in the question above. 但是,我们确实实现了与上述问题中描述的解决方案非常相似的解决方案。
I hope this answer can deliver a good sense of direction for someone who's looking at this for the first time. 我希望这个答案可以为第一次看到这个的人提供一个良好的方向感。

This is how we went about it... 这就是我们如何去做...

What do we need from an API gateway? 我们需要从API网关获得什么?

  1. Highly available 高度可用
  2. Secure 安全
  3. Performant 高性能
  4. Authoritative 权威性
  5. Scalable 可扩展

Solution 1: AWS API Gateway 解决方案1: AWS API网关

pros 利弊

  1. Highly available managed solution. 高度可用的托管解决方案。
  2. Don't need to worry about scalability. 不需要担心可扩展性。
  3. Supports SSL and custom domains. 支持SSL和自定义域。
  4. Authoritative through lambda and IAM. 通过lambda和IAM授权。
  5. Plays nice with other AWS services. 与其他AWS服务一起玩得很好。
  6. Supports API versioning out of the box. 支持开箱即用的API版本控制。
  7. Easy monitoring with CloudWatch. 使用CloudWatch轻松监控。

cons 缺点

  1. Traffic can't be routed directly into an internal network (private VPC segment), meaning an additional gateway would be required. 流量无法直接路由到内部网络(专用VPC段),这意味着需要额外的网关。
    Edit: Amazon API Gateway Supports Endpoint Integrations with Private VPCs. 编辑: Amazon API网关支持与私有VPC的端点集成。 Thanks @Red for mentioning this. 谢谢@Red提到这个。
  2. Slow, our benchmark showed each request through API Gateway added 100-150 ms latency. 慢,我们的基准测试显示通过API网关的每个请求增加了100-150毫秒的延迟。

Solution 2: Kong 解决方案2:

pros 利弊

  1. Scalable, but needs to implemented and managed on our end. 可扩展,但需要在我们的最终实施和管理。
  2. Supports SSL and custom domains. 支持SSL和自定义域。
  3. Authoritative through plugins, with solutions for JWT and OAUTH2 already packaged. 通过插件授权,已经打包了JWT和OAUTH2的解决方案。
  4. RESTful API for easy integration with our authentication server. RESTful API,可轻松与我们的身份验证服务器集成。
  5. Extensible, in case we need some custom logic. 可扩展,以防我们需要一些自定义逻辑。
  6. Fast, our benchmark showed each request through Kong added 20-30 ms latency. 快速,我们的基准测试显示每个请求通过Kong增加了20-30毫秒的延迟。

cons 缺点

  1. Requires management on our end (upgrades, deployment, maintenance). 我们需要管理(升级,部署,维护)。
  2. In order to achieve HA, requires an additional endpoint, in the form of a load balancer to route traffic to the actual GW(s). 为了实现HA,需要额外的端点,以负载均衡器的形式将流量路由到实际的GW。

Implementation 履行

We decided to go with Kong. 我们决定和孔一起去。
The major issue with the hosted solution was the inability to route traffic to our private network, where we also host a private DNS zone. 托管解决方案的主要问题是无法将流量路由到我们的私有网络,我们还在其中托管私有DNS区域。
Additionally, the extensible nature of Kong allowed us to create custom plugins with logic that is relevant to our solutions. 此外,Kong的可扩展性使我们能够创建与我们的解决方案相关的逻辑的自定义插件
We work with an ALB to round robin between multiple instances of Kong in different AZs in order to achieve redundancy and high availability. 我们使用ALB在不同AZ中的多个Kong实例之间进行循环,以实现冗余和高可用性。
The API configuration is saved on a Postgres RDS which is also internal and multi AZ. API配置保存在Postgres RDS上,后者也是内部和多个AZ。

Flow

  1. Client authenticates against our authentication server. 客户端对我们的身份验证服务器进 The authentication server is a micro service behind the Kong GW with a publicly exposed upstream. 认证服务器是Kong GW背后的微服务,具有公开暴露的上游。
  2. Authentication server creates a consumer with a JWT for the individual client. 身份验证服务器为单个客户端创建具有JWT使用者
  3. Authentication server replies with the JWT. 身份验证服务器回复JWT。
  4. Client requests access from an API with the JWT, traffic routed via Kong. 客户端请求使用JWT从API访问,通过Kong路由的流量。
  5. Kong verifies the JWT and routes the request to the micro service with information about the consumer. Kong验证JWT并将请求路由到微服务,并提供有关消费者的信息。
  6. Micro service responds to the client. 微服务响应客户端。

Other 其他

  1. Revoking user access is as easy as deleting the token. 撤消用户访问权限就像删除令牌一样简单。
  2. No sensitive information is stored in the JWT claims. JWT声明中未存储任何敏感信息。
  3. All services know about each other through a private DNS zone . 所有服务都通过私有DNS区域相互了解。

Schema: 架构:

Kong Gateway Schema

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM