-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
remote_ruleset.go
187 lines (158 loc) · 4.66 KB
/
remote_ruleset.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
package rulesets
import (
"context"
"fmt"
"github.com/daveshanley/vacuum/model"
"io"
"net/http"
"os"
"path/filepath"
"slices"
"strings"
)
// CheckForRemoteExtends checks if the extends map contains a remote link
// returns true if it does, false if it does not
func CheckForRemoteExtends(extends map[string]string) bool {
for k := range extends {
if strings.HasPrefix(k, "http") {
return true
}
}
return false
}
// CheckForLocalExtends checks if the extends map contains a local link
// returns true if it does, false if it does not
func CheckForLocalExtends(extends map[string]string) bool {
for k := range extends {
if filepath.Ext(k) == ".yml" ||
filepath.Ext(k) == ".yaml" ||
filepath.Ext(k) == ".json" {
return true
}
}
return false
}
// DownloadRemoteRuleSet downloads a remote ruleset and returns a *RuleSet
// returns an error if it cannot download the ruleset
func DownloadRemoteRuleSet(_ context.Context, location string) (*RuleSet, error) {
if location == "" {
return nil, fmt.Errorf("cannot download ruleset, location is empty")
}
ruleResp, ruleRemoteErr := http.Get(location)
if ruleRemoteErr != nil {
return nil, ruleRemoteErr
}
ruleBytes, bytesErr := io.ReadAll(ruleResp.Body)
if bytesErr != nil {
return nil, bytesErr
}
if len(ruleBytes) <= 0 {
return nil, fmt.Errorf("remote ruleset '%s' is empty, cannot extend", location)
}
downloadedRS, rsErr := CreateRuleSetFromData(ruleBytes)
if rsErr != nil {
return nil, rsErr
}
return downloadedRS, nil
}
// LoadLocalRuleSet loads a local ruleset and returns a *RuleSet
// returns an error if it cannot load the ruleset
func LoadLocalRuleSet(_ context.Context, location string) (*RuleSet, error) {
if location == "" {
return nil, fmt.Errorf("cannot load ruleset, location is empty")
}
ruleBytes, bytesErr := os.ReadFile(location)
if bytesErr != nil {
return nil, bytesErr
}
if len(ruleBytes) <= 0 {
return nil, fmt.Errorf("local ruleset '%s' is empty, cannot extend", location)
}
downloadedRS, rsErr := CreateRuleSetFromData(ruleBytes)
if rsErr != nil {
return nil, rsErr
}
return downloadedRS, nil
}
// SniffOutAllExternalRules takes a ruleset and sniffs out all external rules
// it will recursively sniff out all external rulesets and add them to the ruleset
// it will return an error if it cannot sniff out the ruleset
func SniffOutAllExternalRules(
ctx context.Context,
rsm *ruleSetsModel,
location string,
visited []string,
rs *RuleSet,
remote bool) {
var drs *RuleSet
var err error
if remote {
drs, err = DownloadRemoteRuleSet(ctx, location)
} else {
drs, err = LoadLocalRuleSet(ctx, location)
}
if err != nil {
rsm.logger.Error("cannot open external ruleset",
"location", location, "error", err.Error())
return
}
// iterate over the remote ruleset and add the rules in
for ruleName, ruleValue := range drs.Rules {
rs.Rules[ruleName] = ruleValue
}
for ruleName, ruleValue := range drs.RuleDefinitions {
rs.RuleDefinitions[ruleName] = ruleValue
}
visited = append(visited, location)
// iterate over the extends and extract everything
extends := drs.GetExtendsValue()
// default and explicitly recommended
if extends[SpectralOpenAPI] == SpectralRecommended || extends[SpectralOpenAPI] == SpectralOpenAPI {
// suck in all recommended rules
recommended := rsm.GenerateOpenAPIRecommendedRuleSet()
for k, v := range recommended.Rules {
rs.Rules[k] = v
}
for k, v := range recommended.RuleDefinitions {
rs.RuleDefinitions[k] = v
}
}
// all rules
if extends[SpectralOpenAPI] == SpectralAll {
// suck in all rules
allRules := rsm.openAPIRuleSet
for k, v := range allRules.Rules {
rs.Rules[k] = v
}
for k, v := range allRules.RuleDefinitions {
rs.RuleDefinitions[k] = v
}
}
// no rules!
if extends[SpectralOpenAPI] == SpectralOff {
if rs.DocumentationURI == "" {
rs.DocumentationURI = "https://quobix.com/vacuum/rulesets/no-rules"
}
rs.Rules = make(map[string]*model.Rule)
rs.Description = fmt.Sprintf("All disabled ruleset, processing %d supplied rules", len(rs.RuleDefinitions))
}
// do we have extensions?
if CheckForRemoteExtends(extends) || CheckForLocalExtends(extends) {
for k := range extends {
if strings.HasPrefix(k, "http") ||
filepath.Ext(k) == ".yml" ||
filepath.Ext(k) == ".yaml" ||
filepath.Ext(k) == ".json" {
if slices.Contains(visited, k) {
rsm.logger.Warn("ruleset links to its self, circular rulesets are not permitted",
"extends", k)
return
}
// do down the rabbit hole.
SniffOutAllExternalRules(ctx, rsm, k, visited, rs, remote)
}
}
}
}