Skip to content

Commit ab92e79

Browse files
authored
Ensure ssh keys default value is arr (#4436)
1 parent 5b31733 commit ab92e79

2 files changed

Lines changed: 15 additions & 19 deletions

File tree

packages/playground/src/utils/grid.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export async function loadBalance(grid: GridClient): Promise<Balance> {
9090
export async function loadProfile(grid: GridClient): Promise<Profile> {
9191
return {
9292
mnemonic: grid.clientOptions!.mnemonic,
93-
ssh: await readSSH(grid),
93+
ssh: (await readSSH(grid)) ?? [],
9494
twinId: grid!.twinId,
9595
address: grid.tfclient.address,
9696
relay: grid.getDefaultUrls(network).relay.slice(6),

packages/playground/src/utils/ssh.ts

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,15 @@ class SSHKeysManagement {
3737
];
3838

3939
/**
40-
* Can be a string (legacy), SSHKeyData[] (current), or undefined.
40+
* Can be a string (legacy) or SSHKeyData[] (current).
4141
* The string type is only for migration and should be removed after migration is complete.
42+
* For new accounts without SSH keys, this is initialized to an empty array [].
4243
*/
43-
private oldKeys: string | SSHKeyData[] | undefined;
44+
private oldKeys: string | SSHKeyData[];
4445

4546
constructor() {
4647
const profileManager = useProfileManager();
47-
this.oldKeys = profileManager.profile?.ssh;
48+
this.oldKeys = profileManager.profile?.ssh ?? [];
4849
}
4950

5051
/**
@@ -55,6 +56,7 @@ class SSHKeysManagement {
5556
if (this.migrated()) {
5657
return this.oldKeys as SSHKeyData[];
5758
}
59+
if (typeof this.oldKeys !== "string") return [];
5860
const userKeys: SSHKeyData[] = [];
5961

6062
let keyName = "";
@@ -103,7 +105,7 @@ class SSHKeysManagement {
103105

104106
await storeSSH(grid!, copiedKeys);
105107
profileManager.updateSSH(copiedKeys);
106-
this.oldKeys = profileManager.profile?.ssh;
108+
this.oldKeys = profileManager.profile?.ssh ?? [];
107109
}
108110

109111
/**
@@ -196,23 +198,15 @@ class SSHKeysManagement {
196198
* @returns An array of formatted SSHKeyData.
197199
*/
198200
list(): SSHKeyData[] {
199-
let keys: SSHKeyData[] = [];
201+
if (!this.migrated()) return [];
200202

201-
if (this.migrated()) {
202-
keys = this.oldKeys as unknown as SSHKeyData[];
203-
}
203+
const keys = this.oldKeys as SSHKeyData[];
204+
if (keys.length === 0) return [];
204205

205-
// Profile created for the first time.
206-
if (!keys) {
207-
return [];
208-
}
209-
210-
keys = keys.map(key => ({
206+
return keys.map(key => ({
211207
...key,
212208
fingerPrint: this.calculateFingerprint(key.publicKey),
213209
}));
214-
215-
return keys;
216210
}
217211

218212
/**
@@ -253,8 +247,9 @@ class SSHKeysManagement {
253247
}
254248
needsDefaultNameAssignment(keys?: SSHKeyData[]): boolean {
255249
if (!keys) {
256-
keys = this.oldKeys as SSHKeyData[];
250+
keys = this.migrated() ? (this.oldKeys as SSHKeyData[]) : [];
257251
}
252+
if (keys.length === 0) return false;
258253
return keys.some(key => isAlphanumericWithSpace("Invalid name")(key.name) !== true);
259254
}
260255

@@ -266,8 +261,9 @@ class SSHKeysManagement {
266261
*/
267262
assignDefaultNames(keys?: SSHKeyData[]): SSHKeyData[] {
268263
if (!keys) {
269-
keys = this.oldKeys as SSHKeyData[];
264+
keys = this.migrated() ? (this.oldKeys as SSHKeyData[]) : [];
270265
}
266+
if (keys.length === 0) return [];
271267
const existingValidNames = new Set(
272268
keys.map(k => k.name).filter(name => name && isAlphanumericWithSpace("Invalid name")(name) === true),
273269
);

0 commit comments

Comments
 (0)