2021-09-19 12:00:00 +00:00
|
|
|
// Copyright (c) 2019, Drone IO Inc.
|
|
|
|
// Copyright (c) 2021, Robert Kaussow <mail@thegeeklab.de>
|
2019-02-10 19:00:16 +00:00
|
|
|
|
2019-01-22 23:44:17 +00:00
|
|
|
package linter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/drone/drone-yaml/yaml"
|
|
|
|
)
|
|
|
|
|
2023-02-08 09:14:20 +00:00
|
|
|
//nolint:gochecknoglobals
|
|
|
|
var (
|
|
|
|
os = map[string]struct{}{
|
|
|
|
"linux": {},
|
|
|
|
"windows": {},
|
|
|
|
}
|
|
|
|
arch = map[string]struct{}{
|
|
|
|
"arm": {},
|
|
|
|
"arm64": {},
|
|
|
|
"amd64": {},
|
|
|
|
}
|
|
|
|
)
|
2019-01-22 23:44:17 +00:00
|
|
|
|
2023-02-08 09:14:20 +00:00
|
|
|
var (
|
|
|
|
// ErrDuplicateStepName is returned when two Pipeline steps
|
|
|
|
// have the same name.
|
|
|
|
ErrDuplicateStepName = errors.New("linter: duplicate step names")
|
2019-01-22 23:44:17 +00:00
|
|
|
|
2023-02-08 09:14:20 +00: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-22 23:44:17 +00:00
|
|
|
|
2023-02-08 09:14:20 +00: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-22 23:44:17 +00:00
|
|
|
|
2023-02-08 09:14:20 +00: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-22 23:44:17 +00: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
|
|
|
|
}
|
2023-02-08 09:14:20 +00:00
|
|
|
|
2019-01-22 23:44:17 +00:00
|
|
|
err = checkPlatform(pipeline.Platform)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-08 09:14:20 +00:00
|
|
|
|
2019-01-22 23:44:17 +00:00
|
|
|
names := map[string]struct{}{}
|
2021-09-19 12:00:00 +00:00
|
|
|
if !pipeline.Clone.Disable {
|
2019-01-22 23:44:17 +00:00
|
|
|
names["clone"] = struct{}{}
|
|
|
|
}
|
2023-02-08 09:14:20 +00:00
|
|
|
|
2019-01-22 23:44:17 +00:00
|
|
|
for _, container := range pipeline.Steps {
|
|
|
|
_, ok := names[container.Name]
|
|
|
|
if ok {
|
|
|
|
return ErrDuplicateStepName
|
|
|
|
}
|
2023-02-08 09:14:20 +00:00
|
|
|
|
2019-01-22 23:44:17 +00:00
|
|
|
names[container.Name] = struct{}{}
|
|
|
|
|
|
|
|
err := checkContainer(container, trusted)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = checkDeps(container, names)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-02-08 09:14:20 +00:00
|
|
|
|
2019-01-22 23:44:17 +00:00
|
|
|
for _, container := range pipeline.Services {
|
|
|
|
_, ok := names[container.Name]
|
|
|
|
if ok {
|
|
|
|
return ErrDuplicateStepName
|
|
|
|
}
|
2023-02-08 09:14:20 +00:00
|
|
|
|
2019-01-22 23:44:17 +00:00
|
|
|
names[container.Name] = struct{}{}
|
|
|
|
|
|
|
|
err := checkContainer(container, trusted)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-02-08 09:14:20 +00:00
|
|
|
|
2019-01-22 23:44:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkPlatform(platform yaml.Platform) error {
|
|
|
|
if v := platform.OS; v != "" {
|
|
|
|
_, ok := os[v]
|
|
|
|
if !ok {
|
2023-02-08 09:14:20 +00:00
|
|
|
return fmt.Errorf("%w: %s", ErrUnsupportedOS, v)
|
2019-01-22 23:44:17 +00:00
|
|
|
}
|
|
|
|
}
|
2023-02-08 09:14:20 +00:00
|
|
|
|
2019-01-22 23:44:17 +00:00
|
|
|
if v := platform.Arch; v != "" {
|
|
|
|
_, ok := arch[v]
|
|
|
|
if !ok {
|
2023-02-08 09:14:20 +00:00
|
|
|
return fmt.Errorf("%w: %s", ErrUnsupportedArch, v)
|
2019-01-22 23:44:17 +00:00
|
|
|
}
|
|
|
|
}
|
2023-02-08 09:14:20 +00:00
|
|
|
|
2019-01-22 23:44:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkContainer(container *yaml.Container, trusted bool) error {
|
|
|
|
err := checkPorts(container.Ports, trusted)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-08 09:14:20 +00:00
|
|
|
|
2019-01-22 23:44:17 +00:00
|
|
|
if container.Build == nil && container.Image == "" {
|
2023-02-08 09:14:20 +00:00
|
|
|
return ErrInvalidImage
|
2019-01-22 23:44:17 +00:00
|
|
|
}
|
2023-02-08 09:14:20 +00:00
|
|
|
|
2019-01-22 23:44:17 +00:00
|
|
|
if container.Build != nil && container.Build.Image == "" {
|
2023-02-08 09:14:20 +00:00
|
|
|
return ErrInvalidBuildImage
|
2019-01-22 23:44:17 +00:00
|
|
|
}
|
2023-02-08 09:14:20 +00:00
|
|
|
|
2019-01-22 23:44:17 +00:00
|
|
|
if container.Name == "" {
|
2023-02-08 09:14:20 +00:00
|
|
|
return ErrInvalidName
|
2019-01-22 23:44:17 +00:00
|
|
|
}
|
2023-02-08 09:14:20 +00:00
|
|
|
|
2021-09-19 12:00:00 +00:00
|
|
|
if trusted && container.Privileged {
|
2023-02-08 09:14:20 +00:00
|
|
|
return ErrPrivilegedNotAllowed
|
2019-01-22 23:44:17 +00:00
|
|
|
}
|
2023-02-08 09:14:20 +00:00
|
|
|
|
2021-09-19 12:00:00 +00:00
|
|
|
if trusted && len(container.Devices) > 0 {
|
2023-02-08 09:14:20 +00:00
|
|
|
return ErrMountNotAllowed
|
2019-01-22 23:44:17 +00:00
|
|
|
}
|
2023-02-08 09:14:20 +00:00
|
|
|
|
2021-09-19 12:00:00 +00:00
|
|
|
if trusted && len(container.DNS) > 0 {
|
2023-02-08 09:14:20 +00:00
|
|
|
return ErrDNSNotAllowed
|
2019-01-22 23:44:17 +00:00
|
|
|
}
|
2023-02-08 09:14:20 +00:00
|
|
|
|
2021-09-19 12:00:00 +00:00
|
|
|
if trusted && len(container.DNSSearch) > 0 {
|
2023-02-08 09:14:20 +00:00
|
|
|
return ErrDNSSearchNotAllowed
|
2019-01-22 23:44:17 +00:00
|
|
|
}
|
2023-02-08 09:14:20 +00:00
|
|
|
|
2021-09-19 12:00:00 +00:00
|
|
|
if trusted && len(container.ExtraHosts) > 0 {
|
2023-02-08 09:14:20 +00:00
|
|
|
return ErrExtraHostsNotAllowed
|
2019-01-22 23:44:17 +00:00
|
|
|
}
|
2023-02-08 09:14:20 +00:00
|
|
|
|
|
|
|
if trusted && len(container.NetworkMode) > 0 {
|
|
|
|
return ErrNetworkModeNotAllowed
|
2019-07-29 07:23:35 +00:00
|
|
|
}
|
2023-02-08 09:14:20 +00:00
|
|
|
|
2019-01-22 23:44:17 +00:00
|
|
|
for _, mount := range container.Volumes {
|
|
|
|
switch mount.Name {
|
|
|
|
case "workspace", "_workspace", "_docker_socket":
|
2023-02-08 09:14:20 +00:00
|
|
|
return fmt.Errorf("%w: %s", ErrInvalidVolumeName, mount.Name)
|
2019-01-22 23:44:17 +00:00
|
|
|
}
|
|
|
|
}
|
2023-02-08 09:14:20 +00:00
|
|
|
|
2019-01-22 23:44:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkPorts(ports []*yaml.Port, trusted bool) error {
|
|
|
|
for _, port := range ports {
|
|
|
|
err := checkPort(port, trusted)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-02-08 09:14:20 +00:00
|
|
|
|
2019-01-22 23:44:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkPort(port *yaml.Port, trusted bool) error {
|
2021-09-19 12:00:00 +00:00
|
|
|
if trusted && port.Host != 0 {
|
2023-02-08 09:14:20 +00:00
|
|
|
return ErrHostPortNotAllowed
|
2019-01-22 23:44:17 +00:00
|
|
|
}
|
2023-02-08 09:14:20 +00:00
|
|
|
|
2019-01-22 23:44:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkVolumes(pipeline *yaml.Pipeline, trusted bool) error {
|
|
|
|
for _, volume := range pipeline.Volumes {
|
2023-02-08 09:14:20 +00:00
|
|
|
if volume.Temp != nil {
|
|
|
|
err := checkEmptyDirVolume(volume.Temp, trusted)
|
2019-01-22 23:44:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-02-08 09:14:20 +00:00
|
|
|
|
|
|
|
if volume.Host != nil {
|
|
|
|
err := checkHostPathVolume(trusted)
|
2019-01-22 23:44:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-02-08 09:14:20 +00:00
|
|
|
|
2019-01-22 23:44:17 +00:00
|
|
|
switch volume.Name {
|
|
|
|
case "workspace", "_workspace", "_docker_socket":
|
2023-02-08 09:14:20 +00:00
|
|
|
return fmt.Errorf("%w: %s", ErrInvalidVolumeName, volume.Name)
|
2019-01-22 23:44:17 +00:00
|
|
|
}
|
|
|
|
}
|
2023-02-08 09:14:20 +00:00
|
|
|
|
2019-01-22 23:44:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-08 09:14:20 +00:00
|
|
|
func checkHostPathVolume(trusted bool) error {
|
2021-09-19 12:00:00 +00:00
|
|
|
if trusted {
|
2023-02-08 09:14:20 +00:00
|
|
|
return ErrHostVolumeNotAllowed
|
2019-01-22 23:44:17 +00:00
|
|
|
}
|
2023-02-08 09:14:20 +00:00
|
|
|
|
2019-01-22 23:44:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkEmptyDirVolume(volume *yaml.VolumeEmptyDir, trusted bool) error {
|
2021-09-19 12:00:00 +00:00
|
|
|
if trusted && volume.Medium == "memory" {
|
2023-02-08 09:14:20 +00:00
|
|
|
return ErrTempVolumeNotAllowed
|
2019-01-22 23:44:17 +00:00
|
|
|
}
|
2023-02-08 09:14:20 +00:00
|
|
|
|
2019-01-22 23:44:17 +00: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
|
|
|
|
}
|
2023-02-08 09:14:20 +00:00
|
|
|
|
2019-01-22 23:44:17 +00:00
|
|
|
if container.Name == dep {
|
|
|
|
return ErrCyclicalDependency
|
|
|
|
}
|
|
|
|
}
|
2023-02-08 09:14:20 +00:00
|
|
|
|
2019-01-22 23:44:17 +00:00
|
|
|
return nil
|
|
|
|
}
|