-
Notifications
You must be signed in to change notification settings - Fork 7
/
plugin.go
68 lines (55 loc) · 1.14 KB
/
plugin.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
package main
import (
"io/ioutil"
"os"
"os/exec"
"github.com/Sirupsen/logrus"
)
type (
Plugin struct {
Url string
Username string
Password string
ScanImage string
CaCert string
}
)
func (p Plugin) Exec() error {
os.Setenv("CLAIR_ADDR", p.Url)
_, exist := os.LookupEnv("DOCKER_USER")
if !exist {
os.Setenv("DOCKER_USER", p.Username)
}
_, exist = os.LookupEnv("DOCKER_PASSWORD")
if !exist {
os.Setenv("DOCKER_PASSWORD", p.Password)
}
var commands []*exec.Cmd
if p.CaCert != "" {
commands = append(commands, installCaCert(p.CaCert))
}
commands = append(commands, scanImage(p.ScanImage))
for _, command := range commands {
command.Stdout = os.Stdout
command.Stderr = os.Stderr
err := command.Run()
if err != nil {
logrus.WithFields(logrus.Fields{
"error": err,
}).Fatal("Failed to execute a command")
}
}
return nil
}
func installCaCert(cacert string) *exec.Cmd {
ioutil.WriteFile("/usr/local/share/ca-certificates/ca_cert.crt", []byte(cacert), 0644)
return exec.Command(
"update-ca-certificates",
)
}
func scanImage(image string) *exec.Cmd {
return exec.Command(
"klar",
image,
)
}