generated from steadybit/extension-scaffold
-
Notifications
You must be signed in to change notification settings - Fork 0
/
action_controller_commons.go
143 lines (136 loc) · 3.95 KB
/
action_controller_commons.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
package extjvm
import (
"fmt"
"github.com/steadybit/action-kit/go/action_kit_api/v2"
"github.com/steadybit/extension-jvm/extjvm/utils"
"github.com/steadybit/extension-kit/extutil"
)
var (
patternAttribute = action_kit_api.ActionParameter{
Name: "pattern",
Label: "Request Mapping",
Description: extutil.Ptr("Which request mapping pattern should be used to match the requests?"),
Type: action_kit_api.String,
Required: extutil.Ptr(true),
Options: extutil.Ptr([]action_kit_api.ParameterOption{
action_kit_api.ParameterOptionsFromTargetAttribute{
Attribute: "spring.mvc-mapping",
},
}),
}
methodAttribute = action_kit_api.ActionParameter{
Name: "method",
Label: "Http Method",
Description: extutil.Ptr("Which HTTP methods should be attacked?"),
Type: action_kit_api.String,
Required: extutil.Ptr(true),
DefaultValue: extutil.Ptr("*"),
Options: methodsOptions,
}
methodsOptions = extutil.Ptr([]action_kit_api.ParameterOption{
action_kit_api.ExplicitParameterOption{
Label: "Any",
Value: "*",
},
action_kit_api.ExplicitParameterOption{
Label: "GET",
Value: "GET",
},
action_kit_api.ExplicitParameterOption{
Label: "HEAD",
Value: "HEAD",
},
action_kit_api.ExplicitParameterOption{
Label: "OPTIONS",
Value: "OPTIONS",
},
action_kit_api.ExplicitParameterOption{
Label: "TRACE",
Value: "TRACE",
},
action_kit_api.ExplicitParameterOption{
Label: "POST",
Value: "POST",
},
action_kit_api.ExplicitParameterOption{
Label: "PUT",
Value: "PUT",
},
action_kit_api.ExplicitParameterOption{
Label: "PATCH",
Value: "PATCH",
},
action_kit_api.ExplicitParameterOption{
Label: "DELETE",
Value: "DELETE",
},
})
)
type ControllerState struct {
Pattern string
Method string
HandlerMethods []string
*AttackState
}
func extractPattern(request action_kit_api.PrepareActionRequestBody, state *ControllerState) *action_kit_api.PrepareResult {
pattern := extutil.ToString(request.Config["pattern"])
if pattern == "" {
return &action_kit_api.PrepareResult{
Error: extutil.Ptr(action_kit_api.ActionKitError{
Title: "Pattern is required",
Status: extutil.Ptr(action_kit_api.Errored),
}),
}
}
state.Pattern = pattern
return nil
}
func extractMethod(request action_kit_api.PrepareActionRequestBody, state *ControllerState) *action_kit_api.PrepareResult {
method := extutil.ToString(request.Config["method"])
if method == "" {
return &action_kit_api.PrepareResult{
Error: extutil.Ptr(action_kit_api.ActionKitError{
Title: "Method is required",
Status: extutil.Ptr(action_kit_api.Errored),
}),
}
}
state.Method = method
return nil
}
func extractHandlerMethods(_ action_kit_api.PrepareActionRequestBody, state *ControllerState) *action_kit_api.PrepareResult {
application := FindSpringApplication(state.Pid)
if application == nil {
return &action_kit_api.PrepareResult{
Error: extutil.Ptr(action_kit_api.ActionKitError{
Title: "Spring application not found",
Status: extutil.Ptr(action_kit_api.Errored),
}),
}
}
if application.MvcMappings == nil {
return &action_kit_api.PrepareResult{
Error: extutil.Ptr(action_kit_api.ActionKitError{
Title: "Spring MVC mappings not found",
Status: extutil.Ptr(action_kit_api.Errored),
}),
}
}
relevantMappings := make([]SpringMvcMapping, 0)
for _, m := range *application.MvcMappings {
if utils.ContainsString(m.Patterns, state.Pattern) {
if state.Method == "*" || (len(m.Methods) == 0 && state.Method == "GET") {
relevantMappings = append(relevantMappings, m)
} else if utils.ContainsString(m.Methods, state.Method) {
relevantMappings = append(relevantMappings, m)
}
}
}
configMethods := make([]string, 0)
for _, m := range relevantMappings {
method := fmt.Sprintf("%s#%s", m.HandlerClass, m.HandlerName)
configMethods = append(configMethods, method)
}
state.HandlerMethods = configMethods
return nil
}