Skip to content
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

add tolower to all keywords, and remove return on error for global vars #2852

Merged
merged 1 commit into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions pkg/sources/postman/postman.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,13 @@ type Source struct {

func (s *Source) addKeywords(keywords []string) {
for _, keyword := range keywords {
s.addKeyword(keyword)
s.attemptToAddKeyword(keyword)
}
}

func (s *Source) addKeyword(keyword string) {
func (s *Source) attemptToAddKeyword(keyword string) {
// fast check
keyword = strings.ToLower(keyword)
if _, ok := s.DetectorKeywords[keyword]; ok {
s.keywords[keyword] = struct{}{}
return
Expand Down Expand Up @@ -238,7 +239,7 @@ func (s *Source) scanLocalWorkspace(ctx context.Context, chunksChan chan *source
func (s *Source) scanWorkspace(ctx context.Context, chunksChan chan *sources.Chunk, workspace Workspace) error {
// reset keywords for each workspace
s.resetKeywords()
s.addKeyword(workspace.Name)
s.attemptToAddKeyword(workspace.Name)

// initiate metadata to track the tree structure of postman data
metadata := Metadata{
Expand All @@ -252,7 +253,8 @@ func (s *Source) scanWorkspace(ctx context.Context, chunksChan chan *sources.Chu
ctx.Logger().V(2).Info("starting scanning global variables")
globalVars, err := s.client.GetGlobalVariables(workspace.ID)
if err != nil {
return fmt.Errorf("error getting global variables for workspace %s, %w", workspace.ID, err)
// NOTE: global endpoint is finicky
ctx.Logger().V(2).Error(err, "skipping global variables")
}

metadata.Type = GLOBAL_TYPE
Expand All @@ -279,7 +281,7 @@ func (s *Source) scanWorkspace(ctx context.Context, chunksChan chan *sources.Chu

ctx.Logger().V(2).Info("scanning environment vars", "environment_uuid", metadata.FullID)
for _, word := range strings.Split(envVars.Name, " ") {
s.addKeyword(word)
s.attemptToAddKeyword(word)
}

s.scanVariableData(ctx, chunksChan, metadata, envVars)
Expand Down Expand Up @@ -309,7 +311,7 @@ func (s *Source) scanCollection(ctx context.Context, chunksChan chan *sources.Ch
ctx.Logger().V(2).Info("starting scanning collection", collection.Info.Name, "uuid", collection.Info.UID)
metadata.CollectionInfo = collection.Info
metadata.Type = COLLECTION_TYPE
s.addKeyword(collection.Info.Name)
s.attemptToAddKeyword(collection.Info.Name)

if !metadata.fromLocal {
metadata.FullID = metadata.CollectionInfo.UID
Expand All @@ -333,7 +335,7 @@ func (s *Source) scanCollection(ctx context.Context, chunksChan chan *sources.Ch
}

func (s *Source) scanItem(ctx context.Context, chunksChan chan *sources.Chunk, collection Collection, metadata Metadata, item Item) {
s.addKeyword(item.Name)
s.attemptToAddKeyword(item.Name)

// override the base collection metadata with item-specific metadata
metadata.FolderID = item.ID
Expand Down Expand Up @@ -479,6 +481,8 @@ func (s *Source) scanAuth(ctx context.Context, chunksChan chan *sources.Chunk, m
m.Type += " > authorization"
}

s.attemptToAddKeyword(authData)

m.FieldType = AUTH_TYPE
s.scanData(ctx, chunksChan, s.formatAndInjectKeywords(s.buildSubstitueSet(m, authData)), m)
}
Expand Down Expand Up @@ -594,8 +598,9 @@ func (s *Source) scanVariableData(ctx context.Context, chunksChan chan *sources.

values := []string{}
for _, kv := range variableData.KeyValues {
s.addKeyword(kv.Key)
s.attemptToAddKeyword(kv.Key)
valStr := fmt.Sprintf("%v", kv.Value)
s.attemptToAddKeyword(valStr)
if valStr != "" {
s.sub.add(m, kv.Key, valStr)
} else if kv.SessionValue != "" {
Expand Down
12 changes: 6 additions & 6 deletions pkg/sources/postman/substitution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ func TestSource_KeywordCombinations(t *testing.T) {
},
keywords: make(map[string]struct{}),
}
s.addKeyword("keyword1")
s.addKeyword("keyword2")
s.addKeyword("keyword3")
s.attemptToAddKeyword("keyword1")
s.attemptToAddKeyword("keyword2")
s.attemptToAddKeyword("keyword3")

// remove that \n from the end of the string
got := strings.Split(strings.TrimSuffix(s.keywordCombinations("test"), "\n"), "\n")
Expand Down Expand Up @@ -122,9 +122,9 @@ func TestSource_FormatAndInjectKeywords(t *testing.T) {
},
keywords: make(map[string]struct{}),
}
s.addKeyword("keyword1")
s.addKeyword("keyword2")
s.addKeyword("keyword3")
s.attemptToAddKeyword("keyword1")
s.attemptToAddKeyword("keyword2")
s.attemptToAddKeyword("keyword3")

testCases := []struct {
input []string
Expand Down
Loading