Skip to content
davils-comPublic

About

The service framework of Davils: services built from plugins with dependencies, TLS 1.3 listeners, a mesh with mutual TLS and SPIFFE, IPC, discovery and load balancing, its own Raft and etcd-like store, a certificate authority, a Kubernetes-like control plane and APIs from OpenAPI 3.2 for Kotlin on the JVM, reading nothing from the file system.

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

22 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

kortex

Kortex is the service framework of Davils for Kotlin on the JVM. A service is assembled from plugins in the manner of Ktor: plugins depend on each other, take everything they need from the pipeline of their service and are validated together before anything starts. The same service runs alone as a monolith or as one of many in a mesh, where services find each other, scale horizontally and talk mutual TLS over the network or HTTP over local IPC. A control plane in the manner of Kubernetes keeps the state of the mesh in a store of its own on Raft, without Postgres or Redis, and an elected authority issues the certificates of the mesh. Every API is generated from an OpenAPI 3.2 document by kapi.

Kortex is built on Kore, krypt, Konnect, kapi, kollect, Knot, konfetti and kontinuity, and it is built for critical infrastructure: TLS 1.3 is on, the mesh authenticates every peer, nothing blocks a thread and nothing is read from or written to the file system. It is published under the Apache License 2.0.

Module Artifact What it holds
kortex-core com.davils:kortex-core The kernel: the service, its identity and lifecycle, plugins with dependencies and contributions, the pipeline, the coordinated shutdown, trace context and deadlines
kortex-server com.davils:kortex-server Server: a public listener with TLS 1.3 of krypt and h2 or http/1.1, an IPC listener, a bootstrap listener for enrollment, drain; Health, Info and Metrics of openapi.yaml
kortex-client com.davils:kortex-client Clients: calls to other services on the mesh with mutual TLS or over IPC, balanced across their instances, with retries, a circuit breaker and the trace and deadline of the caller
kortex-store com.davils:kortex-store Store: a key-value store in the manner of etcd in memory, with revisions, transactions, watches and leases, served to the mesh and IPC under grants per principal, alone in one service or replicated on Raft across many
kortex-raft com.davils:kortex-raft RaftNode: Raft with pre-vote, check-quorum, ReadIndex, learners, membership changes, snapshots and leadership transfer, over storage, transport and a state machine of the application; Raft: a member of a group in a service, speaking with its peers over the mesh or IPC
kortex-authority com.davils:kortex-authority Authority: the certificate authority of a mesh, enrolling services with single-use join tokens and renewing their X.509-SVIDs over mutual TLS; enroll and renew
kortex-control-plane com.davils:kortex-control-plane ControlPlane: the registry of a mesh with instances, endpoints, roles, role bindings and access policies, list and watch, denying by default
kortex-agent com.davils:kortex-agent Agent: the agent in every service of a mesh, which renews its certificate, registers its instance with heartbeats and readiness, fills the discovery of the clients from the endpoint slices and lets only the callers the access policies allow through the mesh listener
kortex-postgres com.davils:kortex-postgres Postgres: a database of kostgres, reached on start, checked by readiness, drained on shutdown
kortex-redis com.davils:kortex-redis Redis: a client of kredis, reached on start, checked by readiness, drained on shutdown
kortex-kheck com.davils:kortex-kheck Logging: the events of the service as structured logs of kheck
kortex-testing com.davils:kortex-testing withTestService: a service started for one test and stopped afterwards
kortex-bom com.davils:kortex-bom The bill of materials of every module

Every module requires JDK 25 and Kotlin 2.4 or newer.

Installation

Kortex is published to the Davils repository, not to Maven Central. Declare it for the com.davils group only:

// settings.gradle.kts
dependencyResolutionManagement {
    repositories {
        exclusiveContent {
            forRepository { maven("https://repo.davils.com/releases") }
            filter { includeGroupAndSubgroups("com.davils") }
        }
        mavenCentral()
    }
}
// build.gradle.kts
dependencies {
    implementation(platform("com.davils:kortex-bom:1.0.0"))
    implementation("com.davils:kortex-core")
}

A service

val Greeter = createPlugin(name = "greeter", configuration = ::GreeterBuilder) {
    val server = requires(Server)
    contribute(HealthIndicators, greeterHealth)
    shutdown { drain { task("greeter.drain") { action { drainGreetings() } } } }
    start { Greeter(configuration.greeting, server.get(), scope) }
}

val orders = service(scope = applicationScope, dispatcher = Dispatchers.IO) {
    identity {
        name = "orders"
        version = "1.4.0"
    }
    install(Server)
    install(Greeter) { greeting = "Hello" }
}
orders.start()

service { } runs the setup of every plugin and reports every problem at once: an invalid configuration, a missing or cyclic dependency, a setup without a start. start() starts the plugins in the order of their dependencies on the dispatcher of the service and hands every plugin its pipeline: the identity, its own scope, the dispatcher, the event bus, the meters, the tracer, the clock, the time source and the coordinated shutdown of Knot. When a plugin fails, every plugin that started is stopped again. stop() runs the shutdown phase by phase with the tasks of every plugin.

A monolith

val orders = service(scope = applicationScope, dispatcher = Dispatchers.IO) {
    identity {
        name = "orders"
        version = "1.4.0"
    }
    server {
        http {
            port = Port(8443)
            tls = serverContext
        }
        ipc { address = IpcAddress.Named(IpcName("orders")) }
    }
}
orders.start()

server { } installs the listeners together with Health, Info and Metrics. The public listener speaks TLS 1.3 through the TlsContext of krypt the application hands in and negotiates h2 or http/1.1; the IPC listener lets in only the users of the operating system it names. GET /health/live, /health/ready, /health/startup and /info are served on every listener, /metrics only on IPC and the mesh unless configured otherwise. Readiness fails until every plugin started and from the moment the shutdown begins; the listeners then refuse new calls with 503 and let the calls that already arrived finish within the drain timeout.

A mesh

val payments = service(scope = applicationScope, dispatcher = Dispatchers.IO) {
    identity {
        name = "payments"
        version = "2.1.0"
    }
    server {
        mesh {
            port = Port(9443)
            tls = paymentsMeshContext
        }
        ipc { address = IpcAddress.Named(IpcName("payments")) }
    }
}

val orders = service(scope = applicationScope, dispatcher = Dispatchers.IO) {
    identity {
        name = "orders"
        version = "1.4.0"
    }
    install(Clients) {
        tls = ordersMeshContext
        target("payments") {
            mesh(host = "10.0.0.7", port = Port(9443))
            ipc(IpcAddress.Named(IpcName("payments")))
        }
    }
}
val clients = checkNotNull(orders.plugin(Clients))
val charges = PaymentsChargesClient(clients.api("payments"))
when (val result = charges.createCharge(order)) {
    is PaymentsCreateChargeResult.Created -> confirm(result.body)
    is PaymentsCreateChargeResult.Other -> report(result.status, result.body.title)
    is PaymentsCreateChargeResult.Unexpected -> report(result.status)
    is PaymentsCreateChargeResult.Malformed -> report(result.status, result.reason)
    is PaymentsCreateChargeResult.Failed -> retryLater(result.failure)
}

PaymentsChargesClient stands for a client that kapi generated from the OpenAPI document of payments.

The mesh listener requires the client certificate of every peer and authorizes it, such as by the SPIFFE ids of its trust domain; the client authorizes the server it reaches the same way. Clients sends a call to one instance of the service it names: over IPC when an instance runs on the same host, otherwise on the mesh, and streams of server-sent events and upgrades over the mesh only. It balances by the power of two choices or round robin, leaves out an instance whose calls failed several times in a row for a while, unless it is the last one, and sends the trace and the deadline of the calling coroutine along as traceparent, tracestate and kortex-deadline, which the listeners put into the coroutine context of the call; the mesh and IPC listeners cancel a call when its deadline passes and answer it with 504. api(service) builds a client of kapi for the generated operations of a service with the meters, the tracer and the default retries and circuit breaker of kapi. Until the control plane arrives, the endpoints are static or come from a Discovery of the application.

A store

val orders = service(scope = applicationScope, dispatcher = Dispatchers.IO) {
    identity {
        name = "orders"
        version = "1.4.0"
    }
    server { ipc { address = IpcAddress.Named(IpcName("orders")) } }
    install(Store) {
        quotaBytes = 256L * 1_024 * 1_024
        access {
            allow("spiffe://prod.example/service/billing") { read(KeyRange.Prefix(Key.of("/orders/"))) }
        }
    }
}
val store = checkNotNull(orders.plugin(Store)).client
store.put(Key.of("/orders/42"), "open".encodeToByteString())
val lease = store.grant(30.seconds)
store.watch(KeyRange.Prefix(Key.of("/orders/"))).collect { event -> react(event) }

Store keeps keys and values in the memory of the service in the manner of etcd: every write makes a new revision, a range reads one revision as a whole, a transaction compares and writes at once, a watch sees every change from a revision on, and the keys bound to a lease end with it. The history is compacted by itself once it outgrows its retention, and a write past the quota is refused. Every call returns its failures as values. The service itself may do everything through client; a peer may do only what a grant names, which by default is nothing, through the API of kortex-store/src/main/openapi/store.yaml, served on the mesh and IPC listeners and never on a public one.

val store = service(scope = applicationScope, dispatcher = Dispatchers.IO) {
    install(Server) { mesh { port = Port(8444); tls = meshTls } }
    install(Clients) {
        target("store-2") { tls = toStore2; mesh(host = "10.0.0.2", port = Port(8444)) }
        target("store-3") { tls = toStore3; mesh(host = "10.0.0.3", port = Port(8444)) }
    }
    install(Raft) {
        node {
            id = NodeId(1L)
            members = Membership(setOf(NodeId(1L), NodeId(2L), NodeId(3L)))
        }
        peer(NodeId(2L), principal = "spiffe://prod.example/service/store-2", via = "store-2")
        peer(NodeId(3L), principal = "spiffe://prod.example/service/store-3", via = "store-3")
    }
    install(Store) { isReplicated = true }
}

A replicated store is the state machine of the Raft group of its service. Every write is a command of the log that a majority must take; it may be made on any member, which hands it to the leader. Every range and every lease waits for ReadIndex and sees every write that completed before it, and every member serves its watches from the writes it applied. Grants, renewals and revocations of leases go through the log; only the leader ends a lease whose time passed, and a new leader gives every lease its whole time to live again. A call waits up to leaderWait for a leader and is tried again meanwhile only when it provably never took effect; a write whose fate a member lost fails with StoreFailure.Indeterminate. A member that falls behind the compaction of the log gets a snapshot of the store, at most MAX_REPLICATED_QUOTA_BYTES. Five members keep the store through the loss of two.

An authority

val own = ownIdentity(crypto = primitives, certificate = root, signer = hsm, subject = trustDomain.idOf("service", "authority"))
val authority = service(scope = applicationScope, dispatcher = Dispatchers.IO) {
    install(Server) {
        mesh { port = Port(9443); tls = meshContextOf(own) }
        bootstrap { port = Port(9444); tls = bootstrapContextOf(own) }
    }
    install(Store) { }
    install(Authority) {
        crypto = primitives
        certificate = root
        signer = hsm
        trustDomain = "prod.example"
        record(own.certificate)
    }
}
val token = checkNotNull(authority.plugin(Authority)).token("orders")

val enrolled = enroll(primitives) {
    host = "authority.prod.example"
    trustDomain = "prod.example"
    anchors = listOf(root)
    this.token = handedOverToken
}

Authority issues the X.509-SVIDs of a trust domain with a certificate authority of krypt; its key stays behind the signer of the application, such as an HSM. A service without a certificate enrolls on the bootstrap listener of the authority with a single-use join token and the certificate of the authority, which come together, so that the TLS of its first call is verified, and to the SPIFFE id of the authority only. The authority keeps a token only as a digest under a lease of the store and consumes it in the transaction that checks it. A service renews its certificate over the mesh with renew, for the SPIFFE id it calls as, and hands the new key to its TLS contexts with TlsContext.rotate. Every instance with the signer issues alike, since the certificates, the revocations and the tokens live in the store; one instance is elected through a lease of the store to publish the revocation lists, fenced by the key of its leadership. A certificate is issued only for a certification request whose signature proves its key, and only for a SPIFFE id that comes from the token or the caller, never from the request. Short validities, an hour by default, take the place of revocation checks on the mesh.

A control plane

val controlPlane = service(scope = applicationScope, dispatcher = Dispatchers.IO) {
    install(Server) { mesh { port = Port(9443); tls = meshTls } }
    install(Raft) {
        node {
            id = NodeId(1L)
            members = Membership(setOf(NodeId(1L), NodeId(2L), NodeId(3L)))
        }
        peer(NodeId(2L), principal = "spiffe://prod.example/service/control-plane-2", via = "control-plane-2")
        peer(NodeId(3L), principal = "spiffe://prod.example/service/control-plane-3", via = "control-plane-3")
    }
    install(Store) { isReplicated = true }
    install(ControlPlane) {
        trustDomain = "prod.example"
        admin("spiffe://prod.example/service/operator")
    }
}

ControlPlane keeps the registry of a mesh in the manner of Kubernetes in the store of its service: the instances of the services, which live under a lease of the store as long as they send heartbeats, the endpoint slice of every service with its ready instances, and the roles, role bindings and access policies. Every resource carries a resource version, the revision of the store it was last written at; a write names it to replace exactly that version, and a list followed by a watch from its revision sees every change. The API of control-plane.yaml is served on the mesh only and authorized for the SPIFFE id of every caller: an instance is written only by the service it names, every service of the trust domain reads the endpoints and the access policies, and everything else needs an admin or a role binding. One instance of the control plane is elected through a lease of the store to run the endpoint controller, which acts on every change of the instances and writes the slices fenced by the key of its leadership.

An agent

val enrolled = enroll(primitives) {
    host = "authority.prod.example"
    service = "control-plane"
    trustDomain = "prod.example"
    anchors = listOf(root)
    token = joinToken
}
val credentials = MeshCredentials.of(primitives, enrolled.getOrThrow())
val found = MeshDiscovery()
val orders = service(scope = applicationScope, dispatcher = Dispatchers.IO) {
    identity { name = "orders"; version = "1.4.0" }
    install(Server) { mesh { port = Port(9443); tls = credentials.server } }
    install(Health)
    install(Clients) {
        tls = credentials.client
        discovery = found
        target("control-plane") { mesh(host = "control-plane.prod.example", port = Port(9443)) }
        target("authority") { mesh(host = "control-plane.prod.example", port = Port(9443)) }
    }
    install(Agent) {
        crypto = primitives
        this.credentials = credentials
        discovery = found
        host = "10.0.0.7"
    }
}

Agent runs in every instance of a service like a kubelet. It registers the instance with the control plane under its instance id, with its mesh listener and, when it names its host, its IPC listener, sends a heartbeat every three seconds, registers again when the control plane lost the instance and reports it ready once its watches are synced and the readiness of Health is not down. It lists and watches the endpoint slices and fills the MeshDiscovery of the clients, so that clients.api("payments") balances across the ready instances of payments, over IPC when one runs on the same host. It lists and watches the access policies and lets a caller through the mesh listener only when it is an instance of the same service or a policy for this service names its service as a source; every other call is answered with 403. It renews the certificate of the service when half of its remaining life has passed and hands the new one to the TLS contexts of MeshCredentials without a restart, and it deregisters the instance while the service announces its shutdown.

The control plane runs no agent; the other services reach it and the authority through static targets of their clients. Any number of independent services, such as orders, payments and billing, share one mesh: each enrolls with a token for its own SPIFFE id, scales by starting more instances, and is reachable from another service only once an access policy allows it.

Raft

val node = raftNode(scope = applicationScope, dispatcher = Dispatchers.IO, entropy = SecureEntropy) {
    id = NodeId(7L)
    members = Membership(voters = setOf(NodeId(7L), NodeId(8L), NodeId(9L)))
    transport = RaftTransport { message -> peers.send(message) }
    machine = counters
}
node.start()
val result = node.propose(command)
node.read()

RaftNode runs one member of a Raft group on coroutines: it ticks on its own, keeps its term, its vote and its entries in its storage before it answers, sends through the transport the application hands in and applies the committed commands to its state machine in their order. A follower asks first whether it would win, so that a member cut off from its group cannot disturb it, and a leader steps down once a majority stops answering. Reads wait for ReadIndex, the members change one at a time through learners, a member that lags behind the compaction gets a snapshot and a leader hands its leadership over on request. Every message of a peer is checked against the members, the term and the limits of Raft before the node acts on it. The core runs without time, threads or I/O, so the tests drive it through thousands of histories with dropped, repeated and reordered messages, partitions and crashes and check every history for linearizability.

val store = service(scope = applicationScope, dispatcher = Dispatchers.IO) {
    install(Server) { mesh { port = Port(8444); tls = meshTls } }
    install(Clients) {
        target("store-2") { tls = toStore2; mesh(host = "10.0.0.2", port = Port(8444)) }
        target("store-3") { tls = toStore3; mesh(host = "10.0.0.3", port = Port(8444)) }
    }
    install(Raft) {
        node {
            id = NodeId(1L)
            members = Membership(setOf(NodeId(1L), NodeId(2L), NodeId(3L)))
            machine = counters
        }
        peer(NodeId(2L), principal = "spiffe://prod.example/service/store-2", via = "store-2")
        peer(NodeId(3L), principal = "spiffe://prod.example/service/store-3", via = "store-3")
    }
}

The plugin Raft runs the member of a group inside a service. It sends its messages through Clients to the target of each peer, over the mesh with mutual TLS or over IPC on the same host, batched per peer, a snapshot in chunks, and drops what a peer does not take in time, which Raft sends again. It serves kortex-raft/src/main/openapi/raft.yaml on the mesh and IPC listeners only and hands a message on only when the principal that sent it is the one its sender is configured with.

Architecture

  • Monolith. One service with its public listener, optionally an IPC listener, and the standard plugins: health, readiness, liveness, startup, info and metrics, documented in kortex-server/src/main/openapi/openapi.yaml.
  • Mesh. Three or five control-plane nodes hold the store on Raft, the API server and the authority. Every other service runs the agent: it enrolls with a single-use join token against a pinned authority, renews its certificate over mutual TLS, registers its instance, keeps its lease, reports its readiness and enforces the access policies on its mesh listener. Clients watch the endpoints of the services they call and balance across the ready ones, preferring IPC on the same host.
  • Store. A key-value store in the manner of etcd, with revisions, transactions, watches and leases, that applications can use as well.

Security

Kortex assumes that every request, peer, certificate, token and message may come from an attacker. SECURITY.md describes the threat model, the known limitations and how to report a vulnerability.

Building

./gradlew check

It needs JDK 25 and network access. AGENTS.md holds the rules every change follows and CONTRIBUTING.md how a change gets in.

./gradlew :kortex-benchmark:benchmark

runs the throughput benchmarks of kortex-benchmark with JMH: the local store, Raft in its deterministic simulation, and calls between two services over mutual TLS and over IPC. smokeBenchmark runs them briefly.

License

Copyright 2026 Davils. Licensed under the Apache License, Version 2.0.

About

The service framework of Davils: services built from plugins with dependencies, TLS 1.3 listeners, a mesh with mutual TLS and SPIFFE, IPC, discovery and load balancing, its own Raft and etcd-like store, a certificate authority, a Kubernetes-like control plane and APIs from OpenAPI 3.2 for Kotlin on the JVM, reading nothing from the file system.

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

1 star

Watchers

0 watching

Forks

Releases

Contributors

Languages