2022-12-27 08:36:49 +00:00
|
|
|
package apicall
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-09-09 21:04:08 +00:00
|
|
|
"encoding/json"
|
2022-12-27 08:36:49 +00:00
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
2023-02-09 15:15:51 +00:00
|
|
|
"github.com/go-logr/logr"
|
2022-12-27 08:36:49 +00:00
|
|
|
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
|
2023-04-13 11:29:40 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/config"
|
2022-12-27 08:36:49 +00:00
|
|
|
enginecontext "github.com/kyverno/kyverno/pkg/engine/context"
|
2023-04-13 11:29:40 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/engine/jmespath"
|
2022-12-27 08:36:49 +00:00
|
|
|
"gotest.tools/assert"
|
|
|
|
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
|
|
|
|
)
|
|
|
|
|
2023-11-21 10:01:51 +00:00
|
|
|
var (
|
|
|
|
jp = jmespath.New(config.NewDefaultConfiguration(false))
|
|
|
|
apiConfig = APICallConfiguration{
|
|
|
|
maxAPICallResponseLength: 1 * 1000 * 1000,
|
|
|
|
}
|
2023-12-22 17:05:52 +00:00
|
|
|
apiConfigMaxSizeExceed = APICallConfiguration{
|
|
|
|
maxAPICallResponseLength: 10,
|
|
|
|
}
|
|
|
|
apiConfigWithoutSecurityCheck = APICallConfiguration{
|
|
|
|
maxAPICallResponseLength: 0,
|
|
|
|
}
|
2023-11-21 10:01:51 +00:00
|
|
|
)
|
2023-04-13 11:29:40 +00:00
|
|
|
|
2023-12-22 17:05:52 +00:00
|
|
|
func buildTestServer(responseData []byte, useChunked bool) *httptest.Server {
|
2022-12-27 08:36:49 +00:00
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc("/resource", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method == "GET" {
|
2024-09-09 21:04:08 +00:00
|
|
|
auth := r.Header.Get("Authorization")
|
|
|
|
if auth != "Bearer 1234567890" {
|
|
|
|
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
contentType := r.Header.Get("Content-Type")
|
|
|
|
if contentType != "application/json" {
|
|
|
|
http.Error(w, "StatusUnsupportedMediaType", http.StatusUnsupportedMediaType)
|
|
|
|
return
|
|
|
|
}
|
2023-12-22 17:05:52 +00:00
|
|
|
|
|
|
|
if useChunked {
|
|
|
|
flusher, ok := w.(http.Flusher)
|
|
|
|
if !ok {
|
2024-09-09 21:04:08 +00:00
|
|
|
http.Error(w, "expected http.ResponseWriter to be an http.Flusher", http.StatusInternalServerError)
|
|
|
|
return
|
2023-12-22 17:05:52 +00:00
|
|
|
}
|
2024-09-09 21:04:08 +00:00
|
|
|
chunkSize := len(responseData) / 10
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
data := responseData[i*chunkSize : (i+1)*chunkSize]
|
|
|
|
w.Write(data)
|
2023-12-22 17:05:52 +00:00
|
|
|
flusher.Flush()
|
|
|
|
}
|
2024-09-09 21:04:08 +00:00
|
|
|
w.Write(responseData[10*chunkSize:])
|
|
|
|
flusher.Flush()
|
|
|
|
} else {
|
|
|
|
w.Write(responseData)
|
2023-12-22 17:05:52 +00:00
|
|
|
}
|
|
|
|
|
2022-12-27 08:36:49 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.Method == "POST" {
|
|
|
|
defer r.Body.Close()
|
|
|
|
body, _ := io.ReadAll(r.Body)
|
|
|
|
w.Write(body)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return httptest.NewServer(mux)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_serviceGetRequest(t *testing.T) {
|
2023-12-22 17:05:52 +00:00
|
|
|
testfn := func(t *testing.T, useChunked bool) {
|
|
|
|
serverResponse := []byte(`{ "day": "Sunday" }`)
|
2024-09-09 21:04:08 +00:00
|
|
|
s := buildTestServer(serverResponse, useChunked)
|
2023-12-22 17:05:52 +00:00
|
|
|
defer s.Close()
|
2022-12-27 08:36:49 +00:00
|
|
|
|
2023-12-22 17:05:52 +00:00
|
|
|
entry := kyvernov1.ContextEntry{}
|
|
|
|
ctx := enginecontext.NewContext(jp)
|
2022-12-27 08:36:49 +00:00
|
|
|
|
2023-12-22 17:05:52 +00:00
|
|
|
_, err := New(logr.Discard(), jp, entry, ctx, nil, apiConfig)
|
|
|
|
assert.ErrorContains(t, err, "missing APICall")
|
2022-12-27 08:36:49 +00:00
|
|
|
|
2023-12-22 17:05:52 +00:00
|
|
|
entry.Name = "test"
|
2024-02-02 17:42:20 +00:00
|
|
|
entry.APICall = &kyvernov1.ContextAPICall{
|
|
|
|
APICall: kyvernov1.APICall{
|
|
|
|
Service: &kyvernov1.ServiceCall{
|
|
|
|
URL: s.URL,
|
2024-09-09 21:04:08 +00:00
|
|
|
Headers: []kyvernov1.HTTPHeader{
|
|
|
|
{Key: "Authorization", Value: "Bearer 1234567890"},
|
|
|
|
{Key: "Content-Type", Value: "application/json"},
|
|
|
|
},
|
2024-02-02 17:42:20 +00:00
|
|
|
},
|
2023-12-22 17:05:52 +00:00
|
|
|
},
|
|
|
|
}
|
2022-12-27 08:36:49 +00:00
|
|
|
|
2023-12-22 17:05:52 +00:00
|
|
|
call, err := New(logr.Discard(), jp, entry, ctx, nil, apiConfig)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
_, err = call.FetchAndLoad(context.TODO())
|
|
|
|
assert.ErrorContains(t, err, "invalid request type")
|
|
|
|
|
|
|
|
entry.APICall.Method = "GET"
|
|
|
|
call, err = New(logr.Discard(), jp, entry, ctx, nil, apiConfig)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
_, err = call.FetchAndLoad(context.TODO())
|
|
|
|
assert.ErrorContains(t, err, "HTTP 404")
|
|
|
|
|
|
|
|
entry.APICall.Service.URL = s.URL + "/resource"
|
|
|
|
call, err = New(logr.Discard(), jp, entry, ctx, nil, apiConfig)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
data, err := call.FetchAndLoad(context.TODO())
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Assert(t, data != nil, "nil data")
|
|
|
|
assert.Equal(t, string(serverResponse), string(data))
|
|
|
|
|
|
|
|
call, err = New(logr.Discard(), jp, entry, ctx, nil, apiConfigMaxSizeExceed)
|
|
|
|
assert.NilError(t, err)
|
2024-03-11 07:30:29 +00:00
|
|
|
_, err = call.FetchAndLoad(context.TODO())
|
|
|
|
assert.ErrorContains(t, err, "response length must be less than max allowed response length of 10")
|
2023-12-22 17:05:52 +00:00
|
|
|
|
|
|
|
call, err = New(logr.Discard(), jp, entry, ctx, nil, apiConfigWithoutSecurityCheck)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
data, err = call.FetchAndLoad(context.TODO())
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Assert(t, data != nil, "nil data")
|
|
|
|
assert.Equal(t, string(serverResponse), string(data))
|
|
|
|
}
|
2022-12-27 08:36:49 +00:00
|
|
|
|
2023-12-22 17:05:52 +00:00
|
|
|
t.Run("Standard", func(t *testing.T) { testfn(t, false) })
|
|
|
|
t.Run("Chunked", func(t *testing.T) { testfn(t, true) })
|
2022-12-27 08:36:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_servicePostRequest(t *testing.T) {
|
|
|
|
serverResponse := []byte(`{ "day": "Monday" }`)
|
2023-12-22 17:05:52 +00:00
|
|
|
s := buildTestServer(serverResponse, false)
|
2022-12-27 08:36:49 +00:00
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
entry := kyvernov1.ContextEntry{
|
|
|
|
Name: "test",
|
2024-02-02 17:42:20 +00:00
|
|
|
APICall: &kyvernov1.ContextAPICall{
|
|
|
|
APICall: kyvernov1.APICall{
|
|
|
|
Method: "POST",
|
|
|
|
Service: &kyvernov1.ServiceCall{
|
|
|
|
URL: s.URL + "/resource",
|
|
|
|
},
|
2022-12-27 08:36:49 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-04-13 11:29:40 +00:00
|
|
|
ctx := enginecontext.NewContext(jp)
|
2023-11-21 10:01:51 +00:00
|
|
|
call, err := New(logr.Discard(), jp, entry, ctx, nil, apiConfig)
|
2022-12-27 08:36:49 +00:00
|
|
|
assert.NilError(t, err)
|
2023-06-26 13:31:40 +00:00
|
|
|
data, err := call.FetchAndLoad(context.TODO())
|
2022-12-27 08:36:49 +00:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Equal(t, "{}\n", string(data))
|
|
|
|
|
|
|
|
imageData := `{
|
|
|
|
"containers": {
|
|
|
|
"tomcat": {
|
|
|
|
"reference": "https://ghcr.io/tomcat/tomcat:9",
|
|
|
|
"registry": "https://ghcr.io",
|
|
|
|
"path": "tomcat",
|
|
|
|
"name": "tomcat",
|
|
|
|
"tag": "9"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"initContainers": {
|
|
|
|
"vault": {
|
|
|
|
"reference": "https://ghcr.io/vault/vault:v3",
|
|
|
|
"registry": "https://ghcr.io",
|
|
|
|
"path": "vault",
|
|
|
|
"name": "vault",
|
|
|
|
"tag": "v3"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"ephemeralContainers": {
|
|
|
|
"vault": {
|
|
|
|
"reference": "https://ghcr.io/busybox/busybox:latest",
|
|
|
|
"registry": "https://ghcr.io",
|
|
|
|
"path": "busybox",
|
|
|
|
"name": "busybox",
|
|
|
|
"tag": "latest"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
|
|
|
|
err = ctx.AddContextEntry("images", []byte(imageData))
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
2023-04-26 23:31:44 +00:00
|
|
|
entry.APICall.Data = []kyvernov1.RequestData{
|
2022-12-27 08:36:49 +00:00
|
|
|
{
|
|
|
|
Key: "images",
|
|
|
|
Value: &apiextensionsv1.JSON{
|
|
|
|
Raw: []byte("\"{{ images.[containers, initContainers, ephemeralContainers][].*.reference[] }}\""),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-11-21 10:01:51 +00:00
|
|
|
call, err = New(logr.Discard(), jp, entry, ctx, nil, apiConfig)
|
2022-12-27 08:36:49 +00:00
|
|
|
assert.NilError(t, err)
|
2023-06-26 13:31:40 +00:00
|
|
|
data, err = call.FetchAndLoad(context.TODO())
|
2022-12-27 08:36:49 +00:00
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
expectedResults := `{"images":["https://ghcr.io/tomcat/tomcat:9","https://ghcr.io/vault/vault:v3","https://ghcr.io/busybox/busybox:latest"]}`
|
|
|
|
assert.Equal(t, string(expectedResults)+"\n", string(data))
|
|
|
|
}
|
2024-09-09 21:04:08 +00:00
|
|
|
|
|
|
|
func buildEchoHeaderTestServer() *httptest.Server {
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc("/resource", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method == "GET" {
|
|
|
|
responseData := make(map[string][]string)
|
|
|
|
for k, v := range r.Header {
|
|
|
|
responseData[k] = v
|
|
|
|
}
|
|
|
|
responseBytes, err := json.Marshal(responseData)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Write(responseBytes)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return httptest.NewServer(mux)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_serviceHeaders(t *testing.T) {
|
|
|
|
s := buildEchoHeaderTestServer()
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
entry := kyvernov1.ContextEntry{}
|
|
|
|
ctx := enginecontext.NewContext(jp)
|
|
|
|
|
|
|
|
entry.Name = "test"
|
|
|
|
entry.APICall = &kyvernov1.ContextAPICall{
|
|
|
|
APICall: kyvernov1.APICall{
|
|
|
|
Method: "GET",
|
|
|
|
Service: &kyvernov1.ServiceCall{
|
|
|
|
URL: s.URL + "/resource",
|
|
|
|
Headers: []kyvernov1.HTTPHeader{
|
|
|
|
{Key: "Content-Type", Value: "application/json"},
|
|
|
|
{Key: "Custom-Key", Value: "CustomVal"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
entry.APICall.Service.URL = s.URL + "/resource"
|
|
|
|
call, err := New(logr.Discard(), jp, entry, ctx, nil, apiConfig)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
data, err := call.FetchAndLoad(context.TODO())
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Assert(t, data != nil, "nil data")
|
|
|
|
|
|
|
|
var responseHeaders map[string][]string
|
|
|
|
err = json.Unmarshal(data, &responseHeaders)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Equal(t, 4, len(responseHeaders))
|
|
|
|
assert.Equal(t, "application/json", responseHeaders["Content-Type"][0])
|
|
|
|
assert.Equal(t, "CustomVal", responseHeaders["Custom-Key"][0])
|
|
|
|
}
|