Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(multi-cluster): updated 'solo account init' to upload secrets to all clusters #1399

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions src/commands/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import {Duration} from '../core/time/duration.js';
import {type NamespaceName} from '../core/kube/resources/namespace/namespace_name.js';
import {type DeploymentName} from '../core/config/remote/types.js';
import {type SoloListrTask} from '../types/index.js';

export class AccountCommand extends BaseCommand {
private readonly accountManager: AccountManager;
Expand All @@ -33,7 +34,7 @@
super(opts);

if (!opts || !opts.accountManager)
throw new IllegalArgumentError('An instance of core/AccountManager is required', opts.accountManager as any);

Check warning on line 37 in src/commands/account.ts

View workflow job for this annotation

GitHub Actions / Code Style / Standard

Unexpected any. Specify a different type

this.accountManager = opts.accountManager;
this.accountInfo = null;
Expand Down Expand Up @@ -68,7 +69,7 @@
try {
const privateKey = PrivateKey.fromStringDer(newAccountInfo.privateKey);
newAccountInfo.privateKeyRaw = privateKey.toStringRaw();
} catch (e: Error | any) {

Check warning on line 72 in src/commands/account.ts

View workflow job for this annotation

GitHub Actions / Code Style / Standard

'e' is defined but never used

Check warning on line 72 in src/commands/account.ts

View workflow job for this annotation

GitHub Actions / Code Style / Standard

Unexpected any. Specify a different type
this.logger.error(`failed to retrieve EVM address for accountId ${newAccountInfo.accountId}`);
}
}
Expand Down Expand Up @@ -109,7 +110,7 @@
return this.accountManager.accountInfoQuery(ctx.config.accountId);
}

async updateAccountInfo(ctx: any) {

Check warning on line 113 in src/commands/account.ts

View workflow job for this annotation

GitHub Actions / Code Style / Standard

Unexpected any. Specify a different type
let amount = ctx.config.amount;
if (ctx.config.ed25519PrivateKey) {
if (
Expand Down Expand Up @@ -145,12 +146,13 @@
return await this.accountManager.transferAmount(constants.TREASURY_ACCOUNT_ID, toAccountId, amount);
}

async init(argv: any) {

Check warning on line 149 in src/commands/account.ts

View workflow job for this annotation

GitHub Actions / Code Style / Standard

Unexpected any. Specify a different type
const self = this;

interface Context {
config: {
namespace: NamespaceName;
contexts: string[];
};
updateSecrets: boolean;
accountsBatchedSet: number[][];
Expand All @@ -167,11 +169,13 @@
title: 'Initialize',
task: async (ctx, task) => {
self.configManager.update(argv);
const namespace = await resolveNamespaceFromDeployment(this.localConfig, this.configManager, task);
const config = {namespace};
const config = {
namespace: await resolveNamespaceFromDeployment(this.localConfig, this.configManager, task),
contexts: this.getContexts(),
};

if (!(await this.k8Factory.default().namespaces().has(namespace))) {
throw new SoloError(`namespace ${namespace.name} does not exist`);
if (!(await this.k8Factory.getK8(config.contexts[0]).namespaces().has(config.namespace))) {
throw new SoloError(`namespace ${config.namespace.name} does not exist`);

Check warning on line 178 in src/commands/account.ts

View check run for this annotation

Codecov / codecov/patch

src/commands/account.ts#L178

Added line #L178 was not covered by tests
}

// set config in the context for later tasks to use
Expand All @@ -194,12 +198,11 @@
{
title: 'Prepare for account key updates',
task: async ctx => {
const namespace = await resolveNamespaceFromDeployment(this.localConfig, this.configManager, task);
const secrets = await self.k8Factory
ctx.updateSecrets = await self.k8Factory
.default()
.secrets()
.list(namespace, ['solo.hedera.com/account-id']);
ctx.updateSecrets = secrets.length > 0;
.list(ctx.config.namespace, ['solo.hedera.com/account-id'])
.then(secrets => secrets.length > 0);

ctx.accountsBatchedSet = self.accountManager.batchAccounts(this.systemAccounts);

Expand All @@ -216,16 +219,18 @@
{
title: 'Update special account key sets',
task: ctx => {
const subTasks: any[] = [];
const subTasks: SoloListrTask<Context>[] = [];
const realm = constants.HEDERA_NODE_ACCOUNT_ID_START.realm;
const shard = constants.HEDERA_NODE_ACCOUNT_ID_START.shard;

for (const currentSet of ctx.accountsBatchedSet) {
const accStart = `${realm}.${shard}.${currentSet[0]}`;
const accEnd = `${realm}.${shard}.${currentSet[currentSet.length - 1]}`;
const rangeStr =
accStart !== accEnd
? `${chalk.yellow(accStart)} to ${chalk.yellow(accEnd)}`
: `${chalk.yellow(accStart)}`;

subTasks.push({
title: `Updating accounts [${rangeStr}]`,
task: async (ctx: Context) => {
Expand All @@ -234,6 +239,7 @@
currentSet,
ctx.updateSecrets,
ctx.resultTracker,
ctx.config.contexts,
);
},
});
Expand Down Expand Up @@ -264,6 +270,7 @@
);
}
self.logger.showUser(chalk.gray('Waiting for sockets to be closed....'));

if (ctx.resultTracker.rejectedCount > 0) {
throw new SoloError(
`Account keys updates failed for ${ctx.resultTracker.rejectedCount} accounts.`,
Expand All @@ -290,7 +297,7 @@

try {
await tasks.run();
} catch (e: Error | any) {
} catch (e) {
throw new SoloError(`Error in creating account: ${e.message}`, e);
} finally {
await this.closeConnections();
Expand All @@ -302,7 +309,7 @@
return true;
}

async create(argv: any) {

Check warning on line 312 in src/commands/account.ts

View workflow job for this annotation

GitHub Actions / Code Style / Standard

Unexpected any. Specify a different type
const self = this;
const lease = await self.leaseManager.create();

Expand Down Expand Up @@ -382,7 +389,7 @@

try {
await tasks.run();
} catch (e: Error | any) {

Check warning on line 392 in src/commands/account.ts

View workflow job for this annotation

GitHub Actions / Code Style / Standard

Unexpected any. Specify a different type
throw new SoloError(`Error in creating account: ${e.message}`, e);
} finally {
await lease.release();
Expand All @@ -392,7 +399,7 @@
return true;
}

async update(argv: any) {

Check warning on line 402 in src/commands/account.ts

View workflow job for this annotation

GitHub Actions / Code Style / Standard

Unexpected any. Specify a different type
const self = this;

interface Context {
Expand Down Expand Up @@ -473,7 +480,7 @@

try {
await tasks.run();
} catch (e: Error | any) {

Check warning on line 483 in src/commands/account.ts

View workflow job for this annotation

GitHub Actions / Code Style / Standard

Unexpected any. Specify a different type
throw new SoloError(`Error in updating account: ${e.message}`, e);
} finally {
await this.closeConnections();
Expand All @@ -482,7 +489,7 @@
return true;
}

async get(argv: any) {

Check warning on line 492 in src/commands/account.ts

View workflow job for this annotation

GitHub Actions / Code Style / Standard

Unexpected any. Specify a different type
const self = this;

interface Context {
Expand Down
63 changes: 33 additions & 30 deletions src/core/account_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,7 @@
* @param currentSet - the accounts to update
* @param updateSecrets - whether to delete the secret prior to creating a new secret
* @param resultTracker - an object to keep track of the results from the accounts that are being updated
* @param contexts
* @returns the updated resultTracker object
*/
async updateSpecialAccountsKeys(
Expand All @@ -604,6 +605,7 @@
rejectedCount: number;
fulfilledCount: number;
},
contexts: string[],
) {
const genesisKey = PrivateKey.fromStringED25519(constants.OPERATOR_KEY);
const realm = constants.HEDERA_NODE_ACCOUNT_ID_START.realm;
Expand All @@ -618,6 +620,7 @@
AccountId.fromString(`${realm}.${shard}.${accountNum}`),
genesisKey,
updateSecrets,
contexts,
),
);
}
Expand Down Expand Up @@ -657,19 +660,21 @@
* @param namespace - the namespace of the nodes network
* @param accountId - the account that will get its keys updated
* @param genesisKey - the genesis key to compare against
* @param updateSecrets - whether to delete the secret prior to creating a new secret
* @param updateSecrets - whether to delete the secret before creating a new secret
* @param [contexts]
* @returns the result of the call
*/
async updateAccountKeys(
namespace: NamespaceName,
accountId: AccountId,
genesisKey: PrivateKey,
updateSecrets: boolean,
contexts?: string[],
): Promise<{value: string; status: string} | {reason: string; value: string; status: string}> {
let keys: Key[];
try {
keys = await this.getAccountKeys(accountId);
} catch (e: Error | any) {
} catch (e) {
this.logger.error(`failed to get keys for accountId ${accountId.toString()}, e: ${e.toString()}\n ${e.stack}`);
return {
status: REJECTED,
Expand Down Expand Up @@ -704,37 +709,35 @@
};

try {
const createdOrUpdated = updateSecrets
? await this.k8Factory
.default()
for (const context of contexts) {
const secretName = Templates.renderAccountKeySecretName(accountId);
const secretLabels = Templates.renderAccountKeySecretLabelObject(accountId);
const secretType = SecretType.OPAQUE;

let createdOrUpdated: boolean;

if (updateSecrets) {
createdOrUpdated = await this.k8Factory
.getK8(context)

Check warning on line 721 in src/core/account_manager.ts

View check run for this annotation

Codecov / codecov/patch

src/core/account_manager.ts#L720-L721

Added lines #L720 - L721 were not covered by tests
.secrets()
.replace(
namespace,
Templates.renderAccountKeySecretName(accountId),
SecretType.OPAQUE,
data,
Templates.renderAccountKeySecretLabelObject(accountId),
)
: await this.k8Factory
.default()
.replace(namespace, secretName, secretType, data, secretLabels);

Check warning on line 723 in src/core/account_manager.ts

View check run for this annotation

Codecov / codecov/patch

src/core/account_manager.ts#L723

Added line #L723 was not covered by tests
} else {
createdOrUpdated = await this.k8Factory
.getK8(context)
.secrets()
.create(
namespace,
Templates.renderAccountKeySecretName(accountId),
SecretType.OPAQUE,
data,
Templates.renderAccountKeySecretLabelObject(accountId),
);

if (!createdOrUpdated) {
this.logger.error(`failed to create secret for accountId ${accountId.toString()}`);
return {
status: REJECTED,
reason: REASON_FAILED_TO_CREATE_K8S_S_KEY,
value: accountId.toString(),
};
.create(namespace, secretName, secretType, data, secretLabels);
}

if (!createdOrUpdated) {
this.logger.error(`failed to create secret for accountId ${accountId.toString()}`);
return {
status: REJECTED,
reason: REASON_FAILED_TO_CREATE_K8S_S_KEY,
value: accountId.toString(),
};
}

Check warning on line 738 in src/core/account_manager.ts

View check run for this annotation

Codecov / codecov/patch

src/core/account_manager.ts#L732-L738

Added lines #L732 - L738 were not covered by tests
}
} catch (e: Error | any) {
} catch (e) {
this.logger.error(`failed to create secret for accountId ${accountId.toString()}, e: ${e.toString()}`);
return {
status: REJECTED,
Expand Down
Loading