|
| 1 | +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 2 | +// or more contributor license agreements. Licensed under the Elastic License; |
| 3 | +// you may not use this file except in compliance with the Elastic License. |
| 4 | + |
| 5 | +package semantic |
| 6 | + |
| 7 | +import ( |
| 8 | + "io/fs" |
| 9 | + "path" |
| 10 | + "sort" |
| 11 | + |
| 12 | + "gopkg.in/yaml.v3" |
| 13 | + |
| 14 | + "github.com/elastic/package-spec/v3/code/go/internal/fspath" |
| 15 | + "github.com/elastic/package-spec/v3/code/go/pkg/specerrors" |
| 16 | +) |
| 17 | + |
| 18 | +type policyTemplateWithCategories struct { |
| 19 | + Name string `yaml:"name"` |
| 20 | + DataStreams []string `yaml:"data_streams"` |
| 21 | + Categories []string `yaml:"categories"` |
| 22 | +} |
| 23 | + |
| 24 | +type packageManifestWithCategories struct { |
| 25 | + Type string `yaml:"type"` |
| 26 | + PolicyTemplates []policyTemplateWithCategories `yaml:"policy_templates"` |
| 27 | +} |
| 28 | + |
| 29 | +type dataStreamManifestWithCategories struct { |
| 30 | + Categories []string `yaml:"categories"` |
| 31 | +} |
| 32 | + |
| 33 | +// ValidatePolicyTemplateDatastreamCategories validates that when a policy template |
| 34 | +// entry in the package manifest.yml defines categories, those categories match the |
| 35 | +// categories defined in the manifest.yml of each referenced data stream. |
| 36 | +// Data stream manifests without a categories field are skipped. |
| 37 | +func ValidatePolicyTemplateDatastreamCategories(fsys fspath.FS) specerrors.ValidationErrors { |
| 38 | + var errs specerrors.ValidationErrors |
| 39 | + |
| 40 | + manifestPath := "manifest.yml" |
| 41 | + data, err := fs.ReadFile(fsys, manifestPath) |
| 42 | + if err != nil { |
| 43 | + return specerrors.ValidationErrors{ |
| 44 | + specerrors.NewStructuredErrorf("file \"%s\" is invalid: %w", fsys.Path(manifestPath), errFailedToReadManifest)} |
| 45 | + } |
| 46 | + |
| 47 | + var manifest packageManifestWithCategories |
| 48 | + if err := yaml.Unmarshal(data, &manifest); err != nil { |
| 49 | + return specerrors.ValidationErrors{ |
| 50 | + specerrors.NewStructuredErrorf("file \"%s\" is invalid: %w", fsys.Path(manifestPath), errFailedToParseManifest)} |
| 51 | + } |
| 52 | + |
| 53 | + // only validate integration type packages |
| 54 | + if manifest.Type != packageTypeIntegration { |
| 55 | + return nil |
| 56 | + } |
| 57 | + |
| 58 | + dsCategories, err := readDataStreamManifestCategories(fsys) |
| 59 | + if err != nil { |
| 60 | + return specerrors.ValidationErrors{ |
| 61 | + specerrors.NewStructuredErrorf("file \"%s\" is invalid: %w", fsys.Path(manifestPath), err)} |
| 62 | + } |
| 63 | + |
| 64 | + for _, pt := range manifest.PolicyTemplates { |
| 65 | + // skip policy templates that don't define both categories and data_streams |
| 66 | + if len(pt.Categories) == 0 || len(pt.DataStreams) == 0 { |
| 67 | + continue |
| 68 | + } |
| 69 | + |
| 70 | + for _, dsName := range pt.DataStreams { |
| 71 | + dsCats, hasCats := dsCategories[dsName] |
| 72 | + if !hasCats { |
| 73 | + // data stream manifest has no categories field — nothing to validate |
| 74 | + continue |
| 75 | + } |
| 76 | + |
| 77 | + if !categoriesEqual(pt.Categories, dsCats) { |
| 78 | + dsManifestPath := path.Join("data_stream", dsName, "manifest.yml") |
| 79 | + errs = append(errs, specerrors.NewStructuredErrorf( |
| 80 | + "file \"%s\" is invalid: policy template \"%s\" categories %v do not match data stream \"%s\" manifest categories %v (defined in \"%s\")", |
| 81 | + fsys.Path(manifestPath), |
| 82 | + pt.Name, |
| 83 | + pt.Categories, |
| 84 | + dsName, |
| 85 | + dsCats, |
| 86 | + fsys.Path(dsManifestPath), |
| 87 | + )) |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return errs |
| 93 | +} |
| 94 | + |
| 95 | +// readDataStreamManifestCategories reads the categories field from every |
| 96 | +// data_stream/*/manifest.yml and returns a map of data stream name to its categories. |
| 97 | +// Data streams without a categories field are omitted from the map. |
| 98 | +func readDataStreamManifestCategories(fsys fspath.FS) (map[string][]string, error) { |
| 99 | + result := make(map[string][]string) |
| 100 | + |
| 101 | + manifests, err := fs.Glob(fsys, "data_stream/*/manifest.yml") |
| 102 | + if err != nil { |
| 103 | + return nil, err |
| 104 | + } |
| 105 | + |
| 106 | + for _, file := range manifests { |
| 107 | + data, err := fs.ReadFile(fsys, file) |
| 108 | + if err != nil { |
| 109 | + return nil, err |
| 110 | + } |
| 111 | + |
| 112 | + var m dataStreamManifestWithCategories |
| 113 | + if err := yaml.Unmarshal(data, &m); err != nil { |
| 114 | + return nil, err |
| 115 | + } |
| 116 | + |
| 117 | + if len(m.Categories) == 0 { |
| 118 | + continue |
| 119 | + } |
| 120 | + |
| 121 | + // path is data_stream/{name}/manifest.yml — extract the name component |
| 122 | + dsName := path.Base(path.Dir(file)) |
| 123 | + result[dsName] = m.Categories |
| 124 | + } |
| 125 | + |
| 126 | + return result, nil |
| 127 | +} |
| 128 | + |
| 129 | +// categoriesEqual returns true if both slices contain exactly the same set of |
| 130 | +// categories, regardless of order. |
| 131 | +func categoriesEqual(a, b []string) bool { |
| 132 | + if len(a) != len(b) { |
| 133 | + return false |
| 134 | + } |
| 135 | + aCopy := make([]string, len(a)) |
| 136 | + bCopy := make([]string, len(b)) |
| 137 | + copy(aCopy, a) |
| 138 | + copy(bCopy, b) |
| 139 | + sort.Strings(aCopy) |
| 140 | + sort.Strings(bCopy) |
| 141 | + for i := range aCopy { |
| 142 | + if aCopy[i] != bCopy[i] { |
| 143 | + return false |
| 144 | + } |
| 145 | + } |
| 146 | + return true |
| 147 | +} |
0 commit comments