woodpecker/pipeline/frontend/yaml/linter/linter.go

264 lines
7.1 KiB
Go
Raw Normal View History

// Copyright 2023 Woodpecker Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2017-03-05 07:56:08 +00:00
package linter
import (
"fmt"
2023-11-04 14:30:47 +00:00
"codeberg.org/6543/xyaml"
"go.uber.org/multierr"
2017-03-05 07:56:08 +00:00
"go.woodpecker-ci.org/woodpecker/v2/pipeline/errors"
"go.woodpecker-ci.org/woodpecker/v2/pipeline/frontend/yaml/linter/schema"
"go.woodpecker-ci.org/woodpecker/v2/pipeline/frontend/yaml/types"
2017-07-21 21:52:52 +00:00
)
2017-03-05 07:56:08 +00:00
// A Linter lints a pipeline configuration.
type Linter struct {
trusted bool
}
// New creates a new Linter with options.
func New(opts ...Option) *Linter {
linter := new(Linter)
for _, opt := range opts {
opt(linter)
}
return linter
}
2023-11-04 14:30:47 +00:00
type WorkflowConfig struct {
// File is the path to the configuration file.
File string
// RawConfig is the raw configuration.
RawConfig string
// Config is the parsed configuration.
Workflow *types.Workflow
}
2017-03-05 07:56:08 +00:00
// Lint lints the configuration.
2023-11-04 14:30:47 +00:00
func (l *Linter) Lint(configs []*WorkflowConfig) error {
var linterErr error
2023-11-04 14:30:47 +00:00
for _, config := range configs {
if err := l.lintFile(config); err != nil {
linterErr = multierr.Append(linterErr, err)
}
}
2023-11-04 14:30:47 +00:00
return linterErr
}
func (l *Linter) lintFile(config *WorkflowConfig) error {
var linterErr error
if len(config.Workflow.Steps.ContainerList) == 0 {
linterErr = multierr.Append(linterErr, newLinterError("Invalid or missing steps section", config.File, "steps", false))
}
if err := l.lintContainers(config, "clone"); err != nil {
linterErr = multierr.Append(linterErr, err)
2017-07-21 21:52:52 +00:00
}
2023-11-04 14:30:47 +00:00
if err := l.lintContainers(config, "steps"); err != nil {
linterErr = multierr.Append(linterErr, err)
2017-07-21 21:52:52 +00:00
}
2023-11-04 14:30:47 +00:00
if err := l.lintContainers(config, "services"); err != nil {
linterErr = multierr.Append(linterErr, err)
2017-07-21 21:52:52 +00:00
}
2023-11-04 14:30:47 +00:00
if err := l.lintSchema(config); err != nil {
linterErr = multierr.Append(linterErr, err)
}
2023-11-04 14:30:47 +00:00
if err := l.lintDeprecations(config); err != nil {
linterErr = multierr.Append(linterErr, err)
}
2023-11-04 14:30:47 +00:00
if err := l.lintBadHabits(config); err != nil {
linterErr = multierr.Append(linterErr, err)
}
return linterErr
2017-07-21 21:52:52 +00:00
}
2017-03-05 07:56:08 +00:00
2023-11-04 14:30:47 +00:00
func (l *Linter) lintContainers(config *WorkflowConfig, area string) error {
var linterErr error
2023-11-04 14:30:47 +00:00
var containers []*types.Container
switch area {
case "clone":
containers = config.Workflow.Clone.ContainerList
case "steps":
containers = config.Workflow.Steps.ContainerList
case "services":
containers = config.Workflow.Services.ContainerList
}
2017-03-05 07:56:08 +00:00
for _, container := range containers {
2023-11-04 14:30:47 +00:00
if err := l.lintImage(config, container, area); err != nil {
linterErr = multierr.Append(linterErr, err)
2017-03-05 07:56:08 +00:00
}
if !l.trusted {
2023-11-04 14:30:47 +00:00
if err := l.lintTrusted(config, container, area); err != nil {
linterErr = multierr.Append(linterErr, err)
2017-03-05 07:56:08 +00:00
}
}
2023-11-04 14:30:47 +00:00
if err := l.lintCommands(config, container, area); err != nil {
linterErr = multierr.Append(linterErr, err)
2017-07-21 21:52:52 +00:00
}
2017-03-05 07:56:08 +00:00
}
return linterErr
2017-03-05 07:56:08 +00:00
}
2023-11-04 14:30:47 +00:00
func (l *Linter) lintImage(config *WorkflowConfig, c *types.Container, area string) error {
2017-03-05 07:56:08 +00:00
if len(c.Image) == 0 {
2023-11-04 14:30:47 +00:00
return newLinterError("Invalid or missing image", config.File, fmt.Sprintf("%s.%s", area, c.Name), false)
2017-03-05 07:56:08 +00:00
}
return nil
}
2023-11-04 14:30:47 +00:00
func (l *Linter) lintCommands(config *WorkflowConfig, c *types.Container, field string) error {
2017-07-21 21:52:52 +00:00
if len(c.Commands) == 0 {
return nil
}
if len(c.Settings) != 0 {
2017-07-21 21:52:52 +00:00
var keys []string
for key := range c.Settings {
2017-07-21 21:52:52 +00:00
keys = append(keys, key)
}
2023-11-04 14:30:47 +00:00
return newLinterError(fmt.Sprintf("Cannot configure both commands and custom attributes %v", keys), config.File, fmt.Sprintf("%s.%s", field, c.Name), false)
2017-07-21 21:52:52 +00:00
}
2017-03-05 07:56:08 +00:00
return nil
}
2023-11-04 14:30:47 +00:00
func (l *Linter) lintTrusted(config *WorkflowConfig, c *types.Container, area string) error {
yamlPath := fmt.Sprintf("%s.%s", area, c.Name)
err := ""
2017-03-05 07:56:08 +00:00
if c.Privileged {
2023-11-04 14:30:47 +00:00
err = "Insufficient privileges to use privileged mode"
2017-03-05 07:56:08 +00:00
}
if c.ShmSize != 0 {
2023-11-04 14:30:47 +00:00
err = "Insufficient privileges to override shm_size"
2017-03-05 07:56:08 +00:00
}
if len(c.DNS) != 0 {
2023-11-04 14:30:47 +00:00
err = "Insufficient privileges to use custom dns"
2017-03-05 07:56:08 +00:00
}
if len(c.DNSSearch) != 0 {
2023-11-04 14:30:47 +00:00
err = "Insufficient privileges to use dns_search"
2017-03-05 07:56:08 +00:00
}
if len(c.Devices) != 0 {
2023-11-04 14:30:47 +00:00
err = "Insufficient privileges to use devices"
2017-03-05 07:56:08 +00:00
}
if len(c.ExtraHosts) != 0 {
2023-11-04 14:30:47 +00:00
err = "Insufficient privileges to use extra_hosts"
2017-03-05 07:56:08 +00:00
}
if len(c.NetworkMode) != 0 {
2023-11-04 14:30:47 +00:00
err = "Insufficient privileges to use network_mode"
2017-03-05 07:56:08 +00:00
}
2017-09-08 00:43:33 +00:00
if len(c.IpcMode) != 0 {
2023-11-04 14:30:47 +00:00
err = "Insufficient privileges to use ipc_mode"
2017-09-08 00:43:33 +00:00
}
2017-11-17 22:49:01 +00:00
if len(c.Sysctls) != 0 {
2023-11-04 14:30:47 +00:00
err = "Insufficient privileges to use sysctls"
2017-11-17 22:49:01 +00:00
}
2017-03-05 07:56:08 +00:00
if c.Networks.Networks != nil && len(c.Networks.Networks) != 0 {
2023-11-04 14:30:47 +00:00
err = "Insufficient privileges to use networks"
2017-03-05 07:56:08 +00:00
}
if c.Volumes.Volumes != nil && len(c.Volumes.Volumes) != 0 {
2023-11-04 14:30:47 +00:00
err = "Insufficient privileges to use volumes"
2017-03-05 07:56:08 +00:00
}
2017-09-08 00:43:33 +00:00
if len(c.Tmpfs) != 0 {
2023-11-04 14:30:47 +00:00
err = "Insufficient privileges to use tmpfs"
}
if len(err) != 0 {
return newLinterError(err, config.File, yamlPath, false)
}
2023-11-04 14:30:47 +00:00
return nil
}
2023-11-04 14:30:47 +00:00
func (l *Linter) lintSchema(config *WorkflowConfig) error {
var linterErr error
2023-11-04 14:30:47 +00:00
schemaErrors, err := schema.LintString(config.RawConfig)
if err != nil {
for _, schemaError := range schemaErrors {
linterErr = multierr.Append(linterErr, newLinterError(
schemaError.Description(),
2023-11-04 14:30:47 +00:00
config.File,
schemaError.Field(),
true, // TODO: let pipelines fail if the schema is invalid
))
}
2017-09-08 00:43:33 +00:00
}
return linterErr
}
2023-11-04 14:30:47 +00:00
func (l *Linter) lintDeprecations(config *WorkflowConfig) (err error) {
parsed := new(types.Workflow)
err = xyaml.Unmarshal([]byte(config.RawConfig), parsed)
if err != nil {
return err
}
if parsed.PipelineDontUseIt.ContainerList != nil {
err = multierr.Append(err, &errors.PipelineError{
Type: errors.PipelineErrorTypeDeprecation,
Message: "Please use 'steps:' instead of deprecated 'pipeline:' list",
Data: errors.DeprecationErrorData{
File: config.File,
Field: "pipeline",
Docs: "https://woodpecker-ci.org/docs/next/migrations#next-200",
},
IsWarning: true,
})
}
if parsed.PlatformDontUseIt != "" {
err = multierr.Append(err, &errors.PipelineError{
Type: errors.PipelineErrorTypeDeprecation,
Message: "Please use labels instead of deprecated 'platform' filters",
Data: errors.DeprecationErrorData{
File: config.File,
Field: "platform",
Docs: "https://woodpecker-ci.org/docs/next/migrations#next-200",
},
IsWarning: true,
})
}
if parsed.BranchesDontUseIt != nil {
err = multierr.Append(err, &errors.PipelineError{
Type: errors.PipelineErrorTypeDeprecation,
Message: "Please use global when instead of deprecated 'branches' filter",
Data: errors.DeprecationErrorData{
File: config.File,
Field: "branches",
Docs: "https://woodpecker-ci.org/docs/next/migrations#next-200",
},
IsWarning: true,
})
}
return err
}
2023-11-04 14:30:47 +00:00
func (l *Linter) lintBadHabits(_ *WorkflowConfig) error {
// TODO: add bad habit warnings
2017-03-05 07:56:08 +00:00
return nil
}