Add method to get hash for env

This commit is contained in:
IshaVenikar 2025-01-29 17:47:38 +05:30
parent a1993f88d0
commit c30753ce11
6 changed files with 45 additions and 53 deletions

View File

@ -23,7 +23,6 @@
"express-session": "^1.18.0", "express-session": "^1.18.0",
"fs-extra": "^11.2.0", "fs-extra": "^11.2.0",
"graphql": "^16.8.1", "graphql": "^16.8.1",
"js-yaml": "^4.1.0",
"luxon": "^3.4.4", "luxon": "^3.4.4",
"nanoid": "3", "nanoid": "3",
"nanoid-dictionary": "^5.0.0-beta.1", "nanoid-dictionary": "^5.0.0-beta.1",
@ -55,7 +54,6 @@
"@types/cookie-session": "^2.0.49", "@types/cookie-session": "^2.0.49",
"@types/express-session": "^1.17.10", "@types/express-session": "^1.17.10",
"@types/fs-extra": "^11.0.4", "@types/fs-extra": "^11.0.4",
"@types/js-yaml": "^4.0.9",
"better-sqlite3": "^9.2.2", "better-sqlite3": "^9.2.2",
"copyfiles": "^2.4.1", "copyfiles": "^2.4.1",
"prettier": "^3.1.1", "prettier": "^3.1.1",

View File

@ -4,26 +4,26 @@ import { Project } from './Project';
@Entity() @Entity()
export class Deployer { export class Deployer {
@PrimaryColumn('varchar') @PrimaryColumn('varchar')
deployerLrn!: string; deployerLrn!: string;
@Column('varchar') @Column('varchar')
deployerId!: string; deployerId!: string;
@Column('varchar') @Column('varchar')
deployerApiUrl!: string; deployerApiUrl!: string;
@Column('varchar') @Column('varchar')
baseDomain!: string; baseDomain!: string;
@Column('varchar', { nullable: true}) @Column('varchar', { nullable: true})
publicKey!: string | null; publicKey!: string | null;
@Column('varchar', { nullable: true }) @Column('varchar', { nullable: true })
minimumPayment!: string | null; minimumPayment!: string | null;
@Column('varchar', { nullable: true }) @Column('varchar', { nullable: true })
paymentAddress!: string | null; paymentAddress!: string | null;
@ManyToMany(() => Project, (project) => project.deployers) @ManyToMany(() => Project, (project) => project.deployers)
projects!: Project[]; projects!: Project[];
} }

View File

@ -36,9 +36,7 @@ export interface ApplicationDeploymentRequest {
application: string; application: string;
lrn?: string; lrn?: string;
auction?: string; auction?: string;
config: { config: string;
ref: string;
};
meta: string; meta: string;
payment?: string; payment?: string;
} }

View File

@ -5,7 +5,6 @@ import { Octokit } from 'octokit';
import { inc as semverInc } from 'semver'; import { inc as semverInc } from 'semver';
import { DeepPartial } from 'typeorm'; import { DeepPartial } from 'typeorm';
import * as openpgp from 'openpgp'; import * as openpgp from 'openpgp';
import yaml from 'js-yaml';
import { Account, DEFAULT_GAS_ESTIMATION_MULTIPLIER, Registry as LaconicRegistry, getGasPrice, parseGasAndFees } from '@cerc-io/registry-sdk'; import { Account, DEFAULT_GAS_ESTIMATION_MULTIPLIER, Registry as LaconicRegistry, getGasPrice, parseGasAndFees } from '@cerc-io/registry-sdk';
import { DeliverTxResponse, IndexedTx } from '@cosmjs/stargate'; import { DeliverTxResponse, IndexedTx } from '@cosmjs/stargate';
@ -251,7 +250,7 @@ export class Registry {
apiUrl: string, apiUrl: string,
environmentVariables: { [key: string]: string }, environmentVariables: { [key: string]: string },
dns: string, dns: string,
address: string, requesterAddress: string,
publicKey: string, publicKey: string,
payment?: string | null payment?: string | null
}): Promise<{ }): Promise<{
@ -268,40 +267,14 @@ export class Registry {
// Config to be encrypted // Config to be encrypted
const config = { const config = {
"authorized": [data.address], "authorized": [data.requesterAddress],
"config": { "env": data.environmentVariables }, "config": { "env": data.environmentVariables },
} }
const binaryKey = Buffer.from(data.publicKey, 'base64');
// Convert binary to ASCII-armored format
const publicKeyArmored = `-----BEGIN PGP PUBLIC KEY BLOCK-----\n\n` +
binaryKey.toString('base64').match(/.{1,64}/g)?.join('\n') +
`\n\n-----END PGP PUBLIC KEY BLOCK-----`;
// Read the public key
const publicKey = await openpgp.readKey({ armoredKey: publicKeyArmored });
// Serialize the config (convert to YAML) // Serialize the config (convert to YAML)
const serialized = JSON.stringify(config, null, 2); // Use YAML if needed const serialized = JSON.stringify(config, null, 2);
// Encrypt the config const envHash = await this.generateHash(serialized, data.publicKey, data.apiUrl);
const encrypted = await openpgp.encrypt({
message: await openpgp.createMessage({ text: serialized }),
encryptionKeys: publicKey,
format: 'binary' // Equivalent to armor=False
});
// To get the hash after uploading encrypted env
const response = await fetch(`${data.apiUrl}/upload/config`, {
method: 'POST',
headers: {
'Content-Type': 'application/octet-stream'
},
body: encrypted
});
const envHash = await response.json();
// Create record of type ApplicationDeploymentRequest and publish // Create record of type ApplicationDeploymentRequest and publish
const applicationDeploymentRequest = { const applicationDeploymentRequest = {
@ -312,7 +285,9 @@ export class Registry {
dns: data.dns, dns: data.dns,
// https://git.vdb.to/cerc-io/laconic-registry-cli/commit/129019105dfb93bebcea02fde0ed64d0f8e5983b // https://git.vdb.to/cerc-io/laconic-registry-cli/commit/129019105dfb93bebcea02fde0ed64d0f8e5983b
config: { ref: envHash }, config: JSON.stringify({
ref: envHash
}),
meta: JSON.stringify({ meta: JSON.stringify({
note: `Added by Snowball @ ${DateTime.utc().toFormat( note: `Added by Snowball @ ${DateTime.utc().toFormat(
"EEE LLL dd HH:mm:ss 'UTC' yyyy" "EEE LLL dd HH:mm:ss 'UTC' yyyy"
@ -572,4 +547,30 @@ export class Registry {
assert(this.registryConfig.authority, "Authority doesn't exist"); assert(this.registryConfig.authority, "Authority doesn't exist");
return `lrn://${this.registryConfig.authority}/applications/${appName}`; return `lrn://${this.registryConfig.authority}/applications/${appName}`;
} }
async generateHash(message: string, pubKey: string, url: string): Promise<string> {
const armoredKey = `-----BEGIN PGP PUBLIC KEY BLOCK-----\n\n${pubKey}\n\n-----END PGP PUBLIC KEY BLOCK-----`;
const publicKey = await openpgp.readKey({ armoredKey });
// Encrypt the config
const encrypted = await openpgp.encrypt({
message: await openpgp.createMessage({ text: message }),
encryptionKeys: publicKey,
format: 'binary'
});
// Get the hash after uploading encrypted env
const response = await fetch(`${url}/upload/config`, {
method: 'POST',
headers: {
'Content-Type': 'application/octet-stream'
},
body: encrypted
});
const envHash = await response.json();
return envHash;
};
} }

View File

@ -658,7 +658,7 @@ export class Service {
apiUrl: deployer!.deployerApiUrl!, apiUrl: deployer!.deployerApiUrl!,
payment: data.project.txHash, payment: data.project.txHash,
auctionId: data.project.auctionId, auctionId: data.project.auctionId,
address, requesterAddress: address,
publicKey: deployer!.publicKey! publicKey: deployer!.publicKey!
}); });
} }
@ -674,7 +674,7 @@ export class Service {
dns: `${newDeployment.project.name}-${newDeployment.id}`, dns: `${newDeployment.project.name}-${newDeployment.id}`,
payment: data.project.txHash, payment: data.project.txHash,
auctionId: data.project.auctionId, auctionId: data.project.auctionId,
address, requesterAddress: address,
publicKey: deployer!.publicKey! publicKey: deployer!.publicKey!
}); });
@ -743,7 +743,7 @@ export class Service {
auctionId: project.auctionId!, auctionId: project.auctionId!,
lrn: deployerLrn, lrn: deployerLrn,
apiUrl: deployer!.deployerApiUrl!, apiUrl: deployer!.deployerApiUrl!,
address, requesterAddress: address,
publicKey: deployer!.publicKey! publicKey: deployer!.publicKey!
}); });
} }
@ -759,7 +759,7 @@ export class Service {
apiUrl: deployer!.deployerApiUrl!, apiUrl: deployer!.deployerApiUrl!,
environmentVariables: environmentVariablesObj, environmentVariables: environmentVariablesObj,
dns: `${newDeployment.project.name}-${newDeployment.id}`, dns: `${newDeployment.project.name}-${newDeployment.id}`,
address, requesterAddress: address,
publicKey: deployer!.publicKey! publicKey: deployer!.publicKey!
}); });

View File

@ -6793,11 +6793,6 @@
jest-matcher-utils "^27.0.0" jest-matcher-utils "^27.0.0"
pretty-format "^27.0.0" pretty-format "^27.0.0"
"@types/js-yaml@^4.0.9":
version "4.0.9"
resolved "https://registry.yarnpkg.com/@types/js-yaml/-/js-yaml-4.0.9.tgz#cd82382c4f902fed9691a2ed79ec68c5898af4c2"
integrity sha512-k4MGaQl5TGo/iipqb2UDG2UwjXziSWkh0uysQelTlJpX1qGlpUZYm8PnO4DxG1qBomtJUdYJ6qR6xdIah10JLg==
"@types/json-schema@^7.0.9": "@types/json-schema@^7.0.9":
version "7.0.15" version "7.0.15"
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.15.tgz#596a1747233694d50f6ad8a7869fcb6f56cf5841" resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.15.tgz#596a1747233694d50f6ad8a7869fcb6f56cf5841"