Commit Graph

131 Commits

Author SHA1 Message Date
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
Joris Vink 442bdef79b allow kore to bind to unix sockets via bind_unix. 2018-10-07 20:49:16 +02:00
Joris Vink a927acb7ee Add pledge support under OpenBSD.
All worker processes will now call pledge(2) after dropping
privileges (even if -rn was specified).

By default Kore will use the following promises:
	"stdio rpath inet error"

If your application requires more privileges, you can add more pledges
by setting them in your configuration using the 'pledge' directive:
	pledge dns wpath
2018-07-31 06:51:34 +02:00
Joris Vink 9f2759e7b2 missing newline in error 2018-07-17 14:36:21 +02:00
Joris Vink 0726a26c0c Allow restriction of methods for paths.
Now Kore will automatically send a 400 bad request in case the
method was not allowed on the path.
2018-07-17 14:23:57 +02:00
Joris Vink cffb7ec379 Allow on-the-fly reloading of certificates/keys.
This commit introduces the ability for the keymgr process
to reload the certificates/keys for domains when receiving
a SIGUSR1 signal.

The keymgr receives 2 new configuration options:
	- keymgr_root_path
		The root path where the keymgr will live.
		If -n is not specified when the application starts the
		keymgr process will chroot into here.

	- keymgr_runas_user
		The user the keymgr will drop privileges towards if
		-r was not specified.

All certfile and certkey configuration options are now relative to the
keymgr_root_path configuration setting.

The keymgr process will now also load the certificate for the domain
(rather then the workers) and submit these to the worker processes so
they can be reloaded when required.

Worker processes will refuse connections until the TLS configuration
for a given domain is completed (aka: the workers receive the certificate
for that domain).

Other changes:
	- client_certificates renamed to client_verify.
	- the chroot configuration option is now called root.
	- kore is a little more verbose if privsep options are missing.
	- filemaps are now relative to the root configuration option.
2018-07-11 09:44:29 +02:00
Joris Vink 04077c66b6 Add filemap_ext configuration option.
Allows you to specify the default extensions used for a file served
via a filemap, eg:
	filemap_ext	.html

Gives us ability to provide clean urls.
2018-07-03 19:58:43 +02:00
Joris Vink 202234cf97 filemap and fileref improvements.
- make sure we can serve updated files even if we have an old
  fileref around.

- add filemap_index as a configuration option: allows one to specify
  what file to serve if a directory was requested (eg: index.html)
2018-06-28 23:00:42 +02:00
Joris Vink 80f5425698 Add filemaps.
A filemap is a way of telling Kore to serve files from a directory
much like a traditional webserver can do.

Kore filemaps only handles files. Kore does not generate directory
indexes or deal with non-regular files.

The way files are sent to a client differs a bit per platform and
build options:

default:
  - mmap() backed file transfer due to TLS.

NOTLS=1
  - sendfile() under FreeBSD, macOS and Linux.
  - mmap() backed file for OpenBSD.

The opened file descriptors/mmap'd regions are cached and reused when
appropriate. If a file is no longer in use it will be closed and evicted
from the cache after 30 seconds.

New API's are available allowing developers to use these facilities via:
  void net_send_fileref(struct connection *, struct kore_fileref *);
  void http_response_fileref(struct http_request *, struct kore_fileref *);

Kore will attempt to match media types based on file extensions. A few
default types are built-in. Others can be added via the new "http_media_type"
configuration directive.
2018-06-28 13:27:44 +02:00
Joris Vink e475bd0c92 Add configurable x509 chain validation depth.
You can now per domain configure the depth for x509 chain validation:
	client_verify_depth	1

By default this is 1.

While here change around some log messages and properly set
the callback for x509 verification rather then via hoops and loops.
2018-06-09 12:50:50 +02:00
Joris Vink 85cff54a5f don't let kore_parse_config_file() call fclose.
It doesn't own the FILE pointer, it shouldn't call fclose() on it,
thats just confusing.
2018-04-10 14:39:57 +02:00
Joris Vink 9c337ded1e Change kore_parent_configure() for single binaries.
This function now takes any remaining arguments passed on the command line
after kore parsed its own.

For C the new prototype looks like this:

void kore_parent_configure(int argc, char **argv);

For python code, kore will pass each argument to the function so you
can do things like:

def kore_parent_configure(arg1, arg2):
2018-04-09 12:51:20 +02:00
Joris Vink 548068d2a0 Add http_request_ms configuration option.
This option allows a user to finetune the number of milliseconds
a worker process will max spend inside the http_process() loop.

By default this is 10ms.
2018-03-14 13:41:17 +01:00
Joris Vink ff21fd330f Add python_path configuration option.
Allows adding more items to the python path so imports from inside
python scripts work more sensible depending on how you configure your
app.
2018-03-01 12:55:33 +01:00
Joris Vink dd2dff2318 Rework HTTP and worker processes.
The HTTP layer used to make a copy of each incoming header and its
value for a request. Stop doing that and make HTTP headers zero-copy
all across the board.

This change comes with some api function changes, notably the
http_request_header() function which now takes a const char ** rather
than a char ** out pointer.

This commit also constifies several members of http_request, beware.

Additional rework how the worker processes deal with the accept lock.

Before:
	if a worker held the accept lock and it accepted a new connection
	it would release the lock for others and back off for 500ms before
	attempting to grab the lock again.

	This approach worked but under high load this starts becoming obvious.

Now:
	- workers not holding the accept lock and not having any connections
	  will wait less long before returning from kore_platform_event_wait().

	- workers not holding the accept lock will no longer blindly wait
	  an arbitrary amount in kore_platform_event_wait() but will look
	  at how long until the next lock grab is and base their timeout
	  on that.

	- if a worker its next_lock timeout is up and failed to grab the
	  lock it will try again in half the time again.

	- the worker process holding the lock will when releasing the lock
	  double check if it still has space for newer connections, if it does
	  it will keep the lock until it is full. This prevents the lock from
	  bouncing between several non busy worker processes all the time.

Additional fixes:

- Reduce the number of times we check the timeout list, only do it twice
  per second rather then every event tick.
- Fix solo worker count for TLS (we actually hold two processes, not one).
- Make sure we don't accidentally miscalculate the idle time causing new
  connections under heavy load to instantly drop.
- Swap from gettimeofday() to clock_gettime() now that MacOS caught up.
2018-02-14 13:48:49 +01:00
Joris Vink f348feef82 Add pgsql_queue_limit configuration option.
Limits the number of queued asynchronous queries kore will allow
before starting to return errors for KORE_PGSQL_ASYNC setups.

By default set to 1000.

Avoids uncontrolled growth of the pgsql_queue_wait pool.
2018-02-13 13:21:27 +01:00
Joris Vink 548348f553 2018 2018-01-20 22:51:06 +01:00
Joris Vink b95b623e72 Allow param blocks to be marked as "querystring"
Before params get would mean querystring and anything else
would just count toward a www-encoded body.

Now you can prefix the params block with "qs" indicating that
those configured parameters are allowed to occur in the query
string regardless of the method used.

This means you can do something like:

params qs:post /uri {
	...
}

to specify what the allowed parameters are in the querystring for
a POST request towards /uri.

inspired by and properly fixes #205.
2018-01-16 18:47:50 +01:00
rouzier f0f1296265 Add patch support (#217)
Add PATCH to supported verbs in config and what not.
2018-01-02 22:27:59 +01:00
Joris Vink 6ba7390cc9 Add rand_file configuration option for keymgr.
This option allows the user to specify a file to be used for
seeding the PRNG initially and to write random bytes at exit.

The option is only available if kore has TLS enabled (by default).

If you enable this option Kore will refuse to start if there is
a problem with the file specified (not found, not a file, invalid size, etc).

While here let the keymgr process call RAND_poll() every half hour
to grab more system entropy and seed it into the PRNG.
2017-02-27 20:58:04 -08:00
Joris Vink a0c545884f even more ssl to tls terminology. 2017-02-11 21:33:09 +01:00
Joris Vink 604fae3a01 unfuck parsing http_body_max. 2017-02-06 21:28:33 +01:00
Joris Vink 6592eddcfb add stdint.h 2017-02-06 12:13:29 +01:00
Joris Vink ace8c4e80c Add asynchronous pgsql query support to python.
This commit adds the ability to use python "await" to suspend
execution of your page handler until the query sent to postgresql
has returned a result.

This is built upon the existing asynchrous query framework Kore had.

With this you can now write stuff like:

async def page(req):
	result = await req.pgsql("db", "SELECT name FROM table");
	req.response(200, json.dumps(result).encode("utf-8"))

The above code will fire off a query and suspend itself so Kore can
take care of business as usual until the query is successful at which
point Kore will jump back into the handler and resume.

This does not use threading, it's purely based on Python's excellent
coroutines and generators and Kore its built-in pgsql support.
2017-02-06 11:42:53 +01:00
Joris Vink 3023ca0e57 Allow single binaries to load modules again. 2017-01-26 13:13:23 +01:00
Joris Vink bbcdec82fc Add initial python support.
Based on work done by Stanislav Yudin.
2017-01-24 20:18:12 +01:00
Joris Vink 4ad50caa29 Large changes to the memory subsystem in kore.
- Change pools to use mmap() for allocating regions.
- Change kore_malloc() to use pools for commonly sized objects.
  (split into multiple of 2 buckets, starting at 8 bytes up to 8192).
- Rename kore_mem_free() to kore_free().

The preallocated pools will hold up to 128K of elements per block size.

In case a larger object is to be allocated kore_malloc() will use
malloc() instead.
2016-07-12 13:54:14 +02:00
Joris Vink 39a5f21986 Allow "kore build" to produce single binaries.
Producing single binaries can now be done with building with
"kore build". To get started edit your build.conf and add the
following directives:

single_binary = yes
kore_source = /path/to/kore

optionally you can add kore_flavor to instruct how kore should
be built:

kore_flavor = NOTLS=1

When doing this your build.conf must also include the correct
linking options as the linking is now done fully by kore build.

The binary produced will include your configuration and takes
over a few of kore its command line flags (such as -f, -n or -r).
2016-07-06 16:16:15 +02:00
Joris Vink ae31ec01ac Separate private keys from worker processes.
Kore will now isolate RSA private keys to a separate process (keymgr).

Worker processes that require RSA signing for TLS connections will
communicate with this keymgr process in order to do so.

This behaviour cannot be disabled and is always turned on.
2016-06-08 13:56:38 +02:00
Joris Vink d4435bec8d Correct pretty bad typo. 2016-02-06 14:59:54 +01:00
Joris Vink 40f69924bb Move shared file reading code to kore_read_line() 2016-02-01 22:10:10 +01:00
Joris Vink 17e311dba9 constify kore_parse_config_file() parameter. 2016-02-01 21:59:31 +01:00
Joris Vink ac864a7419 Update output message to match others. 2016-02-01 21:54:09 +01:00