Diagnostic tool: testing API communication from Prosody to Peertube.

This commit is contained in:
John Livingston 2021-06-22 12:57:24 +02:00
parent 7279761c66
commit a526feac19
6 changed files with 83 additions and 7 deletions

View File

@ -4,7 +4,8 @@
### Features
* Diagnostic tool: testing API communication between Peertube and Prosody.
* Diagnostic tool: testing API communication from Peertube to Prosody.
* Diagnostic tool: testing API communication from Prosody to Peertube.
## v3.0.0

View File

@ -1,10 +1,20 @@
local json = require "util.json";
local async = require "util.async";
local http = require "net.http";
module:depends"http";
function check_auth(routes)
local apikey = assert(module:get_option_string("peertubelivechat_test_apikey", nil), "'peertubelivechat_test_apikey' is a required option");
local peertube_url = assert(module:get_option_string("peertubelivechat_test_peertube_api_url", nil), "'peertubelivechat_test_peertube_api_url' is a required option");
local ex = {
headers = {
accept = "application/json";
}
};
local function check_auth(routes)
local function check_request_auth(event)
local apikey = module:get_option_string("peertubelivechat_test_apikey", "")
if apikey == "" then
return false, 500;
end
@ -35,9 +45,43 @@ local function test_peertube_prosody(event)
return json.encode(json_response);
end
local function test_prosody_peertube(event)
local request, response = event.request, event.response;
local ret, err;
http.request(peertube_url, ex, function (body, code)
if math.floor(code / 100) == 2 then
local parsed, parse_err = json.decode(body);
if not parsed then
module:log("debug", "Got invalid JSON from %s: %s", peertube_url, parse_err);
err = problems.format;
else
ret = parsed;
end
else
module:log("debug", "Rejected by API: ", body);
err = "Rejected by API";
end
local json_response;
if not ret then
json_response = {
ok = false;
error = err;
};
else
json_response = ret;
end
response:send(json.encode(json_response));
end);
event.response.headers["Content-Type"] = "application/json";
return true
end
module:provides("http", {
route = check_auth {
["GET /test-peertube-prosody"] = test_peertube_prosody;
-- ["GET /test-prosody-peertube"] = test_prosody_peertube;
["GET /test-prosody-peertube"] = test_prosody_peertube;
};
});

View File

@ -113,6 +113,27 @@ export async function diagProsody (test: string, options: RegisterServerOptions)
return result
}
try {
const apiUrl = `http://localhost:${prosodyPort}/peertubelivechat_test/test-prosody-peertube`
const testResult = await got(apiUrl, {
method: 'GET',
headers: {
authorization: 'Bearer ' + await getAPIKey(options)
},
responseType: 'json',
resolveBodyOnly: true
})
if (testResult.ok === true) {
result.messages.push('API Prosody -> Peertube is OK')
} else {
result.messages.push('API Prosody -> Peertube is KO. Response was: ' + JSON.stringify(testResult))
return result
}
} catch (error) {
result.messages.push('Error when calling Prosody test api (test-prosody-peertube): ' + (error as string))
return result
}
result.ok = true
return result
}

View File

@ -85,6 +85,7 @@ async function getProsodyConfig (options: RegisterServerOptions): Promise<Prosod
'api/'
const authApiUrl = baseApiUrl + 'user' // FIXME: should be protected by apikey, but mod_auth_http cant handle params
const roomApiUrl = baseApiUrl + 'room?apikey=' + apikey + '&jid={room.jid|jid_node}'
const testApiUrl = baseApiUrl + 'test?apikey=' + apikey
const config = new ProsodyConfigContent(paths, prosodyDomain)
config.useHttpAuthentication(authApiUrl)
@ -96,7 +97,7 @@ async function getProsodyConfig (options: RegisterServerOptions): Promise<Prosod
// TODO: add a settings to choose?
config.useDefaultPersistent()
config.useTestModule(apikey)
config.useTestModule(apikey, testApiUrl)
let logLevel: ProsodyLogLevel | undefined
if (logger.level && (typeof logger.level === 'string')) {

View File

@ -236,9 +236,10 @@ class ProsodyConfigContent {
this.muc.set('muc_room_default_persistent', true)
}
useTestModule (apikey: string): void {
useTestModule (prosodyApikey: string, apiurl: string): void {
this.global.add('modules_enabled', 'http_peertubelivechat_test')
this.global.set('peertubelivechat_test_apikey', apikey)
this.global.set('peertubelivechat_test_apikey', prosodyApikey)
this.global.set('peertubelivechat_test_peertube_api_url', apiurl)
}
setLog (level: ProsodyLogLevel, syslog?: ProsodyLogLevel[]): void {

View File

@ -34,6 +34,14 @@ async function initApiRouter (options: RegisterServerOptions): Promise<Router> {
const router = getRouter()
const logger = peertubeHelpers.logger
router.get('/test', asyncMiddleware([
getCheckAPIKeyMiddleware(options),
async (req: Request, res: Response, _next: NextFunction) => {
logger.info('Test api call')
res.json({ ok: true })
}
]))
router.get('/room', asyncMiddleware([
getCheckAPIKeyMiddleware(options),
async (req: Request, res: Response, _next: NextFunction) => {