|
| 1 | +/* |
| 2 | +Copyright 2024 The cert-manager Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package validation |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "reflect" |
| 22 | + |
| 23 | + "github.com/google/cel-go/cel" |
| 24 | + "github.com/google/cel-go/common/types" |
| 25 | + "github.com/google/cel-go/common/types/ref" |
| 26 | + "k8s.io/apiserver/pkg/authentication/serviceaccount" |
| 27 | +) |
| 28 | + |
| 29 | +var ( |
| 30 | + SAType = cel.ObjectType("cm.io.policy.pkg.internal.approver.validation.ServiceAccount") |
| 31 | +) |
| 32 | + |
| 33 | +type saLib struct{} |
| 34 | +type ServiceAccount struct { |
| 35 | + Name string |
| 36 | + Namespace string |
| 37 | +} |
| 38 | + |
| 39 | +func ServiceAccountLib() cel.EnvOption { |
| 40 | + return cel.Lib(&saLib{}) |
| 41 | +} |
| 42 | + |
| 43 | +// ConvertToNative implements ref.Val.ConvertToNative. |
| 44 | +func (sa ServiceAccount) ConvertToNative(typeDesc reflect.Type) (interface{}, error) { |
| 45 | + if reflect.TypeOf(sa).AssignableTo(typeDesc) { |
| 46 | + return sa, nil |
| 47 | + } |
| 48 | + if reflect.TypeOf("").AssignableTo(typeDesc) { |
| 49 | + return serviceaccount.MakeUsername(sa.Namespace, sa.Name), nil |
| 50 | + } |
| 51 | + return nil, fmt.Errorf("type conversion error from 'serviceaccount' to '%v'", typeDesc) |
| 52 | +} |
| 53 | + |
| 54 | +// ConvertToType implements ref.Val.ConvertToType. |
| 55 | +func (sa ServiceAccount) ConvertToType(typeVal ref.Type) ref.Val { |
| 56 | + switch typeVal { |
| 57 | + case SAType: |
| 58 | + return sa |
| 59 | + case types.TypeType: |
| 60 | + return SAType |
| 61 | + } |
| 62 | + return types.NewErr("type conversion error from '%s' to '%s'", SAType, typeVal) |
| 63 | +} |
| 64 | + |
| 65 | +// Equal implements ref.Val.Equal. |
| 66 | +func (sa ServiceAccount) Equal(other ref.Val) ref.Val { |
| 67 | + otherSA, ok := other.(ServiceAccount) |
| 68 | + if !ok { |
| 69 | + return types.MaybeNoSuchOverloadErr(other) |
| 70 | + } |
| 71 | + return types.Bool(sa.Name == otherSA.Name && sa.Namespace == otherSA.Namespace) |
| 72 | +} |
| 73 | + |
| 74 | +// Type implements ref.Val.Type.Y |
| 75 | +func (sa ServiceAccount) Type() ref.Type { |
| 76 | + return SAType |
| 77 | +} |
| 78 | + |
| 79 | +// Value implements ref.Val.Value. |
| 80 | +func (sa ServiceAccount) Value() interface{} { |
| 81 | + return sa |
| 82 | +} |
| 83 | + |
| 84 | +var saLibraryDecls = map[string][]cel.FunctionOpt{ |
| 85 | + "serviceAccount": { |
| 86 | + cel.Overload("username_to_serviceaccount", []*cel.Type{cel.StringType}, SAType, |
| 87 | + cel.UnaryBinding(stringToServiceAccount))}, |
| 88 | + "getName": { |
| 89 | + cel.MemberOverload("serviceaccount_get_name", []*cel.Type{SAType}, cel.StringType, |
| 90 | + cel.UnaryBinding(getServiceAccountName))}, |
| 91 | + "getNamespace": { |
| 92 | + cel.MemberOverload("serviceaccount_get_namespace", []*cel.Type{SAType}, cel.StringType, |
| 93 | + cel.UnaryBinding(getServiceAccountNamespace))}, |
| 94 | + "isServiceAccount": { |
| 95 | + cel.Overload("serviceaccount_is_sa", []*cel.Type{cel.StringType}, cel.BoolType, |
| 96 | + cel.UnaryBinding(isServiceAccount))}, |
| 97 | +} |
| 98 | + |
| 99 | +func stringToServiceAccount(arg ref.Val) ref.Val { |
| 100 | + s, ok := arg.Value().(string) |
| 101 | + if !ok { |
| 102 | + return types.MaybeNoSuchOverloadErr(arg) |
| 103 | + } |
| 104 | + |
| 105 | + ns, name, err := serviceaccount.SplitUsername(s) |
| 106 | + |
| 107 | + if err != nil { |
| 108 | + return types.NewErr("Unable to convert to serviceaccount: err: %s, username: %s", err, s) |
| 109 | + } |
| 110 | + |
| 111 | + return ServiceAccount{ |
| 112 | + Name: name, |
| 113 | + Namespace: ns, |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +func isServiceAccount(arg ref.Val) ref.Val { |
| 118 | + s, ok := arg.Value().(string) |
| 119 | + if !ok { |
| 120 | + return types.MaybeNoSuchOverloadErr(arg) |
| 121 | + } |
| 122 | + |
| 123 | + _, _, err := serviceaccount.SplitUsername(s) |
| 124 | + |
| 125 | + if err != nil { |
| 126 | + return types.False |
| 127 | + } |
| 128 | + |
| 129 | + return types.True |
| 130 | +} |
| 131 | + |
| 132 | +func getServiceAccountName(arg ref.Val) ref.Val { |
| 133 | + s, ok := arg.Value().(ServiceAccount) |
| 134 | + if !ok { |
| 135 | + return types.MaybeNoSuchOverloadErr(arg) |
| 136 | + } |
| 137 | + |
| 138 | + return types.String(s.Name) |
| 139 | +} |
| 140 | + |
| 141 | +func getServiceAccountNamespace(arg ref.Val) ref.Val { |
| 142 | + s, ok := arg.Value().(ServiceAccount) |
| 143 | + if !ok { |
| 144 | + return types.MaybeNoSuchOverloadErr(arg) |
| 145 | + } |
| 146 | + |
| 147 | + return types.String(s.Namespace) |
| 148 | +} |
| 149 | + |
| 150 | +func (*saLib) CompileOptions() []cel.EnvOption { |
| 151 | + options := []cel.EnvOption{} |
| 152 | + for name, overloads := range saLibraryDecls { |
| 153 | + options = append(options, cel.Function(name, overloads...)) |
| 154 | + } |
| 155 | + return options |
| 156 | +} |
| 157 | + |
| 158 | +func (*saLib) ProgramOptions() []cel.ProgramOption { |
| 159 | + return []cel.ProgramOption{} |
| 160 | +} |
0 commit comments