Skip to content

Commit 6085014

Browse files
authored
BUGFIX:variable field len FIPS check-payload works (#157)
Improve FIPS check-payload output parsing Don't log control lines or blanks from check-payload
1 parent 6c171ff commit 6085014

1 file changed

Lines changed: 62 additions & 29 deletions

File tree

cmd/index/bundles/command.go

Lines changed: 62 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ func removeDuplicates(elements []string) []string {
158158
// Define structured types for warnings and errors
159159
type Warning struct {
160160
OperatorName string
161+
RPMName string
161162
ExecutableName string
162163
Status string
163164
Image string
@@ -185,58 +186,90 @@ func ExecuteExternalValidator(imageRef string) (bool, []Warning, []Error, error)
185186
lines := strings.Split(string(output), "\n")
186187
var warnings []Warning
187188
var errors []Error
188-
inFailureReport := false
189-
inWarningReport := false
189+
var currentHeader []string
190+
inFailureReport, inWarningReport := false, false
190191

191192
for _, line := range lines {
192193
log.Infof("External validator line: %s", line)
193194

194195
switch {
195196
case line == "---- Failure Report":
196197
inFailureReport = true
197-
continue
198+
inWarningReport = false
198199
case line == "---- Warning Report":
199200
inWarningReport = true
200-
continue
201-
case line == "---- Successful run" || line == "":
202201
inFailureReport = false
203-
inWarningReport = false
204-
continue
202+
case strings.Contains(line, "Operator Name"):
203+
// Parse header line
204+
currentHeader = strings.Split(line, ",")
205205
case inFailureReport:
206-
parseFailureReportLine(line, &errors)
206+
if currentHeader != nil {
207+
parseReportLine(line, &errors, currentHeader)
208+
}
207209
case inWarningReport:
208-
parseWarningReportLine(line, &warnings)
210+
if currentHeader != nil {
211+
parseReportLine(line, &warnings, currentHeader)
212+
}
213+
}
214+
215+
// Reset states and header for next section
216+
if line == "---- Successful run" || line == "" {
217+
inFailureReport, inWarningReport = false, false
218+
currentHeader = nil
209219
}
210220
}
211221

212222
success := len(errors) == 0
213223
return success, warnings, errors, nil
214224
}
215225

216-
func parseFailureReportLine(line string, errors *[]Error) {
226+
func parseReportLine(line string, report interface{}, header []string) {
227+
// Ignore control lines starting with "----" and blank lines
228+
if strings.HasPrefix(line, "----") || strings.TrimSpace(line) == "" {
229+
return
230+
}
231+
217232
columns := strings.Split(line, ",")
218-
if len(columns) >= 5 {
219-
operatorName, rpmName, executableName, status, image := columns[0], columns[1], columns[2], columns[3], columns[4]
220-
*errors = append(*errors, Error{
221-
OperatorName: strings.TrimSpace(operatorName),
222-
RPMName: strings.TrimSpace(rpmName),
223-
ExecutableName: strings.TrimSpace(executableName),
224-
Status: strings.TrimSpace(status),
225-
Image: strings.TrimSpace(image),
226-
})
233+
if len(columns) < len(header) {
234+
log.Printf("Warning: Line has fewer columns than expected. Skipping line: %s", line)
235+
return
236+
}
237+
238+
data := make(map[string]string)
239+
for i, columnName := range header {
240+
if i < len(columns) {
241+
data[strings.TrimSpace(columnName)] = strings.TrimSpace(columns[i])
242+
}
243+
}
244+
245+
// Dynamically create Warning or Error based on the header
246+
switch v := report.(type) {
247+
case *[]Warning:
248+
warning := Warning{}
249+
fillReportFromData(&warning, data)
250+
*v = append(*v, warning)
251+
case *[]Error:
252+
error := Error{}
253+
fillReportFromData(&error, data)
254+
*v = append(*v, error)
227255
}
228256
}
229257

230-
func parseWarningReportLine(line string, warnings *[]Warning) {
231-
columns := strings.Split(line, ",")
232-
if len(columns) >= 4 {
233-
operatorName, executableName, status, image := columns[0], columns[1], columns[2], columns[3]
234-
*warnings = append(*warnings, Warning{
235-
OperatorName: strings.TrimSpace(operatorName),
236-
ExecutableName: strings.TrimSpace(executableName),
237-
Status: strings.TrimSpace(status),
238-
Image: strings.TrimSpace(image),
239-
})
258+
// fillReportFromData populates a report (Warning or Error) with data from the map
259+
func fillReportFromData(report interface{}, data map[string]string) {
260+
switch v := report.(type) {
261+
case *Warning:
262+
v.OperatorName = data["Operator Name"]
263+
v.RPMName = data["RPM Name"]
264+
v.ExecutableName = data["Executable Name"]
265+
v.Status = data["Status"]
266+
v.Image = data["Image"]
267+
case *Error:
268+
v.OperatorName = data["Operator Name"]
269+
v.RPMName = data["RPM Name"]
270+
v.ExecutableName = data["Executable Name"]
271+
v.Status = data["Status"]
272+
v.Image = data["Image"]
240273
}
241274
}
242275

0 commit comments

Comments
 (0)