import * as plugins from '../plugins.js'; import { Cloudly } from '../classes.cloudly.js'; import type { Cluster } from '../manager.cluster/classes.cluster.js'; /** * in charge of talking to coreflow services on clusters * coreflow runs on a server when ServerManager is done. */ export class CloudlyCoreflowManager { public cloudlyRef: Cloudly; public typedRouter = new plugins.typedrequest.TypedRouter(); constructor(cloudlyRefArg: Cloudly) { this.cloudlyRef = cloudlyRefArg; this.cloudlyRef.typedrouter.addTypedRouter(this.typedRouter); this.typedRouter.addTypedHandler( new plugins.typedrequest.TypedHandler('getIdentityByToken', async (requestData) => { const user = await this.cloudlyRef.authManager.CUser.getInstance({ data: { tokens: [{ token: requestData.token, }] // find the proper user here. } as any }); if (!user) { throw new plugins.typedrequest.TypedResponseError('The supplied token is not valid. No matching user found.'); } if (user.data.type !== 'machine') { throw new plugins.typedrequest.TypedResponseError('The supplied token is not valid. The user is not a machine.'); } let cluster: Cluster; if (user.data.role === 'cluster') { cluster = await this.cloudlyRef.clusterManager.getClusterBy_UserId(user.id); } const expiryTimestamp = Date.now() + 3600 * 1000 * 24 * 365; return { identity: { name: user.data.username, role: user.data.role, type: 'machine', // if someone authenticates by token, they are a machine, no matter what. userId: user.id, expiresAt: expiryTimestamp, ...(cluster ? { clusterId: cluster.id, clusterName: cluster.data.name, } : {}), jwt: await this.cloudlyRef.authManager.smartjwtInstance.createJWT({ status: 'loggedIn', userId: user.id, expiresAt: expiryTimestamp, }) }, }; }) ); // lets enable the getting of cluster configs this.typedRouter.addTypedHandler( new plugins.typedrequest.TypedHandler( 'getClusterConfig', async (dataArg) => { const identity = dataArg.identity; console.log('trying to get clusterConfigSet'); console.log(dataArg); const cluster = await this.cloudlyRef.clusterManager.getClusterBy_Identity( identity ); console.log('got cluster config and sending it back to coreflow'); return { configData: await cluster.createSavableObject(), deploymentDirectives: [], }; } ) ); // lets enable getting of certificates this.typedRouter.addTypedHandler( new plugins.typedrequest.TypedHandler( 'getCertificateForDomain', async (dataArg) => { console.log(`incoming API request for certificate ${dataArg.domainName}`); const cert = await this.cloudlyRef.letsencryptConnector.getCertificateForDomain( dataArg.domainName ); console.log(`got certificate ready for reponse ${dataArg.domainName}`); return { certificate: await cert.createSavableObject(), }; } ) ); } }