Commit Graph

163 Commits

Author SHA1 Message Date
Joris Vink f47f8d3f5e Missing options for HTTP method restrictions.
While Kore supports the OPTIONS method, it was not possible
to specify this in the route methods configuration.

Pointed out via discord.
2023-12-01 00:33:18 +01:00
Joris Vink 4718bae098 Initial lua runtime.
Works enough so one can do basic configuration and handle HTTP.
2023-01-21 23:41:35 +01:00
Joris Vink a2d312d0a0 kore_debug() has been unused for years.
Kill all useless messages, convert useful ones into kore_log() instead.
2022-08-18 15:20:55 +02:00
Daniel Fahlgren 2211bb8f97 Some platforms doesn't have a /tmp/ directory where temporary files can be
stored. Make it possible to override that location compile time.
2022-08-17 13:16:25 +02:00
Joris Vink 25ea10fea7 Be better. 2022-08-10 10:20:18 +02:00
Joris Vink 73be741bfd Allow authenticators on filemaps.
This commit introduces the ability to add authenticators to filemaps.
Just like in normal routes, the authenticators will be resolved first
before allowing access to the filemap entries.

Configuration wise, the authenticator is an optional value after the
filemap config directive:

	filemap / webroot myauth

In the Python API you can now pass the authenticator for a filemap entry
but turning the value of the filemap into a tuple with the first entry
being the path and the second being the auth dict:

	AUTH AUTH={
	    "type": "cookie",
	    "value": "cookiename",
	    "redirect": "/auth/",
	    "verify": verify_cookie
	}

	domain.filemaps({
	    "/css/": "webroot/css",
	    "/secret/": ("webroot/secret", AUTH)
	})
2022-08-10 10:13:01 +02:00
Joris Vink 99a1581e19 Initial work splitting OpenSSL code away.
This work moves all TLS / crypto related code into a tls_openssl.c
file and adds a tls_none.c which contains just stubs.

Allows compilation of Kore with TLS_BACKEND=none to remove building
against OpenSSL.

Also adds code for SHA1/SHA2 taken from openssh-portable so we don't
depend on those being present anymore in libcrypto.
2022-02-17 13:45:28 +01:00
Joris Vink 833ca646e7 i forgot, it's 2022. 2022-01-31 22:02:06 +01:00
Joris Vink a29700f26d Bring back page authentication via config.
Inside of the new route handlers the "authenticate" keyword can
be specified to let the route authenticate via a previously
configured authentication block.

The ability to do this went missing in a previous commit that overhauled
the routing structure of the configuration.
2022-01-31 15:13:34 +01:00
Joris Vink e8e01980fc Python: allow route hooks via kore.route().
Adding the hooks keyword with a dictionary attached to specify
the relevant hooks will hook them for the given route.

Eg:

domain.route("/", self.index, methods=["get"],
    hooks={
        "on_free": self.request_free
    }
)

These are the same hooks available via a normal Kore route configuration.
2021-12-14 23:15:21 +01:00
Joris Vink 86ecb85f03 use correct privsep name for acme 2021-12-02 19:33:20 +01:00
Joris Vink 98d5909b7d bring back python_import and python_path.
These were mistakingly removed a while ago.
2021-10-20 11:20:25 +02:00
Joris Vink f5a58368b7 HTTP improvements.
Introduce an on_headers callback for routes, allowing one to inspect
the headers before the request is processed further.

Additionall,

Add a new way of obtaining HTTP headers. Much like http_argument_get_*()
functions, these new APIs allow you to fetch the data of an HTTP header
as a specified C type.

The new APIs are:

* http_request_header_int16()
* http_request_header_uint16()
* http_request_header_int32()
* http_request_header_uint32()
* http_request_header_int64()
* http_request_header_uint64()
* http_request_header_float()
* http_request_header_double()

Should make it easier to operate in HTTP header data in a safe way.
No need to always roll your own string to int conversion functions.
2021-09-15 22:16:22 +02:00
Joris Vink e98a4ddab5 Change how routes are configured in Kore.
Routes are now configured in a context per route:

route /path {
	handler handler_name
	methods get post head
	validate qs:get id v_id
}

All route related configurations are per-route, allowing multiple
routes for the same path (for different methods).

The param context is removed and merged into the route context now
so that you use the validate keyword to specify what needs validating.
2021-09-15 11:09:52 +02:00
Joris Vink b77d727f72 Add a logfile configuration option.
This will log all output from Kore processes to the specified file.
2021-09-10 13:34:57 +02:00
Joris Vink 3b20cda11c Rework worker startup/privsep config.
Starting with the privsep config, this commit changes the following:

- Removes the root, runas, keymgr_root, keymgr_runas, acme_root and
  acme_runas configuration options.

  Instead these are now configured via a privsep configuration context:

  privsep worker {
      root /tmp
      runas nobody
  }

  This is also configurable via Python using the new kore.privsep() method:

      kore.privsep("worker", root="/tmp", runas="nobody", skip=["chroot"])

Tied into this we also better handle worker startup:

- Per worker process, wait until it signalled it is ready.
- If a worker fails at startup, display its last log lines more clearly.
- Don't start acme process if no domain requires acme.
- Remove each process its individual startup log message in favour
  of a generalized one that displays its PID, root and user.
- At startup, log the kore version and built-ins in a nicer way.
- The worker processes now check things they need to start running
  before signaling they are ready (such as access to CA certs for
  TLS client authentication).
2021-09-07 21:59:22 +02:00
Joris Vink 0ac54eb48d Add a kore.config.skipchroot to the Python API.
If set to True, will skip the chroot() of the Kore workers.

This can be handy in case you want to set your deployment target to
production or docker so you get user changes but you don't want
to chroot the processes.
2021-09-06 14:35:04 +02:00
Joris Vink 5f11f796a8 Allow configuration to pickup values from environment.
Eg:

certfile $CERTFILE

will pickup the value from the set $CERTFILE environment variable.

This works for _any_ Kore configuration option.
2021-09-05 17:53:09 +02:00
Joris Vink 12909b98bb be explicit 2021-05-10 10:32:54 +02:00
Joris Vink 61c06291b6 Add a docker python kore.config.deployment setting.
This keeps kore in the foreground will still doing privsep.
Useful with upcoming official kore docker images.
2021-05-10 10:27:32 +02:00
Joris Vink fb335e1e0c Major Python API improvements.
1) Add @kore.route as a decorator for Python.

This decorator can be used on non-class methods to automatically
declare their route and parameters.

Takes the same arguments as the kore.domain.route function that
exists today.

Provides a nice clean way of setting up Kore if you dont want
a whole class based approach.

2) Remove the requirement for the name for kore.server() and the
kore.domain(attach=) keywords.

Instead of no name was given, the name "default" is used in both
places resulting in less boilerplating.

3) Allow multiple routes to be defined for the same URI as long
as the methods are different. So you can have one method for GET /
and another for POST /.

All changes combined condense the initial experience of getting
a Kore Python app up and running:

eg:

import kore

kore.server(ip="127.0.0.1", port="8888", tls=False)
kore.domain("*")

@kore.route("/", methods=["get"])
async def index(req):
    req.response(200, b'get method')

@kore.route("/", methods=["post"])
async def index_post(req)
    req.response(200, b'post method')
2021-05-02 00:32:47 +02:00
Joris Vink c4a60c54bb resolve tls_dhparam after configure. 2021-04-21 22:39:35 +02:00
Joris Vink cef5ac4003 bump copyright year. 2021-01-11 23:46:08 +01:00
Joris Vink 37f85ed663 rename foreground to kore_foreground. 2021-01-11 23:35:16 +01:00
Joris Vink c83f34c938 Rework http_pretty_error a tiny bit. 2020-03-04 08:36:10 +01:00
Frederic Cambus fe43ed09ac Add the http_pretty_error configuration option.
When enabled, Kore returns HTML error pages for status codes 4xx and
5xx instead of empty content.
2020-03-04 08:22:51 +01:00
Joris Vink db31f37ab0 Add a "return" configuration option.
This hooks into the existing redirection framework but allows you
to quickly deny certain paths with a 403 or other status code.

The snippet below would for example declare a filemap served from 'www'
directory but denying all access to the files under the 'data' directory:

filemap		/	www
deny		/data	403
2020-03-03 11:28:16 +01:00
Frederic Cambus b4ebee5913 Add the http_server_version configuration option.
This allows setting a custom server header from the config file, for
example to mask the version number.
2020-02-19 08:07:02 +01:00
Joris Vink 9d0aef0079 bump copyright 2020-02-10 14:47:33 +01:00
Joris Vink fa2e8ef0b6 Add support for config based redirection.
Inside the domain contexts a 'redirect' rule will allow you to redirect
a request to another URI.

Ex:

Redirect all requests with a 301 to example.com

	redirect ^/.*$ 301 https://example.com

Using capture groups

	redirect ^/account/(.*)$ 301 https://example.com/account/$1

Adding the query string in the mix

	redirect ^/(.*)$ 301 https://example.com/$1?$qs
2020-02-07 06:42:33 +01:00
Joris Vink 6f31e14e83 sprinkle kore_quiet around 2020-01-17 22:25:52 +01:00
Joris Vink 445163f7c5 Add support for setting an email for ACME.
Can be configured via the acme_email configuration option.

eg:

	acme_email john@example.com
2020-01-13 11:00:40 +01:00
Joris Vink f6cd16c567 Replace static/dynamic with a single option: route
Kore will automatically detect if a route is a dynamic or static one
so there is no need for the configuration options to differ anymore.
2019-11-15 08:11:02 +01:00
Joris Vink b3b5aa37b7 Allow acme config via python api 2019-11-13 23:01:24 +01:00
Joris Vink c78535aa5d Add acmev2 (RFC8555) support to Kore.
A new acme process is created that communicates with the acme servers.

This process does not hold any of your private keys (no account keys,
no domain keys etc).

Whenever the acme process requires a signed payload it will ask the keymgr
process to do the signing with the relevant keys.

This process is also sandboxed with pledge+unveil on OpenBSD and seccomp
syscall filtering on Linux.

The implementation only supports the tls-alpn-01 challenge. This means that
you do not need to open additional ports on your machine.

http-01 and dns-01 are currently not supported (no wildcard support).

A new configuration option "acme_provider" is available and can be set
to the acme server its directory. By default this will point to the
live letsencrypt environment:
    https://acme-v02.api.letsencrypt.org/directory

The acme process can be controlled via the following config options:
  - acme_root (where the acme process will chroot/chdir into).
  - acme_runas (the user the acme process will run as).

  If none are set, the values from 'root' and 'runas' are taken.

If you want to turn on acme for domains you do it as follows:

domain kore.io {
	acme yes
}

You do not need to specify certkey/certfile anymore, if they are present
still
they will be overwritten by the acme system.

The keymgr will store all certificates and keys under its root
(keymgr_root), the account key is stored as "/account-key.pem" and all
obtained certificates go under "certificates/<domain>/fullchain.pem" while
keys go under "certificates/<domain>/key.pem".

Kore will automatically renew certificates if they will expire in 7 days
or less.
2019-11-06 19:43:48 +01:00
Joris Vink 8311c036d9 Add seccomp_tracing configuration option for linux.
If set to "yes" then Kore will trace its child processes and properly
notify you of seccomp violations while still allowing the syscalls.

This can be very useful when running Kore on new platforms that have
not been properly tested with seccomp, allowing me to adjust the default
policies as we move further.
2019-10-31 12:52:10 +01:00
Joris Vink cdc3347120 Add kore.sendmsg(object, worker=None) to the python api.
This allows you to send Python objects that can be run through pickle
to other worker processes.

If your application implements koreapp.onmsg() you will be able to receive
these objects.
2019-10-16 12:05:27 +02:00
Joris Vink 46375303cb Allow multiple binds on new server directive. 2019-09-27 20:00:35 +02:00
Joris Vink 7350131232 Allow listening of tls/notls ports at the same time.
Before kore needed to be built with NOTLS=1 to be able to do non TLS
connections. This has been like this for years.

It is time to allow non TLS listeners without having to rebuild Kore.

This commit changes your configuration format and will break existing
applications their config.

Configurations now get listener {} contexts:

listen default {
	bind 127.0.0.1 8888
}

The above will create a listener on 127.0.0.1, port 8888 that will serve
TLS (still the default).

If you want to turn off TLS on that listener, specify "tls no" in that
context.

Domains now need to be attached to a listener:

Eg:
	domain * {
		attach	default
	}

For the Python API this kills kore.bind(), and kore.bind_unix(). They are
replaced with:

	kore.listen("name", ip=None, port=None, path=None, tls=True).
2019-09-27 12:27:04 +02:00
Joris Vink f725ca228c alter python skeleton from kodev create -p.
adds the kore.config.file setting (required a fix for -c) and the
kore.config.deployment option is set to "development".
2019-09-26 19:58:13 +02:00
Joris Vink 937c39f041 Many many Python improvements.
- Kore can now fully be configured via Python code if one wants nothing to
  do with configuration files.

- Kore can now start single python files and no longer requires them to be
  inside a module directory.

- Pass all regex capture groups to the handler methods, allowing you to
  get access to them immediately.

- Change python websocket_handshake to take callable objects directly.

- Added a new deployment configuration option. If set to "dev" or
  "development" Kore will automatically foreground, no chroot / etc.
  If set to "production" Kore *will* chroot, drop privs, etc.

- Many more..

These are all backported from a project that I was working on a while
ago. I decided these should go back into mainline Kore.
2019-09-26 15:49:00 +02:00
Joris Vink 2c88bc6120 Add asynchronous libcurl support.
This commit adds the CURL=1 build option. When enabled allows
you to schedule CURL easy handles onto the Kore event loop.

It also adds an easy to use HTTP client API that abstracts away the
settings required from libcurl to make HTTP requests.

Tied together with HTTP request state machines this means you can
write fully asynchronous HTTP client requests in an easy way.

Additionally this exposes that API to the Python code as well
allowing you do to things like:

	client = kore.httpclient("https://kore.io")
	status, body = await client.get()

Introduces 2 configuration options:
	- curl_recv_max
		Max incoming bytes for a response.

	- curl_timeout
		Timeout in seconds before a transfer is cancelled.

This API also allows you to take the CURL easy handle and send emails
with it, run FTP, etc. All asynchronously.
2019-04-24 00:15:17 +02:00
Joris Vink aa49e181b6 Add http_[header|body]_timeout.
If the HTTP request headers or the HTTP body have not arrived before
these timeouts expire, Kore will send a 408 back to the client.
2019-04-11 20:51:49 +02:00
Joris Vink 39ffa047ca remove dh parameter check at configure time.
this is done when the workers startup if a domain with tls was used.
2019-03-26 21:44:44 +01:00
Joris Vink e4caac9e0c Be more clear when an invalid TLS setup is found 2019-03-26 21:36:45 +01:00
Joris Vink 4238431b9e Add worker_death_policy setting.
By default kore will restart worker processes if they terminate
unexpected. However in certain scenarios you may want to bring down
an entire kore instance if a worker process fails.

By setting worker_death_policy to "terminate" the Kore server will
completely stop if a worker exits unexpected.
2019-03-22 09:49:50 +01:00
Joris Vink eb9b7f7b14 explicitly include sys/types.h
some smaller libc variants do not include this from sys/param.h.
2019-03-06 09:29:46 +01:00
Joris Vink bf1e8e5ffb bump copyright to 2019 2019-02-22 16:57:28 +01:00
Joris Vink baafa4897e Add -q flag.
If specified Kore will run quietly and only log important messages.
2018-11-15 16:01:37 +01:00
Joris Vink 339df66fd5 Add support for TLS 1.3 via OpenSSL 1.1.1.
This commit removes TLS 1.0 support no matter what OpenSSL
you are linking against.

Changes the value of tls_version from 1.2 to both. Meaning if
you link with OpenSSL 1.1.1 you will get 1.2 + 1.3.
2018-10-29 20:38:58 +01:00