oneuptime/GlManager/manager.ts

132 lines
3.8 KiB
TypeScript
Raw Normal View History

2022-02-27 12:24:02 +00:00
import axios from 'axios';
2022-04-14 13:40:04 +00:00
const BASE_URL: string = `${process.env.BACKEND_PROTOCOL}://${process.env['ONEUPTIME_HOST']}`;
2021-11-28 09:31:17 +00:00
2022-04-14 13:57:52 +00:00
const Manager: $TSFixMe = module.exports;
2022-03-19 17:01:37 +00:00
2022-03-25 20:24:05 +00:00
//eslint-disable-next-line @typescript-eslint/no-unused-vars
2022-04-12 20:33:28 +00:00
Manager.create = function (_opts: $TSFixMe): void {
2022-04-14 13:52:24 +00:00
const manager: $TSFixMe = {};
2021-11-28 09:31:17 +00:00
//
// REQUIRED (basic issuance)
//
2022-02-28 12:04:28 +00:00
2022-04-12 12:54:51 +00:00
manager.get = async function ({ servername }: $TSFixMe): void {
2022-04-14 13:40:04 +00:00
const url: string = `${BASE_URL}/api/manager/site?servername=${servername}`;
2022-04-14 13:57:52 +00:00
const response: $TSFixMe = await axios({
2021-11-28 09:31:17 +00:00
url,
method: 'get',
});
return response.data;
};
//
// REQUIRED (basic issuance)
//
2022-02-28 12:04:28 +00:00
2022-04-12 12:54:51 +00:00
manager.set = async function (opts: $TSFixMe): void {
2022-04-14 13:40:04 +00:00
const url: string = `${BASE_URL}/api/manager/site?subject=${opts.subject}`;
2022-04-14 13:57:52 +00:00
const response: $TSFixMe = await axios({
2021-11-28 09:31:17 +00:00
url,
method: 'put',
data: opts,
});
return response.data;
};
//
// Optional (Fully Automatic Renewal)
//
2022-02-28 12:04:28 +00:00
2022-04-12 12:54:51 +00:00
manager.find = async function (opts: $TSFixMe): void {
2021-11-28 09:31:17 +00:00
// { subject, servernames, altnames, renewBefore }
if (opts.subject) {
2022-04-14 13:40:04 +00:00
const url: string = `${BASE_URL}/api/manager/site?subject=${opts.subject}`;
2022-04-14 13:57:52 +00:00
const response: $TSFixMe = await axios({
2021-11-28 09:31:17 +00:00
url,
method: 'get',
});
if (!response.data || response.data.length === 0) {
return [];
}
return [response.data];
}
if (Array.isArray(opts.servernames) && opts.servernames.length > 0) {
2022-04-14 13:40:04 +00:00
const url: string = `${BASE_URL}/api/manager/site/servernames`;
2022-04-14 13:57:52 +00:00
const response: $TSFixMe = await axios({
2021-11-28 09:31:17 +00:00
url,
method: 'post',
data: opts.servernames,
});
return response.data;
}
// i.e. find certs more than 30 days old as default
opts.issuedBefore =
opts.issuedBefore || Date.now() - 30 * 24 * 60 * 60 * 1000;
// i.e. find certs that will expire in less than 45 days as default
opts.expiresBefore =
opts.expiresBefore || Date.now() + 45 * 24 * 60 * 60 * 1000;
// i.e. find certs that should be renewed within 21 days as default
opts.renewBefore =
opts.renewBefore || Date.now() + 21 * 24 * 60 * 60 * 1000;
2022-04-14 13:40:04 +00:00
const url: string = `${BASE_URL}/api/manager/site/opts`;
2022-04-14 13:57:52 +00:00
const response: $TSFixMe = await axios({
2021-11-28 09:31:17 +00:00
url,
method: 'post',
data: opts,
});
return response.data;
};
//
// Optional (Special Remove Functionality)
// The default behavior is to set `deletedAt`
//
2022-02-28 12:04:28 +00:00
2022-04-12 12:54:51 +00:00
manager.remove = async function (opts: $TSFixMe): void {
2022-04-14 13:40:04 +00:00
const url: string = `${BASE_URL}/api/manager/site?subject=${opts.subject}`;
2022-04-14 13:57:52 +00:00
const response: $TSFixMe = await axios({
2021-11-28 09:31:17 +00:00
url,
method: 'delete',
});
return response.data;
};
//
// Optional (special settings save)
// Implemented here because this module IS the fallback
// This is a setter/getter function
//
2022-02-28 12:04:28 +00:00
2022-04-12 12:54:51 +00:00
manager.defaults = async function (opts: $TSFixMe): void {
2021-11-28 09:31:17 +00:00
if (!opts) {
2022-04-14 13:40:04 +00:00
const url: string = `${BASE_URL}/api/manager/default`;
2022-04-14 13:57:52 +00:00
const response: $TSFixMe = await axios({
2021-11-28 09:31:17 +00:00
url,
method: 'get',
});
return response.data ? response.data : {};
}
2022-04-14 13:40:04 +00:00
const url: string = `${BASE_URL}/api/manager/default`;
2022-04-14 13:57:52 +00:00
const response: $TSFixMe = await axios({
2021-11-28 09:31:17 +00:00
url,
method: 'put',
data: opts,
});
return response.data || {};
};
return manager;
};