# loopback-component-kong-sync

> Sync Loopback JS API with Kong

Latest version **1.0.6** (published 2020-02-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install loopback-component-kong-sync
pnpm add loopback-component-kong-sync
yarn add loopback-component-kong-sync
bun add loopback-component-kong-sync
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.6 |
| Published | 2020-02-20 |
| First published | 2019-09-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=10 |
| Dependencies | 4 |
| Unpacked size | 44.4 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Wesley Milan |
| Maintainers | wesleymilan |
| Keywords | NodeJS, kong, loopback, express, api, sync, component, middleware |

## Links

- npm: https://www.npmjs.com/package/loopback-component-kong-sync
- Repository: https://github.com/wesleymilan/loopback-component-kong-sync
- Issues: https://github.com/wesleymilan/loopback-component-kong-sync/issues
- npm.io page: https://npm.io/package/loopback-component-kong-sync

## Dependencies (4)

- [ip](https://npm.io/package/ip.md) ^1.1.5
- [async](https://npm.io/package/async.md) ^2.6.2
- [debug](https://npm.io/package/debug.md) ^2.4.4
- [node-kong-admin](https://npm.io/package/node-kong-admin.md) ^1.0.0

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 1.0.6 (latest) — 2020-02-20
- 1.0.5 — 2019-11-06
- 1.0.4 — 2019-11-01
- 1.0.3 — 2019-10-23
- 1.0.2 — 2019-10-04
- 1.0.1 — 2019-09-30
- 1.0.0 — 2019-09-30

## README

# Loopback Component Kong Sync

Tested with Loopback JS 3 (https://loopback.io/) and Kong 1.1.2 (https://konghq.com/)

This is a Loopback JS component to synchronize routes to Kong API Gateway.

This component map all Models and Methods from Loopback standardizing it and creating the 
related routes in Kong. 

#### How it works:
- Read and map all public models
- Read and map all public methods from public models
- Read Service and resources from Kong
- Compare LB resources with Kong Resources based on MD5 checksum stored as tags on Kong
- Create resources if they don't exist and update in case of changes have being made on LB
- Create targets using local IP or the IP list con config file

#### Setup
Installing using npm:
`npm install loopback-component-kong-sync --save`

After installing it on your project folder you have to input your preferences on 
`component-config.json`

#### component-config.json sessions

The plugin declaration must be inside `loopback-component-kong-sync` index.
You can find a full example of this file in `component-config.example.json`.

##### appVersion: 
You can use this param as a deploy key. Specifying an index of `process.env`
the value of this environment variable will be used on all checksum to ensure that all 
resource will be replaced on kong when you release a new API version.

i.e.: `APP_VERSION`
In this case the value of `process.env.APP_VERSION` now is a part of checksum to determine 
if the LB resources have being modified and must be replaced on Kong.

##### adminUrl: 
Kong admin URL. As you should keep your admin URL private we recommend you set 
here an internal IP of your network.

i.e.: `http://admin.kong.local:8001`

##### apiHost: 
This is your API host address that should be exposed publicity.

i.e.: `api.mywebsite.com`

##### service: 
This param parses the same object as Kong Service Object (https://docs.konghq.com/1.2.x/admin-api/#service-object)

i.e.:
```
"service": {
    "name": "SyncService",
    "retries": 5,
    "protocol": "http",
    "host": "SyncUpstream",
    "port": 80,
    "path": "/",
    "connect_timeout": 60000,
    "write_timeout": 60000,
    "read_timeout": 60000,
    "tags": [
        "SyncService"
    ]
}
```

##### upstream:
This param parses the same object as Kong Upstream Object (https://docs.konghq.com/1.2.x/admin-api/#upstream-object)

i.e.:
```
"upstream": {
    "name": "SyncUpstream",
    "hash_on": "none",
    "hash_fallback": "none",
    "slots": 10000,
    "healthchecks": {
        "active": {
            "https_verify_certificate": false,
            "unhealthy": {
                "http_statuses": [429, 404, 500, 501, 502, 503, 504, 505],
                "timeouts": 2,
                "http_failures": 2,
                "interval": 100
            },
            "http_path": "/",
            "timeout": 1,
            "healthy": {
                "http_statuses": [200, 301, 302, 307],
                "interval": 5,
                "successes": 2
            },
            "concurrency": 10,
            "type": "http"
        }
    },
    "tags": [
        "SyncUpstream",
        "SyncService"
    ]
}
```

##### targets:
This param parses the same object as Kong Target Object (https://docs.konghq.com/1.2.x/admin-api/#target-object)

i.e.:
```
"targets": [
    {
        "target": "127.0.0.1:3001",
        "weight": 100,
        "tags": [
            "SyncTarget",
            "SyncUpstream",
            "SyncService"
        ]
    },
    {
        "target": null,
        "weight": 100,
        "tags": [
            "SyncTarget",
            "SyncUpstream",
            "SyncService"
        ]
    }
]
```

##### plugins:
This param parses the same object as Kong Plugin Object (https://docs.konghq.com/1.2.x/admin-api/#plugin-object)

The plugin object must be inside `service` or `route`. You don't have to set service or route 
fields on plugin object, it will be replaced automatically by the service. In case of you
setting plugin into route, this plugin will be settled for all routes of your API, except
those one you override on model, see bellow.

This component was developed to work with `key-auth` plugin, so we recommend you set this plugin
on route option.  

You can find all bundle Kong plugin here https://docs.konghq.com/hub/. 

i.e.:
```
"plugins": {
    "service": {
        "cors": {
            "name": "cors",
            "service": null,
            "route": null,
            "enabled": true,
            "run_on": "first",
            "protocols": ["http", "https"],
            "tags": [
                "SyncService",
                "cors"
            ],
            "config": {
                "origins": [
                    "https://www.mywebsite.com",
                    "https://mywebsite.com"
                ],
                "methods": ["GET", "POST", "PUT", "PATCH", "DELETE", "HEAD"],
                "headers": [
                    "Origin",
                    "Referer",
                    "Authorization",
                    "Accepts",
                    "Content-Length",
                    "Content-Type",
                    "User-Agent",
                    "Host",
                    "Connection",
                    "Accept-Language",
                    "Accept-Encoding",
                    "Accept-Charset"
                ],
                "exposed_headers": [
                    "User-Agent",
                    "Access-Control-Allow-Origin"
                ],
                "credentials": true,
                "max_age": 3600,
                "preflight_continue": true
            }
        }
    },
    "route": {
        "key-auth": {
            "name": "key-auth",
            "service": null,
            "route": null,
            "enabled": true,
            "run_on": "first",
            "protocols": ["http", "https"],
            "tags": [
                "SyncRoute",
                "key-auth"
            ],
            "config": {
                "key_names": ["authorization","Authorization"],
                "key_in_body": false,
                "hide_credentials": false,
                "anonymous": null,
                "run_on_preflight": false
            }
        }
    }
}
```

##### authentication:
Setting the login methods the component will intercept well succeeded logins and 
create the consumer and the credential automatically on Kong. The same will happens when
a logout method is requested, LB component will keep the consumer but will remove 
the credential.

`simultaneousSessions` is used to avoid Kong database to be overloaded with unused
credentials, so all credentials exceeding the max number of simultaneous sessions
will be removed from Kong even if they are still valid.

i.e.:
```
"authentication": {
    "simultaneousSessions": 5,
    "loginMethods": [
        "/users/login",
        "/users/create",
        "/users/confirm",
        "/users/verify"
    ],
    "logoutMethods": [
        "/users/logout"
    ]
}
```

## Models setup
On `model-config.json` you can override some default options using the param `kong`.

##### Force a method to not be included on Kong even if it's a public method
```
"MyModel": {
    "dataSource": "mysql",
    "public": true,
    "options": {
        "remoting": {
            "sharedMethods": {
                "*": false,
                "search": true
            }
        }
    },
    "kong": {
        "methods": {
            "search": {
                "disabled": true
            }
        }
    }
},
```

##### Force an entire model to not be included on Kong even if it's a public model
```
"MyModel": {
    "dataSource": "mysql",
    "public": true,
    "options": {
        "remoting": {
            "sharedMethods": {
                "*": false,
                "search": true
            }
        }
    },
    "kong": {
        "disabled": true
    }
},
```

##### Disabling plugins on routes
```
"MyUserModel": {
    "dataSource": "mysql",
    "public": true,
    "options": {
        "remoting": {
            "sharedMethods": {
                "*": false,
                "login": true,
                "logout": true
            }
        }
    },
    "kong": {
        "methods": {
            "login": {
                "plugins": {
                    "key-auth": {
                        "disabled": true
                    }
                }
            },
            "logout": {
                "plugins": {
                    "key-auth": {
                        "disabled": true
                    }
                }
            }
        }
    }
},
```

---
_Source: https://npm.io/package/loopback-component-kong-sync · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
