Skip to content

Implement code rewriting for conn-validator #18157

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

Merged
merged 5 commits into from
Sep 13, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http"
"net/http/httputil"
"regexp"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -255,8 +256,12 @@ func createReverseProxy(log *logger.Logger, destinationHost string, reqOpts ...r

log.WithTracing(request.Context()).With("handler", handlerName).With("targetURL", request.URL).Infof("Proxying request to target URL...")
},
ModifyResponse: func(response *http.Response) error {
log.WithContext().With("handler", handlerName).Infof("Host responded with status %s", response.Status)
ModifyResponse: func(res *http.Response) error {
log.WithContext().With("handler", handlerName).Infof("Host responded with status %s", res.Status)
if res.StatusCode >= 500 && res.StatusCode < 600 {
res.Header.Set("Target-System-Status", strconv.Itoa(res.StatusCode))
res.StatusCode = http.StatusBadGateway
}
return nil
},
Transport: &http.Transport{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,62 @@ func TestProxyHandler_ProxyAppConnectorRequests(t *testing.T) {
}
})

t.Run("should rewrite 5xx codes", func(t *testing.T) {
eventPublisherProxyHandler := mux.NewRouter()
eventPublisherProxyServer := httptest.NewServer(eventPublisherProxyHandler)
eventPublisherProxyHost := strings.TrimPrefix(eventPublisherProxyServer.URL, "http://")

application := applicationNotManagedByCompass

cert := `Hash=f4cf22fb633d4df500e371daf703d4b4d14a0ea9d69cd631f95f9e6ba840f8ad;Subject="CN=test-application,OU=OrgUnit,O=Organization,L=Waldorf,ST=Waldorf,C=DE";` +
`URI=,By=spiffe://cluster.local/ns/kyma-system/sa/default;` +
`Hash=6d1f9f3a6ac94ff925841aeb9c15bb3323014e3da2c224ea7697698acf413226;Subject="";` +
`URI=spiffe://cluster.local/ns/istio-system/sa/istio-ingressgateway-service-account`

// publish handler which are overwritten in the tests
var publishHandler http.HandlerFunc
eventPublisherProxyHandler.Path(eventingPathPrefixEvents).HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
publishHandler.ServeHTTP(writer, request)
})

eventPublisherProxyHandler.PathPrefix(eventingDestinationPathPublish).HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
})

// given
appData := controller.CachedAppData{
AppPathPrefixV1: fmt.Sprintf("/%s/v1/events", application.Name),
}

idCache := cache.New(time.Minute, time.Minute)
if application.Spec.CompassMetadata != nil {
appData.ClientIDs = []string{applicationID}
} else {
appData.ClientIDs = []string{}
}

idCache.Set(application.Name, appData, cache.NoExpiration)

proxyHandler := NewProxyHandler(
eventPublisherProxyHost,
eventingDestinationPathPublish,
idCache,
log)

req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("/%s/v2/events", application.Name), nil)
require.NoError(t, err)
req.Header.Set(CertificateInfoHeader, cert)
req = mux.SetURLVars(req, map[string]string{"application": application.Name})

recorder := httptest.NewRecorder()

// when
proxyHandler.ProxyAppConnectorRequests(recorder, req)

// then
assert.Equal(t, http.StatusBadGateway, recorder.Code)
})

t.Run("should return 404 failed when cache doesn't contain the element", func(t *testing.T) {
eventPublisherProxyHandler := mux.NewRouter()
eventPublisherProxyServer := httptest.NewServer(eventPublisherProxyHandler)
Expand Down