|
|
@@ -4,6 +4,7 @@ import errs from "../../lib/error.js";
|
|
|
import jwtdecode from "../../lib/express/jwt-decode.js";
|
|
|
import apiValidator from "../../lib/validator/api.js";
|
|
|
import validator from "../../lib/validator/index.js";
|
|
|
+import { express as logger } from "../../logger.js";
|
|
|
import { getValidationSchema } from "../../schema/index.js";
|
|
|
|
|
|
const router = express.Router({
|
|
|
@@ -27,31 +28,31 @@ router
|
|
|
*
|
|
|
* Retrieve all certificates
|
|
|
*/
|
|
|
- .get((req, res, next) => {
|
|
|
- validator(
|
|
|
- {
|
|
|
- additionalProperties: false,
|
|
|
- properties: {
|
|
|
- expand: {
|
|
|
- $ref: "common#/properties/expand",
|
|
|
- },
|
|
|
- query: {
|
|
|
- $ref: "common#/properties/query",
|
|
|
+ .get(async (req, res, next) => {
|
|
|
+ try {
|
|
|
+ const data = await validator(
|
|
|
+ {
|
|
|
+ additionalProperties: false,
|
|
|
+ properties: {
|
|
|
+ expand: {
|
|
|
+ $ref: "common#/properties/expand",
|
|
|
+ },
|
|
|
+ query: {
|
|
|
+ $ref: "common#/properties/query",
|
|
|
+ },
|
|
|
},
|
|
|
},
|
|
|
- },
|
|
|
- {
|
|
|
- expand: typeof req.query.expand === "string" ? req.query.expand.split(",") : null,
|
|
|
- query: typeof req.query.query === "string" ? req.query.query : null,
|
|
|
- },
|
|
|
- )
|
|
|
- .then((data) => {
|
|
|
- return internalCertificate.getAll(res.locals.access, data.expand, data.query);
|
|
|
- })
|
|
|
- .then((rows) => {
|
|
|
- res.status(200).send(rows);
|
|
|
- })
|
|
|
- .catch(next);
|
|
|
+ {
|
|
|
+ expand: typeof req.query.expand === "string" ? req.query.expand.split(",") : null,
|
|
|
+ query: typeof req.query.query === "string" ? req.query.query : null,
|
|
|
+ },
|
|
|
+ );
|
|
|
+ const rows = await internalCertificate.getAll(res.locals.access, data.expand, data.query);
|
|
|
+ res.status(200).send(rows);
|
|
|
+ } catch (err) {
|
|
|
+ logger.debug(`${req.method.toUpperCase()} ${req.path}: ${err}`);
|
|
|
+ next(err);
|
|
|
+ }
|
|
|
})
|
|
|
|
|
|
/**
|
|
|
@@ -59,16 +60,16 @@ router
|
|
|
*
|
|
|
* Create a new certificate
|
|
|
*/
|
|
|
- .post((req, res, next) => {
|
|
|
- apiValidator(getValidationSchema("/nginx/certificates", "post"), req.body)
|
|
|
- .then((payload) => {
|
|
|
- req.setTimeout(900000); // 15 minutes timeout
|
|
|
- return internalCertificate.create(res.locals.access, payload);
|
|
|
- })
|
|
|
- .then((result) => {
|
|
|
- res.status(201).send(result);
|
|
|
- })
|
|
|
- .catch(next);
|
|
|
+ .post(async (req, res, next) => {
|
|
|
+ try {
|
|
|
+ const payload = await apiValidator(getValidationSchema("/nginx/certificates", "post"), req.body);
|
|
|
+ req.setTimeout(900000); // 15 minutes timeout
|
|
|
+ const result = await internalCertificate.create(res.locals.access, payload);
|
|
|
+ res.status(201).send(result);
|
|
|
+ } catch (err) {
|
|
|
+ logger.debug(`${req.method.toUpperCase()} ${req.path}: ${err}`);
|
|
|
+ next(err);
|
|
|
+ }
|
|
|
});
|
|
|
|
|
|
/**
|
|
|
@@ -88,18 +89,22 @@ router
|
|
|
*
|
|
|
* Test HTTP challenge for domains
|
|
|
*/
|
|
|
- .get((req, res, next) => {
|
|
|
+ .get(async (req, res, next) => {
|
|
|
if (req.query.domains === undefined) {
|
|
|
next(new errs.ValidationError("Domains are required as query parameters"));
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- internalCertificate
|
|
|
- .testHttpsChallenge(res.locals.access, JSON.parse(req.query.domains))
|
|
|
- .then((result) => {
|
|
|
- res.status(200).send(result);
|
|
|
- })
|
|
|
- .catch(next);
|
|
|
+ try {
|
|
|
+ const result = await internalCertificate.testHttpsChallenge(
|
|
|
+ res.locals.access,
|
|
|
+ JSON.parse(req.query.domains),
|
|
|
+ );
|
|
|
+ res.status(200).send(result);
|
|
|
+ } catch (err) {
|
|
|
+ logger.debug(`${req.method.toUpperCase()} ${req.path}: ${err}`);
|
|
|
+ next(err);
|
|
|
+ }
|
|
|
});
|
|
|
|
|
|
/**
|
|
|
@@ -119,35 +124,35 @@ router
|
|
|
*
|
|
|
* Retrieve a specific certificate
|
|
|
*/
|
|
|
- .get((req, res, next) => {
|
|
|
- validator(
|
|
|
- {
|
|
|
- required: ["certificate_id"],
|
|
|
- additionalProperties: false,
|
|
|
- properties: {
|
|
|
- certificate_id: {
|
|
|
- $ref: "common#/properties/id",
|
|
|
- },
|
|
|
- expand: {
|
|
|
- $ref: "common#/properties/expand",
|
|
|
+ .get(async (req, res, next) => {
|
|
|
+ try {
|
|
|
+ const data = await validator(
|
|
|
+ {
|
|
|
+ required: ["certificate_id"],
|
|
|
+ additionalProperties: false,
|
|
|
+ properties: {
|
|
|
+ certificate_id: {
|
|
|
+ $ref: "common#/properties/id",
|
|
|
+ },
|
|
|
+ expand: {
|
|
|
+ $ref: "common#/properties/expand",
|
|
|
+ },
|
|
|
},
|
|
|
},
|
|
|
- },
|
|
|
- {
|
|
|
- certificate_id: req.params.certificate_id,
|
|
|
- expand: typeof req.query.expand === "string" ? req.query.expand.split(",") : null,
|
|
|
- },
|
|
|
- )
|
|
|
- .then((data) => {
|
|
|
- return internalCertificate.get(res.locals.access, {
|
|
|
- id: Number.parseInt(data.certificate_id, 10),
|
|
|
- expand: data.expand,
|
|
|
- });
|
|
|
- })
|
|
|
- .then((row) => {
|
|
|
- res.status(200).send(row);
|
|
|
- })
|
|
|
- .catch(next);
|
|
|
+ {
|
|
|
+ certificate_id: req.params.certificate_id,
|
|
|
+ expand: typeof req.query.expand === "string" ? req.query.expand.split(",") : null,
|
|
|
+ },
|
|
|
+ );
|
|
|
+ const row = await internalCertificate.get(res.locals.access, {
|
|
|
+ id: Number.parseInt(data.certificate_id, 10),
|
|
|
+ expand: data.expand,
|
|
|
+ });
|
|
|
+ res.status(200).send(row);
|
|
|
+ } catch (err) {
|
|
|
+ logger.debug(`${req.method.toUpperCase()} ${req.path}: ${err}`);
|
|
|
+ next(err);
|
|
|
+ }
|
|
|
})
|
|
|
|
|
|
/**
|
|
|
@@ -155,13 +160,16 @@ router
|
|
|
*
|
|
|
* Update and existing certificate
|
|
|
*/
|
|
|
- .delete((req, res, next) => {
|
|
|
- internalCertificate
|
|
|
- .delete(res.locals.access, { id: Number.parseInt(req.params.certificate_id, 10) })
|
|
|
- .then((result) => {
|
|
|
- res.status(200).send(result);
|
|
|
- })
|
|
|
- .catch(next);
|
|
|
+ .delete(async (req, res, next) => {
|
|
|
+ try {
|
|
|
+ const result = await internalCertificate.delete(res.locals.access, {
|
|
|
+ id: Number.parseInt(req.params.certificate_id, 10),
|
|
|
+ });
|
|
|
+ res.status(200).send(result);
|
|
|
+ } catch (err) {
|
|
|
+ logger.debug(`${req.method.toUpperCase()} ${req.path}: ${err}`);
|
|
|
+ next(err);
|
|
|
+ }
|
|
|
});
|
|
|
|
|
|
/**
|
|
|
@@ -181,19 +189,21 @@ router
|
|
|
*
|
|
|
* Upload certificates
|
|
|
*/
|
|
|
- .post((req, res, next) => {
|
|
|
+ .post(async (req, res, next) => {
|
|
|
if (!req.files) {
|
|
|
res.status(400).send({ error: "No files were uploaded" });
|
|
|
- } else {
|
|
|
- internalCertificate
|
|
|
- .upload(res.locals.access, {
|
|
|
- id: Number.parseInt(req.params.certificate_id, 10),
|
|
|
- files: req.files,
|
|
|
- })
|
|
|
- .then((result) => {
|
|
|
- res.status(200).send(result);
|
|
|
- })
|
|
|
- .catch(next);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ const result = await internalCertificate.upload(res.locals.access, {
|
|
|
+ id: Number.parseInt(req.params.certificate_id, 10),
|
|
|
+ files: req.files,
|
|
|
+ });
|
|
|
+ res.status(200).send(result);
|
|
|
+ } catch (err) {
|
|
|
+ logger.debug(`${req.method.toUpperCase()} ${req.path}: ${err}`);
|
|
|
+ next(err);
|
|
|
}
|
|
|
});
|
|
|
|
|
|
@@ -214,16 +224,17 @@ router
|
|
|
*
|
|
|
* Renew certificate
|
|
|
*/
|
|
|
- .post((req, res, next) => {
|
|
|
+ .post(async (req, res, next) => {
|
|
|
req.setTimeout(900000); // 15 minutes timeout
|
|
|
- internalCertificate
|
|
|
- .renew(res.locals.access, {
|
|
|
+ try {
|
|
|
+ const result = await internalCertificate.renew(res.locals.access, {
|
|
|
id: Number.parseInt(req.params.certificate_id, 10),
|
|
|
- })
|
|
|
- .then((result) => {
|
|
|
- res.status(200).send(result);
|
|
|
- })
|
|
|
- .catch(next);
|
|
|
+ });
|
|
|
+ res.status(200).send(result);
|
|
|
+ } catch (err) {
|
|
|
+ logger.debug(`${req.method.toUpperCase()} ${req.path}: ${err}`);
|
|
|
+ next(err);
|
|
|
+ }
|
|
|
});
|
|
|
|
|
|
/**
|
|
|
@@ -243,15 +254,16 @@ router
|
|
|
*
|
|
|
* Renew certificate
|
|
|
*/
|
|
|
- .get((req, res, next) => {
|
|
|
- internalCertificate
|
|
|
- .download(res.locals.access, {
|
|
|
+ .get(async (req, res, next) => {
|
|
|
+ try {
|
|
|
+ const result = await internalCertificate.download(res.locals.access, {
|
|
|
id: Number.parseInt(req.params.certificate_id, 10),
|
|
|
- })
|
|
|
- .then((result) => {
|
|
|
- res.status(200).download(result.fileName);
|
|
|
- })
|
|
|
- .catch(next);
|
|
|
+ });
|
|
|
+ res.status(200).download(result.fileName);
|
|
|
+ } catch (err) {
|
|
|
+ logger.debug(`${req.method.toUpperCase()} ${req.path}: ${err}`);
|
|
|
+ next(err);
|
|
|
+ }
|
|
|
});
|
|
|
|
|
|
/**
|
|
|
@@ -271,18 +283,20 @@ router
|
|
|
*
|
|
|
* Validate certificates
|
|
|
*/
|
|
|
- .post((req, res, next) => {
|
|
|
+ .post(async (req, res, next) => {
|
|
|
if (!req.files) {
|
|
|
res.status(400).send({ error: "No files were uploaded" });
|
|
|
- } else {
|
|
|
- internalCertificate
|
|
|
- .validate({
|
|
|
- files: req.files,
|
|
|
- })
|
|
|
- .then((result) => {
|
|
|
- res.status(200).send(result);
|
|
|
- })
|
|
|
- .catch(next);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ const result = await internalCertificate.validate({
|
|
|
+ files: req.files,
|
|
|
+ });
|
|
|
+ res.status(200).send(result);
|
|
|
+ } catch (err) {
|
|
|
+ logger.debug(`${req.method.toUpperCase()} ${req.path}: ${err}`);
|
|
|
+ next(err);
|
|
|
}
|
|
|
});
|
|
|
|