-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabsint.go
333 lines (290 loc) · 6.92 KB
/
absint.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package main
import (
"encoding/json"
"errors"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/anchore/syft/syft/sbom"
"github.com/xlab/treeprint"
)
type pathtree struct {
parent *pathtree // nil means root
name string
children map[string]*pathtree
}
func (p *pathtree) String() string {
tree := treeprint.New()
p.addbranch(tree)
return tree.String()
}
func (p *pathtree) addbranch(branch treeprint.Tree) {
for _, child := range p.children {
if len(child.children) == 0 {
branch.AddNode(child.name)
} else {
subbranch := branch.AddBranch(child.name)
child.addbranch(subbranch)
}
}
}
func newPathtree(name string, parent *pathtree) *pathtree {
p := &pathtree{
parent: parent,
name: name,
children: make(map[string]*pathtree),
}
if parent != nil {
parent.children[name] = p
}
return p
}
type mutFn func(s *State, args ...string) error
type thunk struct {
mutFn
args []string
}
// State is a representation the cloudbuild container as the steps are being abstractly interpreted.
type State struct {
root *pathtree
cwd *pathtree
containers map[string]*sbom.SBOM
replacements map[string]string
}
func NewState(replacements map[string]string) *State {
root := newPathtree("root", nil)
newPathtree("workspace", root)
return &State{
root: root,
cwd: root,
containers: make(map[string]*sbom.SBOM),
replacements: replacements,
}
}
// ensureSteps ensures that all the images are pulled to local machine. TODO: only pull metadata. We only need entrypoint, cmd, and the sbom.
func (ai *State) ensureSteps(steps []Step) error {
for i := range steps {
step := &steps[i]
// string replacements need to happen first
ai.stringReplacement(&steps[i])
if ai.containers[step.Name] != nil {
continue
}
log.Printf("Pulling %v", step.Name)
cmd := exec.Command("docker", "pull", step.Name)
err := cmd.Run()
if err != nil {
return errors.Join(fmt.Errorf("Unable to ensure %v", step.Name), err, cmd.Err)
}
log.Printf("Inspecting %v", step.Name)
// now that we have pulled the image, we can inspect it
if err = ai.inspectImage(&steps[i]); err != nil {
return err
}
log.Printf("Compiling SBOM for %v", step.Name)
// compile the SBOM
if err = ai.compileSBOM(step); err != nil {
return err
}
}
// check paths
for _, step := range steps {
if step.Dir != "" {
ai.setDir(step)
}
}
for _, step := range steps {
ai.checkEntrypoint(step)
}
return nil
}
func (ai *State) stringReplacement(step *Step) {
log.Printf("Replacing %v with %v", step.Name, ai.replaceStr(step.Name))
step.Name = ai.replaceStr(step.Name)
step.Entrypoint = ai.replaceStr(step.Entrypoint)
for i, arg := range step.Args {
step.Args[i] = ai.replaceStr(arg)
}
step.Dir = ai.replaceStr(step.Dir)
}
func (ai *State) replaceStr(s string) string {
for k, v := range ai.replacements {
s = strings.ReplaceAll(s, k, v)
}
return s
}
func (ai *State) inspectImage(step *Step) (err error) {
cmd := exec.Command("docker", "inspect", step.Name)
out, err := cmd.Output()
if err != nil {
return errors.Join(fmt.Errorf("Unable to inspect %v", step.Name), err, cmd.Err)
}
var inspection []Inspection
err = json.Unmarshal(out, &inspection)
if err != nil {
return err
}
if len(inspection) != 1 {
return fmt.Errorf("Expected 1 inspection, got %d", len(inspection))
}
// setting value - use the convention `steps[i]` instead of `step`
// because sideeffects are cool bro (that was sarcasm)
if len(step.cmd) > 0 {
step.cmd = inspection[0].Config.Cmd[0]
}
if step.Entrypoint == "" {
step.Entrypoint = inspection[0].Config.Entrypoint[0]
}
if step.Entrypoint == "" {
step.Entrypoint = step.cmd
}
if step.Entrypoint == "" {
return fmt.Errorf("No entrypoint or cmd found for %v", step.Name)
}
return nil
}
func (ai *State) compileSBOM(step *Step) (err error) {
// now we compile SBOM
filename := step.Name + ".json"
cmd := exec.Command("docker", "sbom", step.Name, "--format", "syft-json", "-o", filename)
if err = cmd.Run(); err != nil {
return errors.Join(fmt.Errorf("Unable to fetch SBOM for %v", step.Name), err)
}
f, err := os.Open(filename)
if err != nil {
return errors.Join(fmt.Errorf("Unable to open SBOM file %v", filename), err)
}
bom := getBOM(f)
ai.containers[step.Name] = bom
return f.Close()
}
func (ai *State) checkEntrypoint(step Step) error {
bom := ai.containers[step.Name]
if !findEntrypoint(bom, step.Entrypoint) {
return fmt.Errorf("Entrypoint %v not found in %v", step.Entrypoint, step.Name)
}
return nil
}
// workspaceDir is a method that takes a raw directory string and spits out a list of directories to traverse through.
//
// PLENTY OF SIDE EFFECTS
func (ai *State) workspaceDir(raw string, setWorkspace bool) []string {
dir := strings.Split(raw, "/")
idx := 1
switch dir[0] {
case "":
ai.cwd = ai.root
case ".":
case "..":
ai.cwd = ai.cwd.parent
default:
if setWorkspace {
ai.cwd = ai.root.children["workspace"]
}
idx = 0
}
dir = dir[idx:]
return dir
}
func (ai *State) setDir(s Step) {
if s.Dir == "" {
return // cwd it is!
}
dir := ai.workspaceDir(s.Dir, true)
for _, d := range dir {
mkdir(ai, d)
cd(ai, d)
}
}
func (ai *State) execute(s Step) error {
// directory related ones are executed
ai.setDir(s)
if isShell(s.Entrypoint) {
ts := parseShellArgs(s.Args)
for _, t := range ts {
if t.mutFn == nil {
continue
}
if err := t.mutFn(ai, t.args...); err != nil {
return err
}
}
}
return nil
}
func isShell(entrypoint string) bool {
switch entrypoint {
case "/bin/bash", "/bin/sh", "sh":
return true
default:
return false
}
}
func parseShellArgs(args []string) (retVal []thunk) {
if args[0] == "-c" {
args = strings.Split(args[1], "\n")
}
for _, arg := range args {
s := strings.Split(arg, " ")
fn := dirCmds[s[0]]
args := s[1:]
retVal = append(retVal, thunk{fn, args})
}
return retVal
}
var dirCmds = map[string]mutFn{
"mkdir": mkdir,
"cd": cd,
}
func mkdir(ai *State, args ...string) error {
// clean args of \n first
for i := range args {
args[i] = strings.Trim(args[i], "\n")
}
var dashp bool
for _, arg := range args {
if arg == "-p" {
dashp = true
}
}
p := args[len(args)-1]
l := ai.workspaceDir(p, false)
for i, x := range l {
if i == len(l)-1 {
if _, ok := ai.cwd.children[x]; ok {
return nil
}
newPathtree(x, ai.cwd)
return nil
}
if err := cd(ai, x); err != nil {
if !dashp {
return errors.Join(fmt.Errorf("Cannot mkdir %v. Perhaps you didn't pass in -p?", p), err)
}
mkdir(ai, x) // no error will occur
cd(ai, x) // no error will occur
}
}
return nil
}
func cd(ai *State, args ...string) error {
p := args[0]
l := filepath.SplitList(p)
for _, x := range l {
cwd, ok := ai.cwd.children[x]
if !ok {
return fmt.Errorf("path not found: %v", p)
}
ai.cwd = cwd
}
return nil
}
func gitclone(ai *State, args ...string) error {
return nil
}
func gitfetch(ai *State, args ...string) error {
return nil
}