registry config: create table and migrate existing setting

This commit is contained in:
Girish Ramakrishnan
2025-05-07 14:09:10 +02:00
parent 91fd93c724
commit 1bbdabc42f
15 changed files with 441 additions and 123 deletions
-37
View File
@@ -1,37 +0,0 @@
'use strict';
exports = module.exports = {
getRegistryConfig,
setRegistryConfig
};
const assert = require('assert'),
BoxError = require('../boxerror.js'),
docker = require('../docker.js'),
HttpError = require('connect-lastmile').HttpError,
HttpSuccess = require('connect-lastmile').HttpSuccess,
safe = require('safetydance');
async function getRegistryConfig(req, res, next) {
const [error, registryConfig] = await safe(docker.getRegistryConfig());
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, docker.removePrivateFields(registryConfig)));
}
async function setRegistryConfig(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
if (!req.body.provider || typeof req.body.provider !== 'string') return next(new HttpError(400, 'provider is required'));
if (req.body.provider !== 'noop') {
if (typeof req.body.serverAddress !== 'string') return next(new HttpError(400, 'serverAddress is required'));
if ('username' in req.body && typeof req.body.username !== 'string') return next(new HttpError(400, 'username is required'));
if ('email' in req.body && typeof req.body.email !== 'string') return next(new HttpError(400, 'email is required'));
if ('password' in req.body && typeof req.body.password !== 'string') return next(new HttpError(400, 'password is required'));
}
const [error] = await safe(docker.setRegistryConfig(req.body));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200));
}
+86
View File
@@ -0,0 +1,86 @@
'use strict';
exports = module.exports = {
list,
add,
get,
del,
update,
load
};
const assert = require('assert'),
AuditSource = require('../auditsource.js'),
BoxError = require('../boxerror.js'),
dockerRegistries = require('../dockerregistries.js'),
HttpError = require('connect-lastmile').HttpError,
HttpSuccess = require('connect-lastmile').HttpSuccess,
safe = require('safetydance');
async function load(req, res, next) {
assert.strictEqual(typeof req.params.id, 'string');
const [error, result] = await safe(dockerRegistries.get(req.params.id));
if (error) return next(BoxError.toHttpError(error));
if (!result) return next(new HttpError(404, 'Registry not found'));
req.resource = result;
next();
}
async function list(req, res, next) {
assert.strictEqual(typeof req.user, 'object');
const [error, results] = await safe(dockerRegistries.list());
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, { registries: results.map(dockerRegistries.removePrivateFields) }));
}
function get(req, res, next) {
assert.strictEqual(typeof req.resource, 'object');
assert.strictEqual(typeof req.user, 'object');
next(new HttpSuccess(200, dockerRegistries.removePrivateFields(req.resource)));
}
async function add(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
if (!req.body.provider || typeof req.body.provider !== 'string') return next(new HttpError(400, 'provider is required'));
if (typeof req.body.serverAddress !== 'string') return next(new HttpError(400, 'serverAddress is required'));
if ('username' in req.body && typeof req.body.username !== 'string') return next(new HttpError(400, 'username must be a string'));
if ('email' in req.body && typeof req.body.email !== 'string') return next(new HttpError(400, 'email must be a string'));
if ('password' in req.body && typeof req.body.password !== 'string') return next(new HttpError(400, 'password must be a string'));
const [error, id] = await safe(dockerRegistries.add(req.body, AuditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(201, { id }));
}
async function update(req, res, next) {
assert.strictEqual(typeof req.params.id, 'string');
assert.strictEqual(typeof req.body, 'object');
assert.strictEqual(typeof req.resource, 'object');
if (!req.body.provider || typeof req.body.provider !== 'string') return next(new HttpError(400, 'provider is required'));
if (typeof req.body.serverAddress !== 'string') return next(new HttpError(400, 'serverAddress is required'));
if ('username' in req.body && typeof req.body.username !== 'string') return next(new HttpError(400, 'username must be a string'));
if ('email' in req.body && typeof req.body.email !== 'string') return next(new HttpError(400, 'email must be a string'));
if ('password' in req.body && typeof req.body.password !== 'string') return next(new HttpError(400, 'password must be a string'));
const [updateError] = await safe(dockerRegistries.update(req.resource, req.body, AuditSource.fromRequest(req)));
if (updateError) return next(BoxError.toHttpError(updateError));
next(new HttpSuccess(204, {}));
}
async function del(req, res, next) {
assert.strictEqual(typeof req.resource, 'object');
const [error] = await safe(dockerRegistries.del(req.resource, AuditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(204));
}
+1 -1
View File
@@ -13,7 +13,7 @@ exports = module.exports = {
cloudron: require('./cloudron.js'),
dashboard: require('./dashboard.js'),
directoryServer: require('./directoryserver.js'),
docker: require('./docker.js'),
dockerRegistries: require('./dockerregistries.js'),
domains: require('./domains.js'),
eventlog: require('./eventlog.js'),
externalLdap: require('./externalldap.js'),
+1 -1
View File
@@ -134,7 +134,7 @@ async function setup() {
await timers.setTimeout(2000);
// create admin
// create owner
response = await superagent.post(`${serverUrl}/api/v1/provision/activate`)
.query({ setupToken: 'somesetuptoken' })
.send({ username: owner.username, password: owner.password, email: owner.email });
+85
View File
@@ -0,0 +1,85 @@
/* jslint node:true */
/* global it:false */
/* global describe:false */
/* global before:false */
/* global after:false */
'use strict';
const common = require('./common.js'),
constants = require('../../constants.js'),
expect = require('expect.js'),
superagent = require('../../superagent.js');
describe('Docker Registries', function () {
const { setup, cleanup, serverUrl, owner } = common;
before(setup);
after(cleanup);
it('cannot add registry with invalid token', async function () {
const response = await superagent.post(`${serverUrl}/api/v1/docker/registries`)
.query({ access_token: owner.token + 'xx' })
.send({ provider: 'devops', serverAddress: 'registry.devops' })
.ok(() => true);
expect(response.status).to.equal(401);
});
it('cannot add registry without provider', async function () {
const response = await superagent.post(`${serverUrl}/api/v1/docker/registries`)
.query({ access_token: owner.token })
.send({})
.ok(() => true);
expect(response.status).to.equal(400);
});
let id;
it('can add registry', async function () {
const response = await superagent.post(`${serverUrl}/api/v1/docker/registries`)
.query({ access_token: owner.token })
.send({ provider: 'devops', serverAddress: 'registry.devops', password: 'hushhush' });
expect(response.status).to.equal(201);
expect(response.body.id).to.be.a('string');
id = response.body.id;
});
it('can list registries', async function () {
const response = await superagent.get(`${serverUrl}/api/v1/docker/registries`)
.query({ access_token: owner.token });
expect(response.status).to.equal(200);
expect(response.body.registries).to.be.an(Array);
expect(response.body.registries.length).to.be(1);
expect(response.body.registries[0].provider).to.be('devops');
expect(response.body.registries[0].serverAddress).to.be('registry.devops');
expect(response.body.registries[0].password).to.be(constants.SECRET_PLACEHOLDER);
});
it('can update registry', async function () {
const response = await superagent.post(`${serverUrl}/api/v1/docker/registries/${id}`)
.query({ access_token: owner.token })
.send({ provider: 'devops2', serverAddress: 'registry.devops2', password: 'hushhush2' });
expect(response.status).to.equal(204);
});
it('can get registry', async function () {
const response = await superagent.get(`${serverUrl}/api/v1/docker/registries/${id}`)
.query({ access_token: owner.token });
expect(response.status).to.equal(200);
expect(response.body.provider).to.be('devops2');
expect(response.body.serverAddress).to.be('registry.devops2');
expect(response.body.password).to.be(constants.SECRET_PLACEHOLDER);
});
it('can del registry', async function () {
const response = await superagent.del(`${serverUrl}/api/v1/docker/registries/${id}`)
.query({ access_token: owner.token });
expect(response.status).to.equal(204);
});
});