Skip to content

Commit

Permalink
add admin api
Browse files Browse the repository at this point in the history
  • Loading branch information
mfornos committed Oct 25, 2023
1 parent c565bc2 commit ac79a77
Show file tree
Hide file tree
Showing 9 changed files with 289 additions and 29 deletions.
123 changes: 123 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"typescript": "^5.2.2"
},
"dependencies": {
"@fastify/jwt": "^7.2.2",
"@fastify/swagger": "^8.11.0",
"@fastify/swagger-ui": "^1.10.0",
"@sodazone/ocelloids": "^1.1.8",
Expand Down
4 changes: 4 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { logger } from './environment.js';
import { ServerOptions } from './types.js';
import {
Root,
Auth,
Administration,
Persistence,
Configuration,
Monitoring,
Expand Down Expand Up @@ -71,10 +73,12 @@ export async function createServer(
});

await server.register(Root);
await server.register(Auth);
await server.register(Configuration, opts);
await server.register(Persistence, opts);
await server.register(Connector);
await server.register(Monitoring);
await server.register(Administration);

return server;
}
74 changes: 74 additions & 0 deletions src/services/admin/routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { FastifyInstance } from 'fastify';

export default async function Administration(
fastify: FastifyInstance
) {
const { log, storage: { root }, scheduler } = fastify;

fastify.delete(
'/admin/storage/root',
{
onRequest: [fastify.auth]
},
async (request, reply) => {
log.warn(
'Clearing root database %s %j',
request.ip,
request.headers
);
await root.clear();
reply.send();
}
);

fastify.get('/admin/scheduled',{
onRequest: [fastify.auth],
schema: {
hide: true,
response: {
200: {
type: 'array',
items: {
type: 'string'
}
}
}
}
}, async (_, reply) => {
reply.send(await scheduler.allTaskTimes());
});

fastify.get<{
Params: {
id: string
}
}>('/admin/scheduled/:id',{
onRequest: [fastify.auth],
schema: {
hide: true
}
}, async (request, reply) => {
reply.send(
await scheduler.getById(request.params.id)
);
});

fastify.delete<{
Params: {
id: string
}
}>('/admin/scheduled/:id',{
onRequest: [fastify.auth],
schema: {
hide: true,
response: {
200: {
type: 'null'
}
}
}
}, async (request, reply) => {
await scheduler.remove(request.params.id);
reply.send();
});
}
38 changes: 38 additions & 0 deletions src/services/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { FastifyPluginAsync, FastifyReply, FastifyRequest } from 'fastify';
import fp from 'fastify-plugin';
import jwt from '@fastify/jwt';

import { environment } from '../environment.js';

declare module 'fastify' {
interface FastifyInstance {
auth: (
request: FastifyRequest, reply: FastifyReply
) => Promise<void>
}
}

const authPlugin: FastifyPluginAsync
= async fastify => {
if (environment !== 'development' && !process.env.XCMON_SECRET) {
fastify.log.warn('!! Default XCMON_SECRET configured !!');
}

fastify.register(jwt, {
secret: process.env.XCMON_SECRET || 'IAOAbraxasSabaoth'
});
fastify.decorate('auth',
async function (
request: FastifyRequest, reply: FastifyReply
) : Promise<void> {
try {
await request.jwtVerify();
} catch (err) {
reply.status(401).send(err);
}
}
);
};

export default fp(authPlugin);

4 changes: 4 additions & 0 deletions src/services/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import Root from './root.js';
import Auth from './auth.js';
import Administration from './admin/routes.js';
import Configuration from './configuration.js';
import Monitoring from './monitoring/plugin.js';
import Persistence from './persistence/plugin.js';
Expand All @@ -8,6 +10,8 @@ export * from './types.js';

export {
Root,
Auth,
Administration,
Persistence,
Configuration,
Monitoring,
Expand Down
Loading

0 comments on commit ac79a77

Please sign in to comment.