Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adopt pre-commit #291

Merged
merged 5 commits into from
Mar 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = {
env: {
commonjs: true,
es6: true,
node: true,
},
extends: "eslint:recommended",
parserOptions: {
ecmaVersion: 12,
},
root: true,
rules: {
"no-unused-vars": "off",
},
};
12 changes: 5 additions & 7 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,10 @@ jobs:
run: |
npm ci

- name: "`npm run fmt` and check for changes"
run: |
# If this fails, run `npm run fmt` and push the result
# amend if you feel so inclined.
npm run fmt
git diff --exit-code
# Run the pre-commit action
# Repo: https://github.com/pre-commit/action
- uses: actions/setup-python@v2
- uses: pre-commit/[email protected]

- name: npm audit
run: |
Expand All @@ -59,7 +57,7 @@ jobs:
runs-on: ubuntu-20.04
# - https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idstrategy
strategy:
fail-fast: false # Do not cancel all jobs if one fails
fail-fast: false # Do not cancel all jobs if one fails
matrix:
node_version:
- "10"
Expand Down
21 changes: 21 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
repos:
# Autoformat: Bash scripts
- repo: https://github.com/lovesegfault/beautysh
rev: 6.0.1
hooks:
- id: beautysh

# Autoformat: markdown, javacsript
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v2.2.1
hooks:
- id: prettier
args:
- "--trailing-comma=es5"
- "--print-width=100"

# Lint
- repo: https://github.com/pre-commit/mirrors-eslint
rev: v7.21.0
hooks:
- id: eslint
104 changes: 53 additions & 51 deletions CHANGELOG.md

Large diffs are not rendered by default.

59 changes: 28 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ functionality to [JupyterHub] deployments.

- [Install](#install)
- [Usage](#usage)
- [Starting the proxy](#starting-the-proxy)
- [Setting a default target](#setting-a-default-target)
- [Command-line options](#command-line-options)
- [Starting the proxy](#starting-the-proxy)
- [Setting a default target](#setting-a-default-target)
- [Command-line options](#command-line-options)
- [Using the REST API](#using-the-rest-api)
- [REST API Basics](#REST-api-basics)
- [Authenticating via passing a token](#authenticating-via-passing-a-token)
- [Getting the routing table](#getting-the-routing-table)
- [Adding new routes](#adding-new-routes)
- [Deleting routes](#deleting-routes)
- [REST API Basics](#REST-api-basics)
- [Authenticating via passing a token](#authenticating-via-passing-a-token)
- [Getting the routing table](#getting-the-routing-table)
- [Adding new routes](#adding-new-routes)
- [Deleting routes](#deleting-routes)
- [Custom error pages](#custom-error-pages)
- [Host-based routing](#host-based-routing)
- [Troubleshooting](#troubleshooting)
Expand All @@ -49,7 +49,7 @@ To install from the source code found in this GitHub repo:

```bash
git clone https://github.com/jupyterhub/configurable-http-proxy
cd configurable-http-proxy
cd configurable-http-proxy
npm install # Use 'npm install -g' for global install
```

Expand All @@ -60,12 +60,12 @@ npm install # Use 'npm install -g' for global install
The configurable proxy runs two HTTP(S) servers:

- The **public-facing interface** to your application (controlled by `--ip`,
`--port`)
- listens on **all interfaces** by default.
`--port`)
- listens on **all interfaces** by default.
- The **inward-facing REST API** (`--api-ip`, `--api-port`)
- listens on localhost by default
- The REST API uses token authorization, where the token is set in the
`CONFIGPROXY_AUTH_TOKEN` environment variable.
- listens on localhost by default
- The REST API uses token authorization, where the token is set in the
`CONFIGPROXY_AUTH_TOKEN` environment variable.

![](./doc/_static/chp.png)

Expand Down Expand Up @@ -161,8 +161,9 @@ Options:
## Using the REST API

The configurable-http-proxy REST API is documented and available as:

- a nicely rendered, interactive version at the
[petstore swagger site][]
[petstore swagger site][]
- a [swagger specification file][] in this repo

[**Return to top**][]
Expand All @@ -172,14 +173,13 @@ The configurable-http-proxy REST API is documented and available as:
**API Root**

| HTTP method | Endpoint | Function |
|-------------|----------|----------|
| ----------- | -------- | -------- |
| GET | /api/ | API Root |


**Routes**

| HTTP method | Endpoint | Function |
|-------------|--------------------------|-------------------------------------|
| ----------- | ------------------------ | ----------------------------------- |
| GET | /api/routes | [Get all routes in routing table][] |
| POST | /api/routes/{route_spec} | [Add a new route][] |
| DELETE | /api/routes/{route_spec} | [Remove the given route][] |
Expand Down Expand Up @@ -208,7 +208,6 @@ curl -H "Authorization: token $CONFIGPROXY_AUTH_TOKEN" http://localhost:8001/api

GET /api/routes[?inactive_since=ISO8601-timestamp]


**Parameters:**

`inactive_since`: If the `inactive_since` URL
Expand All @@ -219,14 +218,14 @@ passes data to or from the proxy target.

**Response:**

*Status code*
_Status code_

status: 200 OK

*Response body*
_Response body_

A JSON dictionary of the current routing table. This JSON
dictionary *excludes* the default route.
dictionary _excludes_ the default route.

**Behavior:**

Expand All @@ -249,6 +248,7 @@ dictionary with at least one key: `target`, the target host to be proxied.
`target`: The host URL

Example request body:

```json
{
"target": "http://localhost:8002"
Expand Down Expand Up @@ -292,7 +292,7 @@ hit, along with their status code:
routing target. This error **can be prevented** by setting a
[`default target`][] before starting the configurable-http-proxy.

- 503 error: Returned when a route exists, but the upstream server isn't
- 503 error: Returned when a route exists, but the upstream server isn't
responding. This is more common, and can be due to any number of reasons,
including the target service having died, not finished starting, or network
instability.
Expand Down Expand Up @@ -364,17 +364,14 @@ Q: My proxy is not starting. What could be happening?
that is very old, and it is necessary to update node to a recent or `LTS`
version.


[**Return to top**][]



[node-http-proxy]: https://github.com/nodejitsu/node-http-proxy
[JupyterHub]: https://github.com/jupyterhub/jupyterhub
[jupyterhub]: https://github.com/jupyterhub/jupyterhub
[petstore swagger site]: http://petstore.swagger.io/?url=https://raw.githubusercontent.com/jupyterhub/configurable-http-proxy/master/doc/rest-api.yml#/default
[swagger specification file]: https://github.com/jupyterhub/configurable-http-proxy/blob/master/doc/rest-api.yml
[Get all routes in routing table]: #getting-the-routing-table
[Add a new route]: #adding-new-routes
[Remove the given route]: #deleting-routes
[get all routes in routing table]: #getting-the-routing-table
[add a new route]: #adding-new-routes
[remove the given route]: #deleting-routes
[`default target`]: #setting-a-default-target
[**Return to top**]: #table-of-contents
[**return to top**]: #table-of-contents
1 change: 1 addition & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ repository as is configured

To make a tagged release follow the instructions below, but first make sure you
meet the prerequisites:

- To have push rights to the [configurable-http-proxy GitHub
repository](https://github.com/jupyterhub/configurable-http-proxy).
- To have [`bump2version`](https://github.com/c4urself/bump2version) installed
Expand Down
44 changes: 18 additions & 26 deletions bin/configurable-http-proxy
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,22 @@ if (args.sslCiphers) {
].join(":");
}

function _loadSslCa(caFile) {
// When multiple CAs need to be specified, they must be broken into
// an array of certs unfortunately.
var chain = fs.readFileSync(caFile, "utf8");
var ca = [];
var cert = [];
chain.split("\n").forEach(function (line) {
cert.push(line);
if (line.match(/-END CERTIFICATE-/)) {
ca.push(new Buffer(cert.join("\n")));
cert = [];
}
});
return ca;
}

// ssl options
if (args.sslKey || args.sslCert) {
options.ssl = {};
Expand Down Expand Up @@ -200,19 +216,7 @@ if (args.apiSslKey || args.apiSslCert) {
options.apiSsl.cert = fs.readFileSync(args.apiSslCert);
}
if (args.apiSslCa) {
// When multiple CAs need to be specified, they must be broken into
// an array of certs unfortunately.
var chain = fs.readFileSync(args.apiSslCa, "utf8");
var ca = [];
var cert = [];
chain.split("\n").forEach(function (line) {
cert.push(line);
if (line.match(/-END CERTIFICATE-/)) {
ca.push(new Buffer(cert.join("\n")));
cert = [];
}
});
options.apiSsl.ca = ca;
options.apiSsl.ca = _loadSslCa(args.apiSslCa);
}
if (args.sslDhparam) {
options.apiSsl.dhparam = fs.readFileSync(args.sslDhparam);
Expand All @@ -235,19 +239,7 @@ if (args.clientSslKey || args.clientSslCert) {
options.clientSsl.cert = fs.readFileSync(args.clientSslCert);
}
if (args.clientSslCa) {
// When multiple CAs need to be specified, they must be broken into
// an array of certs unfortunately.
var chain = fs.readFileSync(args.clientSslCa, "utf8");
var ca = [];
var cert = [];
chain.split("\n").forEach(function (line) {
cert.push(line);
if (line.match(/-END CERTIFICATE-/)) {
ca.push(new Buffer(cert.join("\n")));
cert = [];
}
});
options.clientSsl.ca = ca;
options.clientSsl.ca = _loadSslCa(args.clientSslCa);
}
if (args.sslDhparam) {
options.clientSsl.dhparam = fs.readFileSync(args.sslDhparam);
Expand Down
46 changes: 23 additions & 23 deletions chp-docker-entrypoint
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,38 @@
# (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of
# "$XYZ_DB_PASSWORD" from a file, especially for Docker's secrets feature)
file_env() {
var=$1
file_var="${var}_FILE"
var_value=$(printenv $var || true)
file_var_value=$(printenv $file_var || true)
default_value=$2
var=$1
file_var="${var}_FILE"
var_value=$(printenv $var || true)
file_var_value=$(printenv $file_var || true)
default_value=$2

if [ -n "$var_value" -a -n "$file_var_value" ]; then
echo >&2 "error: both $var and $file_var are set (but are exclusive)"
exit 1
fi
if [ -n "$var_value" -a -n "$file_var_value" ]; then
echo >&2 "error: both $var and $file_var are set (but are exclusive)"
exit 1
fi

if [ -z "${var_value}" ]; then
if [ -z "${file_var_value}" ]; then
export "${var}"="${default_value}"
else
export "${var}"="$(cat $file_var_value)"
if [ -z "${var_value}" ]; then
if [ -z "${file_var_value}" ]; then
export "${var}"="${default_value}"
else
export "${var}"="$(cat $file_var_value)"
fi
fi
fi

unset "$file_var"
unset "$file_var"
}

file_env 'CONFIGPROXY_AUTH_TOKEN'

case "$@" in
*"--api-ip"*)
break;;
*)
# Default api-ip to all interfaces in docker,
# so that it is accessible to other containers
# and when port-forwarding is requested.
ARGS="--api-ip=0.0.0.0";;
*"--api-ip"*)
break ;;
*)
# Default api-ip to all interfaces in docker,
# so that it is accessible to other containers
# and when port-forwarding is requested.
ARGS="--api-ip=0.0.0.0" ;;
esac

exec configurable-http-proxy $ARGS "$@"
4 changes: 2 additions & 2 deletions doc/rest-api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ paths:
200:
description: "The routing table"
schema:
$ref: '#/definitions/RoutingTable'
$ref: "#/definitions/RoutingTable"
400:
description: "Invalid timestamp provided"
404:
Expand Down Expand Up @@ -84,7 +84,7 @@ definitions:
type: "object"
description: "Maps keys (route path prefixes / hostnames) to their targets"
additionalProperties:
$ref: '#/definitions/RouteTarget'
$ref: "#/definitions/RouteTarget"
RouteTarget:
type: "object"
properties:
Expand Down
22 changes: 11 additions & 11 deletions lib/error/404.html
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<!doctype html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>404: Not Found</title>
</head>
<head>
<meta charset="utf-8" />
<title>404: Not Found</title>
</head>

<body>
<h1>404: Not Found</h1>
<p>No service is registered at this URL</p>
<hr/>
<p>configurable-http-proxy</p>
</body>
<body>
<h1>404: Not Found</h1>
<p>No service is registered at this URL</p>
<hr />
<p>configurable-http-proxy</p>
</body>
</html>
Loading