package socialweb import ( "context" "encoding/json" "errors" "fmt" "log/slog" "net/http" "os" "runtime" "strings" "time" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" kruntime "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/rest" ctrlclient "sigs.k8s.io/controller-runtime/pkg/client" commonv1 "github.com/johnandersen777/socialweb-computer-kcp/api/common/v1alpha1" eventsv1alpha1 "github.com/johnandersen777/socialweb-computer-kcp/api/events/v1alpha1" exposurev1alpha1 "github.com/johnandersen777/socialweb-computer-kcp/api/exposure/v1alpha1" pdsv1alpha1 "github.com/johnandersen777/socialweb-computer-kcp/api/pds/v1alpha1" policyv1alpha1 "github.com/johnandersen777/socialweb-computer-kcp/api/policy/v1alpha1" "github.com/johnandersen777/socialweb-computer-kcp/api/v1alpha1" "github.com/johnandersen777/socialweb-computer-kcp/internal/app/compose" "github.com/johnandersen777/socialweb-computer-kcp/internal/app/marketrequester" "github.com/johnandersen777/socialweb-computer-kcp/internal/atproto/oauth" atpds "github.com/johnandersen777/socialweb-computer-kcp/internal/atproto/pds" "github.com/johnandersen777/socialweb-computer-kcp/internal/atproto/pdsclient" reporemote "github.com/johnandersen777/socialweb-computer-kcp/internal/atprotorepo/remote" "github.com/johnandersen777/socialweb-computer-kcp/internal/bus" "github.com/johnandersen777/socialweb-computer-kcp/internal/component" componentlink "github.com/johnandersen777/socialweb-computer-kcp/internal/controllers/componentlink" "github.com/johnandersen777/socialweb-computer-kcp/internal/controllers/contractevent" eventsctl "github.com/johnandersen777/socialweb-computer-kcp/internal/controllers/events" "github.com/johnandersen777/socialweb-computer-kcp/internal/controllers/infra" "github.com/johnandersen777/socialweb-computer-kcp/internal/controllers/marketidentity" "github.com/johnandersen777/socialweb-computer-kcp/internal/controllers/marketintents" "github.com/johnandersen777/socialweb-computer-kcp/internal/controllers/policy" "github.com/johnandersen777/socialweb-computer-kcp/internal/controllers/repos" ctlruntime "github.com/johnandersen777/socialweb-computer-kcp/internal/controllers/runtime" "github.com/johnandersen777/socialweb-computer-kcp/internal/controllers/runtime/kcpapply" "github.com/johnandersen777/socialweb-computer-kcp/internal/controllers/runtime/kube" "github.com/johnandersen777/socialweb-computer-kcp/internal/controllers/runtime/manifests" contractexposure "github.com/johnandersen777/socialweb-computer-kcp/internal/exposure" "github.com/johnandersen777/socialweb-computer-kcp/internal/marketidentity/atproto" "github.com/johnandersen777/socialweb-computer-kcp/internal/oauthcallback" "github.com/johnandersen777/socialweb-computer-kcp/wasm/internal/browserinfra/exposure" bvault "github.com/johnandersen777/socialweb-computer-kcp/wasm/internal/browserinfra/vault" "github.com/johnandersen777/socialweb-computer-kcp/wasm/internal/browserinfra/vaulthost" "github.com/johnandersen777/socialweb-computer-kcp/wasm/internal/browsersandbox" ) type Config struct { Rest *rest.Config Bus *bus.Bus StateDir string ProviderWorkspace string Origin string IdentityMode v1alpha1.MarketIdentityMode Handle string DID string PDSURL string OAuthClientID string OAuthRedirectURI string // CommsMode is the MarketRequester's spec.comms.mode: Firehose (records // only, no ingress; the default) or Both (the ingress XRPC endpoints too). CommsMode v1alpha1.MarketCommsMode // ForceApply re-applies every provider manifest set even when the snapshot // already holds ready bindings. Dev loops leave it off; schema changes and // first boots turn it on. ForceApply bool // Restore is the socialweb vault snapshot a previous worker persisted. Restore json.RawMessage // OnSnapshot receives a fresh vault snapshot after every write or delete. // It is never called when Restore is set but cannot be read. OnSnapshot func([]byte) // OnRestoreError receives why a non-empty Restore could not be read. OnRestoreError func(error) // IngressProxyHost is the xrpc-subscription relay ingress-mode Exposures // register with (e.g. xrpc.fedproxy.com). Empty serves them on Origin. IngressProxyHost string // OnIngress receives the ingress Exposures the page must subscribe for. OnIngress func([]exposure.Ingress) Log *slog.Logger } type Orchestrator struct { Manager *ctlruntime.Manager Composer *compose.Composer Vault *vaulthost.Controller Exposure *exposure.Controller Identity *marketidentity.Controller cancel context.CancelFunc done chan struct{} } func Start(parent context.Context, cfg Config) (*Orchestrator, error) { if cfg.Rest == nil { return nil, errors.New("socialweb: a kcp rest config is required") } if cfg.Bus == nil { return nil, errors.New("socialweb: a bus is required") } if cfg.StateDir == "" { cfg.StateDir = "/kcp/socialweb" } if cfg.ProviderWorkspace == "" { cfg.ProviderWorkspace = "root:socialweb-provider" } if cfg.Origin == "" { cfg.Origin = "http://127.0.0.1:8090" } if cfg.IdentityMode == "" { cfg.IdentityMode = v1alpha1.MarketIdentityModeOAuthBrowser } log := cfg.Log if log == nil { log = slog.Default() } if err := os.MkdirAll(cfg.StateDir, 0o700); err != nil { return nil, err } // The loopback user is privileged and bypasses workspace CreatedBy // admission. Use a non-privileged cluster-admin identity so workspaces get // spec.createdBy and APIBindings can initialize. impersonated := rest.CopyConfig(cfg.Rest) impersonated.Impersonate = rest.ImpersonationConfig{ UserName: "system:kcp:admin", Groups: []string{"system:kcp:admin"}, } cfg.Rest = impersonated if !cfg.ForceApply && providerReady(parent, cfg.Rest) { fmt.Fprintln(os.Stderr, "socialweb: provider manifests already applied; skipping") } else { fmt.Fprintln(os.Stderr, "socialweb: applying provider manifests") applyDone := make(chan error, 1) go func() { applyDone <- applyProviderSets(parent, cfg, log) }() select { case err := <-applyDone: if err != nil { fmt.Fprintln(os.Stderr, "socialweb: provider manifests:", err) return nil, err } case <-time.After(10 * time.Minute): buf := make([]byte, 1<<20) n := runtime.Stack(buf, true) fmt.Fprintln(os.Stderr, "socialweb: provider manifest apply stalled\n"+string(buf[:n])) return nil, errors.New("socialweb: provider manifest apply stalled") } fmt.Fprintln(os.Stderr, "socialweb: provider manifests applied") } composer, err := compose.New(compose.Config{ Rest: cfg.Rest, Workspace: "root", Bus: cfg.Bus, Log: log, }) if err != nil { return nil, fmt.Errorf("socialweb: composer: %w", err) } mgr, err := ctlruntime.NewManager(ctlruntime.Config{ Rest: cfg.Rest, ExportWorkspace: cfg.ProviderWorkspace, AddToScheme: []func(*kruntime.Scheme) error{ v1alpha1.AddToScheme, exposurev1alpha1.AddToScheme, eventsv1alpha1.AddToScheme, policyv1alpha1.AddToScheme, pdsv1alpha1.AddToScheme, }, Workspaces: func(ws string) bool { return ws == "root" || strings.HasPrefix(ws, "root:") }, Log: log, }) if err != nil { return nil, fmt.Errorf("socialweb: manager: %w", err) } scope := func(ws string) bool { return ws == "root" || strings.HasPrefix(ws, "root:") } vaultCtl, err := vaulthost.Register(mgr, cfg.Bus, vaulthost.Options{Scope: scope}) if err != nil { return nil, err } // Restore before any controller reads the vault: MarketIdentity restores // its OAuth session from it on the first reconcile. // A saved snapshot that cannot be read fails closed: the vault starts // empty and is never persisted, so nothing is saved over the stored one. snapshot, persist, err := planRestore(cfg.Restore) if err != nil { log.Error("saved socialweb state could not be read; not saving over it", "err", err.Error()) fmt.Fprintln(os.Stderr, "socialweb: saved state could not be read; not saving over it:", err) if cfg.OnRestoreError != nil { cfg.OnRestoreError(err) } } else if snapshot != nil { vaultCtl.Restore(snapshot.Vault) fmt.Fprintln(os.Stderr, "socialweb: restored browser vault") } if cfg.OnSnapshot != nil && persist { vaultCtl.Provider().SetOnChange(func() { raw, err := json.Marshal(Snapshot{Version: SnapshotVersion, Vault: vaultCtl.Snapshot()}) if err != nil { log.Warn("encoding the socialweb snapshot", "err", err.Error()) return } cfg.OnSnapshot(raw) }) } exposureCtl, err := exposure.Register(mgr, exposure.Options{ Origin: cfg.Origin, Handlers: cfg.Bus, Scope: scope, Log: log, IngressProxyHost: cfg.IngressProxyHost, Keys: vaultCtl.Provider().Store, OnIngress: cfg.OnIngress, }) if err != nil { return nil, err } // socialweb's own event stream controllers: relay, Jetstream and bidder // PDS streams dial the browser WebSocket (wsconn's js build) and deliver // into the market handler, so a Firehose requester reads bids and guest // events without any ingress. ctx, cancel := context.WithCancel(parent) started := false defer func() { if !started { cancel() } }() watchChildren, watchRepos, watchValues := eventsctl.RecordWatch(workspaceClients(cfg.Rest)) runEvents, err := eventsctl.Register(ctx, mgr, eventsctl.Options{ Bus: cfg.Bus, Dynamic: kube.NewFactory(cfg.Rest), Resolver: &pdsclient.Resolver{Handlers: cfg.Bus}, PLCURL: "https://plc.directory", HTTPClient: http.DefaultClient, StateDir: cfg.StateDir, RecordWatchChildren: watchChildren, RecordWatchRepos: watchRepos, RecordWatchValues: watchValues, Scope: scope, Log: log, }) if err != nil { return nil, err } callbacks := oauthcallback.NewRegistry() identityCtl, err := marketidentity.Register(mgr, marketidentity.Options{ Scope: scope, Vault: func(ws string) atproto.SecretStore { return bvault.NewSecrets(vaultCtl.Provider().Store(ws)) }, Bus: cfg.Bus, Log: log, HTTPClient: http.DefaultClient, OAuth: atproto.OAuthOptions{ ClientID: cfg.OAuthClientID, RedirectURI: cfg.OAuthRedirectURI, Scopes: oauth.RequesterOAuthScope, // Pending PKCE states live in the vault so a reload during the // OAuth dance still completes the callback. StateRef: &commonv1.SecretRef{Mount: "requester", Path: "oauth/requester"}, Directory: BrowserDirectory(), HTTPClient: http.DefaultClient, }, }) if err != nil { return nil, err } callbacks.Register("requester", func(ctx context.Context, w http.ResponseWriter, r *http.Request) error { return identityCtl.CompleteBrowserAuthorization(ctx, "root", "requester", r) }) // The repo controllers provide repo.write and the record readers the // requester signs its market records with (AtprotoRepo bound to the // account's RemoteRepo). The browser has no local PDS and reads secrets // from its own vault. if err := repos.Register(mgr, repos.Options{ Bus: cfg.Bus, Log: log, Dynamic: func(workspace string) (*kube.Client, error) { return kube.ForWorkspace(cfg.Rest, workspace) }, Network: reporemote.NewNetwork(reporemote.NetworkConfig{Directory: BrowserDirectory(), HTTP: http.DefaultClient}), Pds: func(string, string) (*atpds.Server, bool) { return nil, false }, Secrets: func(ctx context.Context, workspace string, ref commonv1.SecretRef) (map[string]any, error) { return bvault.NewSecrets(vaultCtl.Provider().Store(workspace)).Get(ctx, ref) }, Scope: scope, }); err != nil { return nil, fmt.Errorf("socialweb: repo controllers: %w", err) } if _, err := componentlink.Register(mgr, componentlink.Options{ Policy: contractexposure.NewLinkPolicy(cfg.Bus), Registry: component.NewRegistry(), Providers: cfg.Bus, Scope: scope, Log: log, }); err != nil { return nil, err } // Policy runs in the page's fenced sandbox (plan 0037): the PolicyEngine's // spec.sandbox.provider browser opens browsersandbox, whose probe sets the // Fenced and Ready conditions. if err := policy.Register(mgr, policy.Options{ Bus: cfg.Bus, OpenSandbox: browsersandbox.Open, Log: log, }); err != nil { return nil, fmt.Errorf("socialweb: policy controllers: %w", err) } if _, err := marketintents.Register(mgr, marketintents.Options{ Bus: cfg.Bus, Scope: scope, Log: log, Clients: workspaceClients(cfg.Rest), }); err != nil { return nil, err } // The requester resolves its market.contractEvents handle (WebRTC // signaling, guest events) from this controller and exits without it. if _, err := contractevent.Register(mgr, contractevent.Options{ Bus: cfg.Bus, Scope: scope, Log: log, Clients: workspaceClients(cfg.Rest), }); err != nil { return nil, err } // One EphemeralInfra per page session owns that session's ComputeContracts. if _, err := infra.Register(mgr, infra.Options{ Bus: cfg.Bus, Scope: scope, Log: log, Dynamic: func(workspace string) (*kube.Client, error) { return kube.ForWorkspace(cfg.Rest, workspace) }, }); err != nil { return nil, err } if err := marketrequester.Register(mgr, composer); err != nil { return nil, err } if _, err := RegisterRequesterRunner(mgr, RequesterRunnerOptions{ Bus: cfg.Bus, OAuthCallbacks: callbacks, StateDir: cfg.StateDir, Scope: scope, Log: log, }); err != nil { return nil, err } started = true done := make(chan struct{}) go func() { defer close(done) if err := mgr.Start(ctx); err != nil && ctx.Err() == nil { log.Error("socialweb manager exited", "err", err) } }() go func() { if err := runEvents(ctx); err != nil && ctx.Err() == nil { log.Error("socialweb event streams exited", "err", err) } }() orc := &Orchestrator{ Manager: mgr, Composer: composer, Vault: vaultCtl, Exposure: exposureCtl, Identity: identityCtl, cancel: cancel, done: done, } fmt.Fprintln(os.Stderr, "socialweb: manager started; creating MarketRequester") if err := orc.createRoot(ctx, cfg); err != nil { _ = orc.Close() return nil, err } return orc, nil } // providerReady reports whether a restored snapshot already holds the provider // exports and root bindings every set installs. That turns a restart with // persisted state into a ~40s boot instead of a ~2.5 minute apply pass. func providerReady(ctx context.Context, restCfg *rest.Config) bool { kc, err := kube.ForWorkspace(restCfg, "root") if err != nil { return false } items, err := kube.List[map[string]any](ctx, kc.Resource(schema.GroupVersionResource{ Group: "apis.kcp.io", Version: "v1alpha2", Resource: "apibindings", }), metav1.ListOptions{}) if err != nil { return false } ready := map[string]bool{} for _, item := range items { meta, _ := item["metadata"].(map[string]any) name, _ := meta["name"].(string) if name != "" { ready[name] = bindingReady(item) } } for _, name := range []string{ "applications", "computevms", "events", "exposures", "hostprocesses", "pds", "policy", } { if !ready[name] { return false } } return true } func bindingReady(item map[string]any) bool { status, _ := item["status"].(map[string]any) conditions, _ := status["conditions"].([]any) for _, raw := range conditions { condition, _ := raw.(map[string]any) if condition["type"] == "Ready" && condition["status"] == "True" { return true } } return false } func applyProviderSets(ctx context.Context, cfg Config, log *slog.Logger) error { applier, err := kcpapply.New(cfg.Rest, kcpapply.Options{ Base: "root", FieldManager: "socialweb-wasm", WaitTimeout: 3 * time.Minute, Log: log, }) if err != nil { return fmt.Errorf("socialweb: manifest applier: %w", err) } ix, err := manifests.Load() if err != nil { return err } applySet := func(name string, base context.Context) error { set, ok := ix.Set(name) if !ok { return fmt.Errorf("socialweb: deploy/sets.yaml names no %s set", name) } fmt.Fprintf(os.Stderr, "socialweb: applying set %s\n", name) values, err := manifests.DecodeValues(set.Values, nil) if err != nil { return fmt.Errorf("socialweb: decoding values for %s: %w", name, err) } applyCtx, cancel := context.WithTimeout(base, 4*time.Minute) defer cancel() start := time.Now() rep, err := applier.Apply(applyCtx, set, values) if err != nil { return fmt.Errorf("socialweb: applying the %s set: %w", name, err) } fmt.Fprintf(os.Stderr, "socialweb: set %s applied in %s\n", name, time.Since(start).Round(time.Millisecond)) for _, phase := range rep.Phases { for _, wait := range phase.Waits { fmt.Fprintf(os.Stderr, "socialweb: wait set=%s phase=%s kind=%s name=%s ws=%s met=%t seconds=%.2f\n", phase.Set, phase.Phase, wait.Kind, wait.Name, wait.Workspace, wait.Met, wait.Seconds) } } return nil } // Serial on purpose: the apiserver and all apply clients share the single // Go/wasm thread, so concurrent SSA overloads discovery and watch delivery. for _, name := range []string{"provider", "hostprocesses", "applications", "bidder"} { if err := applySet(name, ctx); err != nil { return err } } return nil } func workspaceClients(base *rest.Config) func(string) (ctrlclient.Client, error) { scheme := kruntime.NewScheme() _ = v1alpha1.AddToScheme(scheme) _ = exposurev1alpha1.AddToScheme(scheme) _ = eventsv1alpha1.AddToScheme(scheme) _ = policyv1alpha1.AddToScheme(scheme) _ = pdsv1alpha1.AddToScheme(scheme) return func(ws string) (ctrlclient.Client, error) { cfg := rest.CopyConfig(base) cfg.Host = strings.TrimSuffix(cfg.Host, "/") + "/clusters/" + ws return ctrlclient.New(cfg, ctrlclient.Options{Scheme: scheme}) } } func (o *Orchestrator) createRoot(ctx context.Context, cfg Config) error { kc, err := kube.ForWorkspace(cfg.Rest, "root") if err != nil { return err } obj := &v1alpha1.MarketRequester{ Spec: v1alpha1.MarketRequesterSpec{ Profile: marketrequester.RequesterProfileDefault, Identity: v1alpha1.ApplicationIdentity{ Mode: cfg.IdentityMode, Handle: cfg.Handle, // The page origin is the OAuth client: a static client metadata // file and callback page. Never the ingress Exposure, which the // tab cannot serve while the user is on the PDS. OAuthClientID: cfg.OAuthClientID, OAuthRedirectURI: cfg.OAuthRedirectURI, Did: cfg.DID, PdsURL: cfg.PDSURL, Association: v1alpha1.AssociationSkip, }, Network: &v1alpha1.MarketNetworkValue{PLCDirectoryURL: "https://plc.directory"}, // Firehose (default): records only; the requester writes to its PDS // and reads bids and events from the streams, with no inbound XRPC // and no ingress Exposure. Both (the page's Ingress mode) adds the // relay-hosted XRPC endpoints. Guest secrets fall back to a record // sealed to the guest's key either way. Comms: &v1alpha1.MarketComms{Mode: commsMode(cfg.CommsMode)}, RequesterIngress: &v1alpha1.IngressConfig{ Provider: v1alpha1.IngressProviderWebRTC, WebRTC: &v1alpha1.IngressWebRTCConfig{}, }, SecretsFallback: v1alpha1.SecretsFallbackSealed, Components: browserPolicySandbox(), }, } obj.APIVersion = v1alpha1.GroupVersion.String() obj.Kind = "MarketRequester" obj.Name = "app" // Never overwrite an existing app: the UI edits spec.identity at runtime // and the worker config may be empty on a reload with restored state. // Only the comms mode, secrets fallback and policy sandbox, which the // page owns, are brought up to date (restored state from before Firehose). if cur, err := kube.Get[v1alpha1.MarketRequester](ctx, kc.Resource(v1alpha1.MarketRequesterGVR), "app"); err == nil { if cur.Spec.Comms != nil && cur.Spec.Comms.Mode == obj.Spec.Comms.Mode && cur.Spec.RequesterIngress != nil && cur.Spec.RequesterIngress.ProviderName() == v1alpha1.IngressProviderWebRTC && cur.Spec.SecretsFallback == obj.Spec.SecretsFallback && hasBrowserPolicySandbox(cur.Spec.Components) { return nil } patch, err := json.Marshal(map[string]any{"spec": map[string]any{"comms": obj.Spec.Comms, "requesterIngress": obj.Spec.RequesterIngress, "secretsFallback": obj.Spec.SecretsFallback, "components": obj.Spec.Components}}) if err != nil { return err } _, err = kube.Patch[v1alpha1.MarketRequester](ctx, kc.Resource(v1alpha1.MarketRequesterGVR), "app", types.MergePatchType, patch, metav1.PatchOptions{FieldManager: "socialweb-wasm"}) return err } _, err = kube.Apply[*v1alpha1.MarketRequester](ctx, kc.Resource(v1alpha1.MarketRequesterGVR), obj, kube.ApplyOptions{FieldManager: "socialweb-wasm", Force: true}) return err } // policyEngineKey names the PolicyEngine the requester app composes. var policyEngineKey = compose.ComponentKey(compose.ComponentKindPolicyEngine, compose.PolicyEngineComponent) // browserPolicySandbox makes the composed PolicyEngine run policy in the // page's fenced browser sandbox (plan 0037), as createRoot sets the webrtc // ingress provider: the page cannot run deno. func browserPolicySandbox() v1alpha1.ComponentOverrides { return v1alpha1.ComponentOverrides{policyEngineKey: {Settings: &v1alpha1.ComponentSettings{ PolicyEngine: &v1alpha1.PolicyEngineSettings{ Sandbox: &v1alpha1.PolicyEngineSandboxSettings{Provider: policyv1alpha1.PolicySandboxProviderBrowser}, }, }}} } func hasBrowserPolicySandbox(components v1alpha1.ComponentOverrides) bool { settings := components[policyEngineKey].Settings return settings != nil && settings.PolicyEngine != nil && settings.PolicyEngine.Sandbox != nil && settings.PolicyEngine.Sandbox.Provider == policyv1alpha1.PolicySandboxProviderBrowser } func (o *Orchestrator) Close() error { if o == nil { return nil } if o.cancel != nil { o.cancel() } select { case <-o.done: case <-time.After(15 * time.Second): } return nil } // CompleteRequesterOAuth finishes the browser authorization before the // Requester component exists. The app waits for its identity to sign in, so the // callback cannot live only on the component's Exposure. func (o *Orchestrator) CompleteRequesterOAuth(ctx context.Context, r *http.Request) error { if o == nil || o.Identity == nil { return errors.New("socialweb: the identity controller is not running") } return o.Identity.CompleteBrowserAuthorization(ctx, "root", "requester", r) } // Lookup routes a page-origin request to the registered Exposure handler. func (o *Orchestrator) Lookup(host, path string) (http.Handler, bool) { if o == nil || o.Exposure == nil { return nil, false } return o.Exposure.Lookup(host, path) } // commsMode is the requester comms mode the page asked for, Firehose unless // it named Both or XRPC. func commsMode(m v1alpha1.MarketCommsMode) v1alpha1.MarketCommsMode { switch m { case v1alpha1.MarketCommsModeBoth, v1alpha1.MarketCommsModeXRPC: return m } return v1alpha1.MarketCommsModeFirehose }