2024-05-28 18:45:34 +02:00
import * as plugins from '../plugins.js' ;
import * as paths from '../paths.js' ;
2024-04-20 12:21:41 +02:00
import { SecretBundle } from './classes.secretbundle.js' ;
import { SecretGroup } from './classes.secretgroup.js' ;
2024-05-30 22:49:39 +02:00
import { logger } from '../logger.js' ;
2024-05-28 18:45:34 +02:00
import type { Cloudly } from '../classes.cloudly.js' ;
2024-04-20 12:21:41 +02:00
/**
* The `ConfigVault` class provides methods for reading and writing configuration data to a file.
* It uses the `TypedServer` and `TypedRouter` classes from the `configvault.plugins.js` module to handle HTTP requests and route them to the appropriate handlers.
*
* @class
*/
export class CloudlySecretManager {
// attached classes
public CSecretBundle = plugins . smartdata . setDefaultManagerForDoc ( this , SecretBundle ) ;
public CSecretGroup = plugins . smartdata . setDefaultManagerForDoc ( this , SecretGroup ) ;
// INSTANCE
public cloudlyRef : Cloudly ;
public projectinfo = new plugins . projectinfo . ProjectinfoNpm ( paths . packageDir ) ;
public serviceQenv = new plugins . qenv . Qenv ( paths . packageDir , paths . nogitDir ) ;
public typedrouter : plugins.typedrequest.TypedRouter ;
get db() {
return this . cloudlyRef . mongodbConnector . smartdataDb ;
}
constructor ( cloudlyRefArg : Cloudly ) {
this . cloudlyRef = cloudlyRefArg ;
}
public async start() {
// lets set up a typedrouter
this . typedrouter = new plugins . typedrequest . TypedRouter ( ) ;
this . cloudlyRef . typedrouter . addTypedRouter ( this . typedrouter ) ;
2024-12-21 20:21:54 +01:00
// secretbundle routes
this . typedrouter . addTypedHandler < plugins.servezoneInterfaces.requests.secretbundle.IReq_GetSecretBundles > (
new plugins . typedrequest . TypedHandler (
'getSecretBundles' ,
2024-06-01 05:48:57 +02:00
async ( dataArg , toolsArg ) = > {
2024-08-25 14:29:26 +02:00
await toolsArg . passGuards ( [ this . cloudlyRef . authManager . adminIdentityGuard ] , dataArg ) ;
2024-10-27 19:50:39 +01:00
dataArg . identity . jwt ;
2024-04-20 12:21:41 +02:00
const secretBundles = await SecretBundle . getInstances ( { } ) ;
return {
secretBundles : [
. . . ( await Promise . all (
2024-10-27 19:50:39 +01:00
secretBundles . map ( ( configBundle ) = > configBundle . createSavableObject ( ) ) ,
2024-04-20 12:21:41 +02:00
) ) ,
] ,
} ;
2024-10-27 19:50:39 +01:00
} ,
) ,
2024-04-20 12:21:41 +02:00
) ;
2024-12-21 20:21:54 +01:00
this . typedrouter . addTypedHandler < plugins.servezoneInterfaces.requests.secretbundle.IReq_CreateSecretBundle > (
new plugins . typedrequest . TypedHandler ( 'createSecretBundle' , async ( dataArg ) = > {
const secretBundle = new SecretBundle ( ) ;
secretBundle . id = plugins . smartunique . shortId ( 8 ) ;
secretBundle . data = dataArg . secretBundle . data ;
await secretBundle . save ( ) ;
return {
resultSecretBundle : await secretBundle . createSavableObject ( ) ,
} ;
} ) ,
) ;
this . typedrouter . addTypedHandler < plugins.servezoneInterfaces.requests.secretbundle.IReq_UpdateSecretBundle > (
new plugins . typedrequest . TypedHandler ( 'updateSecretBundle' , async ( dataArg ) = > {
const secretBundle = await SecretBundle . getInstance ( {
id : dataArg.secretBundle.id ,
} ) ;
secretBundle . data = dataArg . secretBundle . data ;
await secretBundle . save ( ) ;
return {
resultSecretBundle : await secretBundle . createSavableObject ( ) ,
} ;
} ) ,
) ;
this . typedrouter . addTypedHandler < plugins.servezoneInterfaces.requests.secretbundle.IReq_DeleteSecretBundleById > (
new plugins . typedrequest . TypedHandler ( 'deleteSecretBundleById' , async ( dataArg ) = > {
const secretBundle = await SecretBundle . getInstance ( {
id : dataArg.secretBundleId ,
} ) ;
await secretBundle . delete ( ) ;
return {
ok : true ,
} ;
} ) ,
) ;
// secretgroup routes
this . typedrouter . addTypedHandler < plugins.servezoneInterfaces.requests.secretgroup.IReq_GetSecretGroups > (
2024-04-20 12:21:41 +02:00
new plugins . typedrequest . TypedHandler (
2024-12-21 20:21:54 +01:00
'getSecretGroups' ,
async ( dataArg , toolsArg ) = > {
await toolsArg . passGuards ( [ this . cloudlyRef . authManager . adminIdentityGuard ] , dataArg ) ;
dataArg . identity . jwt ;
const secretGroups = await SecretGroup . getInstances ( { } ) ;
2024-04-20 12:21:41 +02:00
return {
2024-12-21 20:21:54 +01:00
secretGroups : [
. . . ( await Promise . all (
secretGroups . map ( ( secretGroup ) = > secretGroup . createSavableObject ( ) ) ,
) ) ,
] ,
2024-04-20 12:21:41 +02:00
} ;
2024-10-27 19:50:39 +01:00
} ,
) ,
2024-04-20 12:21:41 +02:00
) ;
2024-12-21 20:21:54 +01:00
this . typedrouter . addTypedHandler < plugins.servezoneInterfaces.requests.secretgroup.IReq_CreateSecretGroup > (
new plugins . typedrequest . TypedHandler ( 'createSecretGroup' , async ( dataArg ) = > {
const secretGroup = new SecretGroup ( ) ;
secretGroup . id = plugins . smartunique . shortId ( 8 ) ;
secretGroup . data = dataArg . secretGroup . data ;
await secretGroup . save ( ) ;
return {
resultSecretGroup : await secretGroup . createSavableObject ( ) ,
} ;
} ) ,
) ;
this . typedrouter . addTypedHandler < plugins.servezoneInterfaces.requests.secretgroup.IReq_UpdateSecretGroup > (
new plugins . typedrequest . TypedHandler ( 'updateSecretGroup' , async ( dataArg ) = > {
const secretGroup = await SecretGroup . getInstance ( {
id : dataArg.secretGroup.id ,
} ) ;
secretGroup . data = dataArg . secretGroup . data ;
await secretGroup . save ( ) ;
return {
resultSecretGroup : await secretGroup . createSavableObject ( ) ,
} ;
} ) ,
) ;
this . typedrouter . addTypedHandler < plugins.servezoneInterfaces.requests.secretgroup.IReq_DeleteSecretGroupById > (
new plugins . typedrequest . TypedHandler ( 'deleteSecretGroupById' , async ( dataArg ) = > {
const secretGroup = await SecretGroup . getInstance ( {
id : dataArg.secretGroupId ,
} ) ;
await secretGroup . delete ( ) ;
return {
ok : true ,
} ;
} ) ,
2024-04-20 12:21:41 +02:00
) ;
this . typedrouter . addTypedHandler (
2024-12-28 19:50:29 +01:00
new plugins . typedrequest . TypedHandler < plugins.servezoneInterfaces.requests.secretbundle.IReq_GetFlatKeyValueObject > (
'getFlatKeyValueObject' ,
2024-04-20 12:21:41 +02:00
async ( dataArg ) = > {
const wantedBundle = await SecretBundle . getInstance ( {
data : {
authorizations : {
// @ts-ignore
$elemMatch : {
2024-12-28 19:50:29 +01:00
secretAccessKey : dataArg.secretBundleAuthorization.secretAccessKey ,
2024-04-20 12:21:41 +02:00
} ,
} ,
} ,
} ) ;
const authorization = await wantedBundle . getAuthorizationFromAuthKey (
2024-12-28 19:50:29 +01:00
dataArg . secretBundleAuthorization . secretAccessKey ,
2024-04-20 12:21:41 +02:00
) ;
return {
2024-12-28 19:50:29 +01:00
flatKeyValueObject : await wantedBundle . getKeyValueObjectForEnvironment (
authorization . environment ,
) ,
2024-04-20 12:21:41 +02:00
} ;
2024-10-27 19:50:39 +01:00
} ,
) ,
2024-04-20 12:21:41 +02:00
) ;
}
public async stop() { }
}