Debian Codegraph @

pkg/cel/library/urls.go

raw
1 /*
2 Copyright 2022 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 "net/url"
21
22 "github.com/google/cel-go/cel"
23 "github.com/google/cel-go/common/types"
24 "github.com/google/cel-go/common/types/ref"
25
26 apiservercel "k8s.io/apiserver/pkg/cel"
27 )
28
29 // URLs provides a CEL function library extension of URL parsing functions.
30 //
31 // url
32 //
33 // Converts a string to a URL or results in an error if the string is not a valid URL. The URL must be an absolute URI
34 // or an absolute path.
35 //
36 // url(<string>) <URL>
37 //
38 // Examples:
39 //
40 // url('https://user:[email protected]:80/path?query=val#fragment') // returns a URL
41 // url('/absolute-path') // returns a URL
42 // url('https://a:b:c/') // error
43 // url('../relative-path') // error
44 //
45 // isURL
46 //
47 // Returns true if a string is a valid URL. The URL must be an absolute URI or an absolute path.
48 //
49 // isURL( <string>) <bool>
50 //
51 // Examples:
52 //
53 // isURL('https://user:[email protected]:80/path?query=val#fragment') // returns true
54 // isURL('/absolute-path') // returns true
55 // isURL('https://a:b:c/') // returns false
56 // isURL('../relative-path') // returns false
57 //
58 // getScheme / getHost / getHostname / getPort / getEscapedPath / getQuery
59 //
60 // Return the parsed components of a URL.
61 //
62 // - getScheme: If absent in the URL, returns an empty string.
63 //
64 // - getHostname: IPv6 addresses are returned without braces, e.g. "::1". If absent in the URL, returns an empty string.
65 //
66 // - getHost: IPv6 addresses are returned with braces, e.g. "[::1]". If absent in the URL, returns an empty string.
67 //
68 // - getEscapedPath: The string returned by getEscapedPath is URL escaped, e.g. "with space" becomes "with%20space".
69 // If absent in the URL, returns an empty string.
70 //
71 // - getPort: If absent in the URL, returns an empty string.
72 //
73 // - getQuery: Returns the query parameters in "matrix" form where a repeated query key is interpreted to
74 // mean that there are multiple values for that key. The keys and values are returned unescaped.
75 // If absent in the URL, returns an empty map.
76 //
77 // <URL>.getScheme() <string>
78 // <URL>.getHost() <string>
79 // <URL>.getHostname() <string>
80 // <URL>.getPort() <string>
81 // <URL>.getEscapedPath() <string>
82 // <URL>.getQuery() <map <string>, <list <string>>
83 //
84 // Examples:
85 //
86 // url('/path').getScheme() // returns ''
87 // url('https://example.com/').getScheme() // returns 'https'
88 // url('https://example.com:80/').getHost() // returns 'example.com:80'
89 // url('https://example.com/').getHost() // returns 'example.com'
90 // url('https://[::1]:80/').getHost() // returns '[::1]:80'
91 // url('https://[::1]/').getHost() // returns '[::1]'
92 // url('/path').getHost() // returns ''
93 // url('https://example.com:80/').getHostname() // returns 'example.com'
94 // url('https://127.0.0.1:80/').getHostname() // returns '127.0.0.1'
95 // url('https://[::1]:80/').getHostname() // returns '::1'
96 // url('/path').getHostname() // returns ''
97 // url('https://example.com:80/').getPort() // returns '80'
98 // url('https://example.com/').getPort() // returns ''
99 // url('/path').getPort() // returns ''
100 // url('https://example.com/path').getEscapedPath() // returns '/path'
101 // url('https://example.com/path with spaces/').getEscapedPath() // returns '/path%20with%20spaces/'
102 // url('https://example.com').getEscapedPath() // returns ''
103 // url('https://example.com/path?k1=a&k2=b&k2=c').getQuery() // returns { 'k1': ['a'], 'k2': ['b', 'c']}
104 // url('https://example.com/path?key with spaces=value with spaces').getQuery() // returns { 'key with spaces': ['value with spaces']}
105 // url('https://example.com/path?').getQuery() // returns {}
106 // url('https://example.com/path').getQuery() // returns {}
107 func URLs() cel.EnvOption {
108 return cel.Lib(urlsLib)
109 }
110
111 var urlsLib = &urls{}
112
113 type urls struct{}
114
115 func (*urls) LibraryName() string {
116 return "kubernetes.urls"
117 }
118
119 func (*urls) Types() []*cel.Type {
120 return []*cel.Type{apiservercel.URLType}
121 }
122
123 func (*urls) declarations() map[string][]cel.FunctionOpt {
124 return urlLibraryDecls
125 }
126
127 var urlLibraryDecls = map[string][]cel.FunctionOpt{
128 "url": {
129 cel.Overload("string_to_url", []*cel.Type{cel.StringType}, apiservercel.URLType,
130 cel.UnaryBinding(stringToUrl))},
131 "getScheme": {
132 cel.MemberOverload("url_get_scheme", []*cel.Type{apiservercel.URLType}, cel.StringType,
133 cel.UnaryBinding(getScheme))},
134 "getHost": {
135 cel.MemberOverload("url_get_host", []*cel.Type{apiservercel.URLType}, cel.StringType,
136 cel.UnaryBinding(getHost))},
137 "getHostname": {
138 cel.MemberOverload("url_get_hostname", []*cel.Type{apiservercel.URLType}, cel.StringType,
139 cel.UnaryBinding(getHostname))},
140 "getPort": {
141 cel.MemberOverload("url_get_port", []*cel.Type{apiservercel.URLType}, cel.StringType,
142 cel.UnaryBinding(getPort))},
143 "getEscapedPath": {
144 cel.MemberOverload("url_get_escaped_path", []*cel.Type{apiservercel.URLType}, cel.StringType,
145 cel.UnaryBinding(getEscapedPath))},
146 "getQuery": {
147 cel.MemberOverload("url_get_query", []*cel.Type{apiservercel.URLType},
148 cel.MapType(cel.StringType, cel.ListType(cel.StringType)),
149 cel.UnaryBinding(getQuery))},
150 "isURL": {
151 cel.Overload("is_url_string", []*cel.Type{cel.StringType}, cel.BoolType,
152 cel.UnaryBinding(isURL))},
153 }
154
155 func (*urls) CompileOptions() []cel.EnvOption {
156 options := []cel.EnvOption{}
157 for name, overloads := range urlLibraryDecls {
158 options = append(options, cel.Function(name, overloads...))
159 }
160 return options
161 }
162
163 func (*urls) ProgramOptions() []cel.ProgramOption {
164 return []cel.ProgramOption{}
165 }
166
167 func stringToUrl(arg ref.Val) ref.Val {
168 s, ok := arg.Value().(string)
169 if !ok {
170 return types.MaybeNoSuchOverloadErr(arg)
171 }
172 // Use ParseRequestURI to check the URL before conversion.
173 // ParseRequestURI requires absolute URLs and is used by the OpenAPIv3 'uri' format.
174 _, err := url.ParseRequestURI(s)
175 if err != nil {
176 return types.NewErr("URL parse error during conversion from string: %v", err)
177 }
178 // We must parse again with Parse since ParseRequestURI incorrectly parses URLs that contain a fragment
179 // part and will incorrectly append the fragment to either the path or the query, depending on which it was adjacent to.
180 u, err := url.Parse(s)
181 if err != nil {
182 // Errors are not expected here since Parse is a more lenient parser than ParseRequestURI.
183 return types.NewErr("URL parse error during conversion from string: %v", err)
184 }
185 return apiservercel.URL{URL: u}
186 }
187
188 func getScheme(arg ref.Val) ref.Val {
189 u, ok := arg.Value().(*url.URL)
190 if !ok {
191 return types.MaybeNoSuchOverloadErr(arg)
192 }
193 return types.String(u.Scheme)
194 }
195
196 func getHost(arg ref.Val) ref.Val {
197 u, ok := arg.Value().(*url.URL)
198 if !ok {
199 return types.MaybeNoSuchOverloadErr(arg)
200 }
201 return types.String(u.Host)
202 }
203
204 func getHostname(arg ref.Val) ref.Val {
205 u, ok := arg.Value().(*url.URL)
206 if !ok {
207 return types.MaybeNoSuchOverloadErr(arg)
208 }
209 return types.String(u.Hostname())
210 }
211
212 func getPort(arg ref.Val) ref.Val {
213 u, ok := arg.Value().(*url.URL)
214 if !ok {
215 return types.MaybeNoSuchOverloadErr(arg)
216 }
217 return types.String(u.Port())
218 }
219
220 func getEscapedPath(arg ref.Val) ref.Val {
221 u, ok := arg.Value().(*url.URL)
222 if !ok {
223 return types.MaybeNoSuchOverloadErr(arg)
224 }
225 return types.String(u.EscapedPath())
226 }
227
228 func getQuery(arg ref.Val) ref.Val {
229 u, ok := arg.Value().(*url.URL)
230 if !ok {
231 return types.MaybeNoSuchOverloadErr(arg)
232 }
233
234 result := map[ref.Val]ref.Val{}
235 for k, v := range u.Query() {
236 result[types.String(k)] = types.NewStringList(types.DefaultTypeAdapter, v)
237 }
238 return types.NewRefValMap(types.DefaultTypeAdapter, result)
239 }
240
241 func isURL(arg ref.Val) ref.Val {
242 s, ok := arg.Value().(string)
243 if !ok {
244 return types.MaybeNoSuchOverloadErr(arg)
245 }
246 _, err := url.ParseRequestURI(s)
247 return types.Bool(err == nil)
248 }