This repository has been archived by the owner on Aug 13, 2024. It is now read-only.
forked from TJCSec/klodd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
152 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,153 +1,150 @@ | ||
import challengeResources from '../../k8s/resource.js' | ||
import { | ||
getInstance, | ||
createInstance, | ||
deleteInstance, | ||
getInstance, | ||
createInstance, | ||
deleteInstance, | ||
} from '../../k8s/instance.js' | ||
import { InstanceCreationError, InstanceExistsError } from '../../error.js' | ||
|
||
const routes = async (fastify, _options) => { | ||
fastify.addHook('preHandler', async (req, res) => { | ||
if (!challengeResources.has(req.params.challengeId)) { | ||
res.notFound('Challenge does not exist') | ||
} | ||
}) | ||
fastify.addHook('preHandler', fastify.authenticate) | ||
const routes = async(fastify, _options) => { | ||
fastify.addHook('preHandler', async(req, res) => { | ||
if (!challengeResources.has(req.params.challengeId)) { | ||
res.notFound('Challenge does not exist') | ||
} | ||
}) | ||
fastify.addHook('preHandler', fastify.authenticate) | ||
|
||
fastify.route({ | ||
method: 'GET', | ||
url: '/:challengeId', | ||
schema: { | ||
response: { | ||
200: { | ||
type: 'object', | ||
properties: { | ||
name: { type: 'string' }, | ||
status: { | ||
type: 'string', | ||
enum: ['Stopped', 'Stopping', 'Unknown', 'Running', 'Starting'], | ||
}, | ||
timeout: { type: 'integer' }, | ||
server: { | ||
type: 'object', | ||
properties: { | ||
kind: { type: 'string' }, | ||
host: { type: 'string' }, | ||
port: { type: 'integer' }, | ||
}, | ||
required: ['kind', 'host'], | ||
}, | ||
time: { | ||
type: 'object', | ||
properties: { | ||
start: { type: 'integer' }, | ||
stop: { type: 'integer' }, | ||
remaining: { type: 'integer' }, | ||
}, | ||
required: ['start', 'stop', 'remaining'], | ||
fastify.route({ | ||
method: 'GET', | ||
url: '/:challengeId/:instanceId', | ||
schema: { | ||
response: { | ||
200: { | ||
type: 'object', | ||
properties: { | ||
name: { type: 'string' }, | ||
status: { | ||
type: 'string', | ||
enum: ['Stopped', 'Stopping', 'Unknown', 'Running', 'Starting'], | ||
}, | ||
timeout: { type: 'integer' }, | ||
server: { | ||
type: 'object', | ||
properties: { | ||
kind: { type: 'string' }, | ||
host: { type: 'string' }, | ||
port: { type: 'integer' }, | ||
}, | ||
required: ['kind', 'host'], | ||
}, | ||
time: { | ||
type: 'object', | ||
properties: { | ||
start: { type: 'integer' }, | ||
stop: { type: 'integer' }, | ||
remaining: { type: 'integer' }, | ||
}, | ||
required: ['start', 'stop', 'remaining'], | ||
}, | ||
}, | ||
required: ['name', 'status', 'timeout'], | ||
}, | ||
}, | ||
}, | ||
required: ['name', 'status', 'timeout'], | ||
}, | ||
}, | ||
}, | ||
handler: async (req, _res) => { | ||
const { challengeId } = req.params | ||
const teamId = req.user.sub | ||
return getInstance(challengeId, teamId) | ||
}, | ||
}) | ||
|
||
fastify.route({ | ||
method: 'POST', | ||
url: '/:challengeId/create', | ||
schema: { | ||
body: { | ||
type: 'object', | ||
properties: { | ||
recaptcha: { type: 'string' }, | ||
handler: async(req, _res) => { | ||
const { challengeId, instanceId } = req.params | ||
return getInstance(challengeId, instanceId) | ||
}, | ||
required: ['recaptcha'], | ||
}, | ||
response: { | ||
200: { | ||
type: 'object', | ||
properties: { | ||
name: { type: 'string' }, | ||
status: { const: 'Starting' }, | ||
timeout: { type: 'integer' }, | ||
server: { | ||
type: 'object', | ||
properties: { | ||
kind: { type: 'string' }, | ||
host: { type: 'string' }, | ||
port: { type: 'integer' }, | ||
}, | ||
required: ['kind', 'host'], | ||
}) | ||
|
||
fastify.route({ | ||
method: 'POST', | ||
url: '/:challengeId/:instanceId', | ||
schema: { | ||
body: { | ||
type: 'object', | ||
properties: { | ||
recaptcha: { type: 'string' }, | ||
}, | ||
required: ['recaptcha'], | ||
}, | ||
response: { | ||
200: { | ||
type: 'object', | ||
properties: { | ||
name: { type: 'string' }, | ||
status: { const: 'Starting' }, | ||
timeout: { type: 'integer' }, | ||
server: { | ||
type: 'object', | ||
properties: { | ||
kind: { type: 'string' }, | ||
host: { type: 'string' }, | ||
port: { type: 'integer' }, | ||
}, | ||
required: ['kind', 'host'], | ||
}, | ||
}, | ||
required: ['name', 'status', 'timeout', 'server'], | ||
}, | ||
}, | ||
}, | ||
required: ['name', 'status', 'timeout', 'server'], | ||
}, | ||
}, | ||
}, | ||
preHandler: [fastify.recaptcha], | ||
handler: async (req, res) => { | ||
const { challengeId } = req.params | ||
const teamId = req.user.sub | ||
preHandler: [fastify.recaptcha], | ||
handler: async(req, res) => { | ||
const { challengeId, instanceId } = req.params | ||
|
||
try { | ||
const instance = await createInstance(challengeId, teamId, req.log) | ||
req.log.info('instance created') | ||
return instance | ||
} catch (err) { | ||
if (err instanceof InstanceExistsError) { | ||
req.log.debug(err) | ||
req.log.debug(err.cause) | ||
return res.conflict(err.message) | ||
} | ||
if (err instanceof InstanceCreationError) { | ||
// this is likely a misconfiguration, so log it | ||
req.log.error(err) | ||
req.log.error(err.cause) | ||
return res.conflict(err.message) | ||
} | ||
throw err | ||
} | ||
}, | ||
}) | ||
try { | ||
const instance = await createInstance(challengeId, instanceId, req.log) | ||
req.log.info('instance created') | ||
return instance | ||
} catch (err) { | ||
if (err instanceof InstanceExistsError) { | ||
req.log.debug(err) | ||
req.log.debug(err.cause) | ||
return res.conflict(err.message) | ||
} | ||
if (err instanceof InstanceCreationError) { | ||
// this is likely a misconfiguration, so log it | ||
req.log.error(err) | ||
req.log.error(err.cause) | ||
return res.conflict(err.message) | ||
} | ||
throw err | ||
} | ||
}, | ||
}) | ||
|
||
fastify.route({ | ||
method: 'POST', | ||
url: '/:challengeId/delete', | ||
schema: { | ||
body: { | ||
type: 'object', | ||
properties: { | ||
recaptcha: { type: 'string' }, | ||
fastify.route({ | ||
method: 'DELETE', | ||
url: '/:challengeId/:instanceId', | ||
schema: { | ||
body: { | ||
type: 'object', | ||
properties: { | ||
recaptcha: { type: 'string' }, | ||
}, | ||
required: ['recaptcha'], | ||
}, | ||
response: { | ||
200: { | ||
type: 'object', | ||
properties: { | ||
name: { type: 'string' }, | ||
status: { const: 'Stopping' }, | ||
timeout: { type: 'integer' }, | ||
}, | ||
required: ['name', 'status', 'timeout'], | ||
}, | ||
}, | ||
}, | ||
required: ['recaptcha'], | ||
}, | ||
response: { | ||
200: { | ||
type: 'object', | ||
properties: { | ||
name: { type: 'string' }, | ||
status: { const: 'Stopping' }, | ||
timeout: { type: 'integer' }, | ||
}, | ||
required: ['name', 'status', 'timeout'], | ||
preHandler: [fastify.recaptcha], | ||
handler: async(req, _res) => { | ||
const { challengeId, instanceId } = req.params | ||
const instance = await deleteInstance(challengeId, instanceId, req.log) | ||
req.log.info('instance deleted') | ||
return instance | ||
}, | ||
}, | ||
}, | ||
preHandler: [fastify.recaptcha], | ||
handler: async (req, _res) => { | ||
const { challengeId } = req.params | ||
const teamId = req.user.sub | ||
const instance = await deleteInstance(challengeId, teamId, req.log) | ||
req.log.info('instance deleted') | ||
return instance | ||
}, | ||
}) | ||
}) | ||
} | ||
|
||
export default routes | ||
export default routes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
import auth from './auth.js' | ||
import challenge from './challenge.js' | ||
|
||
const routes = async (fastify, _options) => { | ||
fastify.register(auth) | ||
fastify.register(challenge, { | ||
prefix: '/challenge', | ||
}) | ||
const routes = async(fastify, _options) => { | ||
fastify.register(auth) | ||
fastify.register(challenge, { | ||
prefix: '/v1', | ||
}) | ||
} | ||
|
||
export default routes | ||
export default routes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters