Skip to content

Commit 1756d4c

Browse files
Microzuul CIGerrit Code Review
Microzuul CI
authored and
Gerrit Code Review
committed
Merge "cli/dev - Add getImagesSecurityIssues command to discover images w/ sec issues"
2 parents 319191e + 9c9ca7f commit 1756d4c

File tree

4 files changed

+117
-1
lines changed

4 files changed

+117
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
55
## [in development]
66

77
### Added
8+
9+
- Dev CLI - Add command "go run ./main.go dev getImagesSecurityIssue" to ease getting a small report of HIGH
10+
and CRITICAL Security issues reported by quay.io on container images used by the sf-operator.
11+
812
### Changed
913

1014
- Zookeeper version bumped to 3.8.4

cli/cmd/dev/dev.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ package dev
1919

2020
import (
2121
"context"
22+
"encoding/json"
2223
"errors"
24+
"fmt"
25+
"net/http"
2326
"os"
2427
"path/filepath"
2528
"strings"
@@ -28,6 +31,7 @@ import (
2831
ms "github.com/softwarefactory-project/sf-operator/cli/cmd/dev/microshift"
2932
cliutils "github.com/softwarefactory-project/sf-operator/cli/cmd/utils"
3033
"github.com/softwarefactory-project/sf-operator/controllers"
34+
"github.com/softwarefactory-project/sf-operator/controllers/libs/base"
3135
"k8s.io/client-go/rest"
3236

3337
"github.com/spf13/cobra"
@@ -293,6 +297,79 @@ func devCloneAsAdmin(kmd *cobra.Command, args []string) {
293297
gerrit.CloneAsAdmin(&env, fqdn, repoName, dest, verify)
294298
}
295299

300+
func getImagesSecurityIssues(kmd *cobra.Command, args []string) {
301+
302+
const quaySFBaseURL = "https://quay.io/api/v1/repository/software-factory/"
303+
304+
type Vuln struct {
305+
Severity string
306+
Link string
307+
Name string
308+
}
309+
310+
type Feature struct {
311+
Name string
312+
Vulnerabilities []Vuln
313+
}
314+
315+
type Layer struct {
316+
Features []Feature
317+
}
318+
319+
type Data struct {
320+
Layer Layer
321+
}
322+
323+
type Scan struct {
324+
Status string
325+
Data Data
326+
}
327+
328+
type Tag struct {
329+
ManifestDigest string `json:"manifest_digest"`
330+
}
331+
332+
type Image struct {
333+
Name string
334+
Tags map[string]Tag
335+
}
336+
337+
getImageDigest := func(image base.Image) string {
338+
339+
url := quaySFBaseURL + image.Name
340+
resp, _ := http.Get(url)
341+
target := Image{}
342+
json.NewDecoder(resp.Body).Decode(&target)
343+
344+
return target.Tags[image.Version].ManifestDigest
345+
346+
}
347+
348+
getImageReport := func(image base.Image) {
349+
350+
digest := getImageDigest(image)
351+
manifest := image.Name + "/manifest/" + digest
352+
url := quaySFBaseURL + manifest + "/security"
353+
resp, _ := http.Get(url)
354+
target := Scan{}
355+
json.NewDecoder(resp.Body).Decode(&target)
356+
357+
println("\nScan result for: " + image.Name)
358+
for _, feature := range target.Data.Layer.Features {
359+
for _, vuln := range feature.Vulnerabilities {
360+
if vuln.Severity == "High" || vuln.Severity == "Critical" {
361+
fmt.Printf("- %s [%s] %s\n", feature.Name, vuln.Severity, vuln.Name)
362+
}
363+
}
364+
}
365+
}
366+
367+
for _, image := range base.GetSelfManagedImages() {
368+
getImageReport(image)
369+
}
370+
371+
}
372+
296373
func MkDevCmd() *cobra.Command {
297374

298375
var (
@@ -337,6 +414,11 @@ func MkDevCmd() *cobra.Command {
337414
ValidArgs: devRunTestsAllowedArgs,
338415
Run: devRunTests,
339416
}
417+
getImagesSecurityIssuesCmd = &cobra.Command{
418+
Use: "getImagesSecurityIssues",
419+
Long: "Return the list of security issues reported by Quay.io (only High and Critical)",
420+
Run: getImagesSecurityIssues,
421+
}
340422
)
341423
// args
342424
wipeCmd.Flags().BoolVar(&deleteData, "rm-data", false, "Delete also persistent data. This will result in data loss, like review history.")
@@ -362,5 +444,8 @@ func MkDevCmd() *cobra.Command {
362444
devCmd.AddCommand(wipeCmd)
363445
devCmd.AddCommand(cloneAsAdminCmd)
364446
devCmd.AddCommand(runTestsCmd)
447+
448+
devCmd.AddCommand(getImagesSecurityIssuesCmd)
449+
365450
return devCmd
366451
}

controllers/libs/base/images.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,16 @@ type Image struct {
2424
Source string `yaml:"source,omitempty"`
2525
}
2626

27-
func getImage(name string) string {
27+
func loadImages() ContainerImages {
2828
var images ContainerImages
2929
if err := yaml.UnmarshalStrict([]byte(imagesYAML), &images); err != nil {
3030
panic(err)
3131
}
32+
return images
33+
}
34+
35+
func getImage(name string) string {
36+
images := loadImages()
3237
for _, image := range images.Images {
3338
if image.Name == name {
3439
return image.Container + ":" + image.Version
@@ -38,6 +43,17 @@ func getImage(name string) string {
3843
panic("Unknown container image: " + name)
3944
}
4045

46+
func GetSelfManagedImages() []Image {
47+
ret := []Image{}
48+
images := loadImages()
49+
for _, image := range images.Images {
50+
if image.Source != "" {
51+
ret = append(ret, image)
52+
}
53+
}
54+
return ret
55+
}
56+
4157
func ZuulExecutorImage() string {
4258
return getImage("zuul-executor")
4359
}

doc/reference/cli/index.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ deployments, beyond what can be defined in a custom resource manifest.
1616
- [create standalone-sf](#create-standalone-sf)
1717
- [run-tests](#run-tests)
1818
- [wipe gerrit](#wipe-gerrit)
19+
- [getImagesSecurityIssues](#getimagessecurityissues)
1920
1. [Init](#init)
2021
1. [Nodepool](#nodepool)
2122
- [configure providers-secrets](#configure-providers-secrets)
@@ -270,6 +271,16 @@ Flags:
270271
|----------|------|-------|----|----|
271272
| --rm-data | boolean | Also delete persistent data (repositories, reviews) | yes | False |
272273

274+
#### getImagesSecurityIssues
275+
276+
To get a report of Security Issues reported by quay.io for container images used by the
277+
sf-operator run: `dev getImageSecurityIssue`. This command helps to decide if we need to
278+
rebuild container images to benefit last security fixes from the base OS.
279+
280+
```sh
281+
sf-operator dev getImagesSecurityIssues
282+
```
283+
273284
### Init
274285

275286
The `init` subcommand can be used to initialize a CLI configuration file, or a sample manifest for deploying Software Factory.

0 commit comments

Comments
 (0)