|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2025 ArangoDB GmbH, Cologne, Germany |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +// Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | +// |
| 20 | + |
| 21 | +package definition |
| 22 | + |
| 23 | +import ( |
| 24 | + "regexp" |
| 25 | + |
| 26 | + shared "github.com/arangodb/kube-arangodb/pkg/apis/shared" |
| 27 | + "github.com/arangodb/kube-arangodb/pkg/util/errors" |
| 28 | +) |
| 29 | + |
| 30 | +var ( |
| 31 | + actionNameRE = regexp.MustCompile(`^[a-z]+(\.[a-z]+)*$`) |
| 32 | +) |
| 33 | + |
| 34 | +func validateActionName(name string) error { |
| 35 | + if !actionNameRE.MatchString(name) { |
| 36 | + return errors.Errorf("Action `%s` does not match the regex", name) |
| 37 | + } |
| 38 | + |
| 39 | + return nil |
| 40 | +} |
| 41 | + |
| 42 | +var _ shared.ValidateInterface = &AuthorizationV1Policy{} |
| 43 | + |
| 44 | +func (x *AuthorizationV1Action) Validate() error { |
| 45 | + if x == nil { |
| 46 | + return errors.Errorf("Actions is not allowed to be nil") |
| 47 | + } |
| 48 | + |
| 49 | + return shared.WithErrors( |
| 50 | + shared.PrefixResourceError("action", validateActionName(x.Name)), |
| 51 | + shared.ValidateRequiredNotEmptyPath("description", &x.Description), |
| 52 | + shared.PrefixResourceError("subActions", shared.ValidateList(x.SubActions, func(s string) error { |
| 53 | + return validateActionName(s) |
| 54 | + })), |
| 55 | + ) |
| 56 | +} |
| 57 | + |
| 58 | +func (x AuthorizationV1Effect) Validate() error { |
| 59 | + switch x { |
| 60 | + case AuthorizationV1Effect_Allow, AuthorizationV1Effect_Deny: |
| 61 | + return nil |
| 62 | + } |
| 63 | + |
| 64 | + return errors.Errorf("Invalid Effect value") |
| 65 | +} |
| 66 | + |
| 67 | +func (x *AuthorizationV1Statement) Validate() error { |
| 68 | + if x == nil { |
| 69 | + return errors.Errorf("Statement is not allowed to be nil") |
| 70 | + } |
| 71 | + |
| 72 | + return shared.WithErrors( |
| 73 | + shared.ValidateRequiredInterfacePath("effect", x.Effect), |
| 74 | + shared.ValidateRequiredNotEmptyPath("description", &x.Description), |
| 75 | + shared.PrefixResourceError("actions", shared.ValidateList(x.Actions, func(s string) error { |
| 76 | + return validateActionName(s) |
| 77 | + })), |
| 78 | + shared.PrefixResourceError("resources", shared.ValidateList(x.Resources, func(s string) error { |
| 79 | + return shared.ValidateRequiredNotEmpty(&s) |
| 80 | + })), |
| 81 | + ) |
| 82 | +} |
| 83 | + |
| 84 | +func (x *AuthorizationV1Policy) Validate() error { |
| 85 | + if x == nil { |
| 86 | + return errors.Errorf("Statement is not allowed to be nil") |
| 87 | + } |
| 88 | + |
| 89 | + return shared.WithErrors( |
| 90 | + shared.PrefixResourceError("name", validateActionName(x.Name)), |
| 91 | + shared.ValidateRequiredNotEmptyPath("description", &x.Description), |
| 92 | + shared.PrefixResourceError("statements", shared.ValidateInterfaceList(x.Statements)), |
| 93 | + ) |
| 94 | +} |
0 commit comments