package catalog import ( "fmt" "path" "regexp" "strings" ) // QuestionTypes are the built-in question types (WIZ-03). var QuestionTypes = map[string]bool{ "boolean": true, "select": true, "multiselect": true, "string": true, "text": true, "integer": true, "decimal": true, "date": true, "duration": true, "hostname": true, "domain": true, "email": true, "ip": true, "cidr": true, "port": true, "url": true, "path": true, "dir": true, "list": true, "keyvalue": true, "package": true, "profile": true, "capability": true, "provider": true, "target": true, "secret": true, } // ListItemTypes may be used as list items. var ListItemTypes = map[string]bool{ "string": true, "hostname": true, "domain": true, "email": true, "ip": true, "cidr": true, "port": true, "url": true, "path": true, "integer": true, } var idRE = regexp.MustCompile(`^[a-z0-9][a-z0-9.-]*[a-z0-9]$|^[a-z0-9]$`) var questionIDRE = regexp.MustCompile(`^[a-zA-Z][a-zA-Z0-9_]*$`) // SafeRelPath reports whether p is a clean relative slash path without // "..", usable as an output file name (SEC-03). func SafeRelPath(p string) bool { if p == "" || strings.HasPrefix(p, "/") || strings.Contains(p, "\\") || strings.Contains(p, ":") { return false } if path.Clean(p) != p { return false } for _, part := range strings.Split(p, "/") { if part == ".." || part == "." || part == "" || strings.HasPrefix(part, ".") { return false } } return true } // Validate checks the catalog's internal consistency and returns all // issues (load issues included). Reference targets outside the catalog // (manual sections, microsites) are checked by knowledge.Lint. func (c *Catalog) Validate() []Issue { v := &validator{c: c, issues: append([]Issue(nil), c.Issues...)} v.root() for _, id := range SortedIDs(c.Capabilities) { v.capability(c.Capabilities[id]) } for _, id := range SortedIDs(c.Packages) { v.pkg(c.Packages[id]) } for _, id := range SortedIDs(c.Profiles) { v.profile(c.Profiles[id]) } for _, id := range SortedIDs(c.Solutions) { v.solution(c.Solutions[id]) } for _, id := range SortedIDs(c.Tasks) { v.solution(c.Tasks[id]) } for _, id := range SortedIDs(c.Examples) { v.example(c.Examples[id]) } for _, id := range SortedIDs(c.Sources) { v.source(c.Sources[id]) } return v.issues } type validator struct { c *Catalog issues []Issue } func (v *validator) errf(e *Entity, id, code, f string, a ...any) { i := Issue{Severity: "error", ID: id, Code: code, Message: fmt.Sprintf(f, a...)} if e != nil { i.File, i.Line = e.File, e.Line } v.issues = append(v.issues, i) } func (v *validator) warnf(e *Entity, id, code, f string, a ...any) { i := Issue{Severity: "warning", ID: id, Code: code, Message: fmt.Sprintf(f, a...)} if e != nil { i.File, i.Line = e.File, e.Line } v.issues = append(v.issues, i) } func (v *validator) root() { c := v.c if c.Spec.WebBase == "" { v.errf(nil, "catalog", "web-base", "catalog.yaml: spec.webBase is required") } seen := map[string]bool{} for _, cat := range c.Spec.Categories { if seen[cat.ID] { v.errf(nil, cat.ID, "duplicate", "duplicate category %q", cat.ID) } seen[cat.ID] = true if cat.Topic != "" && c.BigPicture != nil { if _, ok := c.BigPicture.Topics[cat.Topic]; !ok { v.errf(nil, cat.ID, "bigpicture-ref", "category %q: unknown Big Picture topic %q", cat.ID, cat.Topic) } } if cat.Topic == "" && cat.Title == "" { v.errf(nil, cat.ID, "category", "category %q needs a topic or a title", cat.ID) } } } func (v *validator) capability(cp *Capability) { e := &Entity{File: v.c.capFile[cp.ID]} if !idRE.MatchString(cp.ID) || strings.Contains(cp.ID, "@") { v.errf(e, cp.ID, "id", "capability ID %q: lower case, dot separated, no '@' (ADR-0003)", cp.ID) } if cp.Title == "" { v.errf(e, cp.ID, "required", "capability %q: title is required", cp.ID) } for _, r := range append(append([]string{}, cp.Requires...), cp.Conflicts...) { if _, ok := v.c.Capabilities[r]; !ok { v.errf(e, cp.ID, "capability-ref", "capability %q names unknown capability %q", cp.ID, r) } } for _, x := range cp.External { if !x.Support.Valid() { v.errf(e, cp.ID, "support", "external provider %q: unknown support %q", x.ID, x.Support) } } } func (v *validator) caps(e *Entity, id string, list []string) { for _, cp := range list { if _, ok := v.c.Capabilities[cp]; !ok { v.errf(e, id, "capability-ref", "unknown capability %q", cp) } } } func (v *validator) pkg(p *Package) { e := &p.Entity if !idRE.MatchString(p.Meta.ID) { v.errf(e, p.Meta.ID, "id", "package ID %q is not valid", p.Meta.ID) } if p.Spec.BigPicture != "" && v.c.BigPicture != nil { if _, ok := v.c.BigPicture.Products[p.Spec.BigPicture]; !ok { v.errf(e, p.Meta.ID, "bigpicture-ref", "unknown Big Picture program %q", p.Spec.BigPicture) } } if p.Spec.BigPicture == "" && p.Meta.Summary == "" { v.errf(e, p.Meta.ID, "required", "package needs spec.bigpicture or metadata.summary") } if len(p.Spec.Sources) == 0 { v.errf(e, p.Meta.ID, "sources", "package claims need sources (ANA-02)") } seen := map[string]bool{} for _, m := range p.Spec.Modules { if seen[m.ID] { v.errf(e, p.Meta.ID, "duplicate", "duplicate module %q", m.ID) } seen[m.ID] = true if !m.Support.Valid() { v.errf(e, p.Meta.ID, "support", "module %q: unknown support %q", m.ID, m.Support) } v.caps(e, p.Meta.ID+"/"+m.ID, m.Provides) v.caps(e, p.Meta.ID+"/"+m.ID, m.Requires) if m.Manual != "" { if _, ok := v.c.Sources[m.Manual]; !ok { v.errf(e, p.Meta.ID+"/"+m.ID, "source-ref", "unknown knowledge source %q", m.Manual) } } } } func (v *validator) profile(p *Profile) { e := &p.Entity pkg, ok := v.c.Packages[p.Meta.Package] if !ok { v.errf(e, p.Key(), "package-ref", "profile of unknown package %q", p.Meta.Package) return } if _, err := v.c.ProfileChain(p.Meta.Package, p.Meta.ID); err != nil { v.errf(e, p.Key(), "extends", "%v", err) } for m, state := range p.Spec.Modules { if pkg.Module(m) == nil { v.errf(e, p.Key(), "module-ref", "unknown module %q", m) } if state != "enabled" && state != "disabled" { v.errf(e, p.Key(), "module-state", "module %q: want enabled or disabled, got %q", m, state) } } for _, f := range p.Spec.Config { v.fragment(e, p.Key(), f, nil, 0) } for _, r := range p.Spec.Recommendations { if r.Package != "" { if _, ok := v.c.Packages[r.Package]; !ok { v.errf(e, p.Key(), "package-ref", "recommendation names unknown package %q", r.Package) } } if r.Capability != "" { v.caps(e, p.Key(), []string{r.Capability}) } if r.Reason == "" { v.errf(e, p.Key(), "reason", "a recommendation needs a reason (EX-03)") } } } func (v *validator) fragment(e *Entity, id string, f Fragment, s *Solution, upto int) { if !SafeRelPath(f.File) { v.errf(e, id, "path", "fragment file %q: want a clean relative path", f.File) } if f.Set.Kind == 0 { v.errf(e, id, "fragment", "fragment for %q has no set", f.File) } if s != nil { v.cond(e, id, "fragment "+f.File, f.When, s, upto) } else if len(f.When) > 0 { v.errf(e, id, "fragment", "profile fragments cannot have conditions") } } // cond checks a condition: well-formed and only naming questions before // index upto (ADR-0003). func (v *validator) cond(e *Entity, id, where string, c Cond, s *Solution, upto int) { if len(c) == 0 { return } if _, err := c.Eval(Env{Answers: map[string]any{}}); err != nil { v.errf(e, id, "condition", "%s: %v", where, err) } for _, ref := range c.Refs() { _, idx := s.Question(ref) if idx < 0 { v.errf(e, id, "condition", "%s: unknown question %q", where, ref) } else if idx >= upto { v.errf(e, id, "condition-order", "%s: names question %q, which is not asked before it", where, ref) } } } func (v *validator) targets(e *Entity, id string, t *Targets, s *Solution) { if t == nil { return } for _, p := range t.Packages { if _, ok := v.c.Packages[p]; !ok { v.errf(e, id, "package-ref", "unknown package %q", p) } } v.caps(e, id, t.Capabilities) for _, m := range t.Modules { pkg, mod, _ := strings.Cut(m, "/") p, ok := v.c.Packages[pkg] if !ok || p.Module(mod) == nil { v.errf(e, id, "module-ref", "unknown module %q (want package/module)", m) } } for q, val := range t.Answers { qq, _ := s.Question(q) if qq == nil { v.errf(e, id, "question-ref", "unknown question %q", q) continue } v.optionValue(e, id, qq, val) } } func (v *validator) optionValue(e *Entity, id string, q *Question, val any) { if q.Type != "select" && q.Type != "multiselect" { return } vals := asList(val) if _, isList := val.([]any); !isList { vals = []any{val} } for _, x := range vals { found := false for _, o := range q.Options { if Equal(o.ID, x) { found = true if !o.Support.Selectable() { v.errf(e, id, "default-unselectable", "question %q: value %q is %s and cannot be selected", q.ID, Scalar(x), o.Support) } } } if !found { v.errf(e, id, "option-ref", "question %q has no option %q", q.ID, Scalar(x)) } } } func (v *validator) solution(s *Solution) { e := &s.Entity id := s.Meta.ID if !idRE.MatchString(id) { v.errf(e, id, "id", "ID %q is not valid", id) } if s.Meta.Version == "" { v.errf(e, id, "version", "metadata.version is required (LOCK)") } if !s.Meta.Status.Valid() { v.errf(e, id, "support", "unknown status %q", s.Meta.Status) } if s.Spec.BigPicture != "" && v.c.BigPicture != nil { if _, ok := v.c.BigPicture.Stories[s.Spec.BigPicture]; !ok { v.errf(e, id, "bigpicture-ref", "unknown Big Picture goal %q", s.Spec.BigPicture) } } if s.Meta.Title == "" && s.Spec.BigPicture == "" { v.errf(e, id, "required", "title or spec.bigpicture is required") } if s.Kind == KindSolution && s.Meta.Category != "" { found := false for _, cat := range v.c.Spec.Categories { found = found || cat.ID == s.Meta.Category } if !found { v.errf(e, id, "category", "unknown category %q", s.Meta.Category) } } if s.Kind == KindTask { switch s.Spec.Kind { case "read-only", "generate", "diagnostic", "planning", "mutating", "deployment", "maintenance": default: v.errf(e, id, "task-kind", "spec.kind %q: want read-only, generate, diagnostic, planning, mutating, deployment or maintenance", s.Spec.Kind) } if s.Spec.AppliesTo != nil { for _, sid := range s.Spec.AppliesTo.Solutions { if _, ok := v.c.Solutions[sid]; !ok { v.errf(e, id, "solution-ref", "appliesTo: unknown solution %q", sid) } } v.caps(e, id, s.Spec.AppliesTo.Capabilities) } } v.caps(e, id, s.Spec.Capabilities.Required) v.caps(e, id, s.Spec.Capabilities.Recommended) v.caps(e, id, s.Spec.Capabilities.Optional) nq := len(s.Spec.Questions) for _, pu := range s.Spec.Packages { if _, ok := v.c.Packages[pu.ID]; !ok { v.errf(e, id, "package-ref", "unknown package %q", pu.ID) continue } if pu.Profile != "" { if _, ok := v.c.Profiles[pu.ID+"/"+pu.Profile]; !ok { v.errf(e, id, "profile-ref", "unknown profile %s/%s", pu.ID, pu.Profile) } } v.cond(e, id, "package "+pu.ID, pu.When, s, nq) } seenQ := map[string]bool{} for i := range s.Spec.Questions { q := &s.Spec.Questions[i] qid := id + "." + q.ID if !questionIDRE.MatchString(q.ID) { v.errf(e, qid, "id", "question ID %q: letters, digits and _ only", q.ID) } if seenQ[q.ID] { v.errf(e, qid, "duplicate", "duplicate question %q", q.ID) } seenQ[q.ID] = true if !QuestionTypes[q.Type] { v.errf(e, qid, "type", "unknown question type %q", q.Type) } if q.Type == "list" && !ListItemTypes[q.Item] { v.errf(e, qid, "type", "list needs item: one of the scalar types, got %q", q.Item) } if q.Title == "" { v.errf(e, qid, "required", "question needs a title") } switch q.Level { case "", LevelQuick, LevelGuided, LevelAdvanced: default: v.errf(e, qid, "level", "unknown level %q", q.Level) } if (q.Type == "select" || q.Type == "multiselect") && len(q.Options) == 0 { v.errf(e, qid, "options", "%s question without options", q.Type) } if q.Type == "secret" && q.Default != nil { v.errf(e, qid, "secret-default", "a secret question cannot have a default (SEC-01)") } seenO := map[string]bool{} anySelectable := false for _, o := range q.Options { if seenO[o.ID] { v.errf(e, qid, "duplicate", "duplicate option %q", o.ID) } seenO[o.ID] = true if !o.Support.Valid() { v.errf(e, qid, "support", "option %q: unknown support %q", o.ID, o.Support) } if o.Support.Selectable() { anySelectable = true } else if o.SupportNote == "" { v.errf(e, qid, "support-note", "option %q is %s: supportNote must say why", o.ID, o.Support) } if o.Recommended && o.Reason == "" { v.errf(e, qid, "reason", "recommended option %q needs a reason (EX-03)", o.ID) } v.cond(e, qid, "option "+o.ID+" enabledWhen", o.EnabledWhen, s, i) v.effects(e, qid+"/"+o.ID, o.Effects, s) } if len(q.Options) > 0 && !anySelectable { v.errf(e, qid, "unsatisfiable", "no option of %q can be selected", q.ID) } if q.Default != nil { v.optionValue(e, qid, q, q.Default) } for _, d := range q.DefaultWhen { v.cond(e, qid, "defaultWhen", d.When, s, i) v.optionValue(e, qid, q, d.Value) } v.cond(e, qid, "visibleWhen", q.VisibleWhen, s, i) v.cond(e, qid, "requiredWhen", q.RequiredWhen, s, i) for _, w := range q.ValidateWhen { v.cond(e, qid, "validateWhen", w.When, s, i) } for _, w := range q.Warnings { v.cond(e, qid, "warning", w.When, s, i+1) } if q.Validation.Pattern != "" { if _, err := regexp.Compile(q.Validation.Pattern); err != nil { v.errf(e, qid, "pattern", "validation pattern: %v", err) } } } seenR := map[string]bool{} for _, r := range s.Spec.Rules { rid := id + "#" + r.ID if r.ID == "" || seenR[r.ID] { v.errf(e, rid, "rule-id", "every rule needs a unique id") } seenR[r.ID] = true v.cond(e, rid, "rule when", r.When, s, nq) v.effects(e, rid, r.Effects, s) switch r.Severity { case "", "info", "warning", "error": default: v.errf(e, rid, "severity", "unknown severity %q", r.Severity) } } for i, f := range s.Spec.Config { fid := f.ID if fid == "" { fid = fmt.Sprint(i) } v.fragment(e, id+"#config:"+fid, f, s, nq) if pkg, _, ok := strings.Cut(f.File, "/"); !ok || v.c.Packages[pkg] == nil { v.errf(e, id, "fragment-file", "fragment file %q must start with a package directory", f.File) } } for _, st := range s.Spec.Steps { v.cond(e, id+"#step:"+st.ID, "step", st.When, s, nq) if st.ForEach == "$modules" && st.Package == "" { v.errf(e, id+"#step:"+st.ID, "step", "forEach: $modules needs a package") } else if st.ForEach != "" && st.ForEach != "$modules" { if q, _ := s.Question(st.ForEach); q == nil { v.errf(e, id+"#step:"+st.ID, "question-ref", "forEach names unknown question %q", st.ForEach) } } if st.Package != "" && v.c.Packages[st.Package] == nil { v.errf(e, id+"#step:"+st.ID, "package-ref", "unknown package %q", st.Package) } } for _, ck := range s.Spec.Requirements { v.cond(e, id+"#check:"+ck.ID, "requirement", ck.When, s, nq) } for _, n := range s.Spec.Architecture.Nodes { v.cond(e, id+"#node:"+n.ID, "node", n.When, s, nq) } for _, ed := range s.Spec.Architecture.Edges { v.cond(e, id+"#edge", "edge", ed.When, s, nq) } for _, r := range s.Spec.Related { if v.c.Lookup(r) == nil { v.errf(e, id, "related", "related: unknown solution or task %q", r) } } } func (v *validator) effects(e *Entity, id string, fx Effects, s *Solution) { for _, t := range []*Targets{fx.Requires, fx.Excludes, fx.Conflicts, fx.Implies, fx.Recommends, fx.Discourages} { v.targets(e, id, t, s) } v.caps(e, id, fx.Provides) } func (v *validator) example(x *Example) { e := &x.Entity s, ok := v.c.Solutions[x.Spec.Solution] if !ok { s, ok = v.c.Tasks[x.Spec.Solution] } if !ok { v.errf(e, x.Key(), "solution-ref", "example of unknown solution %q", x.Spec.Solution) return } for k, val := range x.Spec.Parameters { q, _ := s.Question(k) if q == nil { v.errf(e, x.Key(), "question-ref", "unknown parameter %q", k) continue } v.optionValue(e, x.Key(), q, val) } if x.Meta.Title == "" { v.errf(e, x.Key(), "required", "an example needs a title") } } func (v *validator) source(k *KnowledgeSource) { e := &k.Entity if _, ok := v.c.Packages[k.Spec.Package]; !ok { v.errf(e, k.Meta.ID, "package-ref", "unknown package %q", k.Spec.Package) } if k.Spec.Type != "manual" { v.errf(e, k.Meta.ID, "type", "type %q: only manual is supported", k.Spec.Type) } if k.Spec.Format != "markdown" { v.errf(e, k.Meta.ID, "format", "format %q: only markdown is supported", k.Spec.Format) } switch k.Spec.Layout { case "", "core-manual", "docs-tree": default: v.errf(e, k.Meta.ID, "layout", "layout %q: want core-manual or docs-tree", k.Spec.Layout) } if k.Spec.Root != "" && !SafeRelPath(k.Spec.Root) { v.errf(e, k.Meta.ID, "path", "root %q: want a clean relative path", k.Spec.Root) } if !SafeRelPath(k.Spec.Repository) { v.errf(e, k.Meta.ID, "path", "repository %q: want a plain name", k.Spec.Repository) } for _, t := range k.Spec.Topics { v.caps(e, k.Meta.ID+"/"+t.Slug, t.Capabilities) } }