-
Notifications
You must be signed in to change notification settings - Fork 15
/
run_scorecard.go
206 lines (189 loc) · 6.56 KB
/
run_scorecard.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
// Copyright 2021 The Audit Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package actions
import (
"encoding/json"
"errors" //nolint: typecheck
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
goyaml "github.com/goccy/go-yaml"
"github.com/operator-framework/api/pkg/apis/scorecard/v1alpha3"
"github.com/operator-framework/audit/pkg"
"github.com/operator-framework/audit/pkg/models"
log "github.com/sirupsen/logrus"
)
const defaultSDKScorecardImageName = "quay.io/operator-framework/scorecard-test"
const scorecardAnnotation = "operators.operatorframework.io.test.config.v1"
type BundleAnnotations struct {
Annotations map[string]string `yaml:"annotations,omitempty"`
}
func RunScorecard(bundleDir string, auditBundle *models.AuditBundle) *models.AuditBundle {
scorecardTestsPath := filepath.Join(bundleDir, "tests", "scorecard")
annotationsPath := filepath.Join(bundleDir, "metadata", "annotations.yaml")
// If we find the annotations file then, check for the scorecard path on it.
if _, err := os.Stat(annotationsPath); err == nil && !os.IsNotExist(err) {
annFile, err := pkg.ReadFile(annotationsPath)
if err != nil {
msg := fmt.Errorf("unable to read annotations.yaml to check scorecard path: %s", err)
log.Error(msg)
auditBundle.Errors = append(auditBundle.Errors, msg.Error())
}
var bundleAnnotations BundleAnnotations
if err := goyaml.Unmarshal(annFile, &bundleAnnotations); err != nil {
msg := fmt.Errorf("unable to Unmarshal annotations.yaml to check scorecard path: %s", err)
log.Error(msg)
auditBundle.Errors = append(auditBundle.Errors, msg.Error())
}
if len(bundleAnnotations.Annotations) > 0 {
path := bundleAnnotations.Annotations[scorecardAnnotation]
if len(path) > 0 {
scorecardTestsPath = filepath.Join(bundleDir, path)
}
}
}
// Check if it has scorecard manifests
if _, err := os.Stat(scorecardTestsPath); err != nil {
if os.IsNotExist(err) {
if err := os.MkdirAll(scorecardTestsPath, os.ModePerm); err != nil {
auditBundle.Errors = append(auditBundle.Errors,
fmt.Errorf("unable to create scorecard dir test: %s", err).Error())
return auditBundle
}
} else {
auditBundle.Errors = append(auditBundle.Errors,
fmt.Errorf("unexpected error to run scorecard: %s", err).Error())
return auditBundle
}
} else {
err := filepath.Walk(scorecardTestsPath, func(path string, info os.FileInfo, err error) error {
if info != nil && !info.IsDir() && strings.HasSuffix(info.Name(), "yaml") {
scorecardFilePath := filepath.Join(scorecardTestsPath, info.Name())
if existingFile, err := os.ReadFile(scorecardFilePath); err == nil {
var scorecardConfig v1alpha3.Configuration
if err := goyaml.Unmarshal(existingFile, &scorecardConfig); err != nil {
msg := fmt.Errorf("unable to Unmarshal scorecard file %s: %s", info.Name(), err)
log.Error(msg)
auditBundle.Errors = append(auditBundle.Errors, msg.Error())
}
for _, k := range scorecardConfig.Stages {
for _, t := range k.Tests {
if !strings.Contains(t.Image, defaultSDKScorecardImageName) {
auditBundle.HasCustomScorecardTests = true
break
}
}
}
}
}
return nil
})
if err != nil {
msg := fmt.Errorf("unable to walk in scorecard filse: %s", err)
log.Error(msg)
auditBundle.Errors = append(auditBundle.Errors, msg.Error())
}
}
if err := writeScorecardConfig(scorecardTestsPath); err != nil {
msg := fmt.Errorf("unable to write scorecard default tests: %s", err)
log.Error(msg)
auditBundle.Errors = append(auditBundle.Errors, msg.Error())
return auditBundle
}
// run scorecard against bundle
cmd := exec.Command("operator-sdk", "scorecard", bundleDir, "--wait-time=120s", "--output=json")
output, _ := pkg.RunCommand(cmd)
if len(output) < 1 {
log.Errorf("unable to get scorecard output: %s", output)
auditBundle.Errors = append(auditBundle.Errors,
fmt.Errorf("unable to run scorecard: %s", errors.New("unable get scorecard output")).Error())
return auditBundle
}
var scorecardResults v1alpha3.TestList
err := json.Unmarshal(output, &scorecardResults)
if err != nil {
auditBundle.Errors = append(auditBundle.Errors,
fmt.Errorf("unable to run scorecard: %s", err).Error())
return auditBundle
}
auditBundle.ScorecardResults = scorecardResults
return auditBundle
}
// writeScorecardConfig always the config file for audit
func writeScorecardConfig(scorecardConfigPath string) error {
auditScorecardConfig := filepath.Join(scorecardConfigPath, "config.yaml")
cmd := exec.Command("rm", "-rf", auditScorecardConfig)
_, _ = pkg.RunCommand(cmd)
f, err := os.Create(auditScorecardConfig)
if err != nil {
log.Error(err)
return err
}
_, err = f.WriteString(scorecardDefaultConfigFragment)
if err != nil {
return err
}
return nil
}
const scorecardDefaultConfigFragment = `apiVersion: scorecard.operatorframework.io/v1alpha3
kind: Configuration
metadata:
name: config
stages:
- parallel: true
tests:
- entrypoint:
- scorecard-test
- basic-check-spec
image: quay.io/operator-framework/scorecard-test:v1.23.0
labels:
suite: basic
test: basic-check-spec-test
- entrypoint:
- scorecard-test
- olm-bundle-validation
image: quay.io/operator-framework/scorecard-test:v1.23.0
labels:
suite: olm
test: olm-bundle-validation-test
- entrypoint:
- scorecard-test
- olm-crds-have-validation
image: quay.io/operator-framework/scorecard-test:v1.23.0
labels:
suite: olm
test: olm-crds-have-validation-test
- entrypoint:
- scorecard-test
- olm-crds-have-resources
image: quay.io/operator-framework/scorecard-test:v1.23.0
labels:
suite: olm
test: olm-crds-have-resources-test
- entrypoint:
- scorecard-test
- olm-spec-descriptors
image: quay.io/operator-framework/scorecard-test:v1.23.0
labels:
suite: olm
test: olm-spec-descriptors-test
- entrypoint:
- scorecard-test
- olm-status-descriptors
image: quay.io/operator-framework/scorecard-test:v1.23.0
labels:
suite: olm
test: olm-status-descriptors-test`