diff --git a/.gitea/workflows/ci.yaml b/.gitea/workflows/ci.yaml index 241c33d..d42e266 100644 --- a/.gitea/workflows/ci.yaml +++ b/.gitea/workflows/ci.yaml @@ -83,7 +83,7 @@ jobs: # own test binary — and including it triggers a covdata-tool error # on packages with no _test.go files. -coverpkg makes the server's # exercise of store/* count toward coverage. - run: go test -race -coverpkg=./internal/... -coverprofile=cover.out ./internal/server/... ./internal/config/... ./internal/keycloak/... ./internal/llmkeys/... ./internal/erpsetup/... ./internal/scopes/... ./internal/secretbox/... ./internal/roles/... ./internal/telemetry/... + run: go test -race -coverpkg=./internal/... -coverprofile=cover.out ./internal/server/... ./internal/config/... ./internal/keycloak/... ./internal/llmkeys/... ./internal/erpsetup/... ./internal/scopes/... ./internal/secretbox/... ./internal/roles/... ./internal/telemetry/... ./internal/legal/... - name: coverage gate run: | diff --git a/cmd/server/main.go b/cmd/server/main.go index cb6305a..c6c0165 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -17,6 +17,7 @@ import ( "git.breakpilot.com/platform/tenant-registry/internal/config" "git.breakpilot.com/platform/tenant-registry/internal/erp" "git.breakpilot.com/platform/tenant-registry/internal/keycloak" + "git.breakpilot.com/platform/tenant-registry/internal/legal/contractdocs" "git.breakpilot.com/platform/tenant-registry/internal/licence" "git.breakpilot.com/platform/tenant-registry/internal/llmkeys" "git.breakpilot.com/platform/tenant-registry/internal/product" @@ -36,6 +37,13 @@ func main() { slog.Error("config load failed", "err", err) os.Exit(1) } + // The committed contract documents are served byte for byte: a PDF that + // does not match its pinned SHA-256 stops the start. + if err := contractdocs.Check(); err != nil { + slog.Error("contract documents do not match their pins", "err", err) + os.Exit(1) + } + slog.Info("contract documents", "released", len(contractdocs.Default().All()), "gate_on", cfg.ContractDocuments) // Traces to SigNoz when OTEL_EXPORTER_OTLP_ENDPOINT is set; every outbound // client without its own transport (Keycloak, ERP, LiteLLM, OIDC) goes diff --git a/internal/config/config.go b/internal/config/config.go index c767d43..e220c56 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -81,6 +81,12 @@ type Config struct { // StoreImportMaxBytes caps one document-store bundle import (default 2 GiB). StoreImportMaxBytes int64 + // ContractDocuments (CONTRACT_DOCUMENTS "on"/"off", default off) releases + // the contract documents (AGB, AVV, TOM, Leistungsbeschreibungen) in the + // Vertragsunterlagen. Off: the overview lists none and their PDF routes + // answer 404. + ContractDocuments bool + // Platform mail (F3). SMTPHost empty ⇒ notifications queue up unsent. SMTPHost string SMTPPort int @@ -154,6 +160,7 @@ func Load() (*Config, error) { DocumentsS3AccessKey: os.Getenv("DOCUMENTS_S3_ACCESS_KEY"), DocumentsS3SecretKey: os.Getenv("DOCUMENTS_S3_SECRET_KEY"), DocumentsMaxBytes: int64(getint("DOCUMENTS_MAX_MB", 200)) << 20, + ContractDocuments: getenv("CONTRACT_DOCUMENTS", "off") == "on", StoreImportMaxBytes: int64(getint("STORE_IMPORT_MAX_MB", 2048)) << 20, SMTPHost: os.Getenv("SMTP_HOST"), diff --git a/internal/config/config_test.go b/internal/config/config_test.go index acfc72f..b5e93fa 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -86,3 +86,18 @@ func TestLoad_litellmDefaultsAndOverrides(t *testing.T) { t.Errorf("blank list should fall back: %v", cfg.LiteLLMModels) } } + +// Contract documents stay off unless the env says exactly "on". +func TestLoad_contractDocumentsGate(t *testing.T) { + t.Setenv("APP_ENV", "") + for value, want := range map[string]bool{"": false, "off": false, "true": false, "ON": false, "on": true} { + t.Setenv("CONTRACT_DOCUMENTS", value) + cfg, err := Load() + if err != nil { + t.Fatal(err) + } + if cfg.ContractDocuments != want { + t.Errorf("CONTRACT_DOCUMENTS=%q: %v, want %v", value, cfg.ContractDocuments, want) + } + } +} diff --git a/internal/legal/contractdocs/contractdocs.go b/internal/legal/contractdocs/contractdocs.go new file mode 100644 index 0000000..363ab84 --- /dev/null +++ b/internal/legal/contractdocs/contractdocs.go @@ -0,0 +1,209 @@ +// Package contractdocs holds the released contract documents a workspace sees +// in its Vertragsunterlagen: AGB, AVV (the master, with blank lines for the +// customer's details), TOM and the Leistungsbeschreibungen. Each is a fixed +// PDF committed under files/ with its SHA-256 pinned in the table below; the +// pins are checked at startup and by a test, and a PDF is only ever served +// byte for byte. Never replace the PDF of a released version: a new text is a +// new version, i.e. a new PDF and its line in the table. +// +// Releasing a document is one line in released plus its PDF in files/. A +// document not released yet is simply not in the table, and the overview +// does not list it. +package contractdocs + +import ( + "crypto/sha256" + "embed" + "encoding/hex" + "fmt" + "regexp" +) + +//go:embed files/*.pdf +var files embed.FS + +// Kinds of contract documents. +const ( + KindAGB = "agb" // Allgemeine Geschäftsbedingungen + KindAVV = "avv" // Auftragsverarbeitungsvertrag + KindTOM = "tom" // Technische und organisatorische Maßnahmen + KindLB = "lb" // Leistungsbeschreibung (one per module) +) + +// Doc is one released contract document. +type Doc struct { + // ID names the document across versions ("agb", "lb-pruefwerk"); it is + // the path segment of the PDF route. AGB, AVV and TOM have their kind as + // ID, so a workspace's own filled AVV takes the master's place. + ID string + Kind string + Title string + Version string // "1.0.3" + Date string // the "Stand" printed in the document, DD.MM.YYYY + SHA256 string // pinned digest of the PDF + // Which workspaces a Leistungsbeschreibung applies to: Product (a product + // key) when the workspace holds that product, ManagedService (a managed + // service kind) when it has that service, neither: every workspace. + Product string + ManagedService string + File string // file name under files/ + PDF []byte +} + +// released is the table of released documents, in the order the overview +// lists them. Not released yet (add them here with their PDF once they are): +// +// {ID: "agb", Kind: KindAGB, Title: "Allgemeine Geschäftsbedingungen", Version: "1.0.0", Date: "TT.MM.JJJJ", File: "Breakpilot_AGB_1.0.0.pdf", SHA256: "…"}, +// {ID: "avv", Kind: KindAVV, Title: "Auftragsverarbeitungsvertrag", Version: "1.0.0", Date: "TT.MM.JJJJ", File: "Breakpilot_AVV_1.0.0.pdf", SHA256: "…"}, +// {ID: "tom", Kind: KindTOM, Title: "Technische und organisatorische Maßnahmen", Version: "1.0.0", Date: "TT.MM.JJJJ", File: "Breakpilot_TOM_1.0.0.pdf", SHA256: "…"}, +var released = []Doc{ + {ID: "lb-plattform", Kind: KindLB, Title: "Leistungsbeschreibung Breakpilot", Version: "1.0.0", Date: "06.11.2026", + File: "Breakpilot_Leistungsbeschreibung_Plattform_1.0.0.pdf", SHA256: "29205b6b8db23f6e0c213e0f2f6b28b13aaf36656c991789b43dd0610079c65b"}, + {ID: "lb-pruefwerk", Kind: KindLB, Title: "Leistungsbeschreibung Prüfwerk", Version: "1.0.3", Date: "09.11.2026", Product: "pruefwerk", + File: "Breakpilot_Leistungsbeschreibung_Pruefwerk_1.0.3.pdf", SHA256: "59672ea23c17621fac767cef27250d0b14b18a2d881da19408d03287628e6731"}, + {ID: "lb-werkpilot", Kind: KindLB, Title: "Leistungsbeschreibung Werkpilot", Version: "1.0.0", Date: "06.11.2026", Product: "werkpilot", + File: "Breakpilot_Leistungsbeschreibung_Werkpilot_1.0.0.pdf", SHA256: "b57197b60c7fdb15d2ea234cbd522941ae8ed6c4f581be2ae34055380f97aee3"}, + {ID: "lb-firmwerk", Kind: KindLB, Title: "Leistungsbeschreibung Firmwerk", Version: "1.0.0", Date: "06.11.2026", Product: "firmwerk", + File: "Breakpilot_Leistungsbeschreibung_Firmwerk_1.0.0.pdf", SHA256: "7b7e70a426283c75a277ca7983ade0463267c17f99088100ef0cad642df56e0c"}, + {ID: "lb-werkmeister", Kind: KindLB, Title: "Leistungsbeschreibung Werkmeister", Version: "1.0.0", Date: "06.11.2026", Product: "werkmeister", + File: "Breakpilot_Leistungsbeschreibung_Werkmeister_1.0.0.pdf", SHA256: "284b9b00f9649d39a1960e056b9dcbccf48ed2fcfb1f84fba052d8cba3ade1a0"}, + {ID: "lb-managed-git", Kind: KindLB, Title: "Leistungsbeschreibung Managed Git", Version: "1.0.0", Date: "06.11.2026", ManagedService: "git", + File: "Breakpilot_Leistungsbeschreibung_ManagedGit_1.0.0.pdf", SHA256: "d529c510577371d0223a8466434dd89aafbc07335803a6a91cac43223b569c92"}, + {ID: "lb-build-runner", Kind: KindLB, Title: "Leistungsbeschreibung Build-Runner", Version: "1.0.0", Date: "06.11.2026", ManagedService: "ci-runner", + File: "Breakpilot_Leistungsbeschreibung_BuildRunner_1.0.0.pdf", SHA256: "b71909f2177fc2aeb2542f9fad7b3f1ec59d4df8c621a6e4bf66667f9a735638"}, +} + +// titles are the document titles per kind; a workspace's own filled AVV +// carries the AVV title whether or not the master is released. +var titles = map[string]string{ + KindAGB: "Allgemeine Geschäftsbedingungen", + KindAVV: "Auftragsverarbeitungsvertrag", + KindTOM: "Technische und organisatorische Maßnahmen", + KindLB: "Leistungsbeschreibung", +} + +// Title is the title of a kind of document ("" for an unknown kind). +func Title(kind string) string { return titles[kind] } + +// Catalogue is a set of released documents. +type Catalogue struct { + docs []*Doc + byID map[string]*Doc +} + +var def = mustDefault() + +func mustDefault() *Catalogue { + docs := make([]Doc, len(released)) + for i, d := range released { + b, err := files.ReadFile("files/" + d.File) + if err != nil { + panic(fmt.Sprintf("contractdocs: %s: %v", d.ID, err)) + } + d.PDF = b + docs[i] = d + } + return NewCatalogue(docs...) +} + +// Default is the catalogue of committed documents. +func Default() *Catalogue { return def } + +// NewCatalogue builds a catalogue of docs (with their PDFs), in that order. +// It does not check them; Check does. +func NewCatalogue(docs ...Doc) *Catalogue { + c := &Catalogue{byID: map[string]*Doc{}} + for i := range docs { + d := docs[i] + c.docs = append(c.docs, &d) + c.byID[d.ID] = &d + } + return c +} + +// All returns every document in table order. +func (c *Catalogue) All() []*Doc { return c.docs } + +// Get returns the document with that ID, or nil. +func (c *Catalogue) Get(id string) *Doc { return c.byID[id] } + +// Holdings is what a workspace has that decides which Leistungsbeschreibungen +// apply: product keys and managed service kinds. +type Holdings struct { + Products map[string]bool + ManagedServices map[string]bool +} + +// Applies reports whether d applies to a workspace with holdings h. AGB, AVV +// and TOM apply to every workspace, and so does a Leistungsbeschreibung bound +// to neither a product nor a managed service (the platform's). +func (d *Doc) Applies(h Holdings) bool { + switch { + case d.Product != "": + return h.Products[d.Product] + case d.ManagedService != "": + return h.ManagedServices[d.ManagedService] + } + return true +} + +// For returns the documents that apply to a workspace with holdings h. +func (c *Catalogue) For(h Holdings) []*Doc { + out := make([]*Doc, 0, len(c.docs)) + for _, d := range c.docs { + if d.Applies(h) { + out = append(out, d) + } + } + return out +} + +var ( + versionRe = regexp.MustCompile(`^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$`) + dateRe = regexp.MustCompile(`^(0[1-9]|[12][0-9]|3[01])\.(0[1-9]|1[0-2])\.20[0-9]{2}$`) + idRe = regexp.MustCompile(`^[a-z][a-z0-9-]{1,39}$`) +) + +// ValidVersion reports whether v is a document version ("1.0.0"). +func ValidVersion(v string) bool { return versionRe.MatchString(v) } + +// Check verifies every document: its PDF matches the pinned SHA-256 and is a +// PDF, and its table entry is well-formed (unique ID, known kind, version, +// Stand, at most one condition). +func (c *Catalogue) Check() error { + seen := map[string]bool{} + for _, d := range c.docs { + switch { + case !idRe.MatchString(d.ID) || seen[d.ID]: + return fmt.Errorf("contractdocs: id %q is malformed or used twice", d.ID) + case titles[d.Kind] == "": + return fmt.Errorf("contractdocs %s: unknown kind %q", d.ID, d.Kind) + case d.Kind != KindLB && d.ID != d.Kind: + return fmt.Errorf("contractdocs %s: the %s is listed under the id %q", d.ID, d.Kind, d.Kind) + case d.Kind != KindLB && (d.Product != "" || d.ManagedService != ""): + return fmt.Errorf("contractdocs %s: only a Leistungsbeschreibung is bound to a product or service", d.ID) + case d.Product != "" && d.ManagedService != "": + return fmt.Errorf("contractdocs %s: bound to both a product and a managed service", d.ID) + case !ValidVersion(d.Version): + return fmt.Errorf("contractdocs %s: version %q", d.ID, d.Version) + case !dateRe.MatchString(d.Date): + return fmt.Errorf("contractdocs %s: date %q is not DD.MM.YYYY", d.ID, d.Date) + case len(d.PDF) < 5 || string(d.PDF[:5]) != "%PDF-": + return fmt.Errorf("contractdocs %s: not a PDF", d.ID) + } + if got := Sum(d.PDF); got != d.SHA256 { + return fmt.Errorf("contractdocs %s %s: pdf sha256 %s, pinned %s", d.ID, d.Version, got, d.SHA256) + } + seen[d.ID] = true + } + return nil +} + +// Check verifies the committed documents (see Catalogue.Check). +func Check() error { return def.Check() } + +// Sum is the hex SHA-256 of b. +func Sum(b []byte) string { + h := sha256.Sum256(b) + return hex.EncodeToString(h[:]) +} diff --git a/internal/legal/contractdocs/contractdocs_test.go b/internal/legal/contractdocs/contractdocs_test.go new file mode 100644 index 0000000..2a9c893 --- /dev/null +++ b/internal/legal/contractdocs/contractdocs_test.go @@ -0,0 +1,123 @@ +package contractdocs + +import ( + "strings" + "testing" +) + +func TestCommittedDocumentsMatchTheirPins(t *testing.T) { + if err := Check(); err != nil { + t.Fatal(err) + } + // Every file under files/ is in the table: nothing is committed unpinned. + entries, err := files.ReadDir("files") + if err != nil { + t.Fatal(err) + } + inTable := map[string]bool{} + for _, d := range released { + inTable[d.File] = true + } + for _, e := range entries { + if !inTable[e.Name()] { + t.Errorf("files/%s is not in the table", e.Name()) + } + } + if len(entries) != len(released) { + t.Errorf("%d files, %d table entries", len(entries), len(released)) + } +} + +// The released set as of 09.11.2026: the seven Leistungsbeschreibungen, no +// AGB, AVV or TOM yet. +func TestReleasedSet(t *testing.T) { + want := map[string]string{ + "lb-plattform": "1.0.0", "lb-pruefwerk": "1.0.3", "lb-werkpilot": "1.0.0", "lb-firmwerk": "1.0.0", + "lb-werkmeister": "1.0.0", "lb-managed-git": "1.0.0", "lb-build-runner": "1.0.0", + } + all := Default().All() + if len(all) != len(want) { + t.Fatalf("%d documents", len(all)) + } + for _, d := range all { + if want[d.ID] != d.Version || d.Kind != KindLB || !strings.HasPrefix(d.Title, "Leistungsbeschreibung ") { + t.Errorf("%+v", d) + } + if Default().Get(d.ID) != d { + t.Errorf("get %s", d.ID) + } + } + if all[0].ID != "lb-plattform" || Default().Get("avv") != nil || Default().Get("lb-pruefwerk").Date != "09.11.2026" { + t.Error("order, absent AVV or Stand") + } +} + +// The one place that says which Leistungsbeschreibung applies to whom. +func TestApplicability(t *testing.T) { + ids := func(h Holdings) string { + var out []string + for _, d := range Default().For(h) { + out = append(out, d.ID) + } + return strings.Join(out, ",") + } + for name, c := range map[string]struct { + h Holdings + want string + }{ + "nothing booked": {Holdings{}, "lb-plattform"}, + "pruefwerk": {Holdings{Products: map[string]bool{"pruefwerk": true}}, "lb-plattform,lb-pruefwerk"}, + "all products": {Holdings{Products: map[string]bool{"pruefwerk": true, "werkpilot": true, "firmwerk": true, "werkmeister": true, "compliance": true}}, + "lb-plattform,lb-pruefwerk,lb-werkpilot,lb-firmwerk,lb-werkmeister"}, + "managed git": {Holdings{ManagedServices: map[string]bool{"git": true}}, "lb-plattform,lb-managed-git"}, + "git and runner": {Holdings{ManagedServices: map[string]bool{"git": true, "ci-runner": true}}, "lb-plattform,lb-managed-git,lb-build-runner"}, + "service named as product": {Holdings{Products: map[string]bool{"git": true}}, "lb-plattform"}, + } { + if got := ids(c.h); got != c.want { + t.Errorf("%s: %s, want %s", name, got, c.want) + } + } + // AGB, AVV and TOM apply to everyone once released. + for _, k := range []string{KindAGB, KindAVV, KindTOM} { + if !(&Doc{Kind: k}).Applies(Holdings{}) { + t.Errorf("%s does not apply", k) + } + } +} + +func TestTitles(t *testing.T) { + if Title(KindAVV) != "Auftragsverarbeitungsvertrag" || Title(KindAGB) == "" || Title(KindTOM) == "" || Title("x") != "" { + t.Error("titles") + } + if !ValidVersion("1.0.0") || ValidVersion("1.0") || ValidVersion("v1.0.0") || ValidVersion("1.0.0 ") { + t.Error("versions") + } +} + +func TestCheckRefusesBadEntries(t *testing.T) { + pdf := []byte("%PDF-1.7 synthetic") + good := Doc{ID: "avv", Kind: KindAVV, Version: "1.0.0", Date: "01.12.2026", PDF: pdf, SHA256: Sum(pdf)} + if err := NewCatalogue(good).Check(); err != nil { + t.Fatalf("good: %v", err) + } + for name, mod := range map[string]func(d *Doc){ + "changed pdf": func(d *Doc) { d.PDF = []byte("%PDF-1.7 changed") }, + "not a pdf": func(d *Doc) { d.PDF = []byte(""); d.SHA256 = Sum(d.PDF) }, + "bad id": func(d *Doc) { d.ID = "../avv" }, + "avv under other id": func(d *Doc) { d.ID = "avv-master" }, + "unknown kind": func(d *Doc) { d.Kind = "nda" }, + "avv for product": func(d *Doc) { d.Product = "pruefwerk" }, + "both conditions": func(d *Doc) { d.Kind = KindLB; d.Product = "pruefwerk"; d.ManagedService = "git" }, + "bad version": func(d *Doc) { d.Version = "1.0" }, + "iso date": func(d *Doc) { d.Date = "2026-12-01" }, + } { + d := good + mod(&d) + if NewCatalogue(d).Check() == nil { + t.Errorf("%s: not refused", name) + } + } + if NewCatalogue(good, good).Check() == nil { + t.Error("duplicate id not refused") + } +} diff --git a/internal/legal/contractdocs/files/Breakpilot_Leistungsbeschreibung_BuildRunner_1.0.0.pdf b/internal/legal/contractdocs/files/Breakpilot_Leistungsbeschreibung_BuildRunner_1.0.0.pdf new file mode 100644 index 0000000..70e71ee Binary files /dev/null and b/internal/legal/contractdocs/files/Breakpilot_Leistungsbeschreibung_BuildRunner_1.0.0.pdf differ diff --git a/internal/legal/contractdocs/files/Breakpilot_Leistungsbeschreibung_Firmwerk_1.0.0.pdf b/internal/legal/contractdocs/files/Breakpilot_Leistungsbeschreibung_Firmwerk_1.0.0.pdf new file mode 100644 index 0000000..0ebba94 Binary files /dev/null and b/internal/legal/contractdocs/files/Breakpilot_Leistungsbeschreibung_Firmwerk_1.0.0.pdf differ diff --git a/internal/legal/contractdocs/files/Breakpilot_Leistungsbeschreibung_ManagedGit_1.0.0.pdf b/internal/legal/contractdocs/files/Breakpilot_Leistungsbeschreibung_ManagedGit_1.0.0.pdf new file mode 100644 index 0000000..8e4e5fc Binary files /dev/null and b/internal/legal/contractdocs/files/Breakpilot_Leistungsbeschreibung_ManagedGit_1.0.0.pdf differ diff --git a/internal/legal/contractdocs/files/Breakpilot_Leistungsbeschreibung_Plattform_1.0.0.pdf b/internal/legal/contractdocs/files/Breakpilot_Leistungsbeschreibung_Plattform_1.0.0.pdf new file mode 100644 index 0000000..5b71db7 Binary files /dev/null and b/internal/legal/contractdocs/files/Breakpilot_Leistungsbeschreibung_Plattform_1.0.0.pdf differ diff --git a/internal/legal/contractdocs/files/Breakpilot_Leistungsbeschreibung_Pruefwerk_1.0.3.pdf b/internal/legal/contractdocs/files/Breakpilot_Leistungsbeschreibung_Pruefwerk_1.0.3.pdf new file mode 100644 index 0000000..c96df9d Binary files /dev/null and b/internal/legal/contractdocs/files/Breakpilot_Leistungsbeschreibung_Pruefwerk_1.0.3.pdf differ diff --git a/internal/legal/contractdocs/files/Breakpilot_Leistungsbeschreibung_Werkmeister_1.0.0.pdf b/internal/legal/contractdocs/files/Breakpilot_Leistungsbeschreibung_Werkmeister_1.0.0.pdf new file mode 100644 index 0000000..3a91b0d Binary files /dev/null and b/internal/legal/contractdocs/files/Breakpilot_Leistungsbeschreibung_Werkmeister_1.0.0.pdf differ diff --git a/internal/legal/contractdocs/files/Breakpilot_Leistungsbeschreibung_Werkpilot_1.0.0.pdf b/internal/legal/contractdocs/files/Breakpilot_Leistungsbeschreibung_Werkpilot_1.0.0.pdf new file mode 100644 index 0000000..e12c9a7 Binary files /dev/null and b/internal/legal/contractdocs/files/Breakpilot_Leistungsbeschreibung_Werkpilot_1.0.0.pdf differ