Refactor out code for config creation
All checks were successful
Lint / lint (20.x) (pull_request) Successful in 4m35s

This commit is contained in:
IshaVenikar 2025-01-29 18:03:06 +05:30
parent c30753ce11
commit 2242e914c1
2 changed files with 12 additions and 13 deletions

View File

@ -265,16 +265,7 @@ export class Registry {
throw new Error(`No record found for ${lrn}`); throw new Error(`No record found for ${lrn}`);
} }
// Config to be encrypted const envHash = await this.generateConfigHash(data.environmentVariables, data.requesterAddress, data.publicKey, data.apiUrl);
const config = {
"authorized": [data.requesterAddress],
"config": { "env": data.environmentVariables },
}
// Serialize the config (convert to YAML)
const serialized = JSON.stringify(config, null, 2);
const envHash = await this.generateHash(serialized, data.publicKey, data.apiUrl);
// Create record of type ApplicationDeploymentRequest and publish // Create record of type ApplicationDeploymentRequest and publish
const applicationDeploymentRequest = { const applicationDeploymentRequest = {
@ -548,14 +539,22 @@ export class Registry {
return `lrn://${this.registryConfig.authority}/applications/${appName}`; return `lrn://${this.registryConfig.authority}/applications/${appName}`;
} }
async generateHash(message: string, pubKey: string, url: string): Promise<string> { async generateConfigHash(environmentVariables: { [key: string]: string }, requesterAddress: string, pubKey: string, url: string): Promise<string> {
// Config to be encrypted
const config = {
"authorized": [requesterAddress],
"config": { "env": environmentVariables },
}
// Serialize the config (convert to YAML)
const serialized = JSON.stringify(config, null, 2);
const armoredKey = `-----BEGIN PGP PUBLIC KEY BLOCK-----\n\n${pubKey}\n\n-----END PGP PUBLIC KEY BLOCK-----`; const armoredKey = `-----BEGIN PGP PUBLIC KEY BLOCK-----\n\n${pubKey}\n\n-----END PGP PUBLIC KEY BLOCK-----`;
const publicKey = await openpgp.readKey({ armoredKey }); const publicKey = await openpgp.readKey({ armoredKey });
// Encrypt the config // Encrypt the config
const encrypted = await openpgp.encrypt({ const encrypted = await openpgp.encrypt({
message: await openpgp.createMessage({ text: message }), message: await openpgp.createMessage({ text: serialized }),
encryptionKeys: publicKey, encryptionKeys: publicKey,
format: 'binary' format: 'binary'
}); });