/
apis_v1.go
154 lines (129 loc) · 4.42 KB
/
apis_v1.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
package api_gateway
import (
"fmt"
api "github.com/aws/aws-sdk-go-v2/service/apigateway"
agTypes "github.com/aws/aws-sdk-go-v2/service/apigateway/types"
v1 "github.com/khulnasoft-lab/defsec/pkg/providers/aws/apigateway/v1"
defsecTypes "github.com/khulnasoft-lab/defsec/pkg/types"
"github.com/khulnasoft-lab/vul-aws/pkg/concurrency"
)
func (a *adapter) getAPIsV1() ([]v1.API, error) {
a.Tracker().SetServiceLabel("Discovering v1 APIs...")
var input api.GetRestApisInput
var apiRestApis []agTypes.RestApi
for {
output, err := a.clientV1.GetRestApis(a.Context(), &input)
if err != nil {
return nil, err
}
apiRestApis = append(apiRestApis, output.Items...)
a.Tracker().SetTotalResources(len(apiRestApis))
if output.Position == nil {
break
}
input.Position = output.Position
}
a.Tracker().SetServiceLabel("Adapting v1 APIs...")
return concurrency.Adapt(apiRestApis, a.RootAdapter, a.adaptRestAPIV1), nil
}
func (a *adapter) adaptRestAPIV1(restAPI agTypes.RestApi) (*v1.API, error) {
metadata := a.CreateMetadata(fmt.Sprintf("/restapis/%s", *restAPI.Id))
stagesOutput, err := a.clientV1.GetStages(a.Context(), &api.GetStagesInput{
RestApiId: restAPI.Id,
})
if err != nil {
return nil, err
}
var stages []v1.Stage
for _, apiStage := range stagesOutput.Item {
stages = append(stages, a.adaptStageV1(restAPI, apiStage))
}
var resources []v1.Resource
resourcesInput := api.GetResourcesInput{
RestApiId: restAPI.Id,
Embed: []string{"methods"},
Position: nil,
}
for {
resourcesOutput, err := a.clientV1.GetResources(a.Context(), &resourcesInput)
if err != nil {
return nil, err
}
for _, resource := range resourcesOutput.Items {
resources = append(resources, a.adaptResourceV1(restAPI, resource))
}
if resourcesOutput.Position == nil {
break
}
resourcesInput.Position = resourcesOutput.Position
}
name := defsecTypes.StringDefault("", metadata)
if restAPI.Name != nil {
name = defsecTypes.String(*restAPI.Name, metadata)
}
return &v1.API{
Metadata: metadata,
Name: name,
Stages: stages,
Resources: resources,
}, nil
}
func (a *adapter) adaptStageV1(restAPI agTypes.RestApi, stage agTypes.Stage) v1.Stage {
metadata := a.CreateMetadata(fmt.Sprintf("/restapis/%s/stages/%s", *restAPI.Id, *stage.StageName))
var logARN string
if stage.AccessLogSettings != nil && stage.AccessLogSettings.DestinationArn != nil {
logARN = *stage.AccessLogSettings.DestinationArn
}
var methodSettings []v1.RESTMethodSettings
for method, setting := range stage.MethodSettings {
methodSettings = append(methodSettings, v1.RESTMethodSettings{
Metadata: metadata,
Method: defsecTypes.String(method, metadata),
CacheDataEncrypted: defsecTypes.Bool(setting.CacheDataEncrypted, metadata),
CacheEnabled: defsecTypes.Bool(setting.CachingEnabled, metadata),
})
}
name := defsecTypes.StringDefault("", metadata)
if stage.StageName != nil {
name = defsecTypes.String(*stage.StageName, metadata)
}
return v1.Stage{
Metadata: metadata,
Name: name,
AccessLogging: v1.AccessLogging{
Metadata: metadata,
CloudwatchLogGroupARN: defsecTypes.String(logARN, metadata),
},
RESTMethodSettings: methodSettings,
XRayTracingEnabled: defsecTypes.Bool(stage.TracingEnabled, metadata),
}
}
func (a *adapter) adaptResourceV1(restAPI agTypes.RestApi, apiResource agTypes.Resource) v1.Resource {
metadata := a.CreateMetadata(fmt.Sprintf("/restapis/%s/resources/%s", *restAPI.Id, *apiResource.Id))
resource := v1.Resource{
Metadata: metadata,
Methods: nil,
}
for _, method := range apiResource.ResourceMethods {
metadata := a.CreateMetadata(fmt.Sprintf("/restapis/%s/resources/%s/methods/%s", *restAPI.Id, *apiResource.Id, *method.HttpMethod))
httpMethod := defsecTypes.StringDefault("", metadata)
if method.HttpMethod != nil {
httpMethod = defsecTypes.String(*method.HttpMethod, metadata)
}
authType := defsecTypes.StringDefault("", metadata)
if method.AuthorizationType != nil {
authType = defsecTypes.String(*method.AuthorizationType, metadata)
}
keyRequired := defsecTypes.BoolDefault(false, metadata)
if method.ApiKeyRequired != nil {
keyRequired = defsecTypes.Bool(*method.ApiKeyRequired, metadata)
}
resource.Methods = append(resource.Methods, v1.Method{
Metadata: metadata,
HTTPMethod: httpMethod,
AuthorizationType: authType,
APIKeyRequired: keyRequired,
})
}
return resource
}