diff --git a/config/default.yaml b/config/default.yaml index b5fd7b06d..d32d3e17b 100644 --- a/config/default.yaml +++ b/config/default.yaml @@ -18,8 +18,5 @@ storage: logs: 'logs/' thumbnails: 'thumbnails/' -network: - friends: [] - electron: debug: false diff --git a/config/test-1.yaml b/config/test-1.yaml index 3afcb1b04..0a8dd3937 100644 --- a/config/test-1.yaml +++ b/config/test-1.yaml @@ -14,7 +14,3 @@ storage: uploads: 'test1/uploads/' logs: 'test1/logs/' thumbnails: 'test1/thumbnails/' - -network: - friends: - - 'http://localhost:9002' diff --git a/config/test-2.yaml b/config/test-2.yaml index 51cc186eb..40f410559 100644 --- a/config/test-2.yaml +++ b/config/test-2.yaml @@ -14,7 +14,3 @@ storage: uploads: 'test2/uploads/' logs: 'test2/logs/' thumbnails: 'test2/thumbnails/' - -network: - friends: - - 'http://localhost:9003' diff --git a/config/test-3.yaml b/config/test-3.yaml index 7ef01ba4d..87b335228 100644 --- a/config/test-3.yaml +++ b/config/test-3.yaml @@ -14,7 +14,3 @@ storage: uploads: 'test3/uploads/' logs: 'test3/logs/' thumbnails: 'test3/thumbnails/' - -network: - friends: - - 'http://localhost:9001' diff --git a/config/test-4.yaml b/config/test-4.yaml index a4d3bb164..22abc0a58 100644 --- a/config/test-4.yaml +++ b/config/test-4.yaml @@ -14,7 +14,3 @@ storage: uploads: 'test4/uploads/' logs: 'test4/logs/' thumbnails: 'test4/thumbnails/' - -network: - friends: - - 'http://localhost:9002' diff --git a/config/test-5.yaml b/config/test-5.yaml index 0435c17fe..af5619994 100644 --- a/config/test-5.yaml +++ b/config/test-5.yaml @@ -14,8 +14,3 @@ storage: uploads: 'test5/uploads/' logs: 'test5/logs/' thumbnails: 'test5/thumbnails/' - -network: - friends: - - 'http://localhost:9001' - - 'http://localhost:9004' diff --git a/config/test-6.yaml b/config/test-6.yaml index b7dbd4bdd..5b8bf306b 100644 --- a/config/test-6.yaml +++ b/config/test-6.yaml @@ -14,9 +14,3 @@ storage: uploads: 'test6/uploads/' logs: 'test6/logs/' thumbnails: 'test6/thumbnails/' - -network: - friends: - - 'http://localhost:9001' - - 'http://localhost:9002' - - 'http://localhost:9003' diff --git a/server/controllers/api/v1/pods.js b/server/controllers/api/v1/pods.js index f61f2a483..982a1e364 100644 --- a/server/controllers/api/v1/pods.js +++ b/server/controllers/api/v1/pods.js @@ -19,7 +19,7 @@ const Video = mongoose.model('Video') router.get('/', listPodsUrl) router.post('/', validators.podsAdd, addPods) -router.get('/makefriends', +router.post('/makefriends', oAuth.authenticate, admin.ensureIsAdmin, validators.makeFriends, @@ -83,7 +83,9 @@ function listPodsUrl (req, res, next) { } function makeFriends (req, res, next) { - friends.makeFriends(function (err) { + const urls = req.body.urls + + friends.makeFriends(urls, function (err) { if (err) return next(err) res.type('json').status(204).end() diff --git a/server/helpers/custom-validators/misc.js b/server/helpers/custom-validators/misc.js index 782ae3dee..13904ea1b 100644 --- a/server/helpers/custom-validators/misc.js +++ b/server/helpers/custom-validators/misc.js @@ -1,8 +1,11 @@ 'use strict' +const validator = require('express-validator').validator + const miscValidators = { exists: exists, - isArray: isArray + isArray: isArray, + isEachUrl: isEachUrl } function exists (value) { @@ -13,6 +16,12 @@ function isArray (value) { return Array.isArray(value) } +function isEachUrl (urls) { + return urls.every(function (url) { + return validator.isURL(url) + }) +} + // --------------------------------------------------------------------------- module.exports = miscValidators diff --git a/server/initializers/checker.js b/server/initializers/checker.js index 4b6997547..2a33009b4 100644 --- a/server/initializers/checker.js +++ b/server/initializers/checker.js @@ -17,8 +17,8 @@ function checkConfig () { const required = [ 'listen.port', 'webserver.https', 'webserver.host', 'webserver.port', 'database.host', 'database.port', 'database.suffix', - 'storage.certs', 'storage.uploads', 'storage.logs', - 'network.friends', 'electron.debug' ] + 'storage.certs', 'storage.uploads', 'storage.logs', 'storage.thumbnails', + 'electron.debug' ] const miss = [] for (const key of required) { diff --git a/server/lib/friends.js b/server/lib/friends.js index 6a2c37fd7..667055d4c 100644 --- a/server/lib/friends.js +++ b/server/lib/friends.js @@ -1,6 +1,5 @@ 'use strict' -const config = require('config') const each = require('async/each') const eachLimit = require('async/eachLimit') const eachSeries = require('async/eachSeries') @@ -44,7 +43,7 @@ function getMyCertificate (callback) { fs.readFile(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.pub', 'utf8', callback) } -function makeFriends (callback) { +function makeFriends (urls, callback) { const podsScore = {} logger.info('Make friends!') @@ -54,8 +53,6 @@ function makeFriends (callback) { return callback(err) } - const urls = config.get('network.friends') - eachSeries(urls, function (url, callbackEach) { computeForeignPodsList(url, podsScore, callbackEach) }, function (err) { diff --git a/server/middlewares/validators/pods.js b/server/middlewares/validators/pods.js index fda2e865f..7c4d04aff 100644 --- a/server/middlewares/validators/pods.js +++ b/server/middlewares/validators/pods.js @@ -10,6 +10,11 @@ const validatorsPod = { } function makeFriends (req, res, next) { + req.checkBody('urls', 'Should have an array of urls').isArray() + req.checkBody('urls', 'Should be an url').isEachUrl() + + logger.debug('Checking makeFriends parameters', { parameters: req.body }) + friends.hasFriends(function (err, hasFriends) { if (err) { logger.error('Cannot know if we have friends.', { error: err }) diff --git a/server/tests/api/check-params.js b/server/tests/api/check-params.js index fc8b0a42a..ec666417c 100644 --- a/server/tests/api/check-params.js +++ b/server/tests/api/check-params.js @@ -108,10 +108,40 @@ describe('Test parameters validator', function () { }) describe('When making friends', function () { + const body = { + urls: [ 'http://localhost:9002' ] + } + + it('Should fail without urls', function (done) { + request(server.url) + .post(path + '/makefriends') + .set('Authorization', 'Bearer faketoken') + .set('Accept', 'application/json') + .expect(401, done) + }) + + it('Should fail with urls is not an array', function (done) { + request(server.url) + .post(path + '/makefriends') + .send({ urls: 'http://localhost:9002' }) + .set('Authorization', 'Bearer faketoken') + .set('Accept', 'application/json') + .expect(401, done) + }) + + it('Should fail if the array is not composed by urls', function (done) { + request(server.url) + .post(path + '/makefriends') + .send({ urls: [ 'http://localhost:9002', 'localhost:coucou' ] }) + .set('Authorization', 'Bearer faketoken') + .set('Accept', 'application/json') + .expect(401, done) + }) + it('Should fail with a invalid token', function (done) { request(server.url) - .get(path + '/makefriends') - .query({ start: 'hello' }) + .post(path + '/makefriends') + .send(body) .set('Authorization', 'Bearer faketoken') .set('Accept', 'application/json') .expect(401, done) @@ -119,8 +149,8 @@ describe('Test parameters validator', function () { it('Should fail if the user is not an administrator', function (done) { request(server.url) - .get(path + '/makefriends') - .query({ start: 'hello' }) + .post(path + '/makefriends') + .send(body) .set('Authorization', 'Bearer ' + userAccessToken) .set('Accept', 'application/json') .expect(403, done) diff --git a/server/tests/utils/pods.js b/server/tests/utils/pods.js index 366492110..9a9148856 100644 --- a/server/tests/utils/pods.js +++ b/server/tests/utils/pods.js @@ -27,13 +27,38 @@ function makeFriends (url, accessToken, expectedStatus, end) { expectedStatus = 204 } + // Which pod makes friends with which pod + const friendsMatrix = { + 'http://localhost:9001': [ + 'http://localhost:9002' + ], + 'http://localhost:9002': [ + 'http://localhost:9003' + ], + 'http://localhost:9003': [ + 'http://localhost:9001' + ], + 'http://localhost:9004': [ + 'http://localhost:9002' + ], + 'http://localhost:9005': [ + 'http://localhost:9001', + 'http://localhost:9004' + ], + 'http://localhost:9006': [ + 'http://localhost:9001', + 'http://localhost:9002', + 'http://localhost:9003' + ] + } const path = '/api/v1/pods/makefriends' // The first pod make friend with the third request(url) - .get(path) + .post(path) .set('Accept', 'application/json') .set('Authorization', 'Bearer ' + accessToken) + .send({ 'urls': friendsMatrix[url] }) .expect(expectedStatus) .end(function (err, res) { if (err) throw err