Slide 1

Slide 1 text

The dirty little secrets of building large, highly available, scalable HTTP APIs by @dschenkelman

Slide 2

Slide 2 text

Christophers talk: Part 2

Slide 3

Slide 3 text

Why? var express = require('express'); var app = express(); app.get('/', function (req, res) { res.send('Hello World!'); }); var server = app.listen(3000, function () { var host = server.address().address; var port = server.address().port; console.log('Example app listening at http://%s:%s', host, port); });

Slide 4

Slide 4 text

Developers

Slide 5

Slide 5 text

Evolution

Slide 6

Slide 6 text

Pillars

Slide 7

Slide 7 text

Validation

Slide 8

Slide 8 text

enabled: true

Slide 9

Slide 9 text

enabled: 1

Slide 10

Slide 10 text

enabled: 1.0

Slide 11

Slide 11 text

enabled: "false"

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

JSON Schemas "enabled": { "type": "boolean" }

Slide 14

Slide 14 text

Errors for developers { "statusCode": 400, "error": "Bad Request", "message": "Payload validation error: 'Expected type boolean but found type string' on property enabled (true if the rule is enabled, false otherwise).", "errorCode": "invalid_body" }

Slide 15

Slide 15 text

ratify const ZSchemaErrors = require('z-schema-errors'); const errorReporters = ['headers', 'query', 'path', 'payload'].reduce((current, part) => { current[part] = ZSchemaErrors.init({/*...*/}); return current; }, {}); plugins.push({ register: require('ratify'), options: { errorReporters: errorReporters, /*...*/ } });

Slide 16

Slide 16 text

Documentation

Slide 17

Slide 17 text

Old API Explorer

Slide 18

Slide 18 text

Auto-generate docs

Slide 19

Slide 19 text

Swagger

Slide 20

Slide 20 text

Leverage validation "enabled": { "type": "boolean", "description": "true if the connection is enabled, false (default) otherwise" }

Slide 21

Slide 21 text

Customize

Slide 22

Slide 22 text

AuthN & AuthZ

Slide 23

Slide 23 text

Token Exchange API Client API Server 1. Credentials 2. API Token 3. Requests

Slide 24

Slide 24 text

Granular Security

Slide 25

Slide 25 text

Decentralized Issuance

Slide 26

Slide 26 text

Expiration Control

Slide 27

Slide 27 text

Debuggability

Slide 28

Slide 28 text

JSON Web Token JWT

Slide 29

Slide 29 text

Authenticate For all endpoints

Slide 30

Slide 30 text

Authorize Per endpoint

Slide 31

Slide 31 text

Route { method: 'GET', path: '/api/v2/clients/{id}', config: { auth: { strategy: 'jwt', scope: ['read:clients', 'read:client_keys'] }, } }

Slide 32

Slide 32 text

Blacklisting

Slide 33

Slide 33 text

Interval

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

2nd half

Slide 40

Slide 40 text

Stress tests

Slide 41

Slide 41 text

Optimism unsigned long income;

Slide 42

Slide 42 text

Ops Stress

Slide 43

Slide 43 text

What to do…

Slide 44

Slide 44 text

Rate Limiting

Slide 45

Slide 45 text

Token Bucket

Slide 46

Slide 46 text

limitd #port to listen on port: 9001 #db path db: /var/limitd/database #define the bucket types buckets: customers: size: 10 per_second: 10

Slide 47

Slide 47 text

429 X-RateLimit-Limit Maximum amount X-RateLimit-Remaining How many there are left X-RateLimit-Reset When will bucket be full again

Slide 48

Slide 48 text

patova plugins.push({ register: require('patova'), options: { event: 'onPostAuth', // when to perform the limit check type: 'tenant', // bucket address: env.LIMITD_SERVER, extractKey: function(request, reply, done){ const key = request.auth.credentials.__tenant; done(null, key); } } });

Slide 49

Slide 49 text

Geo Redundancy

Slide 50

Slide 50 text

Data Center Failure

Slide 51

Slide 51 text

Natural Disasters

Slide 52

Slide 52 text

Cloud Provider Failure

Slide 53

Slide 53 text

Our Setup

Slide 54

Slide 54 text

Our Setup App Instances MongoDB Elastic Search App Instances MongoDB Elastic Search MongoDB

Slide 55

Slide 55 text

The switch

Slide 56

Slide 56 text

Changes

Slide 57

Slide 57 text

Experiments

Slide 58

Slide 58 text

feature-change const feature_change = require('feature-change'); var options = { expected: cb => mongo_search(mongo_opts, cb), actual: cb => es_search(es_opts, cb), logAction: (current_result, new_result) => { // invoked when there is a difference in the results // (useful for logging) } }; feature_change(options, (err, result) => { // this is the original callback you were using for mongo // err and result always come from mongo_search });

Slide 59

Slide 59 text

Iron out differences

Slide 60

Slide 60 text

Wrap up

Slide 61

Slide 61 text

Shipping is important

Slide 62

Slide 62 text

Remember: evolve

Slide 63

Slide 63 text

Validation & Docs • http://json-schema.org • http://swagger.io • https://auth0.com/docs/api/v2 (example of branded auto-generated docs) • https://github.com/mac-/ratify

Slide 64

Slide 64 text

AuthN & AuthZ • https://tools.ietf.org/html/rfc7519 • http://jwt.io/ • https://auth0.com/blog/2014/12/02/using-json- web-tokens-as-api-keys/ • https://auth0.com/blog/2015/03/10/blacklist-json- web-token-api-keys/ • https://github.com/auth0/hapi-auth-jwt

Slide 65

Slide 65 text

Rate limiting • http://tools.ietf.org/html/rfc6585 • https://github.com/limitd • https://github.com/dschenkelman/patova

Slide 66

Slide 66 text

BaaS • http://security.stackexchange.com/a/83382 • http://www.brendangregg.com/FlameGraphs/ cpuflamegraphs.html • https://github.com/thlorenz/v8-perf/issues/4 • https://github.com/auth0/node-baas • http://docs.aws.amazon.com/AutoScaling/latest/ DeveloperGuide/US_SetUpASLBApp.html

Slide 67

Slide 67 text

Geo Redundancy • http://highscalability.com/blog/2014/12/1/auth0- architecture-running-in-multiple-cloud-providers- and-r.html • https://auth0.com/availability-trust • http://docs.mongodb.org/master/tutorial/deploy- geographically-distributed-replica-set/

Slide 68

Slide 68 text

Feature Changes • http://zachholman.com/talk/move-fast-break- nothing/ • https://auth0.com/blog/2015/10/27/feature- changes-at-auth0/ • https://github.com/dschenkelman/feature- change

Slide 69

Slide 69 text

Thanks https://github.com/dschenkelman/api-secrets-talk @dschenkelman npm i dschenkelman