Skip to content

Commit

Permalink
Merge pull request #509 from yuumasato/handle-no-objects-to-iterate-o…
Browse files Browse the repository at this point in the history
…n-filter

OCPBUGS-33067: Don't fatal error when filter cannot iterate
  • Loading branch information
openshift-merge-bot[bot] committed May 3, 2024
2 parents 78f8f22 + 8042b4f commit e9ca64b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
8 changes: 8 additions & 0 deletions cmd/manager/scap.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const (

var (
MoreThanOneObjErr = errors.New("more than one object returned from the filter")
NullValErr = errors.New("no value was returned from the filter")
)

// resourceFetcherClients just gathers several needed structs together so we can
Expand Down Expand Up @@ -520,6 +521,8 @@ func fetch(ctx context.Context, streamDispatcher streamerDispatcherFn, rfClients
filteredBody, filterErr := filter(ctx, body, rpath.Filter)
if errors.Is(filterErr, MoreThanOneObjErr) {
warnings = append(warnings, filterErr.Error())
} else if errors.Is(filterErr, NullValErr) {
warnings = append(warnings, fmt.Sprintf("couldn't filter '%s': %s", body, filterErr.Error()))
} else if filterErr != nil {
return fmt.Errorf("couldn't filter '%s': %w", body, filterErr)
}
Expand Down Expand Up @@ -554,6 +557,11 @@ func filter(ctx context.Context, rawobj []byte, filter string) ([]byte, error) {
}
if err, ok := v.(error); ok {
DBG("Error while filtering: %s", err)
// gojq may return a diverse set of internal errors caused by null values.
// These errors are happen when a piped filter ends up acting on a null value.
if strings.HasSuffix(err.Error(), ": null") {
return nil, fmt.Errorf("Skipping empty filter result from '%s': %w", filter, NullValErr)
}
return nil, err
}

Expand Down
14 changes: 14 additions & 0 deletions cmd/manager/scap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,20 @@ var _ = Describe("Testing filtering", func() {
Expect(filterErr).Should(MatchError(MoreThanOneObjErr))
})
})
Context("Piped Filtering", func() {
var rawmc []byte
BeforeEach(func() {
nsFile, err := os.Open("../../tests/data/empty_machineconfig.json")
Expect(err).To(BeNil())
var readErr error
rawmc, readErr = io.ReadAll(nsFile)
Expect(readErr).To(BeNil())
})
It("skips filter piping errors", func() {
_, filterErr := filter(context.TODO(), rawmc, `[.items[] | select(.metadata.name | test("^rendered-worker-[0-9a-z]+$|^rendered-master-[0-9a-z]+$"))] | map(.spec.fips == true)`)
Expect(filterErr).Should(MatchError(NullValErr))
})
})
})
})

Expand Down
4 changes: 4 additions & 0 deletions tests/data/empty_machineconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"metadata": {},
"items": null
}

0 comments on commit e9ca64b

Please sign in to comment.