/
precheck.go
294 lines (250 loc) · 9.05 KB
/
precheck.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package precheck
import (
"context"
"encoding/json"
"fmt"
"regexp"
"strings"
"sync"
"github.com/sirupsen/logrus"
"github.com/erda-project/erda/apistructs"
"github.com/erda-project/erda/modules/pipeline/pipengine/pvolumes"
"github.com/erda-project/erda/modules/pipeline/precheck/checkers/actionchecker/api_register"
"github.com/erda-project/erda/modules/pipeline/precheck/checkers/actionchecker/buildpack"
"github.com/erda-project/erda/modules/pipeline/precheck/checkers/actionchecker/release"
"github.com/erda-project/erda/modules/pipeline/precheck/checkers/diceymlchecker"
"github.com/erda-project/erda/modules/pipeline/precheck/prechecktype"
"github.com/erda-project/erda/pkg/parser/pipelineyml"
"github.com/erda-project/erda/pkg/strutil"
)
var hideFileRegexp, _ = regexp.Compile(`^\/(?:[^\/]+\/)*\.[^\/]*`)
var fileRegexp, _ = regexp.Compile(`^\/(\w+\/?)+$`)
// files: key: fileName, value: fileContent
// return: abort, message
func PreCheck(ctx context.Context, pipelineYmlByte []byte, itemForCheck prechecktype.ItemsForCheck) (globalAbort bool, showMessage apistructs.ShowMessage) {
initialize()
defer func() {
if r := recover(); r != nil {
showMessage.AbortRun = true
}
if !showMessage.AbortRun {
showMessage.AbortRun = globalAbort
}
showMessage.Msg = "Pre Check Detected WARNINGS, please confirm risks before run!"
if globalAbort {
showMessage.Msg = "Pre Check Failed, please fix problems and create pipeline again."
}
}()
// nil check
if itemForCheck.Files == nil {
itemForCheck.Files = make(map[string]string)
}
if itemForCheck.ActionSpecs == nil {
itemForCheck.ActionSpecs = make(map[string]apistructs.ActionSpec)
}
// parse pipelineyml
y, err := pipelineyml.New(pipelineYmlByte,
pipelineyml.WithFlatParams(true), // params: map[string]string
pipelineyml.WithEnvs(itemForCheck.Envs),
)
if err != nil {
showMessage.Stacks = append(showMessage.Stacks, err.Error())
return true, showMessage
}
// use DiceYmlPreChecker
if _, ok := itemForCheck.Files["dice.yml"]; ok {
for _, checker := range diceymlPreCheckers {
abort, messages := checker.Check(ctx, itemForCheck.Files["dice.yml"], itemForCheck)
if abort {
globalAbort = abort
}
showMessage.Stacks = append(showMessage.Stacks, messages...)
}
}
// use ActionPreChecker
availableRefs := pipelineyml.Refs{}
availableOutputs := pipelineyml.Outputs{}
for _, stage := range y.Spec().Stages {
stageRefs := pipelineyml.Refs{}
stageOutputs := pipelineyml.Outputs{}
for _, actions := range stage.Actions {
for _, action := range actions {
// check refs / outputs / secrets
precheckY, err := pipelineyml.New(pipelineYmlByte,
pipelineyml.WithEnvs(itemForCheck.Envs),
pipelineyml.WithAliasesToCheckRefOp(itemForCheck.GlobalSnippetConfigLabels, action.Alias),
pipelineyml.WithRefs(availableRefs),
pipelineyml.WithRefOpOutputs(availableOutputs),
pipelineyml.WithAllowMissingCustomScriptOutputs(true),
pipelineyml.WithSecrets(itemForCheck.Secrets),
)
if err != nil {
globalAbort = true
showMessage.Stacks = append(showMessage.Stacks, err.Error())
}
if precheckY != nil {
for _, warn := range precheckY.Warns() {
showMessage.Stacks = append(showMessage.Stacks, strutil.Concat("[WARN] ", warn))
}
}
// check spec exist
actionSpec := itemForCheck.ActionSpecs[action.GetActionTypeVersion()]
// check required params
checkResults := checkRequiredParams(*action, actionSpec)
if len(checkResults) > 0 {
showMessage.Stacks = append(showMessage.Stacks, checkResults...)
}
if errs := CheckCaches(action, itemForCheck.Labels); errs != nil && len(errs) > 0 {
showMessage.Stacks = append(showMessage.Stacks, errs...)
return true, showMessage
}
// type checker
checker, ok := actionPreCheckerMap[action.Type]
if ok {
abort, warnings := checker.Check(ctx, *action, itemForCheck)
if abort {
globalAbort = true
}
var polishWarnings []string
for _, warning := range warnings {
polishWarnings = append(polishWarnings, fmt.Sprintf("taskName: %s, message: %s", action.Alias, warning))
}
showMessage.Stacks = append(showMessage.Stacks, polishWarnings...)
}
// append to stage available
stageRefs[action.Alias.String()] = fmt.Sprintf("ref(%s)", action.Alias)
for _, ns := range action.Namespaces {
stageRefs[ns] = fmt.Sprintf("ref(%s)", ns)
}
stageOutputs[action.Alias] = make(map[string]string)
for _, output := range actionSpec.Outputs {
key := output.Name
stageOutputs[action.Alias][key] = fmt.Sprintf("value(%s)", key)
}
setActionDynamicOutput(action, stageOutputs)
}
}
// add stage refs/outputs to available for next stage
for alias, ref := range stageRefs {
availableRefs[alias] = ref
}
for alias, kv := range stageOutputs {
availableOutputs[alias] = kv
}
}
return globalAbort, showMessage
}
var diceymlPreCheckers []prechecktype.DiceYmlPreChecker
var actionPreCheckerMap map[pipelineyml.ActionType]prechecktype.ActionPreChecker
var initOnce = &sync.Once{}
func initialize() {
initOnce.Do(func() {
// diceyml prechecker
diceymlPreCheckers = append(diceymlPreCheckers, diceymlchecker.New())
// action prechecker map
actionPreCheckers := []prechecktype.ActionPreChecker{
buildpack.New(),
release.New(),
api_register.New(),
}
actionPreCheckerMap = make(map[pipelineyml.ActionType]prechecktype.ActionPreChecker)
for _, checker := range actionPreCheckers {
actionPreCheckerMap[checker.ActionType()] = checker
}
})
}
func CheckCaches(actualAction *pipelineyml.Action, labels map[string]string) []string {
var checkResults []string
caches := actualAction.Caches
if caches == nil {
return nil
}
if labels == nil {
checkResults = append(checkResults, fmt.Sprintf("checkCaches: secrets is empty"))
return checkResults
}
projectID := labels[apistructs.LabelProjectID]
appID := labels[apistructs.LabelAppID]
for _, v := range caches {
isHideFile := hideFileRegexp.MatchString(v.Path)
isFile := fileRegexp.MatchString(v.Path)
isActionAddr := strings.HasPrefix(v.Path, "${")
if !isFile && !isHideFile && !isActionAddr {
checkResults = append(checkResults, fmt.Sprintf("taskName: %s, cache path: %s error: %s ",
actualAction.Alias, v.Path, " just support / begin or action addr ${actionName}, not support ~/ and relative path"))
}
if projectID == "" && appID == "" && v.Key == "" {
checkResults = append(checkResults, fmt.Sprintf("taskName: %s, cache path: %s error: %s ",
actualAction.Alias, v.Path, "if projectID is empty and appID is empty, key can not be empty"))
continue
}
if v.Key != "" {
v.Key = strings.ReplaceAll(v.Key, " ", "")
if !strings.HasPrefix(v.Key, pvolumes.TaskCachePathBasePath) {
checkResults = append(checkResults, fmt.Sprintf("taskName: %s, cache key: %s error: %s ",
actualAction.Alias, v.Key, "Key should be start with "+pvolumes.TaskCachePathBasePath))
}
if !strings.HasSuffix(v.Key, pvolumes.TaskCachePathEndPath) {
checkResults = append(checkResults, fmt.Sprintf("taskName: %s, cache key: %s error: %s ",
actualAction.Alias, v.Key, "Key should be end with "+pvolumes.TaskCachePathEndPath))
}
}
}
return checkResults
}
func checkRequiredParams(actualAction pipelineyml.Action, actionSpec apistructs.ActionSpec) []string {
checkResults := make([]string, 0)
requiredSpecParams := make(map[string]apistructs.ActionSpecParam)
for i := range actionSpec.Params {
paramSpec := actionSpec.Params[i]
if paramSpec.Required {
requiredSpecParams[paramSpec.Name] = paramSpec
}
}
// travel actualAction.Params
for actualParam := range actualAction.Params {
delete(requiredSpecParams, actualParam)
}
for _, param := range requiredSpecParams {
checkResults = append(checkResults, fmt.Sprintf("taskName: %s, message: missing required param: %s",
actualAction.Alias, param.Name))
}
return checkResults
}
func setActionDynamicOutput(action *pipelineyml.Action, stageOutputs pipelineyml.Outputs) {
switch action.Type {
case "api-test":
params := action.Params
if params == nil {
return
}
outParams := params["out_params"]
if outParams == nil {
return
}
var outs []apistructs.APIOutParam
err := json.Unmarshal([]byte(outParams.(string)), &outs)
if err != nil {
logrus.Errorf("unmarshal api-test out_params error: %v", err)
return
}
if outs == nil {
return
}
for _, v := range outs {
stageOutputs[action.Alias][v.Key] = fmt.Sprintf("value(%s)", v.Key)
}
}
}