2023-08-10 09:06:00 +00:00
|
|
|
// 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-06-06 07:14:21 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/pipeline/frontend/yaml/types"
|
2017-03-05 07:56:08 +00:00
|
|
|
)
|
|
|
|
|
2017-07-21 21:52:52 +00:00
|
|
|
const (
|
|
|
|
blockClone uint8 = iota
|
|
|
|
blockPipeline
|
|
|
|
blockServices
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lint lints the configuration.
|
2023-06-06 07:14:21 +00:00
|
|
|
func (l *Linter) Lint(c *types.Workflow) error {
|
|
|
|
if len(c.Steps.ContainerList) == 0 {
|
2017-07-21 21:52:52 +00:00
|
|
|
return fmt.Errorf("Invalid or missing pipeline section")
|
|
|
|
}
|
2023-06-06 07:14:21 +00:00
|
|
|
if err := l.lint(c.Clone.ContainerList, blockClone); err != nil {
|
2017-07-21 21:52:52 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-06-06 07:14:21 +00:00
|
|
|
if err := l.lint(c.Steps.ContainerList, blockPipeline); err != nil {
|
2017-07-21 21:52:52 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-06-06 07:14:21 +00:00
|
|
|
return l.lint(c.Services.ContainerList, blockServices)
|
2017-07-21 21:52:52 +00:00
|
|
|
}
|
2017-03-05 07:56:08 +00:00
|
|
|
|
2023-06-06 07:14:21 +00:00
|
|
|
func (l *Linter) lint(containers []*types.Container, _ uint8) error {
|
2017-03-05 07:56:08 +00:00
|
|
|
for _, container := range containers {
|
|
|
|
if err := l.lintImage(container); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-11-23 14:36:52 +00:00
|
|
|
if !l.trusted {
|
2017-03-05 07:56:08 +00:00
|
|
|
if err := l.lintTrusted(container); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2017-07-21 21:52:52 +00:00
|
|
|
if err := l.lintCommands(container); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-06 07:14:21 +00:00
|
|
|
func (l *Linter) lintImage(c *types.Container) error {
|
2017-03-05 07:56:08 +00:00
|
|
|
if len(c.Image) == 0 {
|
|
|
|
return fmt.Errorf("Invalid or missing image")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-06 07:14:21 +00:00
|
|
|
func (l *Linter) lintCommands(c *types.Container) error {
|
2017-07-21 21:52:52 +00:00
|
|
|
if len(c.Commands) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2021-12-04 15:44:18 +00:00
|
|
|
if len(c.Settings) != 0 {
|
2017-07-21 21:52:52 +00:00
|
|
|
var keys []string
|
2021-12-04 15:44:18 +00:00
|
|
|
for key := range c.Settings {
|
2017-07-21 21:52:52 +00:00
|
|
|
keys = append(keys, key)
|
|
|
|
}
|
|
|
|
return fmt.Errorf("Cannot configure both commands and custom attributes %v", keys)
|
|
|
|
}
|
2017-03-05 07:56:08 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-06 07:14:21 +00:00
|
|
|
func (l *Linter) lintTrusted(c *types.Container) error {
|
2017-03-05 07:56:08 +00:00
|
|
|
if c.Privileged {
|
|
|
|
return fmt.Errorf("Insufficient privileges to use privileged mode")
|
|
|
|
}
|
|
|
|
if c.ShmSize != 0 {
|
|
|
|
return fmt.Errorf("Insufficient privileges to override shm_size")
|
|
|
|
}
|
|
|
|
if len(c.DNS) != 0 {
|
|
|
|
return fmt.Errorf("Insufficient privileges to use custom dns")
|
|
|
|
}
|
|
|
|
if len(c.DNSSearch) != 0 {
|
|
|
|
return fmt.Errorf("Insufficient privileges to use dns_search")
|
|
|
|
}
|
|
|
|
if len(c.Devices) != 0 {
|
|
|
|
return fmt.Errorf("Insufficient privileges to use devices")
|
|
|
|
}
|
|
|
|
if len(c.ExtraHosts) != 0 {
|
|
|
|
return fmt.Errorf("Insufficient privileges to use extra_hosts")
|
|
|
|
}
|
|
|
|
if len(c.NetworkMode) != 0 {
|
|
|
|
return fmt.Errorf("Insufficient privileges to use network_mode")
|
|
|
|
}
|
2017-09-08 00:43:33 +00:00
|
|
|
if len(c.IpcMode) != 0 {
|
|
|
|
return fmt.Errorf("Insufficient privileges to use ipc_mode")
|
|
|
|
}
|
2017-11-17 22:49:01 +00:00
|
|
|
if len(c.Sysctls) != 0 {
|
|
|
|
return fmt.Errorf("Insufficient privileges to use sysctls")
|
|
|
|
}
|
2017-03-05 07:56:08 +00:00
|
|
|
if c.Networks.Networks != nil && len(c.Networks.Networks) != 0 {
|
|
|
|
return fmt.Errorf("Insufficient privileges to use networks")
|
|
|
|
}
|
|
|
|
if c.Volumes.Volumes != nil && len(c.Volumes.Volumes) != 0 {
|
|
|
|
return fmt.Errorf("Insufficient privileges to use volumes")
|
|
|
|
}
|
2017-09-08 00:43:33 +00:00
|
|
|
if len(c.Tmpfs) != 0 {
|
|
|
|
return fmt.Errorf("Insufficient privileges to use tmpfs")
|
|
|
|
}
|
2017-03-05 07:56:08 +00:00
|
|
|
return nil
|
|
|
|
}
|