-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
postman.go
702 lines (622 loc) · 21.3 KB
/
postman.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
package postman
import (
"archive/zip"
"encoding/json"
"fmt"
"io"
"net/url"
"os"
"path"
"path/filepath"
"slices"
"strings"
"github.com/trufflesecurity/trufflehog/v3/pkg/context"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
"github.com/go-errors/errors"
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/source_metadatapb"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/sourcespb"
"github.com/trufflesecurity/trufflehog/v3/pkg/sources"
)
const (
SourceType = sourcespb.SourceType_SOURCE_TYPE_POSTMAN
LINK_BASE_URL = "https://go.postman.co/"
GLOBAL_TYPE = "globals"
ENVIRONMENT_TYPE = "environment"
AUTH_TYPE = "authorization"
REQUEST_TYPE = "request"
FOLDER_TYPE = "folder"
COLLECTION_TYPE = "collection"
EVENT_TYPE = "script"
)
type Source struct {
name string
sourceId sources.SourceID
jobId sources.JobID
verify bool
client *Client
conn *sourcespb.Postman
DetectorKeywords map[string]struct{}
// Keywords are words that are discovered when we walk through postman data.
// These keywords are then injected into data that is sent to the detectors.
keywords map[string]struct{}
sub *Substitution
sources.Progress
sources.CommonSourceUnitUnmarshaller
}
func (s *Source) addKeywords(keywords []string) {
for _, keyword := range keywords {
s.attemptToAddKeyword(keyword)
}
}
func (s *Source) attemptToAddKeyword(keyword string) {
// fast check
keyword = strings.ToLower(keyword)
if _, ok := s.DetectorKeywords[keyword]; ok {
s.keywords[keyword] = struct{}{}
return
}
// slow check. This is to handle the case where the keyword is a substring of a detector keyword
// e.g. "datadog-token" is a variable key in postman, but "datadog" is a detector keyword
for k := range s.DetectorKeywords {
if strings.Contains(keyword, k) {
s.keywords[k] = struct{}{}
}
}
}
func (s *Source) resetKeywords() {
s.keywords = make(map[string]struct{})
}
// Type returns the type of source.
// It is used for matching source types in configuration and job input.
func (s *Source) Type() sourcespb.SourceType {
return SourceType
}
func (s *Source) SourceID() sources.SourceID {
return s.sourceId
}
func (s *Source) JobID() sources.JobID {
return s.jobId
}
// Init returns an initialized Postman source.
func (s *Source) Init(ctx context.Context, name string, jobId sources.JobID, sourceId sources.SourceID, verify bool, connection *anypb.Any, concurrency int) error {
s.name = name
s.sourceId = sourceId
s.jobId = jobId
s.verify = verify
s.keywords = make(map[string]struct{})
s.sub = NewSubstitution()
var conn sourcespb.Postman
if err := anypb.UnmarshalTo(connection, &conn, proto.UnmarshalOptions{}); err != nil {
return errors.WrapPrefix(err, "error unmarshalling connection", 0)
}
s.conn = &conn
switch conn.Credential.(type) {
case *sourcespb.Postman_Token:
if conn.GetToken() == "" {
return errors.New("Postman token is empty")
}
s.client = NewClient(conn.GetToken())
s.client.HTTPClient = common.RetryableHTTPClientTimeout(3)
case *sourcespb.Postman_Unauthenticated:
s.client = nil
// No client needed if reading from local
default:
return errors.New("credential type not implemented for Postman")
}
return nil
}
// Chunks scans the Postman source and sends the data to the chunks chan.
// It scans the local environment, collection, and workspace files, and then scans the Postman API if a token is provided.
// The Postman source is different to our other sources in that we are not relying on the data we read from the source to contain
// all the information we need to scan, i.e, a git chunk contains all the information needed to check if a secret is present in that chunk.
// Postman on the other hand requires us to build context (the keywords and variables) as we scan the data.
// Check out the postman UI to see what I mean.
// Metadata is used to track information that informs the source of the chunk (e.g. the workspace -> collection -> request -> variable hierarchy).
func (s *Source) Chunks(ctx context.Context, chunksChan chan *sources.Chunk, _ ...sources.ChunkingTarget) error {
// Scan local environments
for _, envPath := range s.conn.EnvironmentPaths {
env := VariableData{}
contents, err := os.ReadFile(envPath)
if err != nil {
return err
}
if err = json.Unmarshal(contents, &env); err != nil {
return err
}
s.scanVariableData(ctx, chunksChan, Metadata{EnvironmentName: env.ID, fromLocal: true, Link: envPath}, env)
}
// Scan local workspaces
for _, collectionPath := range s.conn.CollectionPaths {
collection := Collection{}
contents, err := os.ReadFile(collectionPath)
if err != nil {
return err
}
if err = json.Unmarshal(contents, &collection); err != nil {
return err
}
s.scanCollection(ctx, chunksChan, Metadata{CollectionInfo: collection.Info, fromLocal: true, Link: collectionPath}, collection)
}
// Scan local workspaces
for _, workspacePath := range s.conn.WorkspacePaths {
// check if zip file
workspace := Workspace{}
if strings.HasSuffix(workspacePath, ".zip") {
var err error
workspace, err = unpackWorkspace(workspacePath)
if err != nil {
return err
}
}
basename := path.Base(workspacePath)
workspace.ID = strings.TrimSuffix(basename, filepath.Ext(basename))
s.scanLocalWorkspace(ctx, chunksChan, workspace, workspacePath)
}
// Scan workspaces
for _, workspaceID := range s.conn.Workspaces {
w, err := s.client.GetWorkspace(workspaceID)
if err != nil {
return fmt.Errorf("error getting workspace %s: %w", workspaceID, err)
}
if err = s.scanWorkspace(ctx, chunksChan, w); err != nil {
return fmt.Errorf("error scanning workspace %s: %w", workspaceID, err)
}
}
// Scan collections
for _, collectionID := range s.conn.Collections {
if shouldSkip(collectionID, s.conn.IncludeCollections, s.conn.ExcludeCollections) {
continue
}
collection, err := s.client.GetCollection(collectionID)
if err != nil {
return fmt.Errorf("error getting collection %s: %w", collectionID, err)
}
s.scanCollection(ctx, chunksChan, Metadata{}, collection)
}
// Scan personal workspaces (from API token)
if s.conn.Workspaces == nil && s.conn.Collections == nil && s.conn.Environments == nil && s.conn.GetToken() != "" {
workspaces, err := s.client.EnumerateWorkspaces()
if err != nil {
return fmt.Errorf("error enumerating postman workspaces: %w", err)
}
for _, workspace := range workspaces {
if err = s.scanWorkspace(ctx, chunksChan, workspace); err != nil {
return fmt.Errorf("error scanning workspace %s: %w", workspace.ID, err)
}
}
}
return nil
}
func (s *Source) scanLocalWorkspace(ctx context.Context, chunksChan chan *sources.Chunk, workspace Workspace, filePath string) {
// reset keywords for each workspace
s.resetKeywords()
metadata := Metadata{
WorkspaceUUID: workspace.ID,
fromLocal: true,
}
for _, environment := range workspace.EnvironmentsRaw {
metadata.Link = strings.TrimSuffix(path.Base(filePath), path.Ext(filePath)) + "/environments/" + environment.ID + ".json"
s.scanVariableData(ctx, chunksChan, metadata, environment)
}
for _, collection := range workspace.CollectionsRaw {
metadata.Link = strings.TrimSuffix(path.Base(filePath), path.Ext(filePath)) + "/collections/" + collection.Info.PostmanID + ".json"
s.scanCollection(ctx, chunksChan, metadata, collection)
}
}
func (s *Source) scanWorkspace(ctx context.Context, chunksChan chan *sources.Chunk, workspace Workspace) error {
// reset keywords for each workspace
s.resetKeywords()
s.attemptToAddKeyword(workspace.Name)
// initiate metadata to track the tree structure of postman data
metadata := Metadata{
WorkspaceUUID: workspace.ID,
WorkspaceName: workspace.Name,
CreatedBy: workspace.CreatedBy,
Type: "workspace",
}
// scan global variables
ctx.Logger().V(2).Info("starting scanning global variables")
globalVars, err := s.client.GetGlobalVariables(workspace.ID)
if err != nil {
// NOTE: global endpoint is finicky
ctx.Logger().V(2).Error(err, "skipping global variables")
}
metadata.Type = GLOBAL_TYPE
metadata.Link = LINK_BASE_URL + "workspace/" + workspace.ID + "/" + GLOBAL_TYPE
metadata.FullID = workspace.CreatedBy + "-" + globalVars.ID
s.scanVariableData(ctx, chunksChan, metadata, globalVars)
ctx.Logger().V(2).Info("finished scanning global variables")
// gather and scan environment variables
for _, envID := range workspace.Environments {
envVars, err := s.client.GetEnvironmentVariables(envID.UUID)
if err != nil {
ctx.Logger().Error(err, "could not get env variables", "environment_uuid", envID.UUID)
continue
}
if shouldSkip(envID.UUID, s.conn.IncludeEnvironments, s.conn.ExcludeEnvironments) {
continue
}
metadata.Type = ENVIRONMENT_TYPE
metadata.Link = LINK_BASE_URL + "environments/" + envID.UUID
metadata.FullID = envVars.ID
metadata.EnvironmentID = envID.UUID
ctx.Logger().V(2).Info("scanning environment vars", "environment_uuid", metadata.FullID)
for _, word := range strings.Split(envVars.Name, " ") {
s.attemptToAddKeyword(word)
}
s.scanVariableData(ctx, chunksChan, metadata, envVars)
ctx.Logger().V(2).Info("finished scanning environment vars", "environment_uuid", metadata.FullID)
}
ctx.Logger().V(2).Info("finished scanning environments")
// scan all the collections in the workspace.
// at this point we have all the possible
// substitutions from Global and Environment variables
for _, collectionID := range workspace.Collections {
if shouldSkip(collectionID.UUID, s.conn.IncludeCollections, s.conn.ExcludeCollections) {
continue
}
collection, err := s.client.GetCollection(collectionID.UUID)
if err != nil {
return err
}
s.scanCollection(ctx, chunksChan, metadata, collection)
}
return nil
}
// scanCollection scans a collection and all its items, folders, and requests.
// locally scoped Metadata is updated as we drill down into the collection.
func (s *Source) scanCollection(ctx context.Context, chunksChan chan *sources.Chunk, metadata Metadata, collection Collection) {
ctx.Logger().V(2).Info("starting scanning collection", collection.Info.Name, "uuid", collection.Info.UID)
metadata.CollectionInfo = collection.Info
metadata.Type = COLLECTION_TYPE
s.attemptToAddKeyword(collection.Info.Name)
if !metadata.fromLocal {
metadata.FullID = metadata.CollectionInfo.UID
metadata.Link = LINK_BASE_URL + COLLECTION_TYPE + "/" + metadata.FullID
}
// variables must be scanned first before drilling down into the folders and events
// because we need to pick up the substitutions from the top level collection variables
s.scanVariableData(ctx, chunksChan, metadata, VariableData{
KeyValues: collection.Variables,
})
for _, event := range collection.Events {
s.scanEvent(ctx, chunksChan, metadata, event)
}
for _, item := range collection.Items {
s.scanItem(ctx, chunksChan, collection, metadata, item)
}
}
func (s *Source) scanItem(ctx context.Context, chunksChan chan *sources.Chunk, collection Collection, metadata Metadata, item Item) {
s.attemptToAddKeyword(item.Name)
// override the base collection metadata with item-specific metadata
metadata.FolderID = item.ID
metadata.Type = FOLDER_TYPE
if metadata.FolderName != "" {
// keep track of the folder hierarchy
metadata.FolderName = metadata.FolderName + " > " + item.Name
} else {
metadata.FolderName = item.Name
}
if item.UID != "" {
metadata.FullID = item.UID
metadata.Link = LINK_BASE_URL + FOLDER_TYPE + "/" + metadata.FullID
}
// recurse through the folders
for _, subItem := range item.Items {
s.scanItem(ctx, chunksChan, collection, metadata, subItem)
}
// check if there are any requests in the folder
if item.Request.Method != "" {
metadata.RequestID = item.ID
metadata.RequestName = item.Name
metadata.Type = REQUEST_TYPE
if item.UID != "" {
// Route to API endpoint
metadata.FullID = item.UID
metadata.Link = LINK_BASE_URL + REQUEST_TYPE + "/" + item.UID
} else {
// Route to collection.json
metadata.FullID = item.ID
}
s.scanHTTPRequest(ctx, chunksChan, metadata, item.Request)
}
// check if there are any responses in the folder
for _, response := range item.Response {
s.scanHTTPResponse(ctx, chunksChan, metadata, response)
}
for _, event := range item.Events {
s.scanEvent(ctx, chunksChan, metadata, event)
}
// an auth all by its lonesome could be inherited to subfolders and requests
s.scanAuth(ctx, chunksChan, metadata, item.Auth, item.Request.URL)
}
func (s *Source) scanEvent(ctx context.Context, chunksChan chan *sources.Chunk, metadata Metadata, event Event) {
metadata.Type = metadata.Type + " > event"
data := strings.Join(event.Script.Exec, " ")
// Prep direct links. Ignore updating link if it's a local JSON file
if !metadata.fromLocal {
metadata.Link = LINK_BASE_URL + metadata.Type + "/" + metadata.FullID
if event.Listen == "prerequest" {
metadata.Link += "?tab=pre-request-scripts"
} else {
metadata.Link += "?tab=tests"
}
}
s.scanData(ctx, chunksChan, s.formatAndInjectKeywords(s.buildSubstitueSet(metadata, data)), metadata)
}
func (s *Source) scanAuth(ctx context.Context, chunksChan chan *sources.Chunk, m Metadata, auth Auth, u URL) {
var authData string
switch auth.Type {
case "apikey":
var apiKeyValue, apiKeyName string
for _, kv := range auth.Apikey {
switch kv.Key {
case "key":
apiKeyValue = fmt.Sprintf("%v", kv.Value)
case "value":
apiKeyName = fmt.Sprintf("%v", kv.Value)
}
}
authData += fmt.Sprintf("%s=%s\n", apiKeyName, apiKeyValue)
case "awsSigV4", "awsv4":
for _, kv := range auth.AWSv4 {
switch kv.Key {
case "accessKey":
authData += fmt.Sprintf("accessKey:%s ", kv.Value)
case "secretKey":
authData += fmt.Sprintf("secretKey:%s ", kv.Value)
case "region":
authData += fmt.Sprintf("region:%s ", kv.Value)
case "service":
authData += fmt.Sprintf("service:%s ", kv.Value)
}
}
case "bearer":
var bearerKey, bearerValue string
for _, kv := range auth.Bearer {
bearerValue = fmt.Sprintf("%v", kv.Value)
bearerKey = fmt.Sprintf("%v", kv.Key)
}
authData += fmt.Sprintf("%s:%s\n", bearerKey, bearerValue)
case "basic":
username := ""
password := ""
for _, kv := range auth.Basic {
switch kv.Key {
case "username":
username = fmt.Sprintf("%v", kv.Value)
case "password":
password = fmt.Sprintf("%v", kv.Value)
}
}
if u.Raw != "" {
parsedURL, err := url.Parse(u.Raw)
if err != nil {
ctx.Logger().V(2).Info("error parsing URL in basic auth check", "url", u.Raw)
return
}
parsedURL.User = url.User(username + ":" + password)
decodedURL, err := url.PathUnescape(parsedURL.String())
if err != nil {
ctx.Logger().V(2).Info("error parsing URL in basic auth check", "url", u.Raw)
return
}
authData += decodedURL
}
case "oauth2":
for _, oauth := range auth.OAuth2 {
switch oauth.Key {
case "accessToken", "refreshToken", "clientId", "clientSecret", "accessTokenUrl", "authUrl":
authData += fmt.Sprintf("%s:%v ", oauth.Key, oauth.Value)
}
}
case "noauth":
return
default:
return
}
if !m.fromLocal {
m.Link += "?tab=auth"
m.Type += " > authorization"
}
s.attemptToAddKeyword(authData)
m.FieldType = AUTH_TYPE
s.scanData(ctx, chunksChan, s.formatAndInjectKeywords(s.buildSubstitueSet(m, authData)), m)
}
func (s *Source) scanHTTPRequest(ctx context.Context, chunksChan chan *sources.Chunk, metadata Metadata, r Request) {
s.addKeywords(r.URL.Host)
originalType := metadata.Type
// Add in var procesisng for headers
if r.Header != nil {
vars := VariableData{
KeyValues: r.Header,
}
metadata.Type = originalType + " > header"
s.scanVariableData(ctx, chunksChan, metadata, vars)
}
if r.URL.Raw != "" {
metadata.Type = originalType + " > request URL (no query parameters)"
// Note: query parameters are handled separately
u := fmt.Sprintf("%s://%s/%s", r.URL.Protocol, strings.Join(r.URL.Host, "."), strings.Join(r.URL.Path, "/"))
s.scanData(ctx, chunksChan, s.formatAndInjectKeywords(s.buildSubstitueSet(metadata, u)), metadata)
}
if len(r.URL.Query) > 0 {
vars := VariableData{
KeyValues: r.URL.Query,
}
metadata.Type = originalType + " > GET parameters (query)"
s.scanVariableData(ctx, chunksChan, metadata, vars)
}
if r.Auth.Type != "" {
metadata.Type = originalType + " > request auth"
s.scanAuth(ctx, chunksChan, metadata, r.Auth, r.URL)
}
if r.Body.Mode != "" {
metadata.Type = originalType + " > body"
s.scanBody(ctx, chunksChan, metadata, r.Body)
}
}
func (s *Source) scanBody(ctx context.Context, chunksChan chan *sources.Chunk, m Metadata, b Body) {
if !m.fromLocal {
m.Link = m.Link + "?tab=body"
}
originalType := m.Type
switch b.Mode {
case "formdata":
m.Type = originalType + " > form data"
vars := VariableData{
KeyValues: b.FormData,
}
s.scanVariableData(ctx, chunksChan, m, vars)
case "urlencoded":
m.Type = originalType + " > url encoded"
vars := VariableData{
KeyValues: b.URLEncoded,
}
s.scanVariableData(ctx, chunksChan, m, vars)
case "raw", "graphql":
data := b.Raw
if b.Mode == "graphql" {
m.Type = originalType + " > graphql"
data = b.GraphQL.Query + " " + b.GraphQL.Variables
}
if b.Mode == "raw" {
m.Type = originalType + " > raw"
}
s.scanData(ctx, chunksChan, s.formatAndInjectKeywords(s.buildSubstitueSet(m, data)), m)
default:
break
}
}
func (s *Source) scanHTTPResponse(ctx context.Context, chunksChan chan *sources.Chunk, m Metadata, response Response) {
if response.UID != "" {
m.Link = LINK_BASE_URL + "example/" + response.UID
m.FullID = response.UID
}
originalType := m.Type
if response.Header != nil {
vars := VariableData{
KeyValues: response.Header,
}
m.Type = originalType + " > response header"
s.scanVariableData(ctx, chunksChan, m, vars)
}
// Body in a response is just a string
if response.Body != "" {
m.Type = originalType + " > response body"
s.scanData(ctx, chunksChan, s.formatAndInjectKeywords(s.buildSubstitueSet(m, response.Body)), m)
}
if response.OriginalRequest.Method != "" {
m.Type = originalType + " > original request"
s.scanHTTPRequest(ctx, chunksChan, m, response.OriginalRequest)
}
}
func (s *Source) scanVariableData(ctx context.Context, chunksChan chan *sources.Chunk, m Metadata, variableData VariableData) {
if len(variableData.KeyValues) == 0 {
ctx.Logger().V(2).Info("no variables to scan", "type", m.Type, "uuid", m.FullID)
return
}
if !m.fromLocal && m.Type == COLLECTION_TYPE {
m.Link += "?tab=variables"
}
values := []string{}
for _, kv := range variableData.KeyValues {
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 != "" {
valStr = fmt.Sprintf("%v", kv.SessionValue)
}
if valStr == "" {
continue
}
values = append(values, s.buildSubstitueSet(m, valStr)...)
}
m.FieldType = m.Type + " variables"
s.scanData(ctx, chunksChan, s.formatAndInjectKeywords(values), m)
}
func (s *Source) scanData(ctx context.Context, chunksChan chan *sources.Chunk, data string, metadata Metadata) {
if data == "" {
return
}
metadata.FieldType = metadata.Type
chunksChan <- &sources.Chunk{
SourceType: s.Type(),
SourceName: s.name,
SourceID: s.SourceID(),
JobID: s.JobID(),
Data: []byte(data),
SourceMetadata: &source_metadatapb.MetaData{
Data: &source_metadatapb.MetaData_Postman{
Postman: &source_metadatapb.Postman{
Link: metadata.Link,
WorkspaceUuid: metadata.WorkspaceUUID,
WorkspaceName: metadata.WorkspaceName,
CollectionId: metadata.CollectionInfo.UID,
CollectionName: metadata.CollectionInfo.Name,
EnvironmentId: metadata.EnvironmentID,
EnvironmentName: metadata.EnvironmentName,
RequestId: metadata.RequestID,
RequestName: metadata.RequestName,
FolderId: metadata.FolderID,
FolderName: metadata.FolderName,
FieldType: metadata.FieldType,
FieldName: metadata.FieldName,
VariableType: metadata.VarType,
},
},
},
Verify: s.verify,
}
}
// unpackWorkspace unzips the provided zip file and scans the inflated files
// for collections and environments. It populates the CollectionsRaw and
// EnvironmentsRaw fields of the Workspace object.
func unpackWorkspace(workspacePath string) (Workspace, error) {
var workspace Workspace
r, err := zip.OpenReader(workspacePath)
if err != nil {
return workspace, err
}
defer r.Close()
for _, file := range r.File {
rc, err := file.Open()
if err != nil {
return workspace, err
}
contents, err := io.ReadAll(rc)
rc.Close()
if err != nil {
return workspace, err
}
if strings.Contains(file.Name, "collection") {
// read in the collection then scan it
var c Collection
if err = json.Unmarshal(contents, &c); err != nil {
return workspace, err
}
workspace.CollectionsRaw = append(workspace.CollectionsRaw, c)
}
if strings.Contains(file.Name, "environment") {
var e VariableData
if err = json.Unmarshal(contents, &e); err != nil {
return workspace, err
}
workspace.EnvironmentsRaw = append(workspace.EnvironmentsRaw, e)
}
}
return workspace, nil
}
func shouldSkip(uuid string, include []string, exclude []string) bool {
if slices.Contains(exclude, uuid) {
return true
}
if len(include) > 0 && !slices.Contains(include, uuid) {
return true
}
return false
}