-
Notifications
You must be signed in to change notification settings - Fork 787
/
step_post_build.go
254 lines (210 loc) · 6.09 KB
/
step_post_build.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
package post
import (
"fmt"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts/step"
"github.com/pkg/errors"
"github.com/jenkins-x/jx/v2/pkg/cmd/helper"
"github.com/jenkins-x/jx/v2/pkg/kube/services"
"os"
"os/exec"
"strings"
"bufio"
"path/filepath"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts"
"github.com/jenkins-x/jx/v2/pkg/cmd/templates"
"github.com/jenkins-x/jx/v2/pkg/kube"
"github.com/jenkins-x/jx/v2/pkg/log"
"github.com/jenkins-x/jx/v2/pkg/util"
"github.com/spf13/cobra"
)
const (
defaultAnchoreName = "anchore"
)
// StepPostBuildOptions contains the command line flags
type StepPostBuildOptions struct {
step.StepOptions
FullImageName string
OutputFile string
}
type anchoreDetails struct {
URL string
Username string
Password string
}
var (
StepPostBuildLong = templates.LongDesc(`
This pipeline step performs post build actions such as CVE analysis
`)
StepPostBuildExample = templates.Examples(`
jx step post build
`)
)
const podAnnotations = `
podAnnotations:
jenkins-x.io/cve-image-id: %s
`
func NewCmdStepPostBuild(commonOpts *opts.CommonOptions) *cobra.Command {
options := StepPostBuildOptions{
StepOptions: step.StepOptions{
CommonOptions: commonOpts,
},
}
cmd := &cobra.Command{
Use: "build",
Short: "Performs post build actions in a pipeline",
Long: StepPostBuildLong,
Example: StepPostBuildExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
helper.CheckErr(err)
},
}
cmd.Flags().StringVarP(&options.FullImageName, "image", "", "", "The full image name to be analysed including the registry prefix")
return cmd
}
func (o *StepPostBuildOptions) Run() error {
_, err := o.KubeClient()
if err != nil {
return fmt.Errorf("error connecting to Kubernetes cluster: %v", err)
}
// let's try and add image to CVE provider
err = o.addImageCVEProvider()
if err != nil {
return fmt.Errorf("error adding image to CVE provider: %v", err)
}
return nil
}
func (o *StepPostBuildOptions) addImageCVEProvider() error {
if o.FullImageName == "" {
return util.MissingOption("image")
}
client, currentNamespace, err := o.KubeClientAndNamespace()
if err != nil {
return err
}
present, err := services.IsServicePresent(client, kube.AddonServices[defaultAnchoreName], currentNamespace)
if err != nil || !present {
log.Logger().Infof("no CVE provider running in the current %s namespace so skip adding image to be analysed", currentNamespace)
return nil
}
cveProviderHost := os.Getenv("JENKINS_X_DOCKER_REGISTRY_SERVICE_HOST")
if cveProviderHost == "" {
return fmt.Errorf("no JENKINS_X_DOCKER_REGISTRY_SERVICE_HOST env var found")
}
cveProviderPort := os.Getenv("JENKINS_X_DOCKER_REGISTRY_SERVICE_PORT")
if cveProviderPort == "" {
return fmt.Errorf("no JENKINS_X_DOCKER_REGISTRY_SERVICE_PORT env var found")
}
log.Logger().Infof("adding image %s to CVE provider", o.FullImageName)
imageID, err := o.addImageToAnchore()
if err != nil {
return fmt.Errorf("failed to add image %s to anchore engine: %v\n", o.FullImageName, err)
}
err = o.addImageIDtoHelmValues(imageID)
if err != nil {
return fmt.Errorf("failed to add image id %s to helm values: %v\n", imageID, err)
}
// todo use image id to annotate pods during environments helm install / upgrade
// todo then we can use `jx get cve --env staging` and list all CVEs for an environment
log.Logger().Infof("anchore image is %s ", imageID)
return nil
}
func (o *StepPostBuildOptions) addImageToAnchore() (string, error) {
a, err := o.getAnchoreDetails()
if err != nil {
return "", err
}
cmd := exec.Command("anchore-cli", "image", "add", o.FullImageName) // #nosec
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, "ANCHORE_CLI_USER="+a.Username)
cmd.Env = append(cmd.Env, "ANCHORE_CLI_PASS="+a.Password)
cmd.Env = append(cmd.Env, "ANCHORE_CLI_URL="+a.URL)
data, err := cmd.CombinedOutput()
text := string(data)
if err != nil {
return "", err
}
scanner := bufio.NewScanner(strings.NewReader(text))
var imageID string
for scanner.Scan() {
if strings.HasPrefix(scanner.Text(), "Image ID:") {
imageID = strings.Replace(scanner.Text(), "Image ID:", "", -1)
imageID = strings.TrimSpace(imageID)
break
}
}
if imageID == "" {
return "", fmt.Errorf("no Image ID returned from Anchore")
}
return imageID, nil
}
func (o *StepPostBuildOptions) getAnchoreDetails() (anchoreDetails, error) {
var a anchoreDetails
anchoreAuthSvc, err := o.AddonAuthConfigService(kube.ValueKindCVE)
if err != nil {
return a, errors.Wrap(err, "creating the auth config service for anchore")
}
cfg := anchoreAuthSvc.Config()
if cfg == nil {
return a, errors.New("no auth config found anchore")
}
server := cfg.CurrentAuthServer()
if server == nil {
return a, errors.New("no server auth config found for anchore")
}
auth := server.CurrentAuth()
if auth == nil {
return a, fmt.Errorf("no auth configuration found for server %q", server.URL)
}
a.URL = server.URL
a.Username = auth.Username
a.Password = auth.Password
if a.Password == "" {
a.Password = auth.ApiToken
}
if a.Password == "" {
a.Password = auth.BearerToken
}
return a, nil
}
func (o *StepPostBuildOptions) addImageIDtoHelmValues(imageID string) error {
pwd, err := os.Getwd()
if err != nil {
return err
}
charts := filepath.Join(pwd, "charts")
exists, err := util.DirExists(charts)
if err != nil {
return err
}
if !exists {
return fmt.Errorf("no charts folder found are you in the root folder of your project?")
}
// loop through all directories and if there's a values.yaml add image id to the end
err = filepath.Walk(charts, func(path string, f os.FileInfo, err error) error {
if f.IsDir() {
values := filepath.Join(path, "values.yaml")
valuesExist, err := util.FileExists(values)
if err != nil {
return err
}
if valuesExist {
f, err := os.OpenFile(values, os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
return err
}
defer f.Close()
if _, err = f.WriteString(fmt.Sprintf(podAnnotations, imageID)); err != nil {
return err
}
}
}
return nil
})
if err != nil {
return err
}
return nil
}