Skip to content

Commit

Permalink
Merge pull request #214 from jvanz/issue212
Browse files Browse the repository at this point in the history
fix(report): Result field is optional.
  • Loading branch information
fabriziosestito authored Mar 4, 2024
2 parents 7af7bc2 + 26e6d90 commit 8987beb
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
6 changes: 5 additions & 1 deletion internal/report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,11 @@ func newPolicyReportResult(policy policiesv1.Policy, admissionReview *admissionv
}

var message string
if !errored {
// We need to check if Result is not nil because this field is
// optional. If the policy returns "allowed" to the admissionReview,
// the Result field is not checked by Kubernetes.
// https://pkg.go.dev/k8s.io/[email protected]/admission/v1#AdmissionResponse
if !errored && admissionReview.Response.Result != nil {
message = admissionReview.Response.Result.Message
}

Expand Down
4 changes: 2 additions & 2 deletions internal/report/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func TestNewPolicyReportResult(t *testing.T) {
amissionReview: &admissionv1.AdmissionReview{
Response: &admissionv1.AdmissionResponse{
Allowed: true,
Result: &metav1.Status{Message: "The request was allowed"},
Result: nil,
},
},
errored: false,
Expand All @@ -149,7 +149,7 @@ func TestNewPolicyReportResult(t *testing.T) {
Timestamp: now,
Scored: true,
SubjectSelector: &metav1.LabelSelector{},
Description: "The request was allowed",
Description: "",
Properties: map[string]string{
PropertyPolicyUID: "policy-uid",
propertyPolicyResourceVersion: "1",
Expand Down
4 changes: 1 addition & 3 deletions internal/scanner/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ func newMockPolicyServer() *httptest.Server {
admissionReview := admissionv1.AdmissionReview{
Response: &admissionv1.AdmissionResponse{
Allowed: true,
Result: &metav1.Status{
Message: "The request was allowed",
},
Result: nil,
},
}
response, err := json.Marshal(admissionReview)
Expand Down

0 comments on commit 8987beb

Please sign in to comment.