1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | import { Context } from "../IContext";
|
13 | import { RequestMethod } from "../RequestMethod";
|
14 | import { Middleware } from "./IMiddleware";
|
15 | import { MiddlewareControl } from "./MiddlewareControl";
|
16 | import { generateUUID } from "./MiddlewareUtil";
|
17 | import { httpStatusCode, methodStatusCode } from "./options/ChaosHandlerData";
|
18 | import { ChaosHandlerOptions } from "./options/ChaosHandlerOptions";
|
19 | import { ChaosStrategy } from "./options/ChaosStrategy";
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | export class ChaosHandler implements Middleware {
|
28 | |
29 |
|
30 |
|
31 |
|
32 |
|
33 | private options: ChaosHandlerOptions;
|
34 |
|
35 | |
36 |
|
37 |
|
38 |
|
39 |
|
40 | private manualMap: Map<string, Map<string, number>>;
|
41 |
|
42 | |
43 |
|
44 |
|
45 |
|
46 | private nextMiddleware: Middleware;
|
47 |
|
48 | |
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | public constructor(options: ChaosHandlerOptions = new ChaosHandlerOptions(), manualMap?: Map<string, Map<string, number>>) {
|
57 | this.options = options;
|
58 | this.manualMap = manualMap;
|
59 | }
|
60 |
|
61 | |
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 | private createResponseHeaders(chaosHandlerOptions: ChaosHandlerOptions, requestID: string, requestDate: string) {
|
70 | const responseHeader: Headers = chaosHandlerOptions.headers ? new Headers(chaosHandlerOptions.headers) : new Headers();
|
71 | responseHeader.append("Cache-Control", "no-store");
|
72 | responseHeader.append("request-id", requestID);
|
73 | responseHeader.append("client-request-id", requestID);
|
74 | responseHeader.append("x-ms-ags-diagnostic", "");
|
75 | responseHeader.append("Date", requestDate);
|
76 | responseHeader.append("Strict-Transport-Security", "");
|
77 |
|
78 | if (chaosHandlerOptions.statusCode === 429) {
|
79 |
|
80 | responseHeader.append("retry-after", "3");
|
81 | }
|
82 |
|
83 | return responseHeader;
|
84 | }
|
85 |
|
86 | |
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 | private createResponseBody(chaosHandlerOptions: ChaosHandlerOptions, requestID: string, requestDate: string) {
|
95 | if (chaosHandlerOptions.responseBody) {
|
96 | return chaosHandlerOptions.responseBody;
|
97 | }
|
98 | let body: any;
|
99 | if (chaosHandlerOptions.statusCode >= 400) {
|
100 | const codeMessage: string = httpStatusCode[chaosHandlerOptions.statusCode];
|
101 | const errMessage: string = chaosHandlerOptions.statusMessage;
|
102 |
|
103 | body = {
|
104 | error: {
|
105 | code: codeMessage,
|
106 | message: errMessage,
|
107 | innerError: {
|
108 | "request-id": requestID,
|
109 | date: requestDate,
|
110 | },
|
111 | },
|
112 | };
|
113 | } else {
|
114 | body = {};
|
115 | }
|
116 | return body;
|
117 | }
|
118 |
|
119 | |
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 | private createResponse(chaosHandlerOptions: ChaosHandlerOptions, context: Context) {
|
126 | const requestURL = context.request as string;
|
127 | const requestID = generateUUID();
|
128 | const requestDate = new Date();
|
129 | const responseHeader = this.createResponseHeaders(chaosHandlerOptions, requestID, requestDate.toString());
|
130 | const responseBody = this.createResponseBody(chaosHandlerOptions, requestID, requestDate.toString());
|
131 | const init: any = { url: requestURL, status: chaosHandlerOptions.statusCode, statusText: chaosHandlerOptions.statusMessage, headers: responseHeader };
|
132 | context.response = new Response(typeof responseBody === "string" ? responseBody : JSON.stringify(responseBody), init);
|
133 | }
|
134 |
|
135 | |
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 | private async sendRequest(chaosHandlerOptions: ChaosHandlerOptions, context: Context): Promise<void> {
|
143 | this.setStatusCode(chaosHandlerOptions, context.request as string, context.options.method as RequestMethod);
|
144 | if ((chaosHandlerOptions.chaosStrategy === ChaosStrategy.MANUAL && !this.nextMiddleware) || Math.floor(Math.random() * 100) < chaosHandlerOptions.chaosPercentage) {
|
145 | this.createResponse(chaosHandlerOptions, context);
|
146 | } else if (this.nextMiddleware) {
|
147 | await this.nextMiddleware.execute(context);
|
148 | }
|
149 | }
|
150 |
|
151 | |
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 | private getRandomStatusCode(requestMethod: RequestMethod): number {
|
158 | const statusCodeArray: number[] = methodStatusCode[requestMethod] as number[];
|
159 | return statusCodeArray[Math.floor(Math.random() * statusCodeArray.length)];
|
160 | }
|
161 |
|
162 | |
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 | private getRelativeURL(urlMethod: string): string {
|
169 | const pattern = /https?:\/\/graph\.microsoft\.com\/[^/]+(.+?)(\?|$)/;
|
170 | let relativeURL: string;
|
171 | if (pattern.exec(urlMethod) !== null) {
|
172 | relativeURL = pattern.exec(urlMethod)[1];
|
173 | }
|
174 | return relativeURL;
|
175 | }
|
176 |
|
177 | |
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 | private setStatusCode(chaosHandlerOptions: ChaosHandlerOptions, requestURL: string, requestMethod: RequestMethod) {
|
185 | if (chaosHandlerOptions.chaosStrategy === ChaosStrategy.MANUAL) {
|
186 | if (chaosHandlerOptions.statusCode === undefined) {
|
187 |
|
188 | const relativeURL: string = this.getRelativeURL(requestURL);
|
189 | if (this.manualMap.get(relativeURL) !== undefined) {
|
190 |
|
191 | if (this.manualMap.get(relativeURL).get(requestMethod) !== undefined) {
|
192 | chaosHandlerOptions.statusCode = this.manualMap.get(relativeURL).get(requestMethod);
|
193 | }
|
194 |
|
195 | } else {
|
196 |
|
197 | this.manualMap.forEach((value: Map<string, number>, key: string) => {
|
198 | const regexURL = new RegExp(key + "$");
|
199 | if (regexURL.test(relativeURL)) {
|
200 | if (this.manualMap.get(key).get(requestMethod) !== undefined) {
|
201 | chaosHandlerOptions.statusCode = this.manualMap.get(key).get(requestMethod);
|
202 | }
|
203 |
|
204 | }
|
205 | });
|
206 | }
|
207 |
|
208 |
|
209 | }
|
210 | } else {
|
211 |
|
212 | chaosHandlerOptions.statusCode = this.getRandomStatusCode(requestMethod);
|
213 |
|
214 | }
|
215 | }
|
216 |
|
217 | |
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 | private getOptions(context: Context): ChaosHandlerOptions {
|
224 | let options: ChaosHandlerOptions;
|
225 | if (context.middlewareControl instanceof MiddlewareControl) {
|
226 | options = context.middlewareControl.getMiddlewareOptions(ChaosHandlerOptions) as ChaosHandlerOptions;
|
227 | }
|
228 | if (typeof options === "undefined") {
|
229 | options = Object.assign(new ChaosHandlerOptions(), this.options);
|
230 | }
|
231 |
|
232 | return options;
|
233 | }
|
234 |
|
235 | |
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 | public async execute(context: Context): Promise<void> {
|
243 | const chaosHandlerOptions: ChaosHandlerOptions = this.getOptions(context);
|
244 | return await this.sendRequest(chaosHandlerOptions, context);
|
245 | }
|
246 |
|
247 | |
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 | public setNext(next: Middleware): void {
|
254 | this.nextMiddleware = next;
|
255 | }
|
256 | }
|