rework code to enable/disable remote support
we had a generic ssh key management api. this was causing issues because the ssh format is more complicated than what we had implemented. currently, the only use case we have is to add our ssh key. Fixes #600
This commit is contained in:
@@ -1,232 +0,0 @@
|
||||
/* global it:false */
|
||||
/* global describe:false */
|
||||
/* global before:false */
|
||||
/* global after:false */
|
||||
|
||||
'use strict';
|
||||
|
||||
var ssh = require('../../ssh.js'),
|
||||
async = require('async'),
|
||||
config = require('../../config.js'),
|
||||
database = require('../../database.js'),
|
||||
expect = require('expect.js'),
|
||||
superagent = require('superagent'),
|
||||
server = require('../../server.js');
|
||||
|
||||
var SERVER_URL = 'http://localhost:' + config.get('port');
|
||||
|
||||
var USERNAME = 'superadmin', PASSWORD = 'Foobar?1337', EMAIL ='silly@me.com';
|
||||
|
||||
var INVALID_KEY_TYPE = 'ssh-foobar AAAAB3NzaC1yc2EAAAADAQABAAABAQCibC8G04mZy3o3AVMxjUMQoEQj0HSsl6AMVZDQK9A0e8qVRWft4HaRZdw0dW3iFDsEdny7s1zSAc5Kp5y38kJdSyEHGKxvcR8TaghUa8jpmu0sEVOTn+X4UtoonkNuJ0Jnl2tjPYsq5BtJmAeYUa1bKH5CjomCYi5OSfXRtnuZV6SiYX0A1OnZPXFWa/iFwwOUJQvDLGbFkgtJxqhxpc7yvzwFK5B9MNs7LJxA8+kRibJ9LTN1OKWNxb0oSk/PE6PFo9M2Q/SL9uj2IRXRipGj2XcOtZlqcAK5i+aq3UjjAGekztK2srQPcBkWbnI3Oim2N8l2purCfe0AoCCQHK7N nebulon@nebulon';
|
||||
var INVALID_KEY_VALUE = 'ssh-rsa foobar nebulon@nebulon';
|
||||
var INVALID_KEY_IDENTIFIER = 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCibC8G04mZy3o3AVMxjUMQoEQj0HSsl6AMVZDQK9A0e8qVRWft4HaRZdw0dW3iFDsEdny7s1zSAc5Kp5y38kJdSyEHGKxvcR8TaghUa8jpmu0sEVOTn+X4UtoonkNuJ0Jnl2tjPY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
config._reset();
|
||||
config.setFqdn('example-ssh-test.com');
|
||||
|
||||
async.series([
|
||||
server.start.bind(server),
|
||||
|
||||
ssh._clear,
|
||||
database._clear,
|
||||
|
||||
function createAdmin(callback) {
|
||||
superagent.post(SERVER_URL + '/api/v1/cloudron/activate')
|
||||
.query({ setupToken: 'somesetuptoken' })
|
||||
.send({ username: USERNAME, password: PASSWORD, email: EMAIL })
|
||||
.end(function (error, result) {
|
||||
expect(result).to.be.ok();
|
||||
expect(result.statusCode).to.eql(201);
|
||||
|
||||
// stash token for further use
|
||||
token = result.body.token;
|
||||
|
||||
callback();
|
||||
});
|
||||
}
|
||||
], done);
|
||||
}
|
||||
|
||||
function cleanup(done) {
|
||||
database._clear(function (error) {
|
||||
expect(!error).to.be.ok();
|
||||
|
||||
server.stop(done);
|
||||
});
|
||||
}
|
||||
|
||||
describe('SSH API', function () {
|
||||
before(setup);
|
||||
after(cleanup);
|
||||
|
||||
describe('add authorized_keys', function () {
|
||||
it('fails due to missing key', function (done) {
|
||||
superagent.put(SERVER_URL + '/api/v1/cloudron/ssh/authorized_keys')
|
||||
.query({ access_token: token })
|
||||
.end(function (err, res) {
|
||||
expect(res.statusCode).to.equal(400);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('fails due to empty key', function (done) {
|
||||
superagent.put(SERVER_URL + '/api/v1/cloudron/ssh/authorized_keys')
|
||||
.query({ access_token: token })
|
||||
.send({ key: '' })
|
||||
.end(function (err, res) {
|
||||
expect(res.statusCode).to.equal(400);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('fails due to invalid key', function (done) {
|
||||
superagent.put(SERVER_URL + '/api/v1/cloudron/ssh/authorized_keys')
|
||||
.query({ access_token: token })
|
||||
.send({ key: 'foobar' })
|
||||
.end(function (err, res) {
|
||||
expect(res.statusCode).to.equal(400);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('fails due to invalid key type', function (done) {
|
||||
superagent.put(SERVER_URL + '/api/v1/cloudron/ssh/authorized_keys')
|
||||
.query({ access_token: token })
|
||||
.send({ key: INVALID_KEY_TYPE })
|
||||
.end(function (err, res) {
|
||||
expect(res.statusCode).to.equal(400);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('fails due to invalid key value', function (done) {
|
||||
superagent.put(SERVER_URL + '/api/v1/cloudron/ssh/authorized_keys')
|
||||
.query({ access_token: token })
|
||||
.send({ key: INVALID_KEY_VALUE })
|
||||
.end(function (err, res) {
|
||||
expect(res.statusCode).to.equal(400);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('fails due to invalid key identifier', function (done) {
|
||||
superagent.put(SERVER_URL + '/api/v1/cloudron/ssh/authorized_keys')
|
||||
.query({ access_token: token })
|
||||
.send({ key: INVALID_KEY_IDENTIFIER })
|
||||
.end(function (err, res) {
|
||||
expect(res.statusCode).to.equal(400);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('succeeds', function (done) {
|
||||
superagent.put(SERVER_URL + '/api/v1/cloudron/ssh/authorized_keys')
|
||||
.query({ access_token: token })
|
||||
.send({ key: VALID_KEY })
|
||||
.end(function (err, res) {
|
||||
expect(res.statusCode).to.equal(201);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('get authorized_keys', function () {
|
||||
it('fails for non existing key', function (done) {
|
||||
superagent.get(SERVER_URL + '/api/v1/cloudron/ssh/authorized_keys/foobar')
|
||||
.query({ access_token: token })
|
||||
.end(function (err, res) {
|
||||
expect(res.statusCode).to.equal(404);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('succeeds', function (done) {
|
||||
superagent.get(SERVER_URL + '/api/v1/cloudron/ssh/authorized_keys/' + VALID_KEY.split(' ')[2])
|
||||
.query({ access_token: token })
|
||||
.end(function (err, res) {
|
||||
expect(res.statusCode).to.equal(200);
|
||||
expect(res.body).to.be.an('object');
|
||||
expect(res.body.identifier).to.be.a('string');
|
||||
expect(res.body.identifier).to.equal(VALID_KEY.split(' ')[2]);
|
||||
expect(res.body.key).to.equal(VALID_KEY);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('list authorized_keys', function () {
|
||||
it('succeeds', function (done) {
|
||||
superagent.get(SERVER_URL + '/api/v1/cloudron/ssh/authorized_keys')
|
||||
.query({ access_token: token })
|
||||
.end(function (err, res) {
|
||||
expect(res.statusCode).to.equal(200);
|
||||
expect(res.body).to.be.an('object');
|
||||
expect(res.body.keys).to.be.an('array');
|
||||
expect(res.body.keys.length).to.equal(1);
|
||||
expect(res.body.keys[0]).to.be.an('object');
|
||||
expect(res.body.keys[0].identifier).to.be.a('string');
|
||||
expect(res.body.keys[0].identifier).to.equal(VALID_KEY.split(' ')[2]);
|
||||
expect(res.body.keys[0].key).to.equal(VALID_KEY);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('succeeds with two keys', function (done) {
|
||||
superagent.put(SERVER_URL + '/api/v1/cloudron/ssh/authorized_keys')
|
||||
.query({ access_token: token })
|
||||
.send({ key: VALID_KEY_1 })
|
||||
.end(function (err, res) {
|
||||
expect(res.statusCode).to.equal(201);
|
||||
|
||||
superagent.get(SERVER_URL + '/api/v1/cloudron/ssh/authorized_keys')
|
||||
.query({ access_token: token })
|
||||
.end(function (err, res) {
|
||||
expect(res.statusCode).to.equal(200);
|
||||
expect(res.body).to.be.an('object');
|
||||
expect(res.body.keys).to.be.an('array');
|
||||
expect(res.body.keys.length).to.equal(2);
|
||||
expect(res.body.keys[0]).to.be.an('object');
|
||||
expect(res.body.keys[0].identifier).to.be.a('string');
|
||||
expect(res.body.keys[0].identifier).to.equal(VALID_KEY_1.split(' ')[2]);
|
||||
expect(res.body.keys[0].key).to.equal(VALID_KEY_1);
|
||||
expect(res.body.keys[1]).to.be.an('object');
|
||||
expect(res.body.keys[1].identifier).to.be.a('string');
|
||||
expect(res.body.keys[1].identifier).to.equal(VALID_KEY.split(' ')[2]);
|
||||
expect(res.body.keys[1].key).to.equal(VALID_KEY);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('delete authorized_keys', function () {
|
||||
it('fails for non existing key', function (done) {
|
||||
superagent.del(SERVER_URL + '/api/v1/cloudron/ssh/authorized_keys/foobar')
|
||||
.query({ access_token: token })
|
||||
.end(function (err, res) {
|
||||
expect(res.statusCode).to.equal(404);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('succeeds', function (done) {
|
||||
superagent.del(SERVER_URL + '/api/v1/cloudron/ssh/authorized_keys/' + VALID_KEY.split(' ')[2])
|
||||
.query({ access_token: token })
|
||||
.end(function (err, res) {
|
||||
expect(res.statusCode).to.equal(202);
|
||||
|
||||
superagent.get(SERVER_URL + '/api/v1/cloudron/ssh/authorized_keys/' + VALID_KEY.split(' ')[2])
|
||||
.query({ access_token: token })
|
||||
.end(function (err, res) {
|
||||
expect(res.statusCode).to.equal(404);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
139
src/routes/test/support-test.js
Normal file
139
src/routes/test/support-test.js
Normal file
@@ -0,0 +1,139 @@
|
||||
/* global it:false */
|
||||
/* global describe:false */
|
||||
/* global before:false */
|
||||
/* global after:false */
|
||||
|
||||
'use strict';
|
||||
|
||||
var async = require('async'),
|
||||
config = require('../../config.js'),
|
||||
database = require('../../database.js'),
|
||||
expect = require('expect.js'),
|
||||
path = require('path'),
|
||||
safe = require('safetydance'),
|
||||
superagent = require('superagent'),
|
||||
server = require('../../server.js');
|
||||
|
||||
var SERVER_URL = 'http://localhost:' + config.get('port');
|
||||
|
||||
var USERNAME = 'superadmin', PASSWORD = 'Foobar?1337', EMAIL ='silly@me.com';
|
||||
var AUTHORIZED_KEYS_FILE = path.join(config.baseDir(), 'authorized_keys');
|
||||
var token = null;
|
||||
|
||||
function setup(done) {
|
||||
config._reset();
|
||||
config.setFqdn('example-ssh-test.com');
|
||||
safe.fs.unlinkSync(AUTHORIZED_KEYS_FILE);
|
||||
|
||||
async.series([
|
||||
server.start.bind(server),
|
||||
|
||||
database._clear,
|
||||
|
||||
function createAdmin(callback) {
|
||||
superagent.post(SERVER_URL + '/api/v1/cloudron/activate')
|
||||
.query({ setupToken: 'somesetuptoken' })
|
||||
.send({ username: USERNAME, password: PASSWORD, email: EMAIL })
|
||||
.end(function (error, result) {
|
||||
expect(result).to.be.ok();
|
||||
expect(result.statusCode).to.eql(201);
|
||||
|
||||
// stash token for further use
|
||||
token = result.body.token;
|
||||
|
||||
callback();
|
||||
});
|
||||
}
|
||||
], done);
|
||||
}
|
||||
|
||||
function cleanup(done) {
|
||||
database._clear(function (error) {
|
||||
expect(!error).to.be.ok();
|
||||
|
||||
server.stop(done);
|
||||
});
|
||||
}
|
||||
|
||||
describe('Support API', function () {
|
||||
before(setup);
|
||||
after(cleanup);
|
||||
|
||||
describe('remote support', function () {
|
||||
it('get remote support', function (done) {
|
||||
superagent.get(SERVER_URL + '/api/v1/support/remote_support')
|
||||
.query({ access_token: token })
|
||||
.end(function (err, res) {
|
||||
expect(res.statusCode).to.equal(200);
|
||||
expect(res.body.enabled).to.be(false);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('enable remote support', function (done) {
|
||||
superagent.post(SERVER_URL + '/api/v1/support/remote_support')
|
||||
.query({ access_token: token })
|
||||
.send({ enable: true })
|
||||
.end(function (err, res) {
|
||||
expect(res.statusCode).to.equal(202);
|
||||
|
||||
let data = safe.fs.readFileSync(AUTHORIZED_KEYS_FILE, 'utf8');
|
||||
let count = (data.match(/support@cloudron.io/g) || []).length;
|
||||
expect(count).to.be(1);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('returns true when remote support enabled', function (done) {
|
||||
superagent.get(SERVER_URL + '/api/v1/support/remote_support')
|
||||
.query({ access_token: token })
|
||||
.end(function (err, res) {
|
||||
expect(res.statusCode).to.equal(200);
|
||||
expect(res.body.enabled).to.be(true);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('enable remote support (again)', function (done) {
|
||||
superagent.post(SERVER_URL + '/api/v1/support/remote_support')
|
||||
.query({ access_token: token })
|
||||
.send({ enable: true })
|
||||
.end(function (err, res) {
|
||||
expect(res.statusCode).to.equal(202);
|
||||
|
||||
let data = safe.fs.readFileSync(AUTHORIZED_KEYS_FILE, 'utf8');
|
||||
let count = (data.match(/support@cloudron.io/g) || []).length;
|
||||
expect(count).to.be(1);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('disable remote support', function (done) {
|
||||
superagent.post(SERVER_URL + '/api/v1/support/remote_support')
|
||||
.query({ access_token: token })
|
||||
.send({ enable: false })
|
||||
.end(function (err, res) {
|
||||
expect(res.statusCode).to.equal(202);
|
||||
|
||||
let data = safe.fs.readFileSync(AUTHORIZED_KEYS_FILE, 'utf8');
|
||||
let count = (data.match(/support@cloudron.io/g) || []).length;
|
||||
expect(count).to.be(0);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('disable remote support (again)', function (done) {
|
||||
superagent.post(SERVER_URL + '/api/v1/support/remote_support')
|
||||
.query({ access_token: token })
|
||||
.send({ enable: false })
|
||||
.end(function (err, res) {
|
||||
expect(res.statusCode).to.equal(202);
|
||||
|
||||
let data = safe.fs.readFileSync(AUTHORIZED_KEYS_FILE, 'utf8');
|
||||
let count = (data.match(/support@cloudron.io/g) || []).length;
|
||||
expect(count).to.be(0);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user