Skip to content

Commit 1cab4c7

Browse files
authored
Merge pull request #3 from avithe-great/main
feat: Update the output json of the API scanning
2 parents e892b77 + fa9e2b0 commit 1cab4c7

File tree

7 files changed

+37
-31
lines changed

7 files changed

+37
-31
lines changed

config/default.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ openAPISpec: <urlOrPath> # Either filepath or URL
1616

1717
exporter:
1818
jsonReportFilePath: report.json
19+
20+
scanName: default-openapi-spec-scan

internal/apispec/oas.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package apispec
55

66
import (
77
"github.com/pb33f/libopenapi"
8-
"github.com/pb33f/libopenapi/datamodel/high/v3"
8+
v3 "github.com/pb33f/libopenapi/datamodel/high/v3"
99
)
1010

1111
func BuildOASV3Model(specBytes []byte) (*libopenapi.DocumentModel[v3.Document], error) {

internal/config/config.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package config
66
import (
77
"encoding/json"
88
"fmt"
9+
"time"
910

1011
"github.com/spf13/viper"
1112
"go.uber.org/zap"
@@ -36,6 +37,7 @@ type Configuration struct {
3637
Environment Environment `json:"environment"`
3738
OpenAPISpec string `json:"openAPISpec"`
3839
Exporter Exporter `json:"exporter,omitempty"`
40+
ScanName string `json:"scanName"`
3941
}
4042

4143
func (c *Configuration) validate() error {
@@ -59,10 +61,6 @@ func (c *Configuration) validate() error {
5961
return fmt.Errorf("configuration does not contain a valid OpenAPI Specification filepath or URL")
6062
}
6163

62-
if c.Environment.ClusterId == 0 {
63-
return fmt.Errorf("please provide a valid cluster ID")
64-
}
65-
6664
if c.Exporter.JsonReportFilePath == "" {
6765
return fmt.Errorf("configuration does not contain a valid JSON reports file path")
6866
}
@@ -90,6 +88,11 @@ func New(configFilePath string, logger *zap.SugaredLogger) (Configuration, error
9088
logger.Warn("using default JSON report file path: ", defaultJSONReportFilePath)
9189
}
9290

91+
if config.ScanName == "" {
92+
config.ScanName = fmt.Sprintf("openapi-scan-%s", time.Now().Format("20060102-150405"))
93+
logger.Infof("scanName not provided, using generated name: %s", config.ScanName)
94+
}
95+
9396
if err := config.validate(); err != nil {
9497
return Configuration{}, err
9598
}

internal/core/apievent.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@ import (
1111
)
1212

1313
func (m *Manager) findDocuments(collectionName string, clusterId int) (*hashset.Set, error) {
14-
// Todo: Process in batch of maybe 2000
1514
filter := bson.D{
16-
bson.E{Key: "operation", Value: "Api"},
17-
bson.E{Key: "cluster_id", Value: clusterId},
15+
{Key: "operation", Value: "Api"},
1816
}
17+
18+
if clusterId != 0 {
19+
filter = append(filter, bson.E{Key: "cluster_id", Value: clusterId})
20+
}
21+
1922
projection := bson.D{
2023
{Key: "_id", Value: 0},
2124
{Key: "cluster_name", Value: 1},
@@ -28,7 +31,7 @@ func (m *Manager) findDocuments(collectionName string, clusterId int) (*hashset.
2831

2932
cursor, err := m.DBHandler.Database.
3033
Collection(collectionName).
31-
Find(m.Ctx, &filter, &options.FindOptions{
34+
Find(m.Ctx, filter, &options.FindOptions{
3235
Projection: &projection,
3336
})
3437
if err != nil {
@@ -52,6 +55,7 @@ func (m *Manager) findDocuments(collectionName string, clusterId int) (*hashset.
5255
if responseCode == nil {
5356
continue
5457
}
58+
5559
apiEvents.Add(apievent.ApiEvent{
5660
ClusterName: document["cluster_name"].(string),
5761
ServiceName: document["api_event"].(bson.M)["http"].(bson.M)["request"].(bson.M)["headers"].(bson.M)[":authority"].(string),
@@ -61,8 +65,13 @@ func (m *Manager) findDocuments(collectionName string, clusterId int) (*hashset.
6165
Occurrences: int(document["api_event"].(bson.M)["count"].(int32)),
6266
})
6367
}
68+
6469
if apiEvents.Size() == 0 {
65-
m.Logger.Warnf("no documents found in `%s` collection of clusterID: `%d`", collectionName, clusterId)
70+
clusterInfo := fmt.Sprintf("clusterID: `%d`", clusterId)
71+
if clusterId == 0 {
72+
clusterInfo = "all clusters"
73+
}
74+
m.Logger.Warnf("no documents found in `%s` collection for %s", collectionName, clusterInfo)
6675
return nil, nil
6776
}
6877

internal/core/core.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func Run(ctx context.Context, configFilePath string) {
6868

6969
shadowApis, zombieApis := mgr.findShadowAndZombieApi(trie, events, model)
7070
orphanApis := mgr.findOrphanApi(events, model)
71-
if err := mgr.exportJsonReport(mgr.Cfg.Exporter.JsonReportFilePath, shadowApis, zombieApis, orphanApis, model.Model.Info, model.Index.GetConfig().SpecInfo.Version); err != nil {
71+
if err := mgr.exportJsonReport(mgr.Cfg.Exporter.JsonReportFilePath, shadowApis, zombieApis, orphanApis); err != nil {
7272
mgr.Logger.Error(err)
7373
return
7474
}

internal/core/discovery.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
"github.com/emirpasic/gods/sets/hashset"
1111
"github.com/pb33f/libopenapi"
12-
"github.com/pb33f/libopenapi/datamodel/high/v3"
12+
v3 "github.com/pb33f/libopenapi/datamodel/high/v3"
1313

1414
"github.com/5gsec/api-speculator/internal/apievent"
1515
"github.com/5gsec/api-speculator/internal/apispec"

internal/core/reporter.go

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ package core
66
import (
77
"encoding/json"
88
"os"
9-
10-
"github.com/pb33f/libopenapi/datamodel/high/base"
119
)
1210

1311
type API struct {
@@ -19,26 +17,20 @@ type API struct {
1917
}
2018

2119
type apiReport struct {
22-
ClusterId int `json:"clusterId"`
23-
TenantId int `json:"tenantId"`
24-
SpecTitle string `json:"specTitle"`
25-
SpecVersion string `json:"specVersion"`
26-
OASVersion string `json:"oasVersion"`
27-
ShadowAPIs []API `json:"shadowApis,omitempty"`
28-
ZombieAPIs []API `json:"zombieApis,omitempty"`
29-
OrphanAPIs []API `json:"orphanApis,omitempty"`
20+
TenantId int `json:"tenantId"`
21+
ScanName string `json:"scan_name"`
22+
ShadowAPIs []API `json:"shadowApis,omitempty"`
23+
ZombieAPIs []API `json:"zombieApis,omitempty"`
24+
OrphanAPIs []API `json:"orphanApis,omitempty"`
3025
}
3126

32-
func (m *Manager) exportJsonReport(reportFilePath string, shadowApis, zombieApis, orphanApis []API, specInfo *base.Info, openApiVersion string) error {
27+
func (m *Manager) exportJsonReport(reportFilePath string, shadowApis, zombieApis, orphanApis []API) error {
3328
report := apiReport{
34-
ClusterId: m.Cfg.Environment.ClusterId,
35-
TenantId: m.Cfg.Environment.TenantId,
36-
SpecTitle: specInfo.Title,
37-
SpecVersion: specInfo.Version,
38-
OASVersion: openApiVersion,
39-
ShadowAPIs: shadowApis,
40-
ZombieAPIs: zombieApis,
41-
OrphanAPIs: orphanApis,
29+
TenantId: m.Cfg.Environment.TenantId,
30+
ScanName: m.Cfg.ScanName,
31+
ShadowAPIs: shadowApis,
32+
ZombieAPIs: zombieApis,
33+
OrphanAPIs: orphanApis,
4234
}
4335

4436
f, err := os.OpenFile(reportFilePath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o666)

0 commit comments

Comments
 (0)