diff --git a/pkg/config/linters_settings.go b/pkg/config/linters_settings.go index f701a64..cc6b15b 100644 --- a/pkg/config/linters_settings.go +++ b/pkg/config/linters_settings.go @@ -5,7 +5,15 @@ import ( "fmt" ) -var defaultLintersSettings = LintersSettings{} +var defaultLintersSettings = LintersSettings{ + OpenAPI: OpenAPISettings{ + EnumFileExcludes: nil, + HAAbsoluteKeysExcludes: map[string]string{ + "modules/150-user-authn/openapi/config-values.yaml": "properties.publishAPI.properties.https", + }, + KeyBannedNames: []string{"x-examples", "examples", "example"}, + }, +} type LintersSettings struct { OpenAPI OpenAPISettings diff --git a/pkg/linters/openapi/library.go b/pkg/linters/openapi/library.go index 6e9590d..f11d4f8 100644 --- a/pkg/linters/openapi/library.go +++ b/pkg/linters/openapi/library.go @@ -7,6 +7,7 @@ import ( "reflect" "strings" + "github.com/deckhouse/d8-lint/pkg/config" "github.com/deckhouse/d8-lint/pkg/linters/openapi/validators" "github.com/hashicorp/go-multierror" @@ -74,7 +75,7 @@ func GetOpenAPIYAMLFiles(rootPath string) ([]string, error) { } // RunOpenAPIValidator runs validator, get channel with file paths and returns channel with results -func RunOpenAPIValidator(fileC chan fileValidation) chan fileValidation { +func RunOpenAPIValidator(fileC chan fileValidation, cfg *config.OpenAPISettings) chan fileValidation { resultC := make(chan fileValidation, 1) go func() { @@ -87,7 +88,7 @@ func RunOpenAPIValidator(fileC chan fileValidation) chan fileValidation { continue } - runFileParser(vfile.filePath, yamlStruct, parseResultC) + runFileParser(vfile.filePath, yamlStruct, cfg, parseResultC) var result *multierror.Error @@ -176,15 +177,15 @@ func isDeckhouseCRD(data map[any]any) bool { return false } -func (fp fileParser) parseForWrongKeys(m map[any]any) { - keysValidator := validators.NewKeyNameValidator() +func (fp fileParser) parseForWrongKeys(m map[any]any, cfg *config.OpenAPISettings) { + keysValidator := validators.NewKeyNameValidator(cfg) err := keysValidator.Run(fp.fileName, "allfile", m) if err != nil { fp.resultC <- err } } -func runFileParser(fileName string, data map[any]any, resultC chan error) { +func runFileParser(fileName string, data map[any]any, cfg *config.OpenAPISettings, resultC chan error) { // exclude external CRDs if isCRD(data) && !isDeckhouseCRD(data) { close(resultC) @@ -194,14 +195,14 @@ func runFileParser(fileName string, data map[any]any, resultC chan error) { parser := fileParser{ fileName: fileName, keyValidators: map[string]validator{ - "enum": validators.NewEnumValidator(), - "highAvailability": validators.NewHAValidator(), - "https": validators.NewHAValidator(), + "enum": validators.NewEnumValidator(cfg), + "highAvailability": validators.NewHAValidator(cfg), + "https": validators.NewHAValidator(cfg), }, resultC: resultC, } if isDeckhouseCRD(data) { - parser.parseForWrongKeys(data) + parser.parseForWrongKeys(data, cfg) } go parser.startParsing(data, resultC) } diff --git a/pkg/linters/openapi/openapi.go b/pkg/linters/openapi/openapi.go index f13183a..d50c20e 100644 --- a/pkg/linters/openapi/openapi.go +++ b/pkg/linters/openapi/openapi.go @@ -23,14 +23,14 @@ func New(cfg *config.OpenAPISettings) *OpenAPI { } } -func (*OpenAPI) Run(_ context.Context, m *module.Module) (errors.LintRuleErrorsList, error) { +func (o *OpenAPI) Run(_ context.Context, m *module.Module) (errors.LintRuleErrorsList, error) { apiFiles, err := GetOpenAPIYAMLFiles(m.Path) if err != nil { return errors.LintRuleErrorsList{}, err } filesC := make(chan fileValidation, len(apiFiles)) - resultC := RunOpenAPIValidator(filesC) + resultC := RunOpenAPIValidator(filesC, o.cfg) for _, apiFile := range apiFiles { filesC <- fileValidation{ @@ -52,7 +52,7 @@ func (*OpenAPI) Run(_ context.Context, m *module.Module) (errors.LintRuleErrorsL } } - return errors.LintRuleErrorsList{}, nil + return result, nil } func (o *OpenAPI) Name() string { diff --git a/pkg/linters/openapi/validators/enum.go b/pkg/linters/openapi/validators/enum.go index a4f7563..8aec629 100644 --- a/pkg/linters/openapi/validators/enum.go +++ b/pkg/linters/openapi/validators/enum.go @@ -7,6 +7,8 @@ import ( "unicode" "github.com/hashicorp/go-multierror" + + "github.com/deckhouse/d8-lint/pkg/config" ) var ( @@ -15,12 +17,13 @@ var ( type EnumValidator struct { key string - excludes map[string]struct{} + excludes map[string][]string } -func NewEnumValidator() EnumValidator { +func NewEnumValidator(cfg *config.OpenAPISettings) EnumValidator { return EnumValidator{ - key: "enum", + key: "enum", + excludes: cfg.EnumFileExcludes, } } diff --git a/pkg/linters/openapi/validators/ha_and_https.go b/pkg/linters/openapi/validators/ha_and_https.go index dcff06b..9d2a34c 100644 --- a/pkg/linters/openapi/validators/ha_and_https.go +++ b/pkg/linters/openapi/validators/ha_and_https.go @@ -4,24 +4,21 @@ import ( "fmt" "reflect" + "github.com/deckhouse/d8-lint/pkg/config" "github.com/deckhouse/d8-lint/pkg/logger" ) -var ( - absoluteKeysExcludes = map[string]string{ - "modules/150-user-authn/openapi/config-values.yaml": "properties.publishAPI.properties.https", - "global-hooks/openapi/config-values.yaml": "properties.modules.properties.https", - } -) - type HAValidator struct { + absoluteKeysExcludes map[string]string } -func NewHAValidator() HAValidator { - return HAValidator{} +func NewHAValidator(cfg *config.OpenAPISettings) HAValidator { + return HAValidator{ + absoluteKeysExcludes: cfg.HAAbsoluteKeysExcludes, + } } -func (HAValidator) Run(file, absoluteKey string, value any) error { +func (ha HAValidator) Run(file, absoluteKey string, value any) error { m := make(map[any]any) rv := reflect.ValueOf(value) if rv.Kind() != reflect.Map { @@ -35,7 +32,7 @@ func (HAValidator) Run(file, absoluteKey string, value any) error { for key := range m { if key == "default" { - if absoluteKeysExcludes[file] == absoluteKey { + if ha.absoluteKeysExcludes[file] == absoluteKey { continue } return fmt.Errorf("%s is invalid: must have no default value", absoluteKey) diff --git a/pkg/linters/openapi/validators/keys_name_check.go b/pkg/linters/openapi/validators/keys_name_check.go index 54cd5a5..0c34e25 100644 --- a/pkg/linters/openapi/validators/keys_name_check.go +++ b/pkg/linters/openapi/validators/keys_name_check.go @@ -4,18 +4,18 @@ import ( "fmt" "reflect" + "github.com/deckhouse/d8-lint/pkg/config" "github.com/deckhouse/d8-lint/pkg/logger" ) -var ( - bannedNames = []string{"x-examples", "examples", "example"} -) - type KeyNameValidator struct { + bannedNames []string } -func NewKeyNameValidator() KeyNameValidator { - return KeyNameValidator{} +func NewKeyNameValidator(cfg *config.OpenAPISettings) KeyNameValidator { + return KeyNameValidator{ + bannedNames: cfg.KeyBannedNames, + } } func checkMapForBannedKey(m map[any]any, banned []string) error { @@ -52,7 +52,7 @@ func checkMapForBannedKey(m map[any]any, banned []string) error { return nil } -func (KeyNameValidator) Run(file, _ string, value any) error { +func (kn KeyNameValidator) Run(file, _ string, value any) error { m := make(map[any]any) rv := reflect.ValueOf(value) if rv.Kind() != reflect.Map { @@ -64,7 +64,7 @@ func (KeyNameValidator) Run(file, _ string, value any) error { m[key.Interface()] = v.Interface() } - err := checkMapForBannedKey(m, bannedNames) + err := checkMapForBannedKey(m, kn.bannedNames) if err != nil { return fmt.Errorf("%s file validation error: wrong property: %w", file, err) }