Skip to content

Commit

Permalink
POA-2390 Exclude certain Cloud API endpoints from Repro Mode (#50)
Browse files Browse the repository at this point in the history
Tested locally:
```
 has_payload | response_code | method |        host_port         |        path  
-------------+---------------+--------+--------------------------+--------------------
 f           |           200 | GET    | api.getpostman-stage.com | /aoeu
 f           |           200 | POST   | api.getpostman-stage.com | /environments
 f           |           200 | GET    | api.getpostman-stage.com | /environments/aoeu
 f           |           200 | PUT    | api.getpostman-stage.com | /environments/aoeu
 f           |           200 | GET    | api.getpostman.com       | /aoeu
 f           |           200 | POST   | api.getpostman.com       | /environments
 f           |           200 | GET    | api.getpostman.com       | /environments/aoeu
 f           |           200 | PUT    | api.getpostman.com       | /environments/aoeu
 f           |           200 | GET    | localhost                | /aoeu
 f           |           200 | POST   | localhost                | /environments
 f           |           200 | GET    | localhost                | /environments/aoeu
 f           |           200 | PUT    | localhost                | /environments/aoeu
 f           |           404 | POST   | api.getpostman-stage.com | /environments
 f           |           404 | GET    | api.getpostman-stage.com | /environments/aoeu
 f           |           404 | PUT    | api.getpostman-stage.com | /environments/aoeu
 f           |           404 | POST   | api.getpostman.com       | /environments
 f           |           404 | GET    | api.getpostman.com       | /environments/aoeu
 f           |           404 | PUT    | api.getpostman.com       | /environments/aoeu
 t           |           404 | GET    | api.getpostman-stage.com | /aoeu
 t           |           404 | GET    | api.getpostman.com       | /aoeu
 t           |           404 | GET    | localhost                | /aoeu
 t           |           404 | POST   | localhost                | /environments
 t           |           404 | GET    | localhost                | /environments/aoeu
 t           |           404 | PUT    | localhost                | /environments/aoeu
(24 rows)
```
  • Loading branch information
liujed authored Nov 19, 2024
1 parent 8fca630 commit 1b16e0f
Showing 1 changed file with 57 additions and 1 deletion.
58 changes: 57 additions & 1 deletion trace/backend_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package trace
import (
"encoding/base64"
"net"
"os"
"regexp"
"strings"
"sync"
"time"

Expand All @@ -11,9 +14,11 @@ import (
"github.com/akitasoftware/akita-libs/akinet"
kgxapi "github.com/akitasoftware/akita-libs/api_schema"
"github.com/akitasoftware/akita-libs/batcher"
"github.com/akitasoftware/akita-libs/http_rest_methods"
"github.com/akitasoftware/akita-libs/spec_util"
"github.com/akitasoftware/akita-libs/spec_util/ir_hash"
"github.com/akitasoftware/go-utils/optionals"
"github.com/akitasoftware/go-utils/sets"
"github.com/golang/protobuf/proto"
"github.com/pkg/errors"
"github.com/postmanlabs/postman-insights-agent/learn"
Expand Down Expand Up @@ -280,6 +285,55 @@ func (c *BackendCollector) processTLSHandshake(tls akinet.TLSHandshakeMetadata)
return nil
}

var cloudAPIEnvironmentsPathRE = regexp.MustCompile(`^/environments/[^/]+$`)
var cloudAPIHostnames = sets.NewSet[string]()

func init() {
cloudAPIHostnames.Insert("api.getpostman-stage.com")
cloudAPIHostnames.Insert("api.getpostman.com")
cloudAPIHostnames.Insert("api.postman.com")
cloudAPIHostnamesEnv := os.Getenv("XXX_INSIGHTS_AGENT_CLOUD_API_HOSTNAMES")
for _, hostname := range strings.Split(cloudAPIHostnamesEnv, " ") {
cloudAPIHostnames.Insert(strings.ToLower(hostname))
}
}

// Returns true if the witness should be excluded from Repro Mode.
//
// XXX This is a stop-gap hack to exclude certain endpoints for Cloud API from
// Repro Mode.
func excludeWitnessFromReproMode(w *pb.Witness) bool {

httpMeta := w.GetMethod().GetMeta().GetHttp()
if httpMeta == nil {
return false
}

if cloudAPIHostnames.Contains(strings.ToLower(httpMeta.Host)) {
switch httpMeta.Method {
case http_rest_methods.GET.String():
// Exclude GET /environments/{environment}.
if cloudAPIEnvironmentsPathRE.MatchString(httpMeta.PathTemplate) {
return true
}

case http_rest_methods.POST.String():
// Exclude POST /environments.
if httpMeta.PathTemplate == "/environments" {
return true
}

case http_rest_methods.PUT.String():
// Exclude PUT /environments/{environment}.
// Exclude GET /environments/{environment}.
if cloudAPIEnvironmentsPathRE.MatchString(httpMeta.PathTemplate) {
return true
}
}
}
return false
}

func (c *BackendCollector) queueUpload(w *witnessWithInfo) {
// Mark the method as not obfuscated.
w.witness.GetMethod().GetMeta().GetHttp().Obfuscation = pb.HTTPMethodMeta_NONE
Expand All @@ -292,7 +346,9 @@ func (c *BackendCollector) queueUpload(w *witnessWithInfo) {
}
}

if !c.sendWitnessPayloads || !hasOnlyErrorResponses(w.witness.GetMethod()) {
if !c.sendWitnessPayloads ||
!hasOnlyErrorResponses(w.witness.GetMethod()) ||
excludeWitnessFromReproMode(w.witness) {
// Obfuscate the original value so type inference engine can use it on the
// backend without revealing the actual value.
c.obfuscator.ZeroAllPrimitivesInMethod(w.witness.GetMethod())
Expand Down

0 comments on commit 1b16e0f

Please sign in to comment.