Skip to content

Commit 3fe3907

Browse files
Add a bunch of Postman logging (#4154)
Really almost trace level stuff here. Trying to understand why this source dies unexpectedly.
1 parent 9a2b268 commit 3fe3907

File tree

5 files changed

+55
-2
lines changed

5 files changed

+55
-2
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ require (
8383
github.com/pkg/errors v0.9.1
8484
github.com/prometheus/client_golang v1.20.5
8585
github.com/rabbitmq/amqp091-go v1.10.0
86+
github.com/repeale/fp-go v0.11.1
8687
github.com/sassoftware/go-rpmutils v0.4.0
8788
github.com/schollz/progressbar/v3 v3.17.1
8889
github.com/sendgrid/sendgrid-go v3.16.0+incompatible

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,8 @@ github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0leargg
663663
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
664664
github.com/rabbitmq/amqp091-go v1.10.0 h1:STpn5XsHlHGcecLmMFCtg7mqq0RnD+zFr4uzukfVhBw=
665665
github.com/rabbitmq/amqp091-go v1.10.0/go.mod h1:Hy4jKW5kQART1u+JkDTF9YYOQUHXqMuhrgxOEeS7G4o=
666+
github.com/repeale/fp-go v0.11.1 h1:Q/e+gNyyHaxKAyfdbBqvip3DxhVWH453R+kthvSr9Mk=
667+
github.com/repeale/fp-go v0.11.1/go.mod h1:4KrwQJB1VRY+06CA+jTc4baZetr6o2PeuqnKr5ybQUc=
666668
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
667669
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
668670
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=

pkg/sources/postman/postman.go

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import (
2222
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/source_metadatapb"
2323
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/sourcespb"
2424
"github.com/trufflesecurity/trufflehog/v3/pkg/sources"
25+
26+
"github.com/repeale/fp-go"
2527
)
2628

2729
const (
@@ -139,6 +141,7 @@ func (s *Source) Init(ctx context.Context, name string, jobId sources.JobID, sou
139141
// Check out the postman UI to see what I mean.
140142
// Metadata is used to track information that informs the source of the chunk (e.g. the workspace -> collection -> request -> variable hierarchy).
141143
func (s *Source) Chunks(ctx context.Context, chunksChan chan *sources.Chunk, _ ...sources.ChunkingTarget) error {
144+
142145
// Scan local environments
143146
for _, envPath := range s.conn.EnvironmentPaths {
144147
env := VariableData{}
@@ -255,6 +258,13 @@ func (s *Source) scanLocalWorkspace(ctx context.Context, chunksChan chan *source
255258
}
256259

257260
func (s *Source) scanWorkspace(ctx context.Context, chunksChan chan *sources.Chunk, workspace Workspace) error {
261+
ctx.Logger().V(4).Info("scanning workspace",
262+
"workspace_id", workspace.Id,
263+
"collection_uids", fp.Map(func(i IdNameUid) string { return i.Uid })(workspace.Collections),
264+
"environment_uids", fp.Map(func(i IdNameUid) string { return i.Uid })(workspace.Environments),
265+
"collection_raw_uids", fp.Map(func(c Collection) string { return c.Info.Uid })(workspace.CollectionsRaw),
266+
"environment_raw_ids", fp.Map(func(v VariableData) string { return v.Id })(workspace.EnvironmentsRaw),
267+
)
258268
// reset keywords for each workspace
259269
s.resetKeywords()
260270
s.attemptToAddKeyword(workspace.Name)
@@ -321,7 +331,10 @@ func (s *Source) scanWorkspace(ctx context.Context, chunksChan chan *sources.Chu
321331
// scanCollection scans a collection and all its items, folders, and requests.
322332
// locally scoped Metadata is updated as we drill down into the collection.
323333
func (s *Source) scanCollection(ctx context.Context, chunksChan chan *sources.Chunk, metadata Metadata, collection Collection) {
324-
ctx.Logger().V(2).Info("starting to scan collection", "collection_name", collection.Info.Name, "collection_uuid", collection.Info.Uid)
334+
ctx.Logger().V(2).Info("starting to scan collection",
335+
"collection_name", collection.Info.Name,
336+
"collection_uuid", collection.Info.Uid,
337+
"variable_count", len(collection.Variables))
325338
metadata.CollectionInfo = collection.Info
326339
metadata.Type = COLLECTION_TYPE
327340
s.attemptToAddKeyword(collection.Info.Name)
@@ -342,17 +355,33 @@ func (s *Source) scanCollection(ctx context.Context, chunksChan chan *sources.Ch
342355
// collections don't have URLs in the Postman API, but we can scan the Authorization section without it.
343356
s.scanAuth(ctx, chunksChan, metadata, collection.Auth, URL{})
344357

358+
ctx.Logger().V(4).Info("Scanning events in collection",
359+
"collection_uid", collection.Info.Uid,
360+
"event_count", len(collection.Events),
361+
)
345362
for _, event := range collection.Events {
346363
s.scanEvent(ctx, chunksChan, metadata, event)
347364
}
348365

366+
ctx.Logger().V(4).Info("Scanning items in collection",
367+
"collection_uid", collection.Info.Uid,
368+
"item_ids", fp.Map(func(i Item) string { return i.Id })(collection.Items),
369+
)
349370
for _, item := range collection.Items {
350371
s.scanItem(ctx, chunksChan, collection, metadata, item, "")
351372
}
352373

353374
}
354375

355376
func (s *Source) scanItem(ctx context.Context, chunksChan chan *sources.Chunk, collection Collection, metadata Metadata, item Item, parentItemId string) {
377+
ctx.Logger().V(4).Info("Starting to scan item",
378+
"item_uid", item.Uid,
379+
"item_parent_item_id", parentItemId,
380+
"item_descendent_item_uids", fp.Map(func(i Item) string { return i.Uid })(item.Items),
381+
"item_event_count", len(item.Events),
382+
"item_response_count", len(item.Response),
383+
"item_variable_count", len(item.Variable),
384+
)
356385
s.attemptToAddKeyword(item.Name)
357386

358387
// override the base collection metadata with item-specific metadata
@@ -410,6 +439,8 @@ func (s *Source) scanItem(ctx context.Context, chunksChan chan *sources.Chunk, c
410439
// an auth all by its lonesome could be inherited to subfolders and requests
411440
s.scanAuth(ctx, chunksChan, metadata, item.Auth, item.Request.URL)
412441
metadata.LocationType = source_metadatapb.PostmanLocationType_UNKNOWN_POSTMAN
442+
443+
ctx.Logger().V(4).Info("Finished scanning item", "item_uid", item.Uid)
413444
}
414445

415446
func (s *Source) scanEvent(ctx context.Context, chunksChan chan *sources.Chunk, metadata Metadata, event Event) {
@@ -537,6 +568,13 @@ func (s *Source) scanAuth(ctx context.Context, chunksChan chan *sources.Chunk, m
537568
}
538569

539570
func (s *Source) scanHTTPRequest(ctx context.Context, chunksChan chan *sources.Chunk, metadata Metadata, r Request) {
571+
ctx.Logger().V(4).Info("scanning http request",
572+
"request_header_count", len(r.HeaderKeyValue),
573+
"request_has_string_header", r.HeaderString == nil,
574+
"request_url_query_param_count", len(r.URL.Query),
575+
"request_url_path_param_count", len(r.URL.Path),
576+
)
577+
540578
s.addKeywords(r.URL.Host)
541579
originalType := metadata.Type
542580

@@ -590,6 +628,10 @@ func (s *Source) scanHTTPRequest(ctx context.Context, chunksChan chan *sources.C
590628
}
591629

592630
func (s *Source) scanRequestBody(ctx context.Context, chunksChan chan *sources.Chunk, m Metadata, b Body) {
631+
ctx.Logger().V(4).Info("scanning request body",
632+
"request_body_form_data_count", len(b.FormData),
633+
"request_body_url_encoded_param_count", len(b.URLEncoded),
634+
)
593635
if !m.fromLocal {
594636
m.Link = m.Link + "?tab=body"
595637
}

pkg/sources/postman/postman_client.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ func (c *Client) GetWorkspace(ctx context.Context, workspaceUUID string) (Worksp
358358

359359
// GetEnvironmentVariables returns the environment variables for a given environment
360360
func (c *Client) GetEnvironmentVariables(ctx context.Context, environment_uuid string) (VariableData, error) {
361+
ctx.Logger().V(4).Info("getting environment variables", "environment_uuid", environment_uuid)
361362
obj := struct {
362363
VariableData VariableData `json:"environment"`
363364
}{}
@@ -379,6 +380,7 @@ func (c *Client) GetEnvironmentVariables(ctx context.Context, environment_uuid s
379380

380381
// GetCollection returns the collection for a given collection
381382
func (c *Client) GetCollection(ctx context.Context, collection_uuid string) (Collection, error) {
383+
ctx.Logger().V(4).Info("getting collection", "collection_uuid", collection_uuid)
382384
obj := struct {
383385
Collection Collection `json:"collection"`
384386
}{}

pkg/sources/postman/substitution.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,13 @@ func (s *Source) buildSubstituteSet(metadata Metadata, data string, maxRecursion
7272
// buildSubstitution performs variable substitution with a maximum recursion depth
7373
// depth is the current recursion depth
7474
// maxRecursionDepth is the maximum recursion depth to use for variable substitution
75-
func (s *Source) buildSubstitution(data string, metadata Metadata, combos *map[string]struct{}, depth int, maxRecursionDepth int) {
75+
func (s *Source) buildSubstitution(
76+
data string,
77+
metadata Metadata,
78+
combos *map[string]struct{},
79+
depth int,
80+
maxRecursionDepth int,
81+
) {
7682
// Limit recursion depth to prevent stack overflow
7783
if depth > maxRecursionDepth {
7884
(*combos)[data] = struct{}{}

0 commit comments

Comments
 (0)