Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-pkg-server

Source of release 0.6.1

0.6.1 / src / registry.lucb

## Loopback registry API. Reverse-proxy headers never establish identity.
from luce_server import http
import interop
from luce_db import db
from luce_auth import auth
import ownership
from luce_json import json
import os
import c
import strings
import repositories
from luce_git import git
import memory
import io
import http_auth as transport_auth
from luce_crypto import native as crypto
import time
import rate_limit
import site as site_files

let max_release_upload: usize = repositories.max_object_bytes + 24576 + 3309 + 8
## The largest response: a receive advertisement, one line per release tag.
let max_advertisement: usize = 4194304

func failed(code: ErrorCode, message: str) -> interop.Outcome[interop.Reference[http.Response]]:
    let copied = interop.copy_text(message) catch failure:
        return interop.Outcome[interop.Reference[http.Response]].failure(failure.code, interop.Owned[str]("internal error"))
    return interop.Outcome[interop.Reference[http.Response]].failure(code, copied)

func reply(text: str, status: i64 = 200) -> interop.Outcome[interop.Reference[http.Response]]:
    return binary_reply(text.bytes, "text/plain; charset=utf-8", status)

func binary_reply(bytes: const u8[], content_type: str, status: i64 = 200) -> interop.Outcome[interop.Reference[http.Response]]:
    let value = http.Response.bytes(bytes, content_type, status) catch failure:
        return failed(failure.code, "internal error")
    value.value().header("Cache-Control", "no-store") catch failure:
        value.release()
        return failed(failure.code, "internal error")
    return interop.Outcome[interop.Reference[http.Response]].success(interop.Owned[interop.Reference[http.Response]](value, &value.owner.header))

func basic_challenge() -> interop.Outcome[interop.Reference[http.Response]]:
    let value = http.Response.bytes(b"authentication required", "text/plain; charset=utf-8", 401) catch failure:
        return failed(failure.code, "internal error")
    value.value().header("Cache-Control", "no-store") catch failure:
        value.release()
        return failed(failure.code, "internal error")
    value.value().header("WWW-Authenticate", "Basic realm=\"pkg.luciaos.com\", charset=\"UTF-8\"") catch failure:
        value.release()
        return failed(failure.code, "internal error")
    return interop.Outcome[interop.Reference[http.Response]].success(interop.Owned[interop.Reference[http.Response]](value, &value.owner.header))

func rate_limited() -> interop.Outcome[interop.Reference[http.Response]]:
    let value = http.Response.bytes(b"rate limited", "text/plain; charset=utf-8", 429) catch failure:
        return failed(failure.code, "internal error")
    value.value().header("Cache-Control", "no-store") catch failure:
        value.release()
        return failed(failure.code, "internal error")
    value.value().header("Retry-After", "60") catch failure:
        value.release()
        return failed(failure.code, "internal error")
    return interop.Outcome[interop.Reference[http.Response]].success(interop.Owned[interop.Reference[http.Response]](value, &value.owner.header))

func valid_token(token: str) -> bool:
    if token.length != 32: return false
    for byte in token.bytes:
        if not ((byte >= '0' and byte <= '9') or (byte >= 'a' and byte <= 'f')): return false
    return true

func valid_credential_token(token: str) -> bool:
    if token.length != 64: return false
    for byte in token.bytes:
        if not ((byte >= '0' and byte <= '9') or (byte >= 'a' and byte <= 'f')): return false
    return true

func bearer(request: const http.Request*) -> str!:
    let header = (try request.header("Authorization")) else error(auth.invalid, "authentication required")
    if not strings.starts_with(header, "Bearer "): error(auth.invalid, "authentication required")
    let token = (str)header.bytes[7..]
    if not valid_token(token): error(auth.invalid, "authentication required")
    return token

func credential_bearer(request: const http.Request*) -> str!:
    let header = (try request.header("Authorization")) else error(auth.invalid, "authentication required")
    if not strings.starts_with(header, "Bearer "): error(auth.invalid, "authentication required")
    let token = (str)header.bytes[7..]
    if not valid_credential_token(token): error(auth.invalid, "authentication required")
    return token

func field(root: json.View, name: str) -> str!:
    let value = root.member(name) else error(auth.invalid, "missing field")
    return value.text() else error(auth.invalid, "string field required")

struct App:
    var database: interop.Reference[db.Database]
    var authority: interop.Reference[auth.Authority]
    var origin: u8[255]
    var origin_size: usize = 0
    ## Directory of static public release files; empty disables tag releases.
    var site: u8[255]
    var site_size: usize = 0

    func close():
        self.authority.release()
        self.database.release()

    func health(view: interop.View[http.Request]) -> interop.Outcome[interop.Reference[http.Response]]:
        _ = self
        _ = view
        return reply("ok")

    func identity(view: interop.View[http.Request]) -> interop.Outcome[interop.Reference[http.Response]]:
        let token = bearer(view.value()) catch: return reply("unauthorized", 401)
        let who = self.authority.value().verify(token) catch: return reply("unauthorized", 401)
        defer who.release()
        let name = who.value().text_at() catch: return reply("internal error", 500)
        return reply(name)

    func redeem(view: interop.View[http.Request]) -> interop.Outcome[interop.Reference[http.Response]]:
        let body = view.value().body() catch: return reply("invalid body", 400)
        defer body.release()
        let payload = body.value().bytes(4096) catch: return reply("invalid body", 400)
        let document = json.decode((str)payload, 4096) catch: return reply("invalid JSON", 400)
        defer document.release()
        let root = document.value().root()
        if root.kind() != json.Kind.object or root.length() != 3: return reply("invalid fields", 400)
        let code = field(root, "code") catch: return reply("invalid fields", 400)
        let name = field(root, "name") catch: return reply("invalid fields", 400)
        let password = field(root, "password") catch: return reply("invalid fields", 400)
        if not valid_token(code): return reply("invalid invitation", 400)
        if registration_closed(): return reply("registration is closed", 403)
        if not rate_limit.admit_registration(time.unix()): return rate_limited()
        self.authority.value().accept(code, name, password) catch failure:
            if failure.code == auth.conflict: return reply("registration conflict", 409)
            if failure.code == auth.invalid: return reply("invalid registration", 400)
            return reply("registration failed", 503)
        return reply("registered", 201)

    func login(view: interop.View[http.Request]) -> interop.Outcome[interop.Reference[http.Response]]:
        let body = view.value().body() catch: return reply("invalid body", 400)
        defer body.release()
        let payload = body.value().bytes(4096) catch: return reply("invalid body", 400)
        let document = json.decode((str)payload, 4096) catch: return reply("invalid JSON", 400)
        defer document.release()
        let root = document.value().root()
        if root.kind() != json.Kind.object or root.length() != 2: return reply("invalid fields", 400)
        let name = field(root, "name") catch: return reply("invalid fields", 400)
        let password = field(root, "password") catch: return reply("invalid fields", 400)
        if not rate_limit.admit_login(time.unix()): return rate_limited()
        let token = self.authority.value().login(name, password) catch failure:
            if failure.code == auth.busy: return reply("authentication busy", 503)
            return reply("unauthorized", 401)
        defer token.release()
        let text = token.value().text_at() catch: return reply("internal error", 500)
        return reply(text)

    func logout(view: interop.View[http.Request]) -> interop.Outcome[interop.Reference[http.Response]]:
        let token = bearer(view.value()) catch: return reply("unauthorized", 401)
        self.authority.value().revoke(token) catch: return reply("unauthorized", 401)
        return reply("revoked")

    func issue_credential(view: interop.View[http.Request]) -> interop.Outcome[interop.Reference[http.Response]]:
        let token = bearer(view.value()) catch: return reply("unauthorized", 401)
        let who = self.authority.value().verify(token) catch: return reply("unauthorized", 401)
        defer who.release()
        let body = view.value().body() catch: return reply("invalid body", 400)
        defer body.release()
        let payload = body.value().bytes(4096) catch: return reply("invalid body", 400)
        let document = json.decode((str)payload, 4096) catch: return reply("invalid JSON", 400)
        defer document.release()
        let root = document.value().root()
        if root.kind() != json.Kind.object or root.length() != 3: return reply("invalid fields", 400)
        let scope = field(root, "scope") catch: return reply("invalid fields", 400)
        let repository = field(root, "repository") catch: return reply("invalid fields", 400)
        let lifetime_field = root.member("lifetime_seconds") else return reply("invalid fields", 400)
        let lifetime = lifetime_field.integer() else return reply("invalid fields", 400)
        let credential = self.authority.value().issue_credential(token, scope, repository, lifetime) catch failure:
            if failure.code == auth.invalid: return reply("invalid credential request", 400)
            return reply("credential issuance unavailable", 503)
        defer credential.release()
        let text = credential.value().text_at() catch: return reply("internal error", 500)
        return reply(text, 201)

    func revoke_credential(view: interop.View[http.Request]) -> interop.Outcome[interop.Reference[http.Response]]:
        let session = bearer(view.value()) catch: return reply("unauthorized", 401)
        let who = self.authority.value().verify(session) catch: return reply("unauthorized", 401)
        defer who.release()
        let body = view.value().body() catch: return reply("invalid body", 400)
        defer body.release()
        let payload = body.value().bytes(4096) catch: return reply("invalid body", 400)
        let document = json.decode((str)payload, 4096) catch: return reply("invalid JSON", 400)
        defer document.release()
        let root = document.value().root()
        if root.kind() != json.Kind.object or root.length() != 1: return reply("invalid fields", 400)
        let credential = field(root, "token") catch: return reply("invalid fields", 400)
        if not valid_credential_token(credential): return reply("invalid fields", 400)
        self.authority.value().revoke_credential(session, credential) catch: return reply("credential not found", 404)
        return reply("revoked")

    func create_repository(view: interop.View[http.Request]) -> interop.Outcome[interop.Reference[http.Response]]:
        let token = bearer(view.value()) catch: return reply("unauthorized", 401)
        let who = self.authority.value().verify(token) catch: return reply("unauthorized", 401)
        defer who.release()
        let principal = who.value().text_at() catch: return reply("internal error", 500)
        let body = view.value().body() catch: return reply("invalid body", 400)
        defer body.release()
        let payload = body.value().bytes(4096) catch: return reply("invalid body", 400)
        let document = json.decode((str)payload, 4096) catch: return reply("invalid JSON", 400)
        defer document.release()
        let root = document.value().root()
        if root.kind() != json.Kind.object or root.length() != 1: return reply("invalid fields", 400)
        let name = field(root, "name") catch: return reply("invalid fields", 400)
        repositories.create(self.database.value(), principal, name) catch failure:
            if failure.code == auth.invalid: return reply("invalid repository", 400)
            if failure.code == auth.conflict: return reply("repository conflict", 409)
            return reply("repository creation failed", 503)
        return reply("created", 201)

    ## Git smart HTTP for `git push` of release tags, the only thing a client sends the
    ## registry: discovery and receive-pack, with a git:write credential for this
    ## repository. Nothing is served to clone or fetch: installs read the site's files.
    func git_http(view: interop.View[http.Request]) -> interop.Outcome[interop.Reference[http.Response]]:
        let request = view.value()
        let owner = request.parameter("owner") else return reply("invalid path", 400)
        let name = request.parameter("name") else return reply("invalid path", 400)
        if not repositories.valid_name(name): return reply("invalid repository", 400)
        let discovery = request.method() == "GET" or request.method() == "HEAD"
        if discovery:
            let service = (request.query("service") catch: return reply("invalid service", 400)) else return reply("service required", 400)
            if service != "git-receive-pack":
                return reply("the registry takes release tags by push and serves no Git reads; clone from the package's GitHub repository", 403)
        let header = (request.header("Authorization") catch: return basic_challenge()) else return basic_challenge()
        var credential_buffer: u8[128]
        defer crypto.wipe(credential_buffer)
        let credential = transport_auth.decode_basic(header, credential_buffer) catch: return basic_challenge()
        if credential.username != owner: return basic_challenge()
        let who = self.authority.value().authorize_credential(credential.token, auth.scope_git_write, owner, name) catch: return basic_challenge()
        defer who.release()
        let principal = who.value().text_at() catch: return reply("internal error", 500)
        if discovery:
            let output = new u8[max_advertisement] --- in memory.heap catch: return reply("internal error", 500)
            defer free(output) in memory.heap
            let n = repositories.advertise_receive(self.database.value(), principal, owner, name, output) catch failure:
                if failure.code == auth.missing: return reply("repository not found", 404)
                return reply("discovery failed", 503)
            return binary_reply(output[..<n], "application/x-git-receive-pack-advertisement")
        let content_type = (request.header("Content-Type") catch: return reply("invalid content type", 415)) else return reply("content type required", 415)
        if content_type != "application/x-git-receive-pack-request": return reply("unsupported content type", 415)
        # Git compresses only fetch requests; a push body is sent as it is.
        let encoding = (request.header("Content-Encoding") catch: return reply("invalid content encoding", 400)) else ""
        if encoding.length > 0 and encoding != "identity": return reply("unsupported content encoding", 415)
        let body = request.body() catch: return reply("invalid body", 400)
        defer body.release()
        let wire = body.value().bytes((i64)repositories.max_object_bytes) catch: return reply("invalid body", 400)
        # Before a push larger than its buffer, Git sends a lone flush packet to check that
        # it is authorized. It already is by this point; answer with an empty result.
        if wire.length == 4 and (str)wire == "0000": return binary_reply(b"", "application/x-git-receive-pack-result")
        let commands = git.decode_push(wire) catch: return reply("invalid push request", 400)
        var report = false
        var start: usize = 0
        for i in 0..<commands.capabilities.length + 1:
            if i == commands.capabilities.length or commands.capabilities.bytes[i] == ' ':
                if (str)commands.capabilities.bytes[start..<i] == "report-status": report = true
                start = i + 1
        # Reserve bounded status storage before any ref mutation. The parser can
        # return 64 names of up to 4096 bytes, even when policy rejects them.
        let output = new u8[270336] --- in memory.heap catch: return reply("internal error", 500)
        defer free(output) in memory.heap
        var accepted = true
        # Release-rule failures and the limits a push exceeded are reported to the pusher, who
        # can act on them; anything else stays generic.
        var reason_buffer: u8[160]
        var reason_size: usize = 0
        _ = repositories.receive_pack(self.database.value(), principal, owner, name, wire, (str)self.site[..<self.site_size]) catch failure:
            accepted = false
            # The pusher sees only those reasons; the journal keeps every one, as
            # one line of plain text whatever bytes the message holds.
            var log_line: u8[512]
            var used = (strings.format(log_line, f"push to {owner}/{name} refused: ") catch: recover "").length
            for byte in failure.message.bytes:
                if used == log_line.length - 1: break
                log_line[used] = byte if byte >= 0x20 and byte < 0x7f else '?'
                used += 1
            log_line[used] = '\n'
            _ = io.stderr().write(log_line[..<used + 1]) catch: recover 0
            let explained = failure.message if strings.starts_with(failure.message, "release: ") or strings.ends_with(failure.message, " limit exceeded") else ""
            for byte in explained.bytes:
                if reason_size == reason_buffer.length: break
                reason_buffer[reason_size] = byte if byte >= 0x20 and byte < 0x7f else ' '
                reason_size += 1
            recover 0
        let reason = (str)reason_buffer[..<reason_size] if reason_size > 0 else "transaction rejected"
        if not report:
            if not accepted: return reply("push rejected", 409)
            return binary_reply(b"", "application/x-git-receive-pack-result")
        var used = git.encode_packet(git.PacketKind.data, b"unpack ok\n" if accepted else b"unpack rejected\n", output) catch: return reply("internal error", 500)
        for i in 0..<commands.count:
            let command = commands.at(i) catch: return reply("invalid push", 400)
            var line: u8[4352]
            let text = strings.format(line, f"ok {command.name}\n" if accepted else f"ng {command.name} {reason}\n") catch: return reply("internal error", 500)
            used += git.encode_packet(git.PacketKind.data, text.bytes, output[used..]) catch: return reply("internal error", 500)
        used += git.encode_packet(git.PacketKind.flush, b"", output[used..]) catch: return reply("internal error", 500)
        return binary_reply(output[..<used], "application/x-git-receive-pack-result")

let app_type = interop.Type[App]("registry", App.close)

func store_token() -> str!:
    let token = os.env((c.str)"LUCE_REGISTRY_STORE_TOKEN") else error(auth.invalid, "LUCE_REGISTRY_STORE_TOKEN is required")
    if token.length == 0: error(auth.invalid, "empty store token")
    return token

func configured_origin() -> str!:
    let origin = os.env((c.str)"LUCE_REGISTRY_ORIGIN") else ""
    if origin.length > 255: error(auth.invalid, "registry origin exceeds 255 bytes")
    for byte in origin.bytes:
        if byte < 33 or byte > 126: error(auth.invalid, "invalid registry origin")
    return origin

## LUCE_REGISTRY_REGISTRATION=closed refuses every invitation, even a valid one.
func registration_closed() -> bool:
    return (os.env((c.str)"LUCE_REGISTRY_REGISTRATION") else "") == "closed"

func configured_site() -> str!:
    let site = os.env((c.str)"LUCE_REGISTRY_SITE") else ""
    if site.length > 255: error(auth.invalid, "registry site path exceeds 255 bytes")
    if site.length > 0 and site.bytes[0] != '/': error(auth.invalid, "registry site path must be absolute")
    return site

func configure(socket: str) -> interop.Outcome[http.Application]:
    let database = db.Database.connect(socket, store_token() catch failure: trap(failure.message)) catch failure: trap(failure.message)
    let authority = auth.Authority.attach(database.value().store() catch failure: trap(failure.message)) catch failure:
        database.release()
        trap(failure.message)
    let native = new App(database = database, authority = authority) catch failure:
        authority.release()
        database.release()
        trap(failure.message)
    let origin = configured_origin() catch failure: trap(failure.message)
    for i in 0..<origin.length:
        native.origin[i] = origin.bytes[i]
    native.origin_size = origin.length
    let site = configured_site() catch failure: trap(failure.message)
    for i in 0..<site.length:
        native.site[i] = site.bytes[i]
    native.site_size = site.length
    if site.length > 0: site_files.ensure_front_page(site) catch failure: trap(failure.message)
    if site.length > 0: repositories.regenerate_site(native.database.value(), site) catch failure: trap(failure.message)
    let app = interop.Reference[App].adopt(native, app_type) catch failure: trap(failure.message)
    defer app.release()
    var router = http.Router() catch failure: trap(failure.message)
    defer router.close()
    let health = http.Handler.bind(app, App.health) catch failure: trap(failure.message)
    defer health.release()
    router.get("/health", health) catch failure: trap(failure.message)
    let identity = http.Handler.bind(app, App.identity) catch failure: trap(failure.message)
    defer identity.release()
    router.get("/v1/identity", identity) catch failure: trap(failure.message)
    let redeem = http.Handler.bind(app, App.redeem) catch failure: trap(failure.message)
    defer redeem.release()
    router.post("/v1/invites/redeem", redeem) catch failure: trap(failure.message)
    let login = http.Handler.bind(app, App.login) catch failure: trap(failure.message)
    defer login.release()
    router.post("/v1/sessions", login) catch failure: trap(failure.message)
    let logout = http.Handler.bind(app, App.logout) catch failure: trap(failure.message)
    defer logout.release()
    router.post("/v1/sessions/revoke", logout) catch failure: trap(failure.message)
    let issue_credential = http.Handler.bind(app, App.issue_credential) catch failure: trap(failure.message)
    defer issue_credential.release()
    router.post("/v1/credentials", issue_credential) catch failure: trap(failure.message)
    let revoke_credential = http.Handler.bind(app, App.revoke_credential) catch failure: trap(failure.message)
    defer revoke_credential.release()
    router.post("/v1/credentials/revoke", revoke_credential) catch failure: trap(failure.message)
    let create_repository = http.Handler.bind(app, App.create_repository) catch failure: trap(failure.message)
    defer create_repository.release()
    router.post("/v1/repositories", create_repository) catch failure: trap(failure.message)
    let git_http = http.Handler.bind(app, App.git_http) catch failure: trap(failure.message)
    defer git_http.release()
    router.get("/git/{owner}/{name}/info/refs", git_http) catch failure: trap(failure.message)
    router.post("/git/{owner}/{name}/git-receive-pack", git_http) catch failure: trap(failure.message)
    let result = router.application() catch failure: trap(failure.message)
    return interop.Outcome[http.Application].success(interop.Owned[http.Application](result, result.owner))

func run(arguments: str[]) -> i32!:
    if arguments.length != 3: return 2
    _ = try configured_origin()
    var port: i64 = 0
    for byte in arguments[2].bytes:
        if byte < '0' or byte > '9': return 2
        port = port * 10 + (i64)(byte - '0')
        if port > 65535: return 2
    if arguments[2].length == 0: return 2
    let owner = try db.Database.open(arguments[1], try store_token())
    defer owner.release()
    let authority = try auth.Authority.attach(try owner.value().store())
    defer authority.release()
    try repositories.initialize(owner.value())
    try repositories.use_objects(arguments[1])
    try owner.value().listen()
    var socket_buffer: u8[4096]
    let socket = try strings.format(socket_buffer, f"{arguments[1]}.sock")
    var options = http.ServerConfig(address = "127.0.0.1", port = port, network_workers = 2, application_workers = 2,
                                    body_limit = (i64)max_release_upload, memory_body_limit = 65536,
                                    response_limit = (i64)max_advertisement,
                                    request_timeout_ms = 300000, idle_timeout_ms = 15000,
                                    response_timeout_ms = 300000, application_timeout_ms = 300000,
                                    shutdown_timeout_ms = 30000)
    var server = try http.Server(options, interop.WorkerEntry[str, unit, unit].native(configure), socket)
    defer server.close()
    try server.shutdown_on_signals()
    try server.start()
    print(f"READY {try server.port()}")
    try server.run()
    return 0

pub func main(arguments: str[]) -> i32!:
    let result = try run(arguments)
    return ownership.finish_run(result)