@@ -17,7 +17,6 @@ limitations under the License.
1717package auth
1818
1919import (
20- "bytes"
2120 "context"
2221 "encoding/json"
2322 "fmt"
@@ -32,6 +31,7 @@ import (
3231 "go.opentelemetry.io/otel"
3332 corev1listers "k8s.io/client-go/listers/core/v1"
3433 "knative.dev/eventing/pkg/eventingtls"
34+ "knative.dev/eventing/pkg/utils"
3535 "knative.dev/pkg/configmap"
3636 "knative.dev/pkg/network"
3737 "knative.dev/pkg/observability/tracing"
@@ -161,7 +161,7 @@ func (v *Verifier) verifyAuthN(ctx context.Context, audience *string, req *http.
161161// verifyAuthZ verifies if the given idToken is allowed by the resources eventPolicyStatus
162162func (v * Verifier ) verifyAuthZ (ctx context.Context , features feature.Flags , idToken * IDToken , resourceNamespace string , policyRefs []duckv1.AppliedEventPolicyRef , req * http.Request , resp http.ResponseWriter ) error {
163163 if len (policyRefs ) > 0 {
164- req , err := copyRequest (req )
164+ req , err := utils . CopyRequest (req )
165165 if err != nil {
166166 resp .WriteHeader (http .StatusInternalServerError )
167167 return fmt .Errorf ("failed to copy request body: %w" , err )
@@ -335,35 +335,6 @@ func (v *Verifier) getKubernetesOIDCDiscovery(features feature.Flags, client *ht
335335 return openIdConfig , nil
336336}
337337
338- // copyRequest makes a copy of the http request which can be consumed as needed, leaving the original request
339- // able to be consumed as well.
340- func copyRequest (req * http.Request ) (* http.Request , error ) {
341- // check if we actually need to copy the body, otherwise we can return the original request
342- if req .Body == nil || req .Body == http .NoBody {
343- return req , nil
344- }
345-
346- var buf bytes.Buffer
347- if _ , err := buf .ReadFrom (req .Body ); err != nil {
348- return nil , fmt .Errorf ("failed to read request body while copying it: %w" , err )
349- }
350-
351- if err := req .Body .Close (); err != nil {
352- return nil , fmt .Errorf ("failed to close original request body ready while copying request: %w" , err )
353- }
354-
355- // set the original request body to be readable again
356- req .Body = io .NopCloser (& buf )
357-
358- // return a new request with a readable body and same headers as the original
359- // we don't need to set any other fields as cloudevents only uses the headers
360- // and body to construct the Message/Event.
361- return & http.Request {
362- Header : req .Header ,
363- Body : io .NopCloser (bytes .NewReader (buf .Bytes ())),
364- }, nil
365- }
366-
367338type openIDMetadata struct {
368339 Issuer string `json:"issuer"`
369340 JWKSURI string `json:"jwks_uri"`
0 commit comments