Skip to content
Merged
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
14 changes: 10 additions & 4 deletions pkg/eventshub/forwarder/forwarder.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,19 @@ func (o *Forwarder) ServeHTTP(writer http.ResponseWriter, request *http.Request)
defer m.Finish(nil)

event, eventErr := cloudeventsbindings.ToEvent(context.TODO(), m)
headers := make(http.Header)
receivedHeaders := make(http.Header)
headersToBeSent := make(http.Header)
for k, v := range request.Header {
if !strings.HasPrefix(k, "Ce-") {
headers[k] = v
receivedHeaders[k] = v
}
if strings.HasPrefix(k, "Kn-") {
Copy link
Copy Markdown
Contributor Author

@mgencur mgencur Aug 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other option here would be something like this:

if !strings.HasPrefix(k, "Ce-")&&
  !strings.HasPrefix(k, "X-") &&
  k != "Traceparent" &&
  k != "User-Agent" &&
  k != "Accept-Encoding" &&
  k != "Content-Length" &&
  k != "Host" {
  headersToBeSent[k] = v
}

This would forward everything except for the unwanted ones. WDYT?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks ok, to forward those, since they have a meaning for us/eventing

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

your forward everything except seems also reasonable.
I'd perhaps make that some better code, but yeah.

If this current code works, let's do that, and backport

and than think about future, sounds good?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Testing the current solution with forwarding "Kn-*". We can leave any improvements for the future.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works! This is logged at my forwarder:

{"level":"info","ts":"2023-08-09T11:33:00.349Z","logger":"eventshub.event logger","caller":"logger_vent/logger.go:26","msg":"Event: \n-- EventInfo --\n--- Kind: Received ---\n--- Event ---\nContext Attributes,\n  specversion: 1.0\n  type: dev.knative.sources.ping\n  source: /apis/v1/namespaces/test-cktbvfkv/pingsources/pingsource-oqvyeqoj\n  id: b2c43312-8767-4d17-af90-55008697dca1\n  time: 2023-08-09T11:33:00.318564821Z\n\n--- HTTP headers ---\n  Accept-Encoding: gzip\n  Forwarded: for=10.128.2.49;proto=http\n  Traceparent: 00-a4aaf20f277d96c1a067e3d478235fed-91af74b9b2d6ad70-00\n  X-Forwarded-For: 10.128.2.49, 10.129.2.53\n  X-Forwarded-Proto: http\n  Content-Length: 0\n  Kn-Namespace: test-cktbvfkv\n  User-Agent: Go-http-client/1.1\n  X-Request-Id: 6d4020dd-fa21-43ee-ae2c-d52751bfacf2\n  K-Proxy-Request: activator\n  Host: sink-krcxpelq.test-cktbvfkv.svc.cluster.local\n\n--- Origin: '127.0.0.1:55982' ---\n--- Observer: 'sink-krcxpelq' ---\n--- Time: 2023-08-09 11:33:00.34908641 +0000 UTC m=+358.441989515 ---\n--- Sequence: 0 ---\n--- Sent Id:  ' ---\n--------------------\n"}
{"level":"info","ts":"2023-08-09T11:33:00.391Z","logger":"eventshub.event logger","caller":"logger_vent/logger.go:26","msg":"Event: \n-- EventInfo --\n--- Kind: Sent ---\n--- Event ---\nContext Attributes,\n  specversion: 1.0\n  type: dev.knative.sources.ping\n  source: /apis/v1/namespaces/test-cktbvfkv/pingsources/pingsource-oqvyeqoj\n  id: b2c43312-8767-4d17-af90-55008697dca1\n  time: 2023-08-09T11:33:00.318564821Z\n\n--- HTTP headers ---\n  Ce-Source: /apis/v1/namespaces/test-cktbvfkv/pingsources/pingsource-oqvyeqoj\n  Ce-Specversion: 1.0\n  Ce-Type: dev.knative.sources.ping\n  Ce-Time: 2023-08-09T11:33:00.318564821Z\n  Kn-Namespace: test-cktbvfkv\n  Ce-Id: b2c43312-8767-4d17-af90-55008697dca1\n\n--- Origin: 'sink-krcxpelq' ---\n--- Observer: 'sink-krcxpelq' ---\n--- Time: 2023-08-09 11:33:00.391297182 +0000 UTC m=+358.484200268 ---\n--- Sequence: 0 ---\n--- Sent Id:  'b2c43312-8767-4d17-af90-55008697dca1 ---\n--------------------\n"}

headersToBeSent[k] = v
}
}
// Host header is removed from the request.Header map by net/http
if request.Host != "" {
headers.Set("Host", request.Host)
receivedHeaders.Set("Host", request.Host)
}

eventErrStr := ""
Expand All @@ -150,7 +154,7 @@ func (o *Forwarder) ServeHTTP(writer http.ResponseWriter, request *http.Request)
Error: eventErrStr,
Event: event,
Observer: o.Name,
HTTPHeaders: headers,
HTTPHeaders: receivedHeaders,
Origin: request.RemoteAddr,
Time: time.Now(),
Kind: eventshub.EventReceived,
Expand All @@ -166,6 +170,8 @@ func (o *Forwarder) ServeHTTP(writer http.ResponseWriter, request *http.Request)
logging.FromContext(o.ctx).Error("Cannot create the request: ", err)
}

req.Header = headersToBeSent

err = cehttp.WriteRequest(requestCtx, binding.ToMessage(event), req)
if err != nil {
logging.FromContext(o.ctx).Error("Cannot write the event: ", err)
Expand Down