Skip to content

Commit

Permalink
debug
Browse files Browse the repository at this point in the history
  • Loading branch information
e-n-0 committed Aug 30, 2024
1 parent 6bfaba1 commit 9b2e29b
Showing 1 changed file with 72 additions and 63 deletions.
135 changes: 72 additions & 63 deletions contrib/envoyproxy/envoy/cmd/serviceextensions/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"encoding/json"
"net"
"net/http"
"os"
Expand All @@ -13,8 +14,6 @@ import (
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
"gopkg.in/DataDog/dd-trace-go.v1/internal/appsec/dyngo"
"gopkg.in/DataDog/dd-trace-go.v1/internal/appsec/emitter/httpsec/types"
"gopkg.in/DataDog/dd-trace-go.v1/internal/appsec/emitter/sharedsec"
"gopkg.in/DataDog/dd-trace-go.v1/internal/appsec/trace/httptrace"
)

// AppsecCalloutServiceExtensionService defines the struct that follows the ExternalProcessorServer interface.
Expand Down Expand Up @@ -83,9 +82,9 @@ func processHTTPResponse(req *extproc.ProcessingRequest) error {
mu.Unlock()

// Response section
var responseHeaders = req.GetResponseHeaders()
var responseBody = req.GetResponseBody()
var responseTrailers = req.GetResponseTrailers()
// var responseHeaders = req.GetResponseHeaders()
// var responseBody = req.GetResponseBody()
// var responseTrailers = req.GetResponseTrailers()

res := types.HandlerOperationRes{ // TODO: fill all the fields of this struct
Headers: map[string][]string{},
Expand Down Expand Up @@ -114,22 +113,77 @@ func (s *AppsecCalloutServiceExtensionService) Process(stream extproc.ExternalPr
return err
}

if false { // TODO: get response from the stream and finish the request
err := processHTTPResponse(req)
if err != nil {
return err
// Marshall the req object to JSON
jsonReq, err := json.Marshal(req)
println("Received request: ", string(jsonReq))

/*
if false { // TODO: get response from the stream and finish the request
err := processHTTPResponse(req)
if err != nil {
return err
}
}
}
// Request section
var requestHeaders = req.GetRequestHeaders()
var requestBody = req.GetRequestBody()
var requestTrailers = req.GetRequestTrailers()
// Request section
var requestHeaders = req.GetRequestHeaders()
var requestBody = req.GetRequestBody()
var requestTrailers = req.GetRequestTrailers()
// Pretty print
println("Request Headers: ", requestHeaders.GetHeaders().String())
println("Request Body: ", requestBody.GetBody())
println("Request Trailers: ", requestTrailers.GetTrailers().String())
headers := make(map[string][]string)
_, clientIP := httptrace.ClientIPTags(headers, true, "127.0.0.1")
op := &types.Operation{}
args := types.HandlerOperationArgs{ // TODO: fill all the fields of this struct
ClientIP: clientIP,
Headers: headers,
Cookies: headers,
Method: "GET",
PathParams: map[string]string{},
Query: map[string][]string{},
RequestURI: "/",
}
openedRequest := &OpenedRequest{
req: &http.Request{
Header: map[string][]string{}, // TODO: get the good headers thats the only thing needed here
},
respBuffer: &BufferResponseWriter{
buffer: []byte{},
statusCode: 0,
headers: map[string][]string{},
},
op: op,
blocking: false,
}
dyngo.OnData(op, func(a *sharedsec.HTTPAction) {
openedRequest.blocking = true
a.Handler.ServeHTTP(openedRequest.respBuffer, openedRequest.req)
})
dyngo.StartOperation(op, args)
if openedRequest.blocking {
println("Blocking request")
// TODO: block lol
return nil
}
mu.Lock()
defer mu.Unlock()
openedRequests[""] = openedRequest // TODO: get the good id
println("Response sent")
// Pretty print
println("Request Headers: ", requestHeaders.GetHeaders().String())
println("Request Body: ", requestBody.GetBody())
println("Request Trailers: ", requestTrailers.GetTrailers().String())
*/

// Build response
// Empty response because no changes should be made - except on blocking
Expand All @@ -140,51 +194,6 @@ func (s *AppsecCalloutServiceExtensionService) Process(stream extproc.ExternalPr
println("Error sending response: ", err)
return err
}

headers := make(map[string][]string)

_, clientIP := httptrace.ClientIPTags(headers, true, "127.0.0.1")

op := &types.Operation{}
args := types.HandlerOperationArgs{ // TODO: fill all the fields of this struct
ClientIP: clientIP,
Headers: headers,
Cookies: headers,
Method: "GET",
PathParams: map[string]string{},
Query: map[string][]string{},
RequestURI: "/",
}

openedRequest := &OpenedRequest{
req: &http.Request{
Header: map[string][]string{}, // TODO: get the good headers thats the only thing needed here
},
respBuffer: &BufferResponseWriter{
buffer: []byte{},
statusCode: 0,
headers: map[string][]string{},
},
op: op,
blocking: false,
}
dyngo.OnData(op, func(a *sharedsec.HTTPAction) {
openedRequest.blocking = true
a.Handler.ServeHTTP(openedRequest.respBuffer, openedRequest.req)
})

dyngo.StartOperation(op, args)
if openedRequest.blocking {
println("Blocking request")
// TODO: block lol
return nil
}

mu.Lock()
defer mu.Unlock()
openedRequests[""] = openedRequest // TODO: get the good id

println("Response sent")
}
}

Expand Down

0 comments on commit 9b2e29b

Please sign in to comment.