Slide 1

Slide 1 text

Hold the door! Powerful API gateways with Ocelot and ASP.Net Core

Slide 2

Slide 2 text

Dan Patrascu-Baba BIO: Software developer @Amdaris, Founder @Codewrinkles, Co-organizer @ApexVox Blog: http://danpatrascu.com Twitter: @danpdc

Slide 3

Slide 3 text

Agenda • Microservices: short overview • Why an API gateway? • Meet Ocelot! • Ocelot – basic concepts and configuration • Ocelot – working with Authorization

Slide 4

Slide 4 text

Microservices – short overview

Slide 5

Slide 5 text

Basic microservices architecture

Slide 6

Slide 6 text

So why is the API gateway important? Unified entry point to the system • Clients can communicate easier with the system • Certain policies can be centralized • Centrally enforced security • Responses can be aggregated • Centralized caching

Slide 7

Slide 7 text

What should an API gateway do? • Proxying • Service discovery • Response aggregation • Header transformation • Load balancing • Authorization • Rate limiting

Slide 8

Slide 8 text

Meet Ocelot! • Meets all mentioned requirements • Easy to configure • Can be extended with own functionality

Slide 9

Slide 9 text

Ocelot features • Request aggregation • Authentication/authorization • Header transformation • Rate limiting • Caching • Service discovery • Easy to integrate with Service Fabric and Kubernetes • Extensible (by middleware or delegating handlers) • …and many more

Slide 10

Slide 10 text

Ocelot under the hood Request API gateway Middleware 1 Request builder middleware Service 1 Service 2 Service 3 Service 4 New request Response 1 2 3 4 5 6 7 8 9

Slide 11

Slide 11 text

The configuration Easy configuration: 1. A ReRoutes array • The ReRoutes are the objects that tell Ocelot how to treat an upstream request 2. A GlobalConfiguration object • Used to set the baseUrl or override ReRoute configurations if needed 3. Config file should be called ocelot.json

Slide 12

Slide 12 text

…but it gets messier

Slide 13

Slide 13 text

…and messier { "DownstreamPathTemplate": "/", "UpstreamPathTemplate": "/", "UpstreamHttpMethod": [ "Get" ], "AddHeadersToRequest": {}, "AddClaimsToRequest": {}, "RouteClaimsRequirement": {}, "AddQueriesToRequest": {}, "RequestIdKey": "", "FileCacheOptions": { "TtlSeconds": 0, "Region": "" }, DangerousAcceptAnyServerCertifica teValidator": false } "ReRouteIsCaseSensitive": false, "ServiceName": "", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 51876, } ], "QoSOptions": { "ExceptionsAllowedBeforeBreaking": 0, "DurationOfBreak": 0, "TimeoutValue": 0 }, "LoadBalancer": "", "RateLimitOptions": { "ClientWhitelist": [], "EnableRateLimiting": false, "Period": "", "PeriodTimespan": 0, "Limit": 0 }, "AuthenticationOptions": { "AuthenticationProviderKey": "", "AllowedScopes": [] }, "HttpHandlerOptions": { "AllowAutoRedirect": true, "UseCookieContainer": true, "UseTracing": true }, "

Slide 14

Slide 14 text

Ocelot routing

Slide 15

Slide 15 text

Routing basics • Ocelot describes the routing of one request to another as a ReRoute • ReRoutes are defined in the configuration • The DownstreamPathTemplate, DownstreamScheme and DownstreamHostAndPorts define the URL that a request will be forwarded to • The UpstreamPathTemplate is the URL that Ocelot will use to identify which DownstreamPathTemplate to use for a given request • Ocelot you can add placeholders for variables to your Templates in the form of {something} • Both UpStream and Downstream paths should end with a trailing /

Slide 16

Slide 16 text

Authentication / Authorization

Slide 17

Slide 17 text

Features • Easy integration with Identity Server 4, Auth0 and Okta • When using JWT tokens, those can be used for a bunch of useful features like authorization based on claims, scopes, header transformation and many more • You can easily add your custom authentication or token validation systems • Each ReRoute needs to be configured to use the registered authentication / authorization options

Slide 18

Slide 18 text

Header transformation • Custom headers can be added to each request before it is sent to the downstream service • Custom headers can be added to responses that will be sent to consumers • JWT token claims can be transformed into headers

Slide 19

Slide 19 text

DEMO: Ocelot in action

Slide 20

Slide 20 text

Resources • Ocelot docs: https://ocelot.readthedocs.io/en/latest/ • Ocelot repo: https://github.com/ThreeMammals/Ocelot • My blog: http://danpatrascu.com

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

Thank you!