Skip to content

Commit 0ed4a61

Browse files
committed
Add works w/ MicroShift API validations
1 parent 6085014 commit 0ed4a61

2 files changed

Lines changed: 104 additions & 0 deletions

File tree

pkg/actions/run_validators.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ func RunValidators(bundlePath string, auditBundle *models.AuditBundle, indexImag
3030
checkBundleAgainstCommonCriteria(auditBundle)
3131
fromOCPValidator(auditBundle, bundlePath)
3232

33+
// Are there obvious "won't work with MicroShift" APIs in use?
34+
fromWorksWithMicroShiftAPIsValidator(auditBundle)
35+
3336
// If the index is < 4.9 then do thw following check
3437
if strings.Contains(indexImage, "4.6") ||
3538
strings.Contains(indexImage, "4.7") ||
@@ -106,3 +109,19 @@ func fromAuditValidatorsBundleSize(auditBundle *models.AuditBundle) {
106109

107110
auditBundle.ValidatorsResults = append(auditBundle.ValidatorsResults, nonEmptyResults...)
108111
}
112+
113+
func fromWorksWithMicroShiftAPIsValidator(auditBundle *models.AuditBundle) {
114+
validators := validation.WorksWithMicroShiftAPIsValidator
115+
objs := auditBundle.Bundle.ObjectsToValidate()
116+
117+
nonEmptyResults := []errors.ManifestResult{}
118+
results := validators.Validate(objs...)
119+
120+
for _, result := range results {
121+
if result.HasError() || result.HasWarn() {
122+
nonEmptyResults = append(nonEmptyResults, result)
123+
}
124+
}
125+
126+
auditBundle.ValidatorsResults = append(auditBundle.ValidatorsResults, nonEmptyResults...)
127+
}

pkg/validation/microshift.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Copyright 2023 The Audit Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package validation
16+
17+
import (
18+
"fmt"
19+
"strings"
20+
21+
"github.com/operator-framework/api/pkg/manifests"
22+
"github.com/operator-framework/api/pkg/validation/errors"
23+
interfaces "github.com/operator-framework/api/pkg/validation/interfaces"
24+
)
25+
26+
// WorksWithMicroShiftAPIsValidator will check the bundle for RBAC permissions
27+
// and flag usage of non-compliant Kubernetes API groups.
28+
var WorksWithMicroShiftAPIsValidator interfaces.Validator = interfaces.ValidatorFunc(validateWorksWithMicroShiftAPIs)
29+
30+
func validateWorksWithMicroShiftAPIs(objs ...interface{}) (results []errors.ManifestResult) {
31+
for _, obj := range objs {
32+
switch v := obj.(type) {
33+
case *manifests.Bundle:
34+
results = append(results, validateAPIGroups(v))
35+
}
36+
}
37+
38+
return results
39+
}
40+
41+
func validateAPIGroups(bundle *manifests.Bundle) errors.ManifestResult {
42+
result := errors.ManifestResult{}
43+
if bundle == nil {
44+
result.Add(errors.ErrInvalidBundle("Bundle is nil", nil))
45+
return result
46+
}
47+
result.Name = bundle.Name
48+
49+
if bundle.CSV == nil {
50+
result.Add(errors.ErrInvalidBundle("Bundle csv is nil", bundle.Name))
51+
return result
52+
}
53+
54+
errs := checkAPIGroups(bundle)
55+
result.Add(errs...)
56+
57+
return result
58+
}
59+
60+
func checkAPIGroups(bundle *manifests.Bundle) []errors.Error {
61+
var errs []errors.Error
62+
63+
allPermissions := append(bundle.CSV.Spec.InstallStrategy.StrategySpec.ClusterPermissions, bundle.CSV.Spec.InstallStrategy.StrategySpec.Permissions...)
64+
for _, perm := range allPermissions {
65+
for _, rule := range perm.Rules {
66+
for _, apiGroup := range rule.APIGroups {
67+
if !isValidAPIGroup(apiGroup) {
68+
errs = append(errs, errors.WarnFailedValidation(fmt.Sprintf("Found API group usages not compatible with MicroShift: %s", apiGroup), bundle.Name))
69+
}
70+
}
71+
}
72+
}
73+
74+
return errs
75+
}
76+
77+
func isValidAPIGroup(apiGroup string) bool {
78+
// Allow empty apiGroup, which refers to the core API group in Kubernetes
79+
if apiGroup == "" {
80+
return true
81+
}
82+
return strings.HasSuffix(apiGroup, ".k8s.io") ||
83+
apiGroup == "route.openshift.io" ||
84+
apiGroup == "securitycontextconstraints.openshift.io"
85+
}

0 commit comments

Comments
 (0)