Skip to content

Commit c7c07fc

Browse files
authored
Add release stats cve command (#941)
* Add metrics cve command Signed-off-by: Rafael Breno <32229014+rafaelbreno@users.noreply.github.com> * Add ImageScanningRepository const Signed-off-by: Rafael Breno <32229014+rafaelbreno@users.noreply.github.com> * Add base for `stats cve` command Signed-off-by: Rafael Breno <32229014+rafaelbreno@users.noreply.github.com> * Add PrintCVEBySeverity method Signed-off-by: Rafael Breno <32229014+rafaelbreno@users.noreply.github.com> * Add severity counts * Add slack notify and webhook URL * Skip empty notifications * Add Severity flag * Add http client with timeout * switch from logrus to fmt * Update structure to use Release sets Signed-off-by: Rafael Breno <32229014+rafaelbreno@users.noreply.github.com> * Use ecmHTTP Signed-off-by: Rafael Breno <32229014+rafaelbreno@users.noreply.github.com> * Expand structs to be multiline Signed-off-by: Rafael Breno <32229014+rafaelbreno@users.noreply.github.com> * Align CVE struct Signed-off-by: Rafael Breno <32229014+rafaelbreno@users.noreply.github.com> * Add csvHeaderCount const Signed-off-by: Rafael Breno <32229014+rafaelbreno@users.noreply.github.com> * Add workaround to prevent auto-linking image names Signed-off-by: Rafael Breno <32229014+rafaelbreno@users.noreply.github.com> * Add --skip-mirrored flag Signed-off-by: Rafael Breno <32229014+rafaelbreno@users.noreply.github.com> --------- Signed-off-by: Rafael Breno <32229014+rafaelbreno@users.noreply.github.com>
1 parent 0cb798f commit c7c07fc

3 files changed

Lines changed: 587 additions & 21 deletions

File tree

cmd/release/cmd/stats.go

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,20 @@ import (
1010

1111
"github.com/briandowns/spinner"
1212
"github.com/rancher/ecm-distro-tools/release"
13+
"github.com/rancher/ecm-distro-tools/release/metrics"
1314
"github.com/rancher/ecm-distro-tools/repository"
1415
"github.com/spf13/cobra"
1516
"sigs.k8s.io/yaml"
1617
)
1718

1819
var (
19-
repo *string
20-
startDate *string
21-
endDate *string
22-
format *string
20+
repo *string
21+
startDate *string
22+
endDate *string
23+
format *string
24+
webhookURL *string
25+
severity *string
26+
skipMirrored *bool
2327
)
2428

2529
var repoToOwner = map[string]string{
@@ -28,9 +32,16 @@ var repoToOwner = map[string]string{
2832
"k3s": "k3s-io",
2933
}
3034

31-
// statsCmd represents the stats command
35+
// statsCmd represents the base stats parent command
3236
var statsCmd = &cobra.Command{
3337
Use: "stats",
38+
Short: "Statistics commands",
39+
Long: `Retrieve various statistics including releases and CVEs.`,
40+
}
41+
42+
// releasesStatsCmd represents the release statistics command
43+
var releasesStatsCmd = &cobra.Command{
44+
Use: "releases",
3445
Short: "Release statistics",
3546
Long: `Retrieve release statistics for a time period.`,
3647
RunE: func(cmd *cobra.Command, args []string) error {
@@ -83,23 +94,50 @@ var statsCmd = &cobra.Command{
8394
},
8495
}
8596

97+
var cveStatsSubCmd = &cobra.Command{
98+
Use: "cve",
99+
Short: "CVE statistics command",
100+
Long: `Retrieve CVE statistics from current releases.`,
101+
RunE: func(cmd *cobra.Command, args []string) error {
102+
ctx := context.Background()
103+
ghClient := repository.NewGithub(ctx, rootConfig.Auth.GithubToken)
104+
reports, err := metrics.CVEsMetrics(ctx, ghClient)
105+
if err != nil {
106+
return err
107+
}
108+
109+
return reports.CVEsBySeverity(*severity, *webhookURL, *skipMirrored)
110+
},
111+
}
112+
86113
func init() {
87114
rootCmd.AddCommand(statsCmd)
88115

89-
repo = statsCmd.Flags().StringP("repo", "r", "", "repository")
90-
startDate = statsCmd.Flags().StringP("start", "s", "", "start date")
91-
endDate = statsCmd.Flags().StringP("end", "e", "", "end date")
92-
format = statsCmd.Flags().StringP("format", "f", "json", "format (json|yaml)")
116+
statsCmd.AddCommand(releasesStatsCmd)
117+
statsCmd.AddCommand(cveStatsSubCmd)
118+
119+
repo = releasesStatsCmd.Flags().StringP("repo", "r", "", "repository")
120+
startDate = releasesStatsCmd.Flags().StringP("start", "s", "", "start date")
121+
endDate = releasesStatsCmd.Flags().StringP("end", "e", "", "end date")
122+
format = releasesStatsCmd.Flags().StringP("format", "f", "json", "format (json|yaml)")
123+
webhookURL = cveStatsSubCmd.Flags().StringP("webhook-url", "u", "", "Slack webhook URL for sending messages")
124+
severity = cveStatsSubCmd.Flags().StringP("severity", "s", "critical", "severity (critical|high|medium|low)")
125+
skipMirrored = cveStatsSubCmd.Flags().BoolP("skip-mirrored", "m", false, "skip mirrored images when calculating CVE statistics")
93126

94-
if err := statsCmd.MarkFlagRequired("repo"); err != nil {
127+
if err := releasesStatsCmd.MarkFlagRequired("repo"); err != nil {
95128
fmt.Println(err.Error())
96129
os.Exit(1)
97130
}
98-
if err := statsCmd.MarkFlagRequired("start"); err != nil {
131+
if err := releasesStatsCmd.MarkFlagRequired("start"); err != nil {
99132
fmt.Println(err.Error())
100133
os.Exit(1)
101134
}
102-
if err := statsCmd.MarkFlagRequired("end"); err != nil {
135+
if err := releasesStatsCmd.MarkFlagRequired("end"); err != nil {
136+
fmt.Println(err.Error())
137+
os.Exit(1)
138+
}
139+
140+
if err := cveStatsSubCmd.MarkFlagRequired("webhook-url"); err != nil {
103141
fmt.Println(err.Error())
104142
os.Exit(1)
105143
}

cmd/release/config/config.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@ import (
1010
)
1111

1212
const (
13-
RancherGithubOrganization = "rancher"
14-
RancherRepositoryName = "rancher"
15-
RancherPrimeRepositoryName = "rancher-prime"
16-
UIRepositoryName = "ui"
17-
DashboardRepositoryName = "dashboard"
18-
CLIRepositoryName = "cli"
19-
K3sGithubOrganization = "k3s-io"
20-
K3sRepositoryName = "k3s"
21-
K3sK8sRepositoryName = "kubernetes"
13+
RancherGithubOrganization = "rancher"
14+
RancherRepositoryName = "rancher"
15+
RancherPrimeRepositoryName = "rancher-prime"
16+
UIRepositoryName = "ui"
17+
DashboardRepositoryName = "dashboard"
18+
CLIRepositoryName = "cli"
19+
K3sGithubOrganization = "k3s-io"
20+
K3sRepositoryName = "k3s"
21+
K3sK8sRepositoryName = "kubernetes"
22+
ImageScanningRepositoryName = "image-scanning"
2223
)
2324

2425
const (

0 commit comments

Comments
 (0)