-
Notifications
You must be signed in to change notification settings - Fork 88
/
linter.go
192 lines (174 loc) · 5.19 KB
/
linter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Copyright 2019 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by the Polyform License
// that can be found in the LICENSE file.
package linter
import (
"errors"
"fmt"
"path/filepath"
"strings"
"github.com/drone-runners/drone-runner-docker/engine/resource"
"github.com/drone/drone-go/drone"
"github.com/drone/runner-go/manifest"
)
// ErrDuplicateStepName is returned when two Pipeline steps
// have the same name.
var ErrDuplicateStepName = errors.New("linter: duplicate step names")
// Opts provides linting options.
type Opts struct {
Trusted bool
}
// Linter evaluates the pipeline against a set of
// rules and returns an error if one or more of the
// rules are broken.
type Linter struct{}
// New returns a new Linter.
func New() *Linter {
return new(Linter)
}
// Lint executes the linting rules for the pipeline
// configuration.
func (l *Linter) Lint(pipeline manifest.Resource, repo *drone.Repo) error {
return checkPipeline(pipeline.(*resource.Pipeline), repo.Trusted)
}
func checkPipeline(pipeline *resource.Pipeline, trusted bool) error {
// if err := checkNames(pipeline); err != nil {
// return err
// }
if err := checkSteps(pipeline, trusted); err != nil {
return err
}
if err := checkVolumes(pipeline, trusted); err != nil {
return err
}
return nil
}
// func checkNames(pipeline *resource.Pipeline) error {
// names := map[string]struct{}{}
// if !pipeline.Clone.Disable {
// names["clone"] = struct{}{}
// }
// steps := append(pipeline.Services, pipeline.Steps...)
// for _, step := range steps {
// _, ok := names[step.Name]
// if ok {
// return ErrDuplicateStepName
// }
// names[step.Name] = struct{}{}
// }
// return nil
// }
func checkSteps(pipeline *resource.Pipeline, trusted bool) error {
steps := append(pipeline.Services, pipeline.Steps...)
names := map[string]struct{}{}
if !pipeline.Clone.Disable {
names["clone"] = struct{}{}
}
for _, step := range steps {
if step == nil {
return errors.New("linter: nil step")
}
// unique list of names
_, ok := names[step.Name]
if ok {
return ErrDuplicateStepName
}
names[step.Name] = struct{}{}
if err := checkStep(step, trusted); err != nil {
return err
}
if err := checkDeps(step, names); err != nil {
return err
}
}
return nil
}
func checkStep(step *resource.Step, trusted bool) error {
if step.Image == "" {
return errors.New("linter: invalid or missing image")
}
// if step.Name == "" {
// return errors.New("linter: invalid or missing name")
// }
// if len(step.Name) > 100 {
// return errors.New("linter: name exceeds maximum length")
// }
if trusted == false && step.Privileged {
return errors.New("linter: untrusted repositories cannot enable privileged mode")
}
if trusted == false && len(step.Devices) > 0 {
return errors.New("linter: untrusted repositories cannot mount devices")
}
if trusted == false && len(step.DNS) > 0 {
return errors.New("linter: untrusted repositories cannot configure dns")
}
if trusted == false && len(step.DNSSearch) > 0 {
return errors.New("linter: untrusted repositories cannot configure dns_search")
}
if trusted == false && len(step.ExtraHosts) > 0 {
return errors.New("linter: untrusted repositories cannot configure extra_hosts")
}
if trusted == false && len(step.Network) > 0 {
return errors.New("linter: untrusted repositories cannot configure network_mode")
}
if trusted == false && int(step.ShmSize) > 0 {
return errors.New("linter: untrusted repositories cannot configure shm_size")
}
for _, mount := range step.Volumes {
switch mount.Name {
case "workspace", "_workspace", "_docker_socket":
return fmt.Errorf("linter: invalid volume name: %s", mount.Name)
}
if strings.HasPrefix(filepath.Clean(mount.MountPath), "/run/drone") {
return fmt.Errorf("linter: cannot mount volume at /run/drone")
}
}
return nil
}
func checkVolumes(pipeline *resource.Pipeline, trusted bool) error {
for _, volume := range pipeline.Volumes {
if volume.EmptyDir != nil {
err := checkEmptyDirVolume(volume.EmptyDir, trusted)
if err != nil {
return err
}
}
if volume.HostPath != nil {
err := checkHostPathVolume(volume.HostPath, trusted)
if err != nil {
return err
}
}
switch volume.Name {
case "":
return fmt.Errorf("linter: missing volume name")
case "workspace", "_workspace", "_docker_socket":
return fmt.Errorf("linter: invalid volume name: %s", volume.Name)
}
}
return nil
}
func checkHostPathVolume(volume *resource.VolumeHostPath, trusted bool) error {
if trusted == false {
return errors.New("linter: untrusted repositories cannot mount host volumes")
}
return nil
}
func checkEmptyDirVolume(volume *resource.VolumeEmptyDir, trusted bool) error {
if trusted == false && volume.Medium == "memory" {
return errors.New("linter: untrusted repositories cannot mount in-memory volumes")
}
return nil
}
func checkDeps(step *resource.Step, deps map[string]struct{}) error {
for _, dep := range step.DependsOn {
_, ok := deps[dep]
if !ok {
return fmt.Errorf("linter: unknown step dependency detected: %s references %s", step.Name, dep)
}
if step.Name == dep {
return fmt.Errorf("linter: cyclical step dependency detected: %s", dep)
}
}
return nil
}