forked from snyk/vervet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spec.go
168 lines (157 loc) · 4.52 KB
/
spec.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package vervet
import (
"fmt"
"io/fs"
"os"
"path/filepath"
"sort"
"time"
"github.com/bmatcuk/doublestar/v4"
"github.com/getkin/kin-openapi/openapi3"
)
// SpecGlobPattern defines the expected directory structure for the versioned
// OpenAPI specs of a single resource: subdirectories by date, of the form
// YYYY-mm-dd, each containing a spec.yaml file.
const SpecGlobPattern = "**/[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]/spec.yaml"
// SpecVersions defines an OpenAPI specification consisting of one or more
// versioned resources.
type SpecVersions struct {
resources []*ResourceVersions
}
// LoadSpecVersions returns SpecVersions loaded from a directory structure
// containing one or more Resource subdirectories.
func LoadSpecVersions(root string) (*SpecVersions, error) {
epPaths, err := findResources(root)
if err != nil {
return nil, err
}
return LoadSpecVersionsFileset(epPaths)
}
// LoadSpecVersionsFileset returns SpecVersions loaded from a set of spec
// files.
func LoadSpecVersionsFileset(epPaths []string) (*SpecVersions, error) {
resourceMap := map[string][]string{}
for i := range epPaths {
resourcePath := filepath.Dir(filepath.Dir(epPaths[i]))
if resourcePath == "." {
continue
}
resourceMap[resourcePath] = append(resourceMap[resourcePath], epPaths[i])
}
var resourceNames []string
for k := range resourceMap {
resourceNames = append(resourceNames, k)
}
sort.Strings(resourceNames)
svs := &SpecVersions{}
for _, resourcePath := range resourceNames {
specFiles := resourceMap[resourcePath]
eps, err := LoadResourceVersionsFileset(specFiles)
if err != nil {
return nil, fmt.Errorf("failed to load resource at %q: %w", resourcePath, err)
}
svs.resources = append(svs.resources, eps)
}
if err := svs.Validate(); err != nil {
return nil, err
}
return svs, nil
}
// Validate returns an error if there are conflicting resources at a spec version.
func (s *SpecVersions) Validate() error {
for _, v := range s.Versions() {
resourcePaths := map[string]string{}
for _, eps := range s.resources {
ep, err := eps.At(v.String())
if err == ErrNoMatchingVersion {
continue
} else if err != nil {
return fmt.Errorf("validation failed: %w", err)
}
for path := range ep.Paths {
if conflict, ok := resourcePaths[path]; ok {
return fmt.Errorf("conflict: %q %q", conflict, ep.sourcePrefix)
}
resourcePaths[path] = ep.sourcePrefix
}
}
}
return nil
}
// Resources returns a slice of each Resource contained in the spec.
func (s *SpecVersions) Resources() []*ResourceVersions {
return s.resources
}
// Versions returns a slice containing each Version defined by an Resource in
// this specification. Versions are sorted in ascending order.
func (s *SpecVersions) Versions() []*Version {
vset := map[Version]bool{}
for _, eps := range s.resources {
for i := range eps.versions {
vset[*eps.versions[i].Version] = true
}
}
versions := make([]*Version, len(vset))
i := 0
for k := range vset {
v := k
versions[i] = &v
i++
}
sort.Sort(versionSlice(versions))
return versions
}
// At returns the OpenAPI document matching a version string.
func (s *SpecVersions) At(vs string) (*openapi3.T, error) {
if vs == "" {
vs = time.Now().UTC().Format("2006-01-02")
}
v, err := ParseVersion(vs)
if err != nil {
return nil, err
}
var result *openapi3.T
for _, eps := range s.resources {
ep, err := eps.At(v.String())
if err == ErrNoMatchingVersion {
continue
} else if err != nil {
return nil, err
}
if result == nil {
// Assign a clean copy of the contents of the first resource to the
// resulting spec. Marshaling is used to ensure that references in
// the source resource are dropped from the result, which could be
// modified on subsequent merges.
buf, err := ep.T.MarshalJSON()
if err != nil {
return nil, err
}
result = &openapi3.T{}
err = result.UnmarshalJSON(buf)
if err != nil {
return nil, err
}
}
Merge(result, ep.T, false)
}
if result == nil {
return nil, ErrNoMatchingVersion
}
// Remove the API stability extension from the merged OpenAPI spec, this
// extension is only applicable to individual resource version specs.
delete(result.ExtensionProps.Extensions, ExtSnykApiStability)
return result, nil
}
func findResources(root string) ([]string, error) {
var paths []string
err := doublestar.GlobWalk(os.DirFS(root), SpecGlobPattern,
func(path string, d fs.DirEntry) error {
paths = append(paths, filepath.Join(root, path))
return nil
})
if err != nil {
return nil, err
}
return paths, err
}