drone-yaml/yaml/linter/linter.go

271 lines
6.2 KiB
Go
Raw Permalink Normal View History

// Copyright (c) 2019, Drone IO Inc.
// Copyright (c) 2021, Robert Kaussow <mail@thegeeklab.de>
2019-02-10 20:00:16 +01:00
2019-01-23 00:44:17 +01:00
package linter
import (
"errors"
"fmt"
"github.com/drone/drone-yaml/yaml"
)
//nolint:gochecknoglobals
var (
os = map[string]struct{}{
"linux": {},
"windows": {},
}
arch = map[string]struct{}{
"arm": {},
"arm64": {},
"amd64": {},
}
)
2019-01-23 00:44:17 +01:00
var (
// ErrDuplicateStepName is returned when two Pipeline steps
// have the same name.
ErrDuplicateStepName = errors.New("linter: duplicate step names")
2019-01-23 00:44:17 +01:00
// ErrMissingDependency is returned when a Pipeline step
// defines dependencies that are invalid or unknown.
ErrMissingDependency = errors.New("linter: invalid or unknown step dependency")
2019-01-23 00:44:17 +01:00
// ErrCyclicalDependency is returned when a Pipeline step
// defines a cyclical dependency, which would result in an
// infinite execution loop.
ErrCyclicalDependency = errors.New("linter: cyclical step dependency detected")
2019-01-23 00:44:17 +01:00
ErrUnsupportedOS = errors.New("linter: unsupported os")
ErrUnsupportedArch = errors.New("linter: unsupported architecture")
ErrInvalidImage = errors.New("linter: invalid or missing image")
ErrInvalidBuildImage = errors.New("linter: invalid or missing build image")
ErrInvalidName = errors.New("linter: invalid or missing name")
ErrPrivilegedNotAllowed = errors.New("linter: untrusted repositories cannot enable privileged mode")
ErrMountNotAllowed = errors.New("linter: untrusted repositories cannot mount devices")
ErrDNSNotAllowed = errors.New("linter: untrusted repositories cannot configure dns")
ErrDNSSearchNotAllowed = errors.New("linter: untrusted repositories cannot configure dns_search")
ErrExtraHostsNotAllowed = errors.New("linter: untrusted repositories cannot configure extra_hosts")
ErrNetworkModeNotAllowed = errors.New("linter: untrusted repositories cannot configure network_mode")
ErrInvalidVolumeName = errors.New("linter: invalid volume name")
ErrHostPortNotAllowed = errors.New("linter: untrusted repositories cannot map to a host port")
ErrHostVolumeNotAllowed = errors.New("linter: untrusted repositories cannot mount host volumes")
ErrTempVolumeNotAllowed = errors.New("linter: untrusted repositories cannot mount in-memory volumes")
)
2019-01-23 00:44:17 +01:00
// Lint performs lint operations for a resource.
func Lint(resource yaml.Resource, trusted bool) error {
switch v := resource.(type) {
case *yaml.Cron:
return v.Validate()
case *yaml.Pipeline:
return checkPipeline(v, trusted)
case *yaml.Secret:
return v.Validate()
case *yaml.Registry:
return v.Validate()
case *yaml.Signature:
return v.Validate()
default:
return nil
}
}
func checkPipeline(pipeline *yaml.Pipeline, trusted bool) error {
err := checkVolumes(pipeline, trusted)
if err != nil {
return err
}
2019-01-23 00:44:17 +01:00
err = checkPlatform(pipeline.Platform)
if err != nil {
return err
}
2019-01-23 00:44:17 +01:00
names := map[string]struct{}{}
if !pipeline.Clone.Disable {
2019-01-23 00:44:17 +01:00
names["clone"] = struct{}{}
}
2019-01-23 00:44:17 +01:00
for _, container := range pipeline.Steps {
_, ok := names[container.Name]
if ok {
return ErrDuplicateStepName
}
2019-01-23 00:44:17 +01:00
names[container.Name] = struct{}{}
err := checkContainer(container, trusted)
if err != nil {
return err
}
err = checkDeps(container, names)
if err != nil {
return err
}
}
2019-01-23 00:44:17 +01:00
for _, container := range pipeline.Services {
_, ok := names[container.Name]
if ok {
return ErrDuplicateStepName
}
2019-01-23 00:44:17 +01:00
names[container.Name] = struct{}{}
err := checkContainer(container, trusted)
if err != nil {
return err
}
}
2019-01-23 00:44:17 +01:00
return nil
}
func checkPlatform(platform yaml.Platform) error {
if v := platform.OS; v != "" {
_, ok := os[v]
if !ok {
return fmt.Errorf("%w: %s", ErrUnsupportedOS, v)
2019-01-23 00:44:17 +01:00
}
}
2019-01-23 00:44:17 +01:00
if v := platform.Arch; v != "" {
_, ok := arch[v]
if !ok {
return fmt.Errorf("%w: %s", ErrUnsupportedArch, v)
2019-01-23 00:44:17 +01:00
}
}
2019-01-23 00:44:17 +01:00
return nil
}
func checkContainer(container *yaml.Container, trusted bool) error {
err := checkPorts(container.Ports, trusted)
if err != nil {
return err
}
2019-01-23 00:44:17 +01:00
if container.Build == nil && container.Image == "" {
return ErrInvalidImage
2019-01-23 00:44:17 +01:00
}
2019-01-23 00:44:17 +01:00
if container.Build != nil && container.Build.Image == "" {
return ErrInvalidBuildImage
2019-01-23 00:44:17 +01:00
}
2019-01-23 00:44:17 +01:00
if container.Name == "" {
return ErrInvalidName
2019-01-23 00:44:17 +01:00
}
if trusted && container.Privileged {
return ErrPrivilegedNotAllowed
2019-01-23 00:44:17 +01:00
}
if trusted && len(container.Devices) > 0 {
return ErrMountNotAllowed
2019-01-23 00:44:17 +01:00
}
if trusted && len(container.DNS) > 0 {
return ErrDNSNotAllowed
2019-01-23 00:44:17 +01:00
}
if trusted && len(container.DNSSearch) > 0 {
return ErrDNSSearchNotAllowed
2019-01-23 00:44:17 +01:00
}
if trusted && len(container.ExtraHosts) > 0 {
return ErrExtraHostsNotAllowed
2019-01-23 00:44:17 +01:00
}
if trusted && len(container.NetworkMode) > 0 {
return ErrNetworkModeNotAllowed
2019-07-29 09:23:35 +02:00
}
2019-01-23 00:44:17 +01:00
for _, mount := range container.Volumes {
switch mount.Name {
case "workspace", "_workspace", "_docker_socket":
return fmt.Errorf("%w: %s", ErrInvalidVolumeName, mount.Name)
2019-01-23 00:44:17 +01:00
}
}
2019-01-23 00:44:17 +01:00
return nil
}
func checkPorts(ports []*yaml.Port, trusted bool) error {
for _, port := range ports {
err := checkPort(port, trusted)
if err != nil {
return err
}
}
2019-01-23 00:44:17 +01:00
return nil
}
func checkPort(port *yaml.Port, trusted bool) error {
if trusted && port.Host != 0 {
return ErrHostPortNotAllowed
2019-01-23 00:44:17 +01:00
}
2019-01-23 00:44:17 +01:00
return nil
}
func checkVolumes(pipeline *yaml.Pipeline, trusted bool) error {
for _, volume := range pipeline.Volumes {
if volume.Temp != nil {
err := checkEmptyDirVolume(volume.Temp, trusted)
2019-01-23 00:44:17 +01:00
if err != nil {
return err
}
}
if volume.Host != nil {
err := checkHostPathVolume(trusted)
2019-01-23 00:44:17 +01:00
if err != nil {
return err
}
}
2019-01-23 00:44:17 +01:00
switch volume.Name {
case "workspace", "_workspace", "_docker_socket":
return fmt.Errorf("%w: %s", ErrInvalidVolumeName, volume.Name)
2019-01-23 00:44:17 +01:00
}
}
2019-01-23 00:44:17 +01:00
return nil
}
func checkHostPathVolume(trusted bool) error {
if trusted {
return ErrHostVolumeNotAllowed
2019-01-23 00:44:17 +01:00
}
2019-01-23 00:44:17 +01:00
return nil
}
func checkEmptyDirVolume(volume *yaml.VolumeEmptyDir, trusted bool) error {
if trusted && volume.Medium == "memory" {
return ErrTempVolumeNotAllowed
2019-01-23 00:44:17 +01:00
}
2019-01-23 00:44:17 +01:00
return nil
}
func checkDeps(container *yaml.Container, deps map[string]struct{}) error {
for _, dep := range container.DependsOn {
_, ok := deps[dep]
if !ok {
return ErrMissingDependency
}
2019-01-23 00:44:17 +01:00
if container.Name == dep {
return ErrCyclicalDependency
}
}
2019-01-23 00:44:17 +01:00
return nil
}