-
-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathvalidate_component.go
More file actions
353 lines (302 loc) · 10.5 KB
/
Copy pathvalidate_component.go
File metadata and controls
353 lines (302 loc) · 10.5 KB
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package exec
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/go-viper/mapstructure/v2"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
errUtils "github.com/cloudposse/atmos/errors"
cfg "github.com/cloudposse/atmos/pkg/config"
"github.com/cloudposse/atmos/pkg/env"
log "github.com/cloudposse/atmos/pkg/logger"
"github.com/cloudposse/atmos/pkg/perf"
"github.com/cloudposse/atmos/pkg/schema"
"github.com/cloudposse/atmos/pkg/ui/spinner"
u "github.com/cloudposse/atmos/pkg/utils"
)
// getBasePathToUse returns the appropriate base path for file resolution.
// It prefers BasePathAbsolute for correct resolution when base_path is relative
// (e.g., when using ATMOS_CLI_CONFIG_PATH), falling back to BasePath for backward compatibility.
func getBasePathToUse(atmosConfig *schema.AtmosConfiguration) string {
if atmosConfig.BasePathAbsolute != "" {
return atmosConfig.BasePathAbsolute
}
return atmosConfig.BasePath
}
// getWorkflowsDirToUse returns the appropriate workflows directory for file resolution. It
// prefers the precomputed WorkflowsDirAbsolutePath (set by AtmosConfigAbsolutePaths, the same
// mechanism cloudposse/atmos#2864 uses for the top-level base_path), falling back to joining
// the raw BasePath/Workflows.BasePath for callers that construct an AtmosConfiguration by hand
// without running it through AtmosConfigAbsolutePaths first (e.g. tests).
func getWorkflowsDirToUse(atmosConfig *schema.AtmosConfiguration) string {
if atmosConfig.WorkflowsDirAbsolutePath != "" {
return atmosConfig.WorkflowsDirAbsolutePath
}
// u.JoinPath (unlike filepath.Join) returns an already-absolute Workflows.BasePath as-is
// instead of nesting it under BasePath.
return u.JoinPath(atmosConfig.BasePath, atmosConfig.Workflows.BasePath)
}
// enableProvenanceForRichOutput sets atmosConfig.TrackProvenance when the
// resolved output format is "rich". Provenance is enabled only for the rich
// invocation: it lets the command map JSON Schema fields back to the
// effective stack value without changing normal component-validation cost
// or behavior for other formats.
func enableProvenanceForRichOutput(atmosConfig *schema.AtmosConfiguration, outputFormat string) {
if strings.EqualFold(strings.TrimSpace(outputFormat), "rich") {
atmosConfig.TrackProvenance = true
}
}
// validateComponentFlags holds the `validate component` flags
// ExecuteValidateComponentCmd needs to run validation.
type validateComponentFlags struct {
stack string
schemaPath string
schemaType string
modulePaths []string
timeout int
}
// readValidateComponentFlags reads the flags ExecuteValidateComponentCmd needs
// from the command's flag set.
func readValidateComponentFlags(flags *pflag.FlagSet) (validateComponentFlags, error) {
var f validateComponentFlags
var err error
if f.stack, err = flags.GetString("stack"); err != nil {
return f, err
}
if f.schemaPath, err = flags.GetString("schema-path"); err != nil {
return f, err
}
if f.schemaType, err = flags.GetString("schema-type"); err != nil {
return f, err
}
if f.modulePaths, err = flags.GetStringSlice("module-paths"); err != nil {
return f, err
}
if f.timeout, err = flags.GetInt("timeout"); err != nil {
return f, err
}
return f, nil
}
// ExecuteValidateComponentCmd executes `validate component` command. The
// outputFormat argument is the already-resolved config -> env var -> CLI
// flag precedence value that the caller computes via its normal format
// selection, used to enable provenance tracking for "rich" output
// regardless of which source supplied it.
func ExecuteValidateComponentCmd(cmd *cobra.Command, args []string, outputFormat string) (string, string, error) {
defer perf.Track(nil, "exec.ExecuteValidateComponentCmd")()
info, err := ProcessCommandLineArgs("", cmd, args, nil)
if err != nil {
return "", "", err
}
atmosConfig, err := cfg.InitCliConfig(info, true)
if err != nil {
return "", "", err
}
enableProvenanceForRichOutput(&atmosConfig, outputFormat)
if len(args) != 1 {
return "", "", errUtils.ErrInvalidComponentArgument
}
componentName := args[0]
// Initialize spinner.
s := spinner.New(fmt.Sprintf("Validating Atmos Component: %s", componentName))
s.Start()
flags, err := readValidateComponentFlags(cmd.Flags())
if err != nil {
s.Stop()
return "", "", err
}
_, err = ExecuteValidateComponent(&atmosConfig, info, componentName, flags.stack, flags.schemaPath, flags.schemaType, flags.modulePaths, flags.timeout)
if err != nil {
s.Error("Component validation failed")
return "", "", err
}
s.Success("Component validated successfully")
log.Debug("Component validation completed", "component", componentName, "stack", flags.stack)
return componentName, flags.stack, nil
}
// ExecuteValidateComponent validates a component in a stack using JsonSchema or OPA schema documents.
func ExecuteValidateComponent(
atmosConfig *schema.AtmosConfiguration,
configAndStacksInfo schema.ConfigAndStacksInfo,
componentName string,
stack string,
schemaPath string,
schemaType string,
modulePaths []string,
timeoutSeconds int,
) (bool, error) {
defer perf.Track(atmosConfig, "exec.ExecuteValidateComponent")()
configAndStacksInfo.ComponentFromArg = componentName
configAndStacksInfo.Stack = stack
configAndStacksInfo.ComponentType = cfg.TerraformComponentType
configAndStacksInfo, err := ProcessStacks(atmosConfig, configAndStacksInfo, true, true, true, nil, nil)
if err != nil {
configAndStacksInfo.ComponentType = cfg.HelmfileComponentType
configAndStacksInfo, err = ProcessStacks(atmosConfig, configAndStacksInfo, true, true, true, nil, nil)
if err != nil {
configAndStacksInfo.ComponentType = cfg.PackerComponentType
configAndStacksInfo, err = ProcessStacks(atmosConfig, configAndStacksInfo, true, true, true, nil, nil)
if err != nil {
return false, err
}
}
}
componentSection := configAndStacksInfo.ComponentSection
return ValidateComponent(atmosConfig, componentName, componentSection, schemaPath, schemaType, modulePaths, timeoutSeconds)
}
// ValidateComponent validates the component config using JsonSchema or OPA schema documents.
func ValidateComponent(
atmosConfig *schema.AtmosConfiguration,
componentName string,
componentSection map[string]any,
schemaPath string,
schemaType string,
modulePaths []string,
timeoutSeconds int,
) (bool, error) {
defer perf.Track(atmosConfig, "exec.ValidateComponent")()
ok := true
var err error
if schemaPath != "" && schemaType != "" {
log.Debug("Validating", "component", componentName, "schema", schemaType, "file", schemaPath)
ok, err = validateComponentInternal(atmosConfig, componentSection, schemaPath, schemaType, modulePaths, timeoutSeconds)
if err != nil {
return false, err
}
} else {
validations, err := FindValidationSection(componentSection)
if err != nil {
return false, err
}
for _, v := range validations {
if v.Disabled {
continue
}
// Command line parameters override the validation config in YAML
var finalSchemaPath string
var finalSchemaType string
var finalModulePaths []string
var finalTimeoutSeconds int
if schemaPath != "" {
finalSchemaPath = schemaPath
} else {
finalSchemaPath = v.SchemaPath
}
if schemaType != "" {
finalSchemaType = schemaType
} else {
finalSchemaType = v.SchemaType
}
if len(modulePaths) > 0 {
finalModulePaths = modulePaths
} else {
finalModulePaths = v.ModulePaths
}
if timeoutSeconds > 0 {
finalTimeoutSeconds = timeoutSeconds
} else {
finalTimeoutSeconds = v.Timeout
}
log.Debug("Validating", "component", componentName, "schema", finalSchemaType, "file", finalSchemaPath)
if v.Description != "" {
log.Debug(v.Description)
}
ok2, err := validateComponentInternal(atmosConfig, componentSection, finalSchemaPath, finalSchemaType, finalModulePaths, finalTimeoutSeconds)
if err != nil {
return false, err
}
if !ok2 {
ok = false
}
}
}
return ok, nil
}
func validateComponentInternal(
atmosConfig *schema.AtmosConfiguration,
componentSection map[string]any,
schemaPath string,
schemaType string,
modulePaths []string,
timeoutSeconds int,
) (bool, error) {
if schemaType != "jsonschema" && schemaType != "opa" {
return false, fmt.Errorf("invalid schema type '%s'. Supported types: jsonschema, opa", schemaType)
}
// Check if the file pointed to by 'schemaPath' exists.
// If not, join it with the schemas `base_path` from the CLI config.
var filePath string
if u.FileExists(schemaPath) {
filePath = schemaPath
} else {
basePathToUse := getBasePathToUse(atmosConfig)
switch schemaType {
case "jsonschema":
{
filePath = filepath.Join(basePathToUse, atmosConfig.GetResourcePath("jsonschema").BasePath, schemaPath)
}
case "opa":
{
filePath = filepath.Join(basePathToUse, atmosConfig.GetResourcePath("opa").BasePath, schemaPath)
}
}
if !u.FileExists(filePath) {
return false, fmt.Errorf("the file '%s' does not exist for schema type '%s'", schemaPath, schemaType)
}
}
fileContent, err := os.ReadFile(filePath)
if err != nil {
return false, err
}
schemaText := string(fileContent)
var ok bool
// Add the process environment variables to the component section.
componentSection[cfg.ProcessEnvSectionName] = env.EnvironToMap()
switch schemaType {
case "jsonschema":
{
ok, err = ValidateWithJsonSchema(componentSection, filePath, schemaText)
if err != nil {
return false, err
}
}
case "opa":
{
basePathToUse := getBasePathToUse(atmosConfig)
modulePathsAbsolute, err := u.JoinPaths(filepath.Join(basePathToUse, atmosConfig.GetResourcePath("opa").BasePath), modulePaths)
if err != nil {
return false, err
}
ok, err = ValidateWithOpa(componentSection, filePath, modulePathsAbsolute, timeoutSeconds)
if err != nil {
return false, err
}
}
case "opa_legacy":
{
ok, err = ValidateWithOpaLegacy(componentSection, filePath, schemaText, timeoutSeconds)
if err != nil {
return false, err
}
}
}
return ok, nil
}
// FindValidationSection finds the 'validation' section in the component config.
func FindValidationSection(componentSection map[string]any) (schema.Validation, error) {
defer perf.Track(nil, "exec.FindValidationSection")()
validationSection := map[string]any{}
if i, ok := componentSection["settings"].(map[string]any); ok {
if i2, ok2 := i["validation"].(map[string]any); ok2 {
validationSection = i2
}
}
var result schema.Validation
err := mapstructure.Decode(validationSection, &result)
if err != nil {
return nil, err
}
return result, nil
}