|
| 1 | +# Discovery Service Project |
| 2 | + |
| 3 | +## Overview |
| 4 | +This project implements a public discovery service designed for decentralized, secure peer discovery. The core innovation is the use of **Custom Certificate Authorities (CAs)** to define isolated "Universes". Clients register and discover peers within their own Universe, identified and secured purely by mTLS. |
| 5 | + |
| 6 | +The service emulates a Kubernetes API, allowing interaction via `kubectl`, including support for **Server-Side Apply**. |
| 7 | + |
| 8 | +## Key Concepts |
| 9 | + |
| 10 | +### 1. The "Universe" |
| 11 | +- A **Universe** is an isolated scope for peer discovery. |
| 12 | +- It is cryptographically defined by the **SHA256 hash of the Root CA's Public Key**. |
| 13 | +- Any client possessing a valid certificate signed by a specific CA belongs to that CA's Universe. |
| 14 | +- Different CAs = Different Universes. There is no crossover. |
| 15 | + |
| 16 | +### 2. Authentication & Authorization |
| 17 | +- **Mechanism**: Mutual TLS (mTLS). |
| 18 | +- **Client Identity**: Derived from the **Common Name (CN)** of the leaf certificate. |
| 19 | +- **Universe Context**: Derived from the **Root CA** presented in the TLS handshake. |
| 20 | +- **Requirement**: Clients **MUST** present the full certificate chain (Leaf + Root CA) during the handshake. The server does not maintain a pre-configured trust store for these custom CAs; it uses the presented chain to determine the scope. |
| 21 | + |
| 22 | +### 3. API Resources |
| 23 | +- **DiscoveryEndpoint** (`discovery.kops.k8s.io/v1`): Represents a peer in the discovery network. Can optionally hold OIDC configuration (Issuer URL, JWKS). |
| 24 | +- **Validation**: A client with CN `client1` can only Create/Update a `DiscoveryEndpoint` named `client1`. |
| 25 | +- **Apply Support**: The server supports `PATCH` requests to facilitate `kubectl apply --server-side`. |
| 26 | + |
| 27 | +### 4. OIDC Discovery |
| 28 | +The server acts as an OIDC Discovery Provider for the Universe. |
| 29 | +- **Public Endpoints**: |
| 30 | + - `/.well-known/openid-configuration`: Returns the OIDC discovery document. |
| 31 | + - `/openid/v1/jwks`: Returns the JSON Web Key Set (JWKS). |
| 32 | +- **Data Source**: These endpoints serve data uploaded by clients via the `DiscoveryEndpoint` resource. |
| 33 | + |
| 34 | +## Architecture |
| 35 | + |
| 36 | +### Project Structure |
| 37 | +- `cmd/discovery-server/`: Main entry point. Wires up the HTTP server with TLS configuration. |
| 38 | +- `pkg/discovery/`: |
| 39 | + - `auth.go`: logic for inspecting TLS `PeerCertificates` to extract the Universe ID (CA hash) and Client ID. |
| 40 | + - `store.go`: In-memory thread-safe storage (`MemoryStore`) mapping Universe IDs to lists of `DiscoveryEndpoint` objects. |
| 41 | + - `server.go`: HTTP handlers implementing the K8s API emulation for `/apis/discovery.kops.k8s.io/v1`. |
| 42 | + - `k8s_types.go`: Definitions of `DiscoveryEndpoint`, `DiscoveryEndpointList`, `TypeMeta`, `ObjectMeta` etc. |
| 43 | + |
| 44 | +### Data Model |
| 45 | +- **DiscoveryEndpoint**: The core resource. Contains `Spec.Addresses` and metadata. |
| 46 | +- **Universe**: Contains a map of `DiscoveryEndpoint` objects (keyed by name). |
| 47 | +- **Unified Types**: The API type `DiscoveryEndpoint` is used directly for in-memory storage, ensuring zero conversion overhead. |
| 48 | + |
| 49 | +## Security Model |
| 50 | +- **Trust Delegation**: The server delegates trust to the CA. If you hold the CA key, you control the Universe. |
| 51 | +- **Isolation**: The server ensures that a client presenting a cert chain for `CA_A` cannot read or write data to the Universe defined by `CA_B`. |
| 52 | +- **Ephemeral**: The current implementation uses in-memory storage. Data is lost on restart. |
| 53 | + |
| 54 | +## Building and Running |
| 55 | + |
| 56 | +### Build |
| 57 | +```bash |
| 58 | +go build ./cmd/discovery-server |
| 59 | +``` |
| 60 | + |
| 61 | +### Run |
| 62 | + |
| 63 | +See docs/walkthrough.md for instructions on testing functionality. |
0 commit comments