| 1 |
/* |
| 2 |
Copyright 2023 The Kubernetes 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 library |
| 18 |
|
| 19 |
import ( |
| 20 |
"fmt" |
| 21 |
"net/netip" |
| 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 |
|
| 27 |
apiservercel "k8s.io/apiserver/pkg/cel" |
| 28 |
) |
| 29 |
|
| 30 |
// CIDR provides a CEL function library extension of CIDR notation parsing functions. |
| 31 |
// |
| 32 |
// cidr |
| 33 |
// |
| 34 |
// Converts a string in CIDR notation to a network address representation or results in an error if the string is not a valid CIDR notation. |
| 35 |
// The CIDR must be an IPv4 or IPv6 subnet address with a mask. |
| 36 |
// Leading zeros in IPv4 address octets are not allowed. |
| 37 |
// IPv4-mapped IPv6 addresses (e.g. ::ffff:1.2.3.4/24) are not allowed. |
| 38 |
// |
| 39 |
// cidr(<string>) <CIDR> |
| 40 |
// |
| 41 |
// Examples: |
| 42 |
// |
| 43 |
// cidr('192.168.0.0/16') // returns an IPv4 address with a CIDR mask |
| 44 |
// cidr('::1/128') // returns an IPv6 address with a CIDR mask |
| 45 |
// cidr('192.168.0.0/33') // error |
| 46 |
// cidr('::1/129') // error |
| 47 |
// cidr('192.168.0.1/16') // error, because there are non-0 bits after the prefix |
| 48 |
// |
| 49 |
// isCIDR |
| 50 |
// |
| 51 |
// Returns true if a string is a valid CIDR notation respresentation of a subnet with mask. |
| 52 |
// The CIDR must be an IPv4 or IPv6 subnet address with a mask. |
| 53 |
// Leading zeros in IPv4 address octets are not allowed. |
| 54 |
// IPv4-mapped IPv6 addresses (e.g. ::ffff:1.2.3.4/24) are not allowed. |
| 55 |
// |
| 56 |
// isCIDR(<string>) <bool> |
| 57 |
// |
| 58 |
// Examples: |
| 59 |
// |
| 60 |
// isCIDR('192.168.0.0/16') // returns true |
| 61 |
// isCIDR('::1/128') // returns true |
| 62 |
// isCIDR('192.168.0.0/33') // returns false |
| 63 |
// isCIDR('::1/129') // returns false |
| 64 |
// |
| 65 |
// containsIP / containerCIDR / ip / masked / prefixLength |
| 66 |
// |
| 67 |
// - containsIP: Returns true if a the CIDR contains the given IP address. |
| 68 |
// The IP address must be an IPv4 or IPv6 address. |
| 69 |
// May take either a string or IP address as an argument. |
| 70 |
// |
| 71 |
// - containsCIDR: Returns true if a the CIDR contains the given CIDR. |
| 72 |
// The CIDR must be an IPv4 or IPv6 subnet address with a mask. |
| 73 |
// May take either a string or CIDR as an argument. |
| 74 |
// |
| 75 |
// - ip: Returns the IP address representation of the CIDR. |
| 76 |
// |
| 77 |
// - masked: Returns the CIDR representation of the network address with a masked prefix. |
| 78 |
// This can be used to return the canonical form of the CIDR network. |
| 79 |
// |
| 80 |
// - prefixLength: Returns the prefix length of the CIDR in bits. |
| 81 |
// This is the number of bits in the mask. |
| 82 |
// |
| 83 |
// Examples: |
| 84 |
// |
| 85 |
// cidr('192.168.0.0/24').containsIP(ip('192.168.0.1')) // returns true |
| 86 |
// cidr('192.168.0.0/24').containsIP(ip('192.168.1.1')) // returns false |
| 87 |
// cidr('192.168.0.0/24').containsIP('192.168.0.1') // returns true |
| 88 |
// cidr('192.168.0.0/24').containsIP('192.168.1.1') // returns false |
| 89 |
// cidr('192.168.0.0/16').containsCIDR(cidr('192.168.10.0/24')) // returns true |
| 90 |
// cidr('192.168.1.0/24').containsCIDR(cidr('192.168.2.0/24')) // returns false |
| 91 |
// cidr('192.168.0.0/16').containsCIDR('192.168.10.0/24') // returns true |
| 92 |
// cidr('192.168.1.0/24').containsCIDR('192.168.2.0/24') // returns false |
| 93 |
// cidr('192.168.0.1/24').ip() // returns ipAddr('192.168.0.1') |
| 94 |
// cidr('192.168.0.1/24').ip().family() // returns '4' |
| 95 |
// cidr('::1/128').ip() // returns ipAddr('::1') |
| 96 |
// cidr('::1/128').ip().family() // returns '6' |
| 97 |
// cidr('192.168.0.0/24').masked() // returns cidr('192.168.0.0/24') |
| 98 |
// cidr('192.168.0.1/24').masked() // returns cidr('192.168.0.0/24') |
| 99 |
// cidr('192.168.0.0/24') == cidr('192.168.0.0/24').masked() // returns true, CIDR was already in canonical format |
| 100 |
// cidr('192.168.0.1/24') == cidr('192.168.0.1/24').masked() // returns false, CIDR was not in canonical format |
| 101 |
// cidr('192.168.0.0/16').prefixLength() // returns 16 |
| 102 |
// cidr('::1/128').prefixLength() // returns 128 |
| 103 |
func CIDR() cel.EnvOption { |
| 104 |
return cel.Lib(cidrsLib) |
| 105 |
} |
| 106 |
|
| 107 |
var cidrsLib = &cidrs{} |
| 108 |
|
| 109 |
type cidrs struct{} |
| 110 |
|
| 111 |
func (*cidrs) LibraryName() string { |
| 112 |
return "kubernetes.net.cidr" |
| 113 |
} |
| 114 |
|
| 115 |
func (*cidrs) declarations() map[string][]cel.FunctionOpt { |
| 116 |
return cidrLibraryDecls |
| 117 |
} |
| 118 |
|
| 119 |
func (*cidrs) Types() []*cel.Type { |
| 120 |
return []*cel.Type{apiservercel.CIDRType, apiservercel.IPType} |
| 121 |
} |
| 122 |
|
| 123 |
var cidrLibraryDecls = map[string][]cel.FunctionOpt{ |
| 124 |
"cidr": { |
| 125 |
cel.Overload("string_to_cidr", []*cel.Type{cel.StringType}, apiservercel.CIDRType, |
| 126 |
cel.UnaryBinding(stringToCIDR)), |
| 127 |
}, |
| 128 |
"containsIP": { |
| 129 |
cel.MemberOverload("cidr_contains_ip_string", []*cel.Type{apiservercel.CIDRType, cel.StringType}, cel.BoolType, |
| 130 |
cel.BinaryBinding(cidrContainsIPString)), |
| 131 |
cel.MemberOverload("cidr_contains_ip_ip", []*cel.Type{apiservercel.CIDRType, apiservercel.IPType}, cel.BoolType, |
| 132 |
cel.BinaryBinding(cidrContainsIP)), |
| 133 |
}, |
| 134 |
"containsCIDR": { |
| 135 |
cel.MemberOverload("cidr_contains_cidr_string", []*cel.Type{apiservercel.CIDRType, cel.StringType}, cel.BoolType, |
| 136 |
cel.BinaryBinding(cidrContainsCIDRString)), |
| 137 |
cel.MemberOverload("cidr_contains_cidr", []*cel.Type{apiservercel.CIDRType, apiservercel.CIDRType}, cel.BoolType, |
| 138 |
cel.BinaryBinding(cidrContainsCIDR)), |
| 139 |
}, |
| 140 |
"ip": { |
| 141 |
cel.MemberOverload("cidr_ip", []*cel.Type{apiservercel.CIDRType}, apiservercel.IPType, |
| 142 |
cel.UnaryBinding(cidrToIP)), |
| 143 |
}, |
| 144 |
"prefixLength": { |
| 145 |
cel.MemberOverload("cidr_prefix_length", []*cel.Type{apiservercel.CIDRType}, cel.IntType, |
| 146 |
cel.UnaryBinding(prefixLength)), |
| 147 |
}, |
| 148 |
"masked": { |
| 149 |
cel.MemberOverload("cidr_masked", []*cel.Type{apiservercel.CIDRType}, apiservercel.CIDRType, |
| 150 |
cel.UnaryBinding(masked)), |
| 151 |
}, |
| 152 |
"isCIDR": { |
| 153 |
cel.Overload("is_cidr", []*cel.Type{cel.StringType}, cel.BoolType, |
| 154 |
cel.UnaryBinding(isCIDR)), |
| 155 |
}, |
| 156 |
"string": { |
| 157 |
cel.Overload("cidr_to_string", []*cel.Type{apiservercel.CIDRType}, cel.StringType, |
| 158 |
cel.UnaryBinding(cidrToString)), |
| 159 |
}, |
| 160 |
} |
| 161 |
|
| 162 |
func (*cidrs) CompileOptions() []cel.EnvOption { |
| 163 |
options := []cel.EnvOption{cel.Types(apiservercel.CIDRType)} |
| 164 |
for name, overloads := range cidrLibraryDecls { |
| 165 |
options = append(options, cel.Function(name, overloads...)) |
| 166 |
} |
| 167 |
return options |
| 168 |
} |
| 169 |
|
| 170 |
func (*cidrs) ProgramOptions() []cel.ProgramOption { |
| 171 |
return []cel.ProgramOption{} |
| 172 |
} |
| 173 |
|
| 174 |
func stringToCIDR(arg ref.Val) ref.Val { |
| 175 |
s, ok := arg.Value().(string) |
| 176 |
if !ok { |
| 177 |
return types.MaybeNoSuchOverloadErr(arg) |
| 178 |
} |
| 179 |
|
| 180 |
net, err := parseCIDR(s) |
| 181 |
if err != nil { |
| 182 |
return types.NewErr("network address parse error during conversion from string: %v", err) |
| 183 |
} |
| 184 |
|
| 185 |
return apiservercel.CIDR{ |
| 186 |
Prefix: net, |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
func cidrToString(arg ref.Val) ref.Val { |
| 191 |
cidr, ok := arg.(apiservercel.CIDR) |
| 192 |
if !ok { |
| 193 |
return types.MaybeNoSuchOverloadErr(arg) |
| 194 |
} |
| 195 |
|
| 196 |
return types.String(cidr.Prefix.String()) |
| 197 |
} |
| 198 |
|
| 199 |
func cidrContainsIPString(arg ref.Val, other ref.Val) ref.Val { |
| 200 |
return cidrContainsIP(arg, stringToIP(other)) |
| 201 |
} |
| 202 |
|
| 203 |
func cidrContainsCIDRString(arg ref.Val, other ref.Val) ref.Val { |
| 204 |
return cidrContainsCIDR(arg, stringToCIDR(other)) |
| 205 |
} |
| 206 |
|
| 207 |
func cidrContainsIP(arg ref.Val, other ref.Val) ref.Val { |
| 208 |
cidr, ok := arg.(apiservercel.CIDR) |
| 209 |
if !ok { |
| 210 |
return types.MaybeNoSuchOverloadErr(other) |
| 211 |
} |
| 212 |
|
| 213 |
ip, ok := other.(apiservercel.IP) |
| 214 |
if !ok { |
| 215 |
return types.MaybeNoSuchOverloadErr(arg) |
| 216 |
} |
| 217 |
|
| 218 |
return types.Bool(cidr.Contains(ip.Addr)) |
| 219 |
} |
| 220 |
|
| 221 |
func cidrContainsCIDR(arg ref.Val, other ref.Val) ref.Val { |
| 222 |
cidr, ok := arg.(apiservercel.CIDR) |
| 223 |
if !ok { |
| 224 |
return types.MaybeNoSuchOverloadErr(arg) |
| 225 |
} |
| 226 |
|
| 227 |
containsCIDR, ok := other.(apiservercel.CIDR) |
| 228 |
if !ok { |
| 229 |
return types.MaybeNoSuchOverloadErr(other) |
| 230 |
} |
| 231 |
|
| 232 |
return types.Bool(cidr.Overlaps(containsCIDR.Prefix) && cidr.Prefix.Bits() <= containsCIDR.Prefix.Bits()) |
| 233 |
} |
| 234 |
|
| 235 |
func prefixLength(arg ref.Val) ref.Val { |
| 236 |
cidr, ok := arg.(apiservercel.CIDR) |
| 237 |
if !ok { |
| 238 |
return types.MaybeNoSuchOverloadErr(arg) |
| 239 |
} |
| 240 |
|
| 241 |
return types.Int(cidr.Prefix.Bits()) |
| 242 |
} |
| 243 |
|
| 244 |
func isCIDR(arg ref.Val) ref.Val { |
| 245 |
s, ok := arg.Value().(string) |
| 246 |
if !ok { |
| 247 |
return types.MaybeNoSuchOverloadErr(arg) |
| 248 |
} |
| 249 |
|
| 250 |
_, err := parseCIDR(s) |
| 251 |
return types.Bool(err == nil) |
| 252 |
} |
| 253 |
|
| 254 |
func cidrToIP(arg ref.Val) ref.Val { |
| 255 |
cidr, ok := arg.(apiservercel.CIDR) |
| 256 |
if !ok { |
| 257 |
return types.MaybeNoSuchOverloadErr(arg) |
| 258 |
} |
| 259 |
|
| 260 |
return apiservercel.IP{ |
| 261 |
Addr: cidr.Prefix.Addr(), |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
func masked(arg ref.Val) ref.Val { |
| 266 |
cidr, ok := arg.(apiservercel.CIDR) |
| 267 |
if !ok { |
| 268 |
return types.MaybeNoSuchOverloadErr(arg) |
| 269 |
} |
| 270 |
|
| 271 |
maskedCIDR := cidr.Prefix.Masked() |
| 272 |
return apiservercel.CIDR{ |
| 273 |
Prefix: maskedCIDR, |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
// parseCIDR parses a string into an CIDR. |
| 278 |
// We use this function to parse CIDR notation in the CEL library |
| 279 |
// so that we can share the common logic of rejecting strings |
| 280 |
// that IPv4-mapped IPv6 addresses or contain non-zero bits after the mask. |
| 281 |
func parseCIDR(raw string) (netip.Prefix, error) { |
| 282 |
net, err := netip.ParsePrefix(raw) |
| 283 |
if err != nil { |
| 284 |
return netip.Prefix{}, fmt.Errorf("network address parse error during conversion from string: %v", err) |
| 285 |
} |
| 286 |
|
| 287 |
if net.Addr().Is4In6() { |
| 288 |
return netip.Prefix{}, fmt.Errorf("IPv4-mapped IPv6 address %q is not allowed", raw) |
| 289 |
} |
| 290 |
|
| 291 |
return net, nil |
| 292 |
} |