Implement Knowledge City platform, organizer and DAV integration
Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming. Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
This commit is contained in:
1 parent
288e6e94ac
commit
c1175d389f
699 files changed
+110197
-4117
No files matched your search
@@ -0,0 +1,515 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type gatewaySession struct {
|
||||
VimSettings json.RawMessage
|
||||
Token string
|
||||
PluginID string
|
||||
Filename string
|
||||
ContentType string
|
||||
ExpiresAtMS int64
|
||||
Workspace string
|
||||
Files map[string]gatewayFile
|
||||
LastError string
|
||||
}
|
||||
|
||||
type gatewayFile struct {
|
||||
SemanticPath string
|
||||
RevisionUUID string
|
||||
LastContent string
|
||||
}
|
||||
|
||||
type gateway struct {
|
||||
origin string
|
||||
client *http.Client
|
||||
mu sync.Mutex
|
||||
sessions map[string]gatewaySession
|
||||
codeProxy *httputil.ReverseProxy
|
||||
excalidrawProxy *httputil.ReverseProxy
|
||||
}
|
||||
|
||||
func main() {
|
||||
origin := strings.TrimRight(os.Getenv("PAGESWIKI_ORIGIN"), "/")
|
||||
if origin == "" {
|
||||
origin = "http://app:3000"
|
||||
}
|
||||
codeOrigin := os.Getenv("CODE_SERVER_ORIGIN")
|
||||
if codeOrigin == "" {
|
||||
codeOrigin = "http://code-server:8080"
|
||||
}
|
||||
codeURL, err := url.Parse(codeOrigin)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
proxy := httputil.NewSingleHostReverseProxy(codeURL)
|
||||
originalDirector := proxy.Director
|
||||
proxy.Director = func(req *http.Request) {
|
||||
req.URL.Path = strings.TrimPrefix(req.URL.Path, "/code")
|
||||
if req.URL.Path == "" {
|
||||
req.URL.Path = "/"
|
||||
}
|
||||
originalDirector(req)
|
||||
}
|
||||
excalidrawOrigin := os.Getenv("EXCALIDRAW_VIEWER_ORIGIN")
|
||||
if excalidrawOrigin == "" {
|
||||
excalidrawOrigin = "http://excalidraw-viewer:8080"
|
||||
}
|
||||
excalidrawURL, err := url.Parse(excalidrawOrigin)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
excalidrawProxy := httputil.NewSingleHostReverseProxy(excalidrawURL)
|
||||
excalidrawDirector := excalidrawProxy.Director
|
||||
excalidrawProxy.Director = func(req *http.Request) {
|
||||
req.URL.Path = strings.TrimPrefix(req.URL.Path, "/excalidraw")
|
||||
if req.URL.Path == "" {
|
||||
req.URL.Path = "/"
|
||||
}
|
||||
excalidrawDirector(req)
|
||||
}
|
||||
g := &gateway{origin: origin, client: &http.Client{Timeout: 30 * time.Second}, sessions: map[string]gatewaySession{}, codeProxy: proxy, excalidrawProxy: excalidrawProxy}
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/health", func(w http.ResponseWriter, _ *http.Request) { w.Write([]byte("ok")) })
|
||||
mux.HandleFunc("/open", g.open)
|
||||
mux.HandleFunc("/code/open", g.open)
|
||||
mux.HandleFunc("/vim/open", g.open)
|
||||
mux.HandleFunc("/edit", g.edit)
|
||||
mux.HandleFunc("/save", g.save)
|
||||
mux.HandleFunc("/code/", g.code)
|
||||
mux.HandleFunc("/vim/api/session", g.vimSession)
|
||||
mux.HandleFunc("/vim/api/save", g.vimSave)
|
||||
vimFiles := http.FileServer(http.Dir("/vimwasm"))
|
||||
vimHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/vim/" {
|
||||
data, err := os.ReadFile("/vimwasm/index.html")
|
||||
if err != nil {
|
||||
http.Error(w, "vim.wasm frontend is unavailable", http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
_, _ = w.Write(data)
|
||||
return
|
||||
}
|
||||
http.StripPrefix("/vim/", vimFiles).ServeHTTP(w, r)
|
||||
})
|
||||
mux.Handle("/vim/", isolationHeaders(vimHandler))
|
||||
mux.Handle("/excalidraw/", excalidrawHeaders(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
g.excalidrawProxy.ServeHTTP(w, r)
|
||||
})))
|
||||
log.Printf("adapter gateway origin=%s listen=:3000", origin)
|
||||
log.Fatal(http.ListenAndServe(":3000", mux))
|
||||
}
|
||||
|
||||
func excalidrawHeaders(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Security-Policy", "default-src 'self'; script-src 'self' 'unsafe-eval' 'wasm-unsafe-eval'; style-src 'self' 'unsafe-inline'; img-src 'self' data: blob:; font-src 'self' data:; worker-src 'self' blob:; connect-src 'self'; frame-ancestors 'self'; object-src 'none'; base-uri 'none'")
|
||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||
w.Header().Set("Referrer-Policy", "no-referrer")
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
func isolationHeaders(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Cross-Origin-Opener-Policy", "same-origin")
|
||||
w.Header().Set("Cross-Origin-Embedder-Policy", "require-corp")
|
||||
w.Header().Set("Cross-Origin-Resource-Policy", "same-origin")
|
||||
w.Header().Set("Origin-Agent-Cluster", "?1")
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
func randomID() string {
|
||||
b := make([]byte, 24)
|
||||
_, _ = rand.Read(b)
|
||||
return hex.EncodeToString(b)
|
||||
}
|
||||
|
||||
func ensureCoderDirectory(root, directory string) error {
|
||||
relative, err := filepath.Rel(root, directory)
|
||||
if err != nil || relative == ".." || strings.HasPrefix(relative, ".."+string(filepath.Separator)) {
|
||||
return fmt.Errorf("workspace directory escapes root")
|
||||
}
|
||||
current := root
|
||||
for _, component := range strings.Split(relative, string(filepath.Separator)) {
|
||||
if component == "" || component == "." {
|
||||
continue
|
||||
}
|
||||
current = filepath.Join(current, component)
|
||||
if err := os.Mkdir(current, 0700); err != nil && !os.IsExist(err) {
|
||||
return err
|
||||
}
|
||||
if err := os.Chown(current, 1000, 1000); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *gateway) request(method, path, bearer string, input any, output any) error {
|
||||
var body io.Reader
|
||||
if input != nil {
|
||||
data, _ := json.Marshal(input)
|
||||
body = bytes.NewReader(data)
|
||||
}
|
||||
req, err := http.NewRequest(method, g.origin+path, body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
if bearer != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+bearer)
|
||||
}
|
||||
res, err := g.client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
if res.StatusCode < 200 || res.StatusCode >= 300 {
|
||||
message, _ := io.ReadAll(io.LimitReader(res.Body, 4096))
|
||||
return fmt.Errorf("Pages Wiki HTTP %d: %s", res.StatusCode, strings.TrimSpace(string(message)))
|
||||
}
|
||||
return json.NewDecoder(res.Body).Decode(output)
|
||||
}
|
||||
|
||||
func (g *gateway) open(w http.ResponseWriter, r *http.Request) {
|
||||
publicPrefix := strings.TrimRight(strings.TrimSpace(r.Header.Get("X-Forwarded-Prefix")), "/")
|
||||
ticket := strings.TrimSpace(r.URL.Query().Get("ticket"))
|
||||
if ticket == "" {
|
||||
http.Error(w, "ticket is required", 400)
|
||||
return
|
||||
}
|
||||
var exchanged struct {
|
||||
SessionToken string `json:"session_token"`
|
||||
PluginID string `json:"plugin_id"`
|
||||
Filename string `json:"filename"`
|
||||
ContentType string `json:"content_type"`
|
||||
ExpiresAtMS int64 `json:"expires_at_ms"`
|
||||
}
|
||||
if err := g.request("POST", "/_/api/v1/adapter/session/exchange", "",
|
||||
map[string]string{"ticket": ticket}, &exchanged); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadGateway)
|
||||
return
|
||||
}
|
||||
var workspaceData struct {
|
||||
VimSettings json.RawMessage `json:"vim_settings"`
|
||||
Files []struct {
|
||||
Path string `json:"path"`
|
||||
SemanticPath string `json:"semantic_path"`
|
||||
RevisionUUID string `json:"revision_uuid"`
|
||||
ContentBase64 string `json:"content_base64"`
|
||||
} `json:"files"`
|
||||
}
|
||||
if err := g.request("GET", "/_/api/v1/adapter/session/workspace", exchanged.SessionToken, nil, &workspaceData); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadGateway)
|
||||
return
|
||||
}
|
||||
id := randomID()
|
||||
workspace := filepath.Join("/workspaces", id)
|
||||
if err := os.MkdirAll(workspace, 0700); err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
_ = os.Chown(workspace, 1000, 1000)
|
||||
files := make(map[string]gatewayFile, len(workspaceData.Files))
|
||||
filename := "document.txt"
|
||||
for index, source := range workspaceData.Files {
|
||||
clean := filepath.Clean(filepath.FromSlash(source.Path))
|
||||
if clean == "." || filepath.IsAbs(clean) || strings.HasPrefix(clean, ".."+string(filepath.Separator)) {
|
||||
continue
|
||||
}
|
||||
data, decodeErr := base64.StdEncoding.DecodeString(source.ContentBase64)
|
||||
if decodeErr != nil {
|
||||
http.Error(w, "invalid workspace content", http.StatusBadGateway)
|
||||
return
|
||||
}
|
||||
target := filepath.Join(workspace, clean)
|
||||
if _, duplicate := files[clean]; duplicate {
|
||||
http.Error(w, "workspace contains duplicate filenames", http.StatusConflict)
|
||||
return
|
||||
}
|
||||
if err := ensureCoderDirectory(workspace, filepath.Dir(target)); err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
if err := os.WriteFile(target, data, 0600); err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
_ = os.Chown(target, 1000, 1000)
|
||||
files[clean] = gatewayFile{SemanticPath: source.SemanticPath,
|
||||
RevisionUUID: source.RevisionUUID, LastContent: source.ContentBase64}
|
||||
if index == 0 {
|
||||
filename = clean
|
||||
}
|
||||
}
|
||||
if len(files) == 0 {
|
||||
http.Error(w, "workspace contains no editable files", http.StatusBadGateway)
|
||||
return
|
||||
}
|
||||
g.mu.Lock()
|
||||
g.sessions[id] = gatewaySession{Token: exchanged.SessionToken, PluginID: exchanged.PluginID, Filename: filename,
|
||||
ContentType: exchanged.ContentType, ExpiresAtMS: exchanged.ExpiresAtMS,
|
||||
Workspace: workspace, Files: files, VimSettings: workspaceData.VimSettings}
|
||||
g.mu.Unlock()
|
||||
cookiePath := "/"
|
||||
if publicPrefix != "" {
|
||||
cookiePath = publicPrefix + "/"
|
||||
}
|
||||
http.SetCookie(w, &http.Cookie{Name: "pageswiki_adapter", Value: id, Path: cookiePath, HttpOnly: true, SameSite: http.SameSiteLaxMode, MaxAge: 8 * 3600})
|
||||
if exchanged.PluginID == "vim.wasm" {
|
||||
go g.expire(id)
|
||||
http.Redirect(w, r, publicPrefix+"/vim/", http.StatusFound)
|
||||
return
|
||||
}
|
||||
go g.watch(id)
|
||||
http.Redirect(w, r, publicPrefix+"/code/?folder="+url.QueryEscape(workspace), http.StatusFound)
|
||||
}
|
||||
|
||||
func (g *gateway) session(r *http.Request) (gatewaySession, bool) {
|
||||
cookie, err := r.Cookie("pageswiki_adapter")
|
||||
if err != nil {
|
||||
return gatewaySession{}, false
|
||||
}
|
||||
g.mu.Lock()
|
||||
defer g.mu.Unlock()
|
||||
s, ok := g.sessions[cookie.Value]
|
||||
if !ok || s.ExpiresAtMS < time.Now().UnixMilli() {
|
||||
delete(g.sessions, cookie.Value)
|
||||
return gatewaySession{}, false
|
||||
}
|
||||
return s, true
|
||||
}
|
||||
|
||||
var editorTemplate = template.Must(template.New("editor").Parse(`<!doctype html>
|
||||
<html><head><meta charset="utf-8"><title>{{.Filename}}</title>
|
||||
<style>html,body,form{height:100%;margin:0}form{display:grid;grid-template-rows:auto 1fr}.bar{padding:.5rem;background:#20242b;color:#eee}textarea{resize:none;border:0;padding:1rem;font:14px/1.5 monospace;background:#11151a;color:#e8e8e8;outline:0}</style></head>
|
||||
<body><form method="post" action="/save"><div class="bar"><strong>{{.Filename}}</strong> <button type="submit">Save as new Revision</button> {{.Status}}</div><textarea name="content" spellcheck="false">{{.Content}}</textarea></form></body></html>`))
|
||||
|
||||
func (g *gateway) edit(w http.ResponseWriter, r *http.Request) {
|
||||
s, ok := g.session(r)
|
||||
if !ok {
|
||||
http.Error(w, "session expired", 401)
|
||||
return
|
||||
}
|
||||
var content struct {
|
||||
ContentBase64 string `json:"content_base64"`
|
||||
}
|
||||
if err := g.request("GET", "/_/api/v1/adapter/session/content", s.Token, nil, &content); err != nil {
|
||||
http.Error(w, err.Error(), 502)
|
||||
return
|
||||
}
|
||||
data, err := base64.StdEncoding.DecodeString(content.ContentBase64)
|
||||
if err != nil {
|
||||
http.Error(w, "invalid content", 502)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
_ = editorTemplate.Execute(w, map[string]any{"Filename": s.Filename, "Content": string(data), "Status": r.URL.Query().Get("status")})
|
||||
}
|
||||
|
||||
func (g *gateway) save(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "method not allowed", 405)
|
||||
return
|
||||
}
|
||||
s, ok := g.session(r)
|
||||
if !ok {
|
||||
http.Error(w, "session expired", 401)
|
||||
return
|
||||
}
|
||||
r.Body = http.MaxBytesReader(w, r.Body, 65<<20)
|
||||
if err := r.ParseForm(); err != nil {
|
||||
http.Error(w, err.Error(), 400)
|
||||
return
|
||||
}
|
||||
var output map[string]any
|
||||
err := g.request("PUT", "/_/api/v1/adapter/session/content", s.Token, map[string]string{
|
||||
"content_base64": base64.StdEncoding.EncodeToString([]byte(r.FormValue("content"))),
|
||||
"message": "hosted editor: " + s.Filename,
|
||||
}, &output)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusConflict)
|
||||
return
|
||||
}
|
||||
http.Redirect(w, r, "/edit?status=saved", http.StatusFound)
|
||||
}
|
||||
|
||||
func (g *gateway) code(w http.ResponseWriter, r *http.Request) {
|
||||
if _, ok := g.session(r); !ok {
|
||||
http.Error(w, "session expired", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
g.codeProxy.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
func (g *gateway) vimSession(w http.ResponseWriter, r *http.Request) {
|
||||
session, ok := g.session(r)
|
||||
if !ok || session.PluginID != "vim.wasm" {
|
||||
http.Error(w, "session expired", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
type vimFile struct {
|
||||
Path string `json:"path"`
|
||||
Content string `json:"content"`
|
||||
}
|
||||
files := make([]vimFile, 0, len(session.Files))
|
||||
for relative, tracked := range session.Files {
|
||||
data, err := base64.StdEncoding.DecodeString(tracked.LastContent)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
files = append(files, vimFile{Path: filepath.ToSlash(relative), Content: string(data)})
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"initial": filepath.ToSlash(session.Filename), "files": files, "vim_settings": session.VimSettings})
|
||||
}
|
||||
|
||||
func (g *gateway) vimSave(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPut {
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
session, ok := g.session(r)
|
||||
if !ok || session.PluginID != "vim.wasm" {
|
||||
http.Error(w, "session expired", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
var input struct {
|
||||
Path string `json:"path"`
|
||||
ContentBase64 string `json:"content_base64"`
|
||||
}
|
||||
r.Body = http.MaxBytesReader(w, r.Body, 65<<20)
|
||||
if err := json.NewDecoder(r.Body).Decode(&input); err != nil {
|
||||
http.Error(w, "invalid JSON", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
clean := filepath.Clean(filepath.FromSlash(strings.TrimPrefix(input.Path, "/work/")))
|
||||
tracked, exists := session.Files[clean]
|
||||
if !exists {
|
||||
http.Error(w, "file is outside the adapter workspace", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
var output struct {
|
||||
RevisionUUID string `json:"revision_uuid"`
|
||||
}
|
||||
err := g.request("PUT", "/_/api/v1/adapter/session/content", session.Token,
|
||||
map[string]string{"content_base64": input.ContentBase64, "path": tracked.SemanticPath,
|
||||
"revision_uuid": tracked.RevisionUUID, "message": "vim.wasm write: " + clean}, &output)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusConflict)
|
||||
return
|
||||
}
|
||||
cookie, _ := r.Cookie("pageswiki_adapter")
|
||||
g.mu.Lock()
|
||||
current, exists := g.sessions[cookie.Value]
|
||||
if exists {
|
||||
updated := current.Files[clean]
|
||||
updated.LastContent = input.ContentBase64
|
||||
updated.RevisionUUID = output.RevisionUUID
|
||||
current.Files[clean] = updated
|
||||
g.sessions[cookie.Value] = current
|
||||
}
|
||||
g.mu.Unlock()
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"revision_uuid": output.RevisionUUID})
|
||||
}
|
||||
|
||||
func (g *gateway) expire(id string) {
|
||||
g.mu.Lock()
|
||||
session, ok := g.sessions[id]
|
||||
g.mu.Unlock()
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
delay := time.Until(time.UnixMilli(session.ExpiresAtMS))
|
||||
if delay > 0 {
|
||||
timer := time.NewTimer(delay)
|
||||
defer timer.Stop()
|
||||
<-timer.C
|
||||
}
|
||||
g.mu.Lock()
|
||||
session, ok = g.sessions[id]
|
||||
delete(g.sessions, id)
|
||||
g.mu.Unlock()
|
||||
if ok {
|
||||
if err := os.RemoveAll(session.Workspace); err != nil {
|
||||
log.Printf("remove expired workspace %s: %v", session.Workspace, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (g *gateway) watch(id string) {
|
||||
ticker := time.NewTicker(2 * time.Second)
|
||||
defer ticker.Stop()
|
||||
defer func() {
|
||||
g.mu.Lock()
|
||||
session, ok := g.sessions[id]
|
||||
delete(g.sessions, id)
|
||||
g.mu.Unlock()
|
||||
if ok {
|
||||
if err := os.RemoveAll(session.Workspace); err != nil {
|
||||
log.Printf("remove expired workspace %s: %v", session.Workspace, err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
for range ticker.C {
|
||||
g.mu.Lock()
|
||||
session, ok := g.sessions[id]
|
||||
g.mu.Unlock()
|
||||
if !ok || session.ExpiresAtMS < time.Now().UnixMilli() {
|
||||
return
|
||||
}
|
||||
for relative, tracked := range session.Files {
|
||||
data, err := os.ReadFile(filepath.Join(session.Workspace, relative))
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
encoded := base64.StdEncoding.EncodeToString(data)
|
||||
if encoded == tracked.LastContent {
|
||||
continue
|
||||
}
|
||||
var output struct {
|
||||
RevisionUUID string `json:"revision_uuid"`
|
||||
}
|
||||
err = g.request("PUT", "/_/api/v1/adapter/session/content", session.Token,
|
||||
map[string]string{"content_base64": encoded, "path": tracked.SemanticPath,
|
||||
"revision_uuid": tracked.RevisionUUID, "message": "code-server autosave: " + relative}, &output)
|
||||
g.mu.Lock()
|
||||
current, exists := g.sessions[id]
|
||||
if exists {
|
||||
currentFile := current.Files[relative]
|
||||
if err == nil {
|
||||
currentFile.LastContent = encoded
|
||||
currentFile.RevisionUUID = output.RevisionUUID
|
||||
current.Files[relative] = currentFile
|
||||
current.LastError = ""
|
||||
} else {
|
||||
current.LastError = err.Error()
|
||||
}
|
||||
g.sessions[id] = current
|
||||
}
|
||||
g.mu.Unlock()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,254 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/ed25519"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
type externalCampusEnrollment struct {
|
||||
FQDN string
|
||||
Token string
|
||||
HTTP bool
|
||||
NoDefaultCampus bool
|
||||
}
|
||||
|
||||
type campusEnrollmentSnapshot struct {
|
||||
Campus store.Campus
|
||||
Instance store.FederationInstance
|
||||
Domains []store.Domain
|
||||
Manifest store.SignedCampusManifest `json:"manifest"`
|
||||
}
|
||||
|
||||
func parseExternalCampusEnrollment(args []string) (*externalCampusEnrollment, error) {
|
||||
if len(args) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
noDefaultCampus := false
|
||||
filtered := make([]string, 0, len(args))
|
||||
for _, arg := range args {
|
||||
if arg == "--no-default-campus" {
|
||||
if noDefaultCampus {
|
||||
return nil, errors.New("--no-default-campus was specified twice")
|
||||
}
|
||||
noDefaultCampus = true
|
||||
continue
|
||||
}
|
||||
filtered = append(filtered, arg)
|
||||
}
|
||||
if len(filtered) != 3 ||
|
||||
(filtered[0] != "--add-external-campus" &&
|
||||
filtered[0] != "--add-external-campus-http") {
|
||||
return nil, errors.New(
|
||||
"usage: pageswiki [--no-default-campus] " +
|
||||
"[--add-external-campus FQDN TOKEN | " +
|
||||
"--add-external-campus-http FQDN TOKEN]",
|
||||
)
|
||||
}
|
||||
return &externalCampusEnrollment{
|
||||
FQDN: strings.TrimSpace(filtered[1]),
|
||||
Token: strings.TrimSpace(filtered[2]),
|
||||
HTTP: filtered[0] == "--add-external-campus-http",
|
||||
NoDefaultCampus: noDefaultCampus,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func enrollExternalCampus(
|
||||
dataDir string,
|
||||
st *store.Store,
|
||||
enrollment *externalCampusEnrollment,
|
||||
) error {
|
||||
if enrollment == nil {
|
||||
return nil
|
||||
}
|
||||
instanceFQDN := strings.TrimSpace(os.Getenv("PAGESWIKI_INSTANCE_FQDN"))
|
||||
if instanceFQDN == "" {
|
||||
return errors.New(
|
||||
"PAGESWIKI_INSTANCE_FQDN is required for external Campus enrollment",
|
||||
)
|
||||
}
|
||||
snapshot, scheme, err := fetchCampusSnapshot(
|
||||
dataDir,
|
||||
enrollment,
|
||||
instanceFQDN,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := applyEnrollmentSnapshot(st, snapshot, "", instanceFQDN, scheme); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := saveCampusEnrollment(
|
||||
dataDir,
|
||||
snapshot.Campus.UUID,
|
||||
enrollment,
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
if enrollment.NoDefaultCampus {
|
||||
_, err = st.RemoveUnusedBootstrapCampus()
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func applyEnrollmentSnapshot(st *store.Store, snapshot *campusEnrollmentSnapshot, expectedCampusUUID, instanceFQDN, scheme string) error {
|
||||
if snapshot.Manifest.SchemaVersion != 0 {
|
||||
return st.ApplySignedManagedCampusManifest(snapshot.Manifest, expectedCampusUUID, instanceFQDN, scheme)
|
||||
}
|
||||
// Transitional compatibility for pre-manifest controllers. New controllers
|
||||
// always emit a signed manifest; this branch can be removed after upgrades.
|
||||
return st.ApplyManagedCampusSnapshot(snapshot.Campus, snapshot.Domains, instanceFQDN, scheme)
|
||||
}
|
||||
|
||||
func fetchCampusSnapshot(
|
||||
dataDir string,
|
||||
enrollment *externalCampusEnrollment,
|
||||
instanceFQDN string,
|
||||
) (*campusEnrollmentSnapshot, string, error) {
|
||||
privateKey, fingerprint, err := ensureInstanceIdentity(dataDir)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
payload, err := json.Marshal(map[string]any{
|
||||
"manifest_version": 2,
|
||||
"capability_signature": base64.RawStdEncoding.EncodeToString(ed25519.Sign(privateKey, []byte(store.FederationCapabilityMessage(instanceFQDN, enrollment.Token)))),
|
||||
"fqdn": instanceFQDN,
|
||||
"fingerprint": fingerprint,
|
||||
"public_key": base64.RawStdEncoding.EncodeToString(
|
||||
privateKey.Public().(ed25519.PublicKey),
|
||||
),
|
||||
"signature": base64.RawStdEncoding.EncodeToString(
|
||||
ed25519.Sign(
|
||||
privateKey,
|
||||
[]byte(instanceFQDN+"\n"+enrollment.Token),
|
||||
),
|
||||
),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
scheme := "https"
|
||||
if enrollment.HTTP {
|
||||
scheme = "http"
|
||||
}
|
||||
endpoint := fmt.Sprintf(
|
||||
"%s://%s/_/api/v1/federation/enroll",
|
||||
scheme,
|
||||
enrollment.FQDN,
|
||||
)
|
||||
request, err := http.NewRequest(
|
||||
http.MethodPost,
|
||||
endpoint,
|
||||
bytes.NewReader(payload),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
request.Header.Set("Authorization", "Bearer "+enrollment.Token)
|
||||
client := &http.Client{Timeout: 20 * time.Second}
|
||||
response, err := client.Do(request)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
defer response.Body.Close()
|
||||
if response.StatusCode != http.StatusOK {
|
||||
message, _ := io.ReadAll(io.LimitReader(response.Body, 4<<10))
|
||||
detail := strings.TrimSpace(string(message))
|
||||
if detail != "" {
|
||||
return nil, "", fmt.Errorf(
|
||||
"Campus controller enrollment failed: %s: %s",
|
||||
response.Status,
|
||||
detail,
|
||||
)
|
||||
}
|
||||
return nil, "", fmt.Errorf(
|
||||
"Campus controller enrollment failed: %s",
|
||||
response.Status,
|
||||
)
|
||||
}
|
||||
var snapshot campusEnrollmentSnapshot
|
||||
if err := json.NewDecoder(response.Body).Decode(&snapshot); err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
return &snapshot, scheme, nil
|
||||
}
|
||||
|
||||
func saveCampusEnrollment(
|
||||
dataDir string,
|
||||
campusUUID string,
|
||||
enrollment *externalCampusEnrollment,
|
||||
) error {
|
||||
tokenDir := filepath.Join(dataDir, "federation")
|
||||
if err := os.MkdirAll(tokenDir, 0700); err != nil {
|
||||
return err
|
||||
}
|
||||
tokenPath := filepath.Join(tokenDir, campusUUID+".token")
|
||||
if err := os.WriteFile(
|
||||
tokenPath,
|
||||
[]byte(enrollment.Token+"\n"),
|
||||
0600,
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
if enrollment.NoDefaultCampus {
|
||||
markerPath := filepath.Join(
|
||||
tokenDir,
|
||||
campusUUID+".no-default-campus",
|
||||
)
|
||||
if err := os.WriteFile(markerPath, nil, 0600); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
scheme := "https"
|
||||
if enrollment.HTTP {
|
||||
scheme = "http"
|
||||
}
|
||||
endpointPath := filepath.Join(tokenDir, campusUUID+".endpoint")
|
||||
return os.WriteFile(
|
||||
endpointPath,
|
||||
[]byte(scheme+"://"+enrollment.FQDN+"\n"),
|
||||
0600,
|
||||
)
|
||||
}
|
||||
|
||||
func ensureInstanceIdentity(
|
||||
dataDir string,
|
||||
) (ed25519.PrivateKey, string, error) {
|
||||
keyPath := filepath.Join(dataDir, "instance-ed25519.key")
|
||||
privateKey, err := os.ReadFile(keyPath)
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
_, generated, generateErr := ed25519.GenerateKey(rand.Reader)
|
||||
if generateErr != nil {
|
||||
return nil, "", generateErr
|
||||
}
|
||||
if writeErr := os.WriteFile(keyPath, generated, 0600); writeErr != nil {
|
||||
return nil, "", writeErr
|
||||
}
|
||||
privateKey = generated
|
||||
} else if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
if len(privateKey) != ed25519.PrivateKeySize {
|
||||
return nil, "", errors.New("invalid instance Ed25519 private key")
|
||||
}
|
||||
publicKey := ed25519.PrivateKey(privateKey).Public().(ed25519.PublicKey)
|
||||
sum := sha256.Sum256(publicKey)
|
||||
return ed25519.PrivateKey(privateKey),
|
||||
"sha256:" + hex.EncodeToString(sum[:]),
|
||||
nil
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package main
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestExternalCampusEnrollmentArguments(t *testing.T) {
|
||||
enrollment, err := parseExternalCampusEnrollment([]string{
|
||||
"--add-external-campus",
|
||||
"campus.example.test",
|
||||
"secret-token",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if enrollment.FQDN != "campus.example.test" ||
|
||||
enrollment.Token != "secret-token" {
|
||||
t.Fatalf("enrollment = %#v", enrollment)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExternalCampusHTTPEnrollmentArguments(t *testing.T) {
|
||||
enrollment, err := parseExternalCampusEnrollment([]string{
|
||||
"--add-external-campus-http",
|
||||
"campus.internal.test",
|
||||
"secret-token",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !enrollment.HTTP {
|
||||
t.Fatal("HTTP enrollment did not select HTTP transport")
|
||||
}
|
||||
}
|
||||
|
||||
func TestExternalCampusNoDefaultCampusArgument(t *testing.T) {
|
||||
for _, args := range [][]string{
|
||||
{
|
||||
"--no-default-campus",
|
||||
"--add-external-campus",
|
||||
"campus.example.test",
|
||||
"secret-token",
|
||||
},
|
||||
{
|
||||
"--add-external-campus-http",
|
||||
"campus.internal.test",
|
||||
"secret-token",
|
||||
"--no-default-campus",
|
||||
},
|
||||
} {
|
||||
enrollment, err := parseExternalCampusEnrollment(args)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !enrollment.NoDefaultCampus {
|
||||
t.Fatalf("NoDefaultCampus is false for %#v", args)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNoDefaultCampusRequiresExternalCampus(t *testing.T) {
|
||||
if _, err := parseExternalCampusEnrollment([]string{
|
||||
"--no-default-campus",
|
||||
}); err == nil {
|
||||
t.Fatal("--no-default-campus without enrollment was accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInstanceFingerprintIsPersistent(t *testing.T) {
|
||||
dataDir := t.TempDir()
|
||||
_, first, err := ensureInstanceIdentity(dataDir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, second, err := ensureInstanceIdentity(dataDir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if first != second {
|
||||
t.Fatalf("fingerprints differ: %q, %q", first, second)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"log"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
const managedCampusReconcileInterval = 5 * time.Second
|
||||
|
||||
func startManagedCampusReconciler(dataDir string, st *store.Store) {
|
||||
instanceFQDN := strings.TrimSpace(os.Getenv("PAGESWIKI_INSTANCE_FQDN"))
|
||||
if instanceFQDN == "" {
|
||||
return
|
||||
}
|
||||
go func() {
|
||||
ticker := time.NewTicker(managedCampusReconcileInterval)
|
||||
defer ticker.Stop()
|
||||
for range ticker.C {
|
||||
reconcileManagedCampuses(dataDir, st, instanceFQDN)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func reconcileManagedCampuses(
|
||||
dataDir string,
|
||||
st *store.Store,
|
||||
instanceFQDN string,
|
||||
) {
|
||||
campuses, err := st.ListCampuses()
|
||||
if err != nil {
|
||||
log.Printf("managed Campus reconciliation: %v", err)
|
||||
return
|
||||
}
|
||||
for _, campus := range campuses {
|
||||
if campus.AuthorityMode != "managed" || !campus.Enabled {
|
||||
continue
|
||||
}
|
||||
if err := reconcileManagedCampus(
|
||||
dataDir,
|
||||
st,
|
||||
campus,
|
||||
instanceFQDN,
|
||||
); err != nil {
|
||||
_ = st.RecordCampusContact(campus.UUID, err.Error())
|
||||
if campus.LastContactError != err.Error() {
|
||||
log.Printf(
|
||||
"managed Campus %s reconciliation failed: %v",
|
||||
campus.FQDN,
|
||||
err,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func reconcileManagedCampus(
|
||||
dataDir string,
|
||||
st *store.Store,
|
||||
campus store.Campus,
|
||||
instanceFQDN string,
|
||||
) error {
|
||||
tokenDir := filepath.Join(dataDir, "federation")
|
||||
token, err := os.ReadFile(
|
||||
filepath.Join(tokenDir, campus.UUID+".token"),
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
controllerAddress := campus.Transport + "://" + campus.FQDN
|
||||
endpoint, err := os.ReadFile(
|
||||
filepath.Join(tokenDir, campus.UUID+".endpoint"),
|
||||
)
|
||||
if err == nil {
|
||||
controllerAddress = strings.TrimSpace(string(endpoint))
|
||||
} else if !errors.Is(err, os.ErrNotExist) {
|
||||
return err
|
||||
}
|
||||
parsed, err := url.Parse(controllerAddress)
|
||||
if err != nil || parsed.Host == "" {
|
||||
return errors.New("invalid saved Campus controller endpoint")
|
||||
}
|
||||
enrollment := &externalCampusEnrollment{
|
||||
FQDN: parsed.Host,
|
||||
Token: strings.TrimSpace(string(token)),
|
||||
HTTP: parsed.Scheme == "http",
|
||||
}
|
||||
snapshot, scheme, err := fetchCampusSnapshot(
|
||||
dataDir,
|
||||
enrollment,
|
||||
instanceFQDN,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if snapshot.Campus.UUID != campus.UUID && snapshot.Manifest.Campus.UUID != campus.UUID {
|
||||
return errors.New("Campus controller identity changed")
|
||||
}
|
||||
if err := applyEnrollmentSnapshot(st, snapshot, campus.UUID, instanceFQDN, scheme); err != nil {
|
||||
return err
|
||||
}
|
||||
markerPath := filepath.Join(
|
||||
tokenDir,
|
||||
campus.UUID+".no-default-campus",
|
||||
)
|
||||
if _, err := os.Stat(markerPath); err == nil {
|
||||
_, err = st.RemoveUnusedBootstrapCampus()
|
||||
return err
|
||||
} else if !errors.Is(err, os.ErrNotExist) {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"pageswiki/internal/db"
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
func TestManagedCampusReconciliationDeploysAssignedDomain(t *testing.T) {
|
||||
dataDir := t.TempDir()
|
||||
database, err := db.Open(dataDir + "/pages.db")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = database.Close() })
|
||||
if err := db.Migrate(database); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
st := store.New(database)
|
||||
if err := st.EnsureConfigTable(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
const (
|
||||
campusUUID = "1140999c-b690-4c38-850d-8e04572e489e"
|
||||
domainUUID = "2251000d-c7a1-4d49-961e-9f15683f590f"
|
||||
instanceFQDN = "node.example.test"
|
||||
)
|
||||
campus := store.Campus{
|
||||
UUID: campusUUID,
|
||||
Name: "external",
|
||||
Title: "External Campus",
|
||||
DefaultLang: "en",
|
||||
AuthorityMode: "controller",
|
||||
FQDN: "campus.example.test",
|
||||
Enabled: true,
|
||||
}
|
||||
domain := store.Domain{
|
||||
UUID: domainUUID,
|
||||
Name: "deployed",
|
||||
Title: "Deployed Domain",
|
||||
Domain: "deployed.example.test",
|
||||
Enabled: true,
|
||||
CampusUUID: campusUUID,
|
||||
Placement: "remote",
|
||||
InstanceFQDN: instanceFQDN,
|
||||
InstanceFingerprint: "sha256:test",
|
||||
}
|
||||
controller := httptest.NewServer(http.HandlerFunc(
|
||||
func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/_/api/v1/federation/enroll" {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{
|
||||
"campus": campus,
|
||||
"domains": []store.Domain{domain},
|
||||
})
|
||||
},
|
||||
))
|
||||
t.Cleanup(controller.Close)
|
||||
|
||||
if err := st.ApplyManagedCampusSnapshot(
|
||||
campus,
|
||||
nil,
|
||||
instanceFQDN,
|
||||
"http",
|
||||
); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
enrollment := &externalCampusEnrollment{
|
||||
FQDN: strings.TrimPrefix(controller.URL, "http://"),
|
||||
Token: "test-token",
|
||||
HTTP: true,
|
||||
NoDefaultCampus: true,
|
||||
}
|
||||
if err := saveCampusEnrollment(
|
||||
dataDir,
|
||||
campusUUID,
|
||||
enrollment,
|
||||
); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
managed, err := st.GetCampusByUUID(campusUUID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := reconcileManagedCampus(
|
||||
dataDir,
|
||||
st,
|
||||
*managed,
|
||||
instanceFQDN,
|
||||
); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
deployed, err := st.GetDomainByUUID(domainUUID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if deployed.Placement != "local" || !deployed.Enabled {
|
||||
t.Fatalf("deployed Domain = %#v", deployed)
|
||||
}
|
||||
campuses, err := st.ListCampuses()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(campuses) != 1 ||
|
||||
campuses[0].UUID != campusUUID ||
|
||||
campuses[0].AuthorityMode != "managed" {
|
||||
t.Fatalf("Campuses after reconciliation = %#v", campuses)
|
||||
}
|
||||
}
|
||||
+25
-3
@@ -3,13 +3,18 @@ package main
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"pageswiki/internal/db"
|
||||
"pageswiki/internal/server"
|
||||
"pageswiki/internal/store"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func main() {
|
||||
enrollment, err := parseExternalCampusEnrollment(os.Args[1:])
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
dataDir :=
|
||||
os.Getenv(
|
||||
@@ -20,7 +25,7 @@ func main() {
|
||||
dataDir = "./data"
|
||||
}
|
||||
|
||||
err := os.MkdirAll(
|
||||
err = os.MkdirAll(
|
||||
dataDir,
|
||||
0755,
|
||||
)
|
||||
@@ -53,7 +58,20 @@ func main() {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
st := store.New(database)
|
||||
documentRoot, err :=
|
||||
store.MigrateLegacyDocumentRoot(
|
||||
dataDir,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
st :=
|
||||
store.NewWithDocumentRoot(
|
||||
database,
|
||||
documentRoot,
|
||||
)
|
||||
|
||||
err = st.EnsureConfigTable()
|
||||
|
||||
@@ -66,6 +84,10 @@ func main() {
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if err := enrollExternalCampus(dataDir, st, enrollment); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
startManagedCampusReconciler(dataDir, st)
|
||||
|
||||
srv := server.New(st)
|
||||
|
||||
|
||||
Reference in new issue
Block a user