-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathGraphRandomErrorPlugin.cs
More file actions
326 lines (287 loc) · 12.6 KB
/
GraphRandomErrorPlugin.cs
File metadata and controls
326 lines (287 loc) · 12.6 KB
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using DevProxy.Abstractions.Proxy;
using DevProxy.Abstractions.Plugins;
using DevProxy.Abstractions.Models;
using DevProxy.Abstractions.Utils;
using DevProxy.Plugins.Utils;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System.CommandLine;
using System.CommandLine.Parsing;
using System.Globalization;
using System.Net;
using System.Text.Json;
using System.Text.RegularExpressions;
using Titanium.Web.Proxy.Http;
using Titanium.Web.Proxy.Models;
using DevProxy.Plugins.Models;
namespace DevProxy.Plugins.Behavior;
enum GraphRandomErrorFailMode
{
Random,
PassThru
}
public sealed class GraphRandomErrorConfiguration
{
public IEnumerable<int> AllowedErrors { get; set; } = [];
public int Rate { get; set; } = 50;
public int RetryAfterInSeconds { get; set; } = 5;
}
public sealed class GraphRandomErrorPlugin(
ILogger<GraphRandomErrorPlugin> logger,
ISet<UrlToWatch> urlsToWatch,
IProxyConfiguration proxyConfiguration,
IConfigurationSection pluginConfigurationSection) :
BasePlugin<GraphRandomErrorConfiguration>(
logger,
urlsToWatch,
proxyConfiguration,
pluginConfigurationSection)
{
private const string _allowedErrorsOptionName = "--allowed-errors";
private const string _rateOptionName = "--failure-rate";
private readonly Dictionary<string, HttpStatusCode[]> _methodStatusCode = new()
{
{
"GET", new[] {
HttpStatusCode.TooManyRequests,
HttpStatusCode.InternalServerError,
HttpStatusCode.BadGateway,
HttpStatusCode.ServiceUnavailable,
HttpStatusCode.GatewayTimeout
}
},
{
"POST", new[] {
HttpStatusCode.TooManyRequests,
HttpStatusCode.InternalServerError,
HttpStatusCode.BadGateway,
HttpStatusCode.ServiceUnavailable,
HttpStatusCode.GatewayTimeout,
HttpStatusCode.InsufficientStorage
}
},
{
"PUT", new[] {
HttpStatusCode.TooManyRequests,
HttpStatusCode.InternalServerError,
HttpStatusCode.BadGateway,
HttpStatusCode.ServiceUnavailable,
HttpStatusCode.GatewayTimeout,
HttpStatusCode.InsufficientStorage
}
},
{
"PATCH", new[] {
HttpStatusCode.TooManyRequests,
HttpStatusCode.InternalServerError,
HttpStatusCode.BadGateway,
HttpStatusCode.ServiceUnavailable,
HttpStatusCode.GatewayTimeout
}
},
{
"DELETE", new[] {
HttpStatusCode.TooManyRequests,
HttpStatusCode.InternalServerError,
HttpStatusCode.BadGateway,
HttpStatusCode.ServiceUnavailable,
HttpStatusCode.GatewayTimeout,
HttpStatusCode.InsufficientStorage
}
}
};
private readonly Random _random = new();
public override string Name => nameof(GraphRandomErrorPlugin);
public override Option[] GetOptions()
{
var _allowedErrors = new Option<IEnumerable<int>>(_allowedErrorsOptionName, ["-a"])
{
Description = "List of errors that Dev Proxy may produce",
HelpName = "allowed errors",
AllowMultipleArgumentsPerToken = true
};
var _rateOption = new Option<int?>(_rateOptionName, ["-f"])
{
Description = "The percentage of chance that a request will fail",
HelpName = "failure rate"
};
// Add validation for rate option
_rateOption.Validators.Add(result =>
{
var rate = result.GetValueOrDefault<int?>();
if (rate is not null && (rate < 0 || rate > 100))
{
result.AddError($"Rate must be between 0 and 100. Received: {rate}");
}
});
return [_allowedErrors, _rateOption];
}
public override void OptionsLoaded(OptionsLoadedArgs e)
{
ArgumentNullException.ThrowIfNull(e);
base.OptionsLoaded(e);
var parseResult = e.ParseResult;
// Configure the allowed errors
var allowedErrors = parseResult.GetValueForOption<IEnumerable<int>?>(_allowedErrorsOptionName, e.Options);
if (allowedErrors?.Any() ?? false)
{
Configuration.AllowedErrors = [.. allowedErrors];
}
if (Configuration.AllowedErrors.Any())
{
foreach (var k in _methodStatusCode.Keys)
{
_methodStatusCode[k] = [.. _methodStatusCode[k].Where(e => Configuration.AllowedErrors.Any(a => (int)e == a))];
}
}
var rate = parseResult.GetValueForOption<int?>(_rateOptionName, e.Options);
if (rate is not null)
{
Configuration.Rate = rate.Value;
}
}
public override Task BeforeRequestAsync(ProxyRequestArgs e)
{
Logger.LogTrace("{Method} called", nameof(BeforeRequestAsync));
ArgumentNullException.ThrowIfNull(e);
var state = e.ResponseState;
if (state.HasBeenSet)
{
Logger.LogRequest("Response already set", MessageType.Skipped, new(e.Session));
return Task.CompletedTask;
}
if (!e.HasRequestUrlMatch(UrlsToWatch))
{
Logger.LogRequest("URL not matched", MessageType.Skipped, new(e.Session));
return Task.CompletedTask;
}
var failMode = ShouldFail();
if (failMode == GraphRandomErrorFailMode.PassThru && Configuration.Rate != 100)
{
Logger.LogRequest("Pass through", MessageType.Skipped, new(e.Session));
return Task.CompletedTask;
}
if (ProxyUtils.IsGraphBatchUrl(e.Session.HttpClient.Request.RequestUri))
{
FailBatch(e);
}
else
{
FailResponse(e);
}
state.HasBeenSet = true;
Logger.LogTrace("Left {Name}", nameof(BeforeRequestAsync));
return Task.CompletedTask;
}
// uses config to determine if a request should be failed
private GraphRandomErrorFailMode ShouldFail() => _random.Next(1, 100) <= Configuration.Rate ? GraphRandomErrorFailMode.Random : GraphRandomErrorFailMode.PassThru;
private void FailResponse(ProxyRequestArgs e)
{
// pick a random error response for the current request method
var methodStatusCodes = _methodStatusCode[e.Session.HttpClient.Request.Method ?? "GET"];
var errorStatus = methodStatusCodes[_random.Next(0, methodStatusCodes.Length)];
UpdateProxyResponse(e, errorStatus);
}
private void FailBatch(ProxyRequestArgs e)
{
var batchResponse = new GraphBatchResponsePayload();
var batch = JsonSerializer.Deserialize<GraphBatchRequestPayload>(e.Session.HttpClient.Request.BodyString, ProxyUtils.JsonSerializerOptions);
if (batch == null)
{
UpdateProxyBatchResponse(e, batchResponse);
return;
}
var responses = new List<GraphBatchResponsePayloadResponse>();
foreach (var request in batch.Requests)
{
try
{
// pick a random error response for the current request method
var methodStatusCodes = _methodStatusCode[request.Method];
var errorStatus = methodStatusCodes[_random.Next(0, methodStatusCodes.Length)];
var response = new GraphBatchResponsePayloadResponse
{
Id = request.Id,
Status = (int)errorStatus,
Body = new GraphBatchResponsePayloadResponseBody
{
Error = new()
{
Code = new Regex("([A-Z])").Replace(errorStatus.ToString(), m => { return $" {m.Groups[1]}"; }).Trim(),
Message = "Some error was generated by the proxy.",
}
}
};
if (errorStatus == HttpStatusCode.TooManyRequests)
{
var retryAfterDate = DateTime.Now.AddSeconds(Configuration.RetryAfterInSeconds);
var requestUrl = ProxyUtils.GetAbsoluteRequestUrlFromBatch(e.Session.HttpClient.Request.RequestUri, request.Url);
var throttledRequests = e.GlobalData[RetryAfterPlugin.ThrottledRequestsKey] as List<ThrottlerInfo>;
throttledRequests?.Add(new(GraphUtils.BuildThrottleKey(requestUrl), ShouldThrottle, retryAfterDate));
response.Headers = new() { { "Retry-After", Configuration.RetryAfterInSeconds.ToString(CultureInfo.InvariantCulture) } };
}
responses.Add(response);
}
catch { }
}
batchResponse.Responses = [.. responses];
UpdateProxyBatchResponse(e, batchResponse);
}
private ThrottlingInfo ShouldThrottle(Request request, string throttlingKey)
{
var throttleKeyForRequest = GraphUtils.BuildThrottleKey(request);
return new(throttleKeyForRequest == throttlingKey ? Configuration.RetryAfterInSeconds : 0, "Retry-After");
}
private void UpdateProxyResponse(ProxyRequestArgs e, HttpStatusCode errorStatus)
{
var session = e.Session;
var requestId = Guid.NewGuid().ToString();
var requestDate = DateTime.Now.ToString(CultureInfo.CurrentCulture);
var request = session.HttpClient.Request;
var headers = ProxyUtils.BuildGraphResponseHeaders(request, requestId, requestDate);
if (errorStatus == HttpStatusCode.TooManyRequests)
{
var retryAfterDate = DateTime.Now.AddSeconds(Configuration.RetryAfterInSeconds);
if (!e.GlobalData.TryGetValue(RetryAfterPlugin.ThrottledRequestsKey, out var value))
{
value = new List<ThrottlerInfo>();
e.GlobalData.Add(RetryAfterPlugin.ThrottledRequestsKey, value);
}
var throttledRequests = value as List<ThrottlerInfo>;
throttledRequests?.Add(new(GraphUtils.BuildThrottleKey(request), ShouldThrottle, retryAfterDate));
headers.Add(new("Retry-After", Configuration.RetryAfterInSeconds.ToString(CultureInfo.InvariantCulture)));
}
var body = JsonSerializer.Serialize(new GraphErrorResponseBody(
new()
{
Code = new Regex("([A-Z])").Replace(errorStatus.ToString(), m => { return $" {m.Groups[1]}"; }).Trim(),
Message = BuildApiErrorMessage(request),
InnerError = new()
{
RequestId = requestId,
Date = requestDate
}
}),
ProxyUtils.JsonSerializerOptions
);
Logger.LogRequest($"{(int)errorStatus} {errorStatus}", MessageType.Chaos, new(e.Session));
session.GenericResponse(body ?? string.Empty, errorStatus, headers.Select(h => new HttpHeader(h.Name, h.Value)));
}
private void UpdateProxyBatchResponse(ProxyRequestArgs ev, GraphBatchResponsePayload response)
{
// failed batch uses a fixed 424 error status code
var errorStatus = HttpStatusCode.FailedDependency;
var session = ev.Session;
var requestId = Guid.NewGuid().ToString();
var requestDate = DateTime.Now.ToString(CultureInfo.CurrentCulture);
var request = session.HttpClient.Request;
var headers = ProxyUtils.BuildGraphResponseHeaders(request, requestId, requestDate);
var body = JsonSerializer.Serialize(response, ProxyUtils.JsonSerializerOptions);
Logger.LogRequest($"{(int)errorStatus} {errorStatus}", MessageType.Chaos, new(ev.Session));
session.GenericResponse(body, errorStatus, headers.Select(h => new HttpHeader(h.Name, h.Value)));
}
private static string BuildApiErrorMessage(Request r) => $"Some error was generated by the proxy. {(ProxyUtils.IsGraphRequest(r) ? ProxyUtils.IsSdkRequest(r) ? "" : string.Join(' ', MessageUtils.BuildUseSdkForErrorsMessage()) : "")}";
}