-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrated last pieces of legacy fixture code
- Loading branch information
1 parent
9ef04e7
commit 9a1f3a5
Showing
8 changed files
with
181 additions
and
225 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
...ing_api_integration/common/fixtures/plugins/actions_simulators/server/slack_simulation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { schema } from '@kbn/config-schema'; | ||
import { | ||
RequestHandlerContext, | ||
KibanaRequest, | ||
KibanaResponseFactory, | ||
IKibanaResponse, | ||
IRouter, | ||
} from 'kibana/server'; | ||
|
||
export function initPlugin(router: IRouter, path: string) { | ||
router.post( | ||
{ | ||
path, | ||
options: { | ||
authRequired: false, | ||
}, | ||
validate: { | ||
body: schema.object({ | ||
text: schema.string(), | ||
}), | ||
}, | ||
}, | ||
// ServiceNow simulator: create a servicenow action pointing here, and you can get | ||
// different responses based on the message posted. See the README.md for | ||
// more info. | ||
async function ( | ||
context: RequestHandlerContext, | ||
req: KibanaRequest<any, any, any, any>, | ||
res: KibanaResponseFactory | ||
): Promise<IKibanaResponse<any>> { | ||
const body = req.body; | ||
const text = body && body.text; | ||
|
||
if (text == null) { | ||
return res.badRequest({ body: 'bad request to slack simulator' }); | ||
} | ||
|
||
switch (text) { | ||
case 'success': | ||
return res.ok({ body: 'ok' }); | ||
|
||
case 'no_text': | ||
return res.badRequest({ body: 'no_text' }); | ||
|
||
case 'invalid_payload': | ||
return res.badRequest({ body: 'invalid_payload' }); | ||
|
||
case 'invalid_token': | ||
return res.forbidden({ body: 'invalid_token' }); | ||
|
||
case 'status_500': | ||
return res.internalError({ body: 'simulated slack 500 response' }); | ||
|
||
case 'rate_limit': | ||
const response = { | ||
retry_after: 1, | ||
ok: false, | ||
error: 'rate_limited', | ||
}; | ||
|
||
return res.custom({ | ||
body: Buffer.from('ok'), | ||
statusCode: 429, | ||
headers: { | ||
'retry-after': '1', | ||
}, | ||
}); | ||
} | ||
|
||
return res.badRequest({ body: 'unknown request to slack simulator' }); | ||
} | ||
); | ||
} |
83 changes: 83 additions & 0 deletions
83
...g_api_integration/common/fixtures/plugins/actions_simulators/server/webhook_simulation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
import expect from '@kbn/expect'; | ||
import http from 'http'; | ||
import { fromNullable, map, filter, getOrElse } from 'fp-ts/lib/Option'; | ||
import { pipe } from 'fp-ts/lib/pipeable'; | ||
import { constant } from 'fp-ts/lib/function'; | ||
|
||
export async function initPlugin() { | ||
return http.createServer((request, response) => { | ||
const credentials = pipe( | ||
fromNullable(request.headers.authorization), | ||
map((authorization) => authorization.split(/\s+/)), | ||
filter((parts) => parts.length > 1), | ||
map((parts) => Buffer.from(parts[1], 'base64').toString()), | ||
filter((credentialsPart) => credentialsPart.indexOf(':') !== -1), | ||
map((credentialsPart) => { | ||
const [username, password] = credentialsPart.split(':'); | ||
return { username, password }; | ||
}), | ||
getOrElse(constant({ username: '', password: '' })) | ||
); | ||
|
||
if (request.method === 'POST' || 'PUT') { | ||
const data: unknown[] = []; | ||
request.on('data', (chunk) => { | ||
data.push(chunk); | ||
}); | ||
request.on('end', () => { | ||
const body = JSON.parse(data.toString()); | ||
switch (body) { | ||
case 'success': | ||
response.statusCode = 200; | ||
response.end('OK'); | ||
return; | ||
case 'authenticate': | ||
return validateAuthentication(credentials, response); | ||
case 'success_post_method': | ||
return validateRequestUsesMethod(request.method ?? '', 'post', response); | ||
case 'success_put_method': | ||
return validateRequestUsesMethod(request.method ?? '', 'put', response); | ||
case 'failure': | ||
response.statusCode = 500; | ||
response.end('Error'); | ||
return; | ||
} | ||
response.statusCode = 400; | ||
response.end( | ||
`unknown request to webhook simulator [${body ? `content: ${body}` : `no content`}]` | ||
); | ||
return; | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
function validateAuthentication(credentials: any, res: any) { | ||
try { | ||
expect(credentials).to.eql({ | ||
username: 'elastic', | ||
password: 'changeme', | ||
}); | ||
res.statusCode = 200; | ||
res.end('OK'); | ||
} catch (ex) { | ||
res.statusCode = 403; | ||
res.end(`the validateAuthentication operation failed. ${ex.message}`); | ||
} | ||
} | ||
|
||
function validateRequestUsesMethod(requestMethod: string, method: string, res: any) { | ||
try { | ||
expect(requestMethod).to.eql(method); | ||
res.statusCode = 200; | ||
res.end('OK'); | ||
} catch (ex) { | ||
res.statusCode = 403; | ||
res.end(`the validateAuthentication operation failed. ${ex.message}`); | ||
} | ||
} |
26 changes: 0 additions & 26 deletions
26
.../test/alerting_api_integration/common/fixtures/plugins/actions_simulators_legacy/index.ts
This file was deleted.
Oops, something went wrong.
7 changes: 0 additions & 7 deletions
7
...t/alerting_api_integration/common/fixtures/plugins/actions_simulators_legacy/package.json
This file was deleted.
Oops, something went wrong.
74 changes: 0 additions & 74 deletions
74
...ing_api_integration/common/fixtures/plugins/actions_simulators_legacy/slack_simulation.ts
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.