generated from steadybit/extension-scaffold
-
Notifications
You must be signed in to change notification settings - Fork 0
/
action_jdbc_template_delay.go
199 lines (181 loc) · 7.07 KB
/
action_jdbc_template_delay.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
/*
* Copyright 2023 steadybit GmbH. All rights reserved.
*/
package extjvm
import (
"context"
"github.com/steadybit/action-kit/go/action_kit_api/v2"
"github.com/steadybit/action-kit/go/action_kit_sdk"
"github.com/steadybit/extension-kit/extbuild"
"github.com/steadybit/extension-kit/extutil"
"time"
)
type jdbcTemplateDelay struct{}
type JdbcTemplateDelayState struct {
Delay time.Duration
DelayJitter bool
Operations string
JdbcUrl string
*AttackState
}
// Make sure action implements all required interfaces
var (
_ action_kit_sdk.Action[JdbcTemplateDelayState] = (*jdbcTemplateDelay)(nil)
_ action_kit_sdk.ActionWithStop[JdbcTemplateDelayState] = (*jdbcTemplateDelay)(nil)
)
func NewJdbcTemplateDelay() action_kit_sdk.Action[JdbcTemplateDelayState] {
return &jdbcTemplateDelay{}
}
func (l *jdbcTemplateDelay) NewEmptyState() JdbcTemplateDelayState {
return JdbcTemplateDelayState{
AttackState: &AttackState{},
}
}
// Describe returns the action description for the platform with all required information.
func (l *jdbcTemplateDelay) Describe() action_kit_api.ActionDescription {
return action_kit_api.ActionDescription{
Id: TargetIDPrefix + ".spring-jdbctemplate-delay-attack",
Label: "JDBC Template Delay",
Description: "Delay a Spring JDBC Template response by the given duration.",
Version: extbuild.GetSemverVersionStringOrUnknown(),
Icon: extutil.Ptr(jdbcTemplateDelayIcon),
TargetSelection: extutil.Ptr(action_kit_api.TargetSelection{
// The target type this action is for
TargetType: targetID + "(application.type=spring;spring.jdbc-template)",
// You can provide a list of target templates to help the user select targets.
// A template can be used to pre-fill a selection
SelectionTemplates: extutil.Ptr(targetSelectionTemplates),
}),
// Category for the targets to appear in
Category: extutil.Ptr(category),
// To clarify the purpose of the action, you can set a kind.
// Attack: Will cause harm to targets
// Check: Will perform checks on the targets
// LoadTest: Will perform load tests on the targets
// Other
Kind: action_kit_api.Attack,
// How the action is controlled over time.
// External: The agent takes care and calls stop then the time has passed. Requires a duration parameter. Use this when the duration is known in advance.
// Internal: The action has to implement the status endpoint to signal when the action is done. Use this when the duration is not known in advance.
// Instantaneous: The action is done immediately. Use this for actions that happen immediately, e.g. a reboot.
TimeControl: action_kit_api.External,
// The parameters for the action
Parameters: []action_kit_api.ActionParameter{
{
Name: "operations",
Label: "Operation",
Description: extutil.Ptr("Which operation should be attacked?"),
Type: action_kit_api.String,
DefaultValue: extutil.Ptr("*"),
Required: extutil.Ptr(true),
Options: extutil.Ptr([]action_kit_api.ParameterOption{
action_kit_api.ExplicitParameterOption{
Label: "Any",
Value: "*",
},
action_kit_api.ExplicitParameterOption{
Label: "Reads",
Value: "r",
},
action_kit_api.ExplicitParameterOption{
Label: "Writes",
Value: "w",
},
}),
},
{
Name: "duration",
Label: "Duration",
Description: extutil.Ptr("How long should the traffic be dropped?"),
Type: action_kit_api.Duration,
DefaultValue: extutil.Ptr("30s"),
Required: extutil.Ptr(true),
},
{
Name: "delay",
Label: "Delay",
Description: extutil.Ptr("How long should the db access be delayed?"),
Type: action_kit_api.Duration,
DefaultValue: extutil.Ptr("500ms"),
Required: extutil.Ptr(true),
},
{
Name: "delayJitter",
Label: "Jitter",
Description: extutil.Ptr("Add random +/-30% jitter to response delay?"),
Type: action_kit_api.Boolean,
DefaultValue: extutil.Ptr("true"),
Required: extutil.Ptr(true),
Advanced: extutil.Ptr(true),
},
{
Name: "jdbcUrl",
Label: "JDBC connection url",
Description: extutil.Ptr("Which JDBC connection should be attacked?"),
Type: action_kit_api.String,
DefaultValue: extutil.Ptr("*"),
Required: extutil.Ptr(true),
Advanced: extutil.Ptr(true),
Options: extutil.Ptr([]action_kit_api.ParameterOption{
action_kit_api.ExplicitParameterOption{
Label: "Any",
Value: "*",
},
action_kit_api.ParameterOptionsFromTargetAttribute{
Attribute: "datasource.jdbc-url",
},
}),
},
},
Stop: extutil.Ptr(action_kit_api.MutatingEndpointReference{}),
}
}
// Prepare is called before the action is started.
// It can be used to validate the parameters and prepare the action.
// It must not cause any harmful effects.
// The passed in state is included in the subsequent calls to start/status/stop.
// So the state should contain all information needed to execute the action and even more important: to be able to stop it.
func (l *jdbcTemplateDelay) Prepare(_ context.Context, state *JdbcTemplateDelayState, request action_kit_api.PrepareActionRequestBody) (*action_kit_api.PrepareResult, error) {
state.JdbcUrl = extutil.ToString(request.Config["jdbcUrl"])
state.Operations = extutil.ToString(request.Config["operations"])
parsedDelay := extutil.ToUInt64(request.Config["delay"])
var delay time.Duration
if parsedDelay == 0 {
delay = 0
} else {
delay = time.Duration(parsedDelay) * time.Millisecond
}
state.Delay = delay
delayJitter := extutil.ToBool(request.Config["delayJitter"])
state.DelayJitter = delayJitter
errResult := extractDuration(request, state.AttackState)
if errResult != nil {
return errResult, nil
}
errResult = extractPid(request, state.AttackState)
if errResult != nil {
return errResult, nil
}
var config = map[string]interface{}{
"attack-class": "com.steadybit.attacks.javaagent.instrumentation.SpringJdbcTemplateDelayInstrumentation",
"duration": int(state.Duration / time.Millisecond),
"delay": int(state.Delay / time.Millisecond),
"delayJitter": state.DelayJitter,
"operations": state.Operations,
"jdbc-url": state.JdbcUrl,
}
return commonPrepareEnd(config, state.AttackState, request)
}
// Start is called to start the action
// You can mutate the state here.
// You can use the result to return messages/errors/metrics or artifacts
func (l *jdbcTemplateDelay) Start(_ context.Context, state *JdbcTemplateDelayState) (*action_kit_api.StartResult, error) {
return commonStart(state.AttackState)
}
// Stop is called to stop the action
// It will be called even if the start method did not complete successfully.
// It should be implemented in a immutable way, as the agent might to retries if the stop method timeouts.
// You can use the result to return messages/errors/metrics or artifacts
func (l *jdbcTemplateDelay) Stop(_ context.Context, state *JdbcTemplateDelayState) (*action_kit_api.StopResult, error) {
return commonStop(state.AttackState)
}