2024-05-28 16:45:34 +00:00
|
|
|
import * as plugins from '../plugins.js';
|
|
|
|
import { Cloudly } from '../classes.cloudly.js';
|
2024-04-20 10:21:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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<plugins.servezoneInterfaces.requests.identity.IRequest_Any_Cloudly_CoreflowManager_GetIdentityByJumpCode>(
|
|
|
|
new plugins.typedrequest.TypedHandler('getIdentityByJumpCode', async (requestData) => {
|
2024-08-25 12:29:26 +00:00
|
|
|
const cluster =
|
|
|
|
await this.cloudlyRef.clusterManager.getClusterBy_JumpCode(
|
2024-04-20 10:21:41 +00:00
|
|
|
requestData.jumpCode
|
|
|
|
);
|
|
|
|
|
2024-08-25 12:29:26 +00:00
|
|
|
if (!cluster) {
|
|
|
|
throw new plugins.typedrequest.TypedResponseError('The supplied jumpCode is not valid. No cluster found.');
|
2024-04-20 10:21:41 +00:00
|
|
|
}
|
|
|
|
|
2024-08-25 12:29:26 +00:00
|
|
|
const user = await this.cloudlyRef.authManager.CUser.getInstance({
|
|
|
|
id: cluster.data.userId,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
throw new plugins.typedrequest.TypedResponseError('The supplied jumpCode is not valid. No user found.');
|
|
|
|
}
|
|
|
|
|
|
|
|
const expiryTimestamp = Date.now() + 3600 * 1000 * 24 * 365;
|
2024-04-20 10:21:41 +00:00
|
|
|
return {
|
2024-08-25 12:29:26 +00:00
|
|
|
identity: {
|
|
|
|
name: cluster.data.name,
|
|
|
|
role: 'cluster',
|
|
|
|
type: 'machine',
|
|
|
|
userId: cluster.data.userId,
|
|
|
|
expiresAt: expiryTimestamp,
|
|
|
|
clusterId: cluster.id,
|
|
|
|
clusterName: cluster.data.name,
|
2024-06-01 03:48:57 +00:00
|
|
|
jwt: await this.cloudlyRef.authManager.smartjwtInstance.createJWT({
|
|
|
|
status: 'loggedIn',
|
2024-08-25 12:29:26 +00:00
|
|
|
userId: cluster.data.userId,
|
|
|
|
expiresAt: expiryTimestamp,
|
2024-06-01 03:48:57 +00:00
|
|
|
})
|
2024-04-20 10:21:41 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
// lets enable the getting of cluster configs
|
|
|
|
this.typedRouter.addTypedHandler(
|
|
|
|
new plugins.typedrequest.TypedHandler<plugins.servezoneInterfaces.requests.config.IRequest_Any_Cloudly_GetClusterConfig>(
|
|
|
|
'getClusterConfig',
|
|
|
|
async (dataArg) => {
|
2024-08-25 12:29:26 +00:00
|
|
|
const identity = dataArg.identity;
|
2024-04-20 10:21:41 +00:00
|
|
|
console.log('trying to get clusterConfigSet');
|
|
|
|
console.log(dataArg);
|
2024-08-25 12:29:26 +00:00
|
|
|
const cluster =
|
|
|
|
await this.cloudlyRef.clusterManager.getClusterBy_Identity(
|
|
|
|
identity
|
2024-04-20 10:21:41 +00:00
|
|
|
);
|
|
|
|
console.log('got cluster config and sending it back to coreflow');
|
|
|
|
return {
|
2024-08-25 12:29:26 +00:00
|
|
|
configData: await cluster.createSavableObject(),
|
2024-06-01 03:48:57 +00:00
|
|
|
deploymentDirectives: [],
|
2024-04-20 10:21:41 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
// lets enable getting of certificates
|
|
|
|
this.typedRouter.addTypedHandler(
|
2024-08-25 12:29:26 +00:00
|
|
|
new plugins.typedrequest.TypedHandler<plugins.servezoneInterfaces.requests.certificate.IRequest_Any_Cloudly_GetCertificateForDomain>(
|
|
|
|
'getCertificateForDomain',
|
2024-04-20 10:21:41 +00:00
|
|
|
async (dataArg) => {
|
2024-08-25 12:29:26 +00:00
|
|
|
console.log(`incoming API request for certificate ${dataArg.domainName}`);
|
2024-04-20 10:21:41 +00:00
|
|
|
const cert = await this.cloudlyRef.letsencryptConnector.getCertificateForDomain(
|
2024-08-25 12:29:26 +00:00
|
|
|
dataArg.domainName
|
2024-04-20 10:21:41 +00:00
|
|
|
);
|
2024-08-25 12:29:26 +00:00
|
|
|
console.log(`got certificate ready for reponse ${dataArg.domainName}`);
|
2024-04-20 10:21:41 +00:00
|
|
|
return {
|
|
|
|
certificate: await cert.createSavableObject(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|