-
Notifications
You must be signed in to change notification settings - Fork 2
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
Handle all requests #58
Open
Ark2307
wants to merge
9
commits into
feature/filter_header
Choose a base branch
from
feature/handle_all_requests
base: feature/filter_header
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b2d2e61
handling all request in different topic
Ark2307 d23ab3b
adding error logs
Ark2307 6e7dbd2
Adding more logs
Ark2307 b510ce9
"Adding some logs"
Ark2307 57d4429
Sending responses when request response match
Ark2307 9968890
Fixing comments
Ark2307 8b64dab
Merge branch 'feature/filter_header' into feature/handle_all_requests
Ark2307 a44aa11
Resolving comments
Ark2307 8358835
Handle closing of new kafka writer
Ark2307 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -192,33 +192,102 @@ func checkIfIp(host string) bool { | |
return net.ParseIP(chunks[0]) != nil | ||
} | ||
|
||
func processAllRequests(bd *bidi, isPending bool, allRequests []http.Request, allRequestsContent []string, ignoreCloudMetadataCalls bool) { | ||
if len(allRequests) == 0 { | ||
return | ||
} | ||
|
||
i := 0 | ||
|
||
for { | ||
if len(allRequests) < (i + 1) { | ||
break | ||
} | ||
currentReq := &allRequests[i] | ||
currentReqHeader := make(map[string]string) | ||
// Loop over all values for the name. | ||
for name, values := range currentReq.Header { | ||
// Loop over all values for the name. | ||
for _, value := range values { | ||
currentReqHeader[name] = value | ||
} | ||
} | ||
|
||
currentReqHeader["host"] = currentReq.Host | ||
|
||
if ignoreCloudMetadataCalls && currentReq.Host == "169.254.169.254" { | ||
i++ | ||
continue | ||
} | ||
|
||
currentReqHeaderString, _ := json.Marshal(currentReqHeader) | ||
value := map[string]string{ | ||
"path": currentReq.URL.String(), | ||
"requestHeaders": string(currentReqHeaderString), | ||
"method": currentReq.Method, | ||
"requestPayload": allRequestsContent[i], | ||
"ip": bd.key.net.Src().String(), | ||
"time": fmt.Sprint(time.Now().Unix()), | ||
"type": currentReq.Proto, | ||
"akto_vxlan_id": fmt.Sprint(bd.vxlanID), | ||
"is_pending": fmt.Sprint(isPending), | ||
"source": bd.source, | ||
"responseHeaders": "", | ||
"responsePayload": "", | ||
"statusCode": fmt.Sprint(-1), | ||
"status": "", | ||
"akto_account_id": fmt.Sprint(1000000), | ||
} | ||
|
||
out, _ := json.Marshal(value) | ||
ctx := context.Background() | ||
go Produce(allRequestsKafkaWriter, ctx, string(out)) // Replace `nil` with the actual Kafka writer | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. close the kafka writers |
||
|
||
i++ | ||
} | ||
} | ||
|
||
func tryReadFromBD(bd *bidi, isPending bool) { | ||
reader := bufio.NewReader(bytes.NewReader(bd.a.bytes)) | ||
i := 0 | ||
requests := []http.Request{} | ||
allRequests := []http.Request{} | ||
requestsContent := []string{} | ||
|
||
allRequestsContent := []string{} | ||
var invalidRequestsFound bool | ||
for { | ||
req, err := http.ReadRequest(reader) | ||
if err == io.EOF || err == io.ErrUnexpectedEOF { | ||
break | ||
} else if err != nil { | ||
invalidRequestsFound = true | ||
printLog(fmt.Sprintf("HTTP-request error: %s \n", err)) | ||
return | ||
continue | ||
} | ||
body, err := ioutil.ReadAll(req.Body) | ||
req.Body.Close() | ||
if err != nil { | ||
printLog(fmt.Sprintf("Got body err: %s\n", err)) | ||
return | ||
invalidRequestsFound = true | ||
continue | ||
} | ||
|
||
requests = append(requests, *req) | ||
requestsContent = append(requestsContent, string(body)) | ||
if !invalidRequestsFound { | ||
requests = append(requests, *req) | ||
requestsContent = append(requestsContent, string(body)) | ||
} | ||
allRequests = append(allRequests, *req) | ||
allRequestsContent = append(allRequestsContent, string(body)) | ||
i++ | ||
} | ||
|
||
if len(requests) == 0 { | ||
requestProtectionEnabled := os.Getenv("REQUEST_PROTECTION_ENABLED") | ||
var shouldSendAllRequests = len(requestProtectionEnabled) > 0 | ||
|
||
if len(requests) == 0 || invalidRequestsFound { | ||
if(shouldSendAllRequests){ | ||
processAllRequests(bd, isPending, allRequests, allRequestsContent, ignoreCloudMetadataCalls) | ||
} | ||
return | ||
} | ||
|
||
|
@@ -228,20 +297,24 @@ func tryReadFromBD(bd *bidi, isPending bool) { | |
responses := []http.Response{} | ||
responsesContent := []string{} | ||
|
||
var validResponses = true | ||
|
||
for { | ||
|
||
resp, err := http.ReadResponse(reader, nil) | ||
if err == io.EOF || err == io.ErrUnexpectedEOF { | ||
break | ||
} else if err != nil { | ||
printLog(fmt.Sprintf("HTTP Request error: %s\n", err)) | ||
return | ||
validResponses = false | ||
break | ||
} | ||
|
||
body, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
printLog(fmt.Sprintf("Got body err: %s\n", err)) | ||
return | ||
validResponses = false | ||
break | ||
} | ||
encoding := resp.Header["Content-Encoding"] | ||
var r io.Reader | ||
|
@@ -250,7 +323,8 @@ func tryReadFromBD(bd *bidi, isPending bool) { | |
r, err = gzip.NewReader(r) | ||
if err != nil { | ||
printLog(fmt.Sprintf("HTTP-gunzip "+"Failed to gzip decode: %s", err)) | ||
return | ||
validResponses = false | ||
break | ||
} | ||
} | ||
if err == nil { | ||
|
@@ -267,7 +341,10 @@ func tryReadFromBD(bd *bidi, isPending bool) { | |
i++ | ||
} | ||
|
||
if len(requests) != len(responses) { | ||
if !validResponses || len(requests) != len(responses) { | ||
if(shouldSendAllRequests){ | ||
processAllRequests(bd, isPending, allRequests, allRequestsContent, ignoreCloudMetadataCalls) | ||
} | ||
return | ||
} | ||
|
||
|
@@ -359,7 +436,12 @@ func tryReadFromBD(bd *bidi, isPending bool) { | |
trafficCollectorCount.Inc(1) | ||
|
||
//printLog("req-resp.String() " + string(out)) | ||
|
||
// send function. | ||
go Produce(kafkaWriter, ctx, string(out)) | ||
if(shouldSendAllRequests){ | ||
go Produce(allRequestsKafkaWriter, ctx, string(out)) | ||
} | ||
i++ | ||
} | ||
} | ||
|
@@ -417,6 +499,7 @@ func createAndGetAssembler(vxlanID int, source string) *tcpassembly.Assembler { | |
} | ||
|
||
var kafkaWriter *kafka.Writer | ||
var allRequestsKafkaWriter *kafka.Writer | ||
|
||
func flushAll() { | ||
for _, v := range assemblerMap { | ||
|
@@ -568,6 +651,7 @@ func run(handle *pcap.Handle, apiCollectionId int, source string) { | |
bytesInEpoch = time.Now() | ||
time.Sleep(10 * time.Second) | ||
kafkaWriter.Close() | ||
allRequestsKafkaWriter.Close() | ||
break | ||
} | ||
|
||
|
@@ -605,6 +689,14 @@ func initKafka() { | |
kafka_url := getKafkaUrl() | ||
printLog("kafka_url: " + kafka_url) | ||
|
||
kafka_protection_url := os.Getenv("AKTO_KAFKA_PROTECTION_URL") | ||
|
||
if len(kafka_protection_url) == 0 { | ||
kafka_protection_url = kafka_url | ||
} | ||
|
||
printLog("kafka_protection_url: " + kafka_protection_url) | ||
|
||
bytesInThresholdInput := os.Getenv("AKTO_BYTES_IN_THRESHOLD") | ||
if len(bytesInThresholdInput) > 0 { | ||
bytesInThreshold, err = strconv.Atoi(bytesInThresholdInput) | ||
|
@@ -632,6 +724,7 @@ func initKafka() { | |
|
||
for { | ||
kafkaWriter = GetKafkaWriter(kafka_url, "akto.api.logs", kafka_batch_size, kafka_batch_time_secs_duration*time.Second) | ||
allRequestsKafkaWriter = GetKafkaWriter(kafka_protection_url, "akto.api.protection", kafka_batch_size, kafka_batch_time_secs_duration*time.Second) | ||
logMemoryStats() | ||
log.Println("logging kafka stats before pushing message") | ||
logKafkaStats() | ||
|
@@ -647,10 +740,12 @@ func initKafka() { | |
if err != nil { | ||
log.Println("error establishing connection with kafka, sending message failed, retrying in 2 seconds", err) | ||
kafkaWriter.Close() | ||
allRequestsKafkaWriter.Close() | ||
time.Sleep(time.Second * 2) | ||
} else { | ||
log.Println("connection establishing with kafka successfully") | ||
kafkaWriter.Completion = kafkaCompletion() | ||
allRequestsKafkaWriter.Completion = kafkaCompletion() | ||
break | ||
} | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add all the fields.. it's ok to have empty values but not null