|
| 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