Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose the incoming request URL to CEL expressions. #647

Merged
merged 1 commit into from
Jun 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions docs/cel_expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,20 @@ headers are also available.
<pre>header['X-Test'][0] == 'test-value'</pre>
</td>
</tr>
<tr>
<th>
requestURL
</th>
<td>
string
</td>
<td>
This is the URL for the incoming HTTP request.
</td>
<td>
<pre>requestURL.parseURL().path</pre>
</td>
</tr>
</table>

NOTE: The header value is a Go `http.Header`, which is
Expand Down
10 changes: 8 additions & 2 deletions pkg/interceptors/cel/cel.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,9 @@ func makeCelEnv(request *http.Request, ns string, k kubernetes.Interface) (*cel.
celext.Strings(),
cel.Declarations(
decls.NewIdent("body", mapStrDyn, nil),
decls.NewIdent("header", mapStrDyn, nil)))
decls.NewIdent("header", mapStrDyn, nil),
decls.NewIdent("requestURL", decls.String, nil),
))
}

func makeEvalContext(body []byte, r *http.Request) (map[string]interface{}, error) {
Expand All @@ -169,5 +171,9 @@ func makeEvalContext(body []byte, r *http.Request) (map[string]interface{}, erro
if err != nil {
return nil, err
}
return map[string]interface{}{"body": jsonMap, "header": r.Header}, nil
return map[string]interface{}{
"body": jsonMap,
"header": r.Header,
"requestURL": r.URL.String(),
}, nil
}
24 changes: 20 additions & 4 deletions pkg/interceptors/cel/cel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"regexp"
"strings"
Expand Down Expand Up @@ -207,6 +208,7 @@ func TestInterceptor_ExecuteTrigger(t *testing.T) {
}
w := NewInterceptor(tt.CEL, kubeClient, "testing-ns", logger)
request := &http.Request{
URL: mustParseURL(t, "https://testing.example.com"),
Body: tt.payload,
Header: http.Header{
"Content-Type": []string{"application/json"},
Expand Down Expand Up @@ -298,6 +300,7 @@ func TestInterceptor_ExecuteTrigger_Errors(t *testing.T) {
Logger: logger,
}
request := &http.Request{
URL: mustParseURL(t, "https://example.com/testing"),
Body: ioutil.NopCloser(bytes.NewReader(tt.payload)),
GetBody: func() (io.ReadCloser, error) {
return ioutil.NopCloser(bytes.NewReader(tt.payload)), nil
Expand Down Expand Up @@ -337,7 +340,8 @@ func TestExpressionEvaluation(t *testing.T) {
refParts := strings.Split(testRef, "/")
header := http.Header{}
header.Add("X-Test-Header", "value")
evalEnv := map[string]interface{}{"body": jsonMap, "header": header}
req := httptest.NewRequest(http.MethodPost, "https://example.com/testing?param=value", nil)
evalEnv := map[string]interface{}{"body": jsonMap, "header": header, "requestURL": req.URL.String()}
tests := []struct {
name string
expr string
Expand Down Expand Up @@ -447,6 +451,11 @@ func TestExpressionEvaluation(t *testing.T) {
expr: "body.multiURL.parseURL().queryStrings['query']",
want: types.NewStringList(reg, []string{"search", "results"}),
},
{
name: "parse request url",
expr: "requestURL.parseURL().path",
want: types.String("/testing"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(rt *testing.T) {
Expand All @@ -457,11 +466,10 @@ func TestExpressionEvaluation(t *testing.T) {
rt.Error(err)
}
}
env, err := makeCelEnv(&http.Request{}, testNS, kubeClient)
env, err := makeCelEnv(req, testNS, kubeClient)
if err != nil {
t.Fatal(err)
}

got, err := evaluate(tt.expr, env, evalEnv)
if err != nil {
rt.Errorf("evaluate() got an error %s", err)
Expand All @@ -472,7 +480,6 @@ func TestExpressionEvaluation(t *testing.T) {
rt.Errorf("error evaluating expression: %s", got)
return
}

if !got.Equal(tt.want).(types.Bool) {
rt.Errorf("evaluate() = %s, want %s", got, tt.want)
}
Expand Down Expand Up @@ -640,3 +647,12 @@ func makeSecret() *corev1.Secret {
},
}
}

func mustParseURL(t *testing.T, u string) *url.URL {
t.Helper()
parsed, err := url.Parse(u)
if err != nil {
t.Fatal(err)
}
return parsed
}