-
Notifications
You must be signed in to change notification settings - Fork 437
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
fix(toolkit): attach lifetime to generated ssh keys. #5578
Changes from 207 commits
b7399c5
588594d
1c57e45
814ba96
b5d2c88
08ba106
3caba57
f2894ba
f0f5086
5be1f8d
3c096cd
13b2bc0
8fbbc8d
57dbde6
fb9f1e8
f94fe64
fdec798
545b8a7
4f8c680
222fcfd
0e5f7a2
e87f24d
3764b4a
1c3be08
93e23f0
d742eef
056e807
153bf62
bd71166
e027e40
9d62154
f28809b
3cc9aa6
732778f
2cbb723
26faa78
ea30d86
52a2fde
8fac525
004ac0b
f85f0ee
29d6b62
506be9a
2022004
8560da2
9022ec2
fd1703d
9e0c16e
1f53a3f
eb92609
cec3ede
024eb7b
38559be
b217377
19ad794
7794b67
adae37b
d3a4276
0772dd2
b4c03c3
b76e22f
98f03fb
6cc4cc5
d147b22
d72f9c9
bd709da
bffa367
8062ed6
f7999b1
f2a7786
cf05c8e
f0900d7
10566c1
2b749a3
bf25145
de97b43
1914fe8
ce7beb9
a5cf7f3
177aa07
dd82ff9
76eb0fd
5ad533f
6efd4fd
4a68497
ba21411
5ec5496
6c03515
7e6c56e
19d3e2c
886a41e
a4a4626
8b36799
72ae902
b08bbdb
c4348b4
02b2a2b
e373be8
0f2c01d
718c402
a21d6f8
60566f5
e15c0a2
41eade4
33fbd79
8cb386d
84fa815
dca607e
f6ac8e3
e15e184
6950712
d951675
2a63ae3
1087678
84ea20a
eed3a09
4b94c83
65ad6ac
febac95
ee149b7
d70c415
aa56c93
90b248b
950ca3c
f17580d
ac4e711
7441de1
ef427f2
a0cea9d
740baf8
c60d540
2a08bb7
24a25d0
27179ab
a8a92ae
d6896c1
e201693
3bbabd1
8a733e6
8a7e1ae
9ac421f
819203f
65133a4
de04def
9a55520
b0c1f02
59e9f9c
53a678b
0a26c22
fc4af9e
2f393c8
6eb3450
f9c5d91
f72acc7
19eb5d1
ca86046
57ad523
dd05a0d
40abd17
ae3851c
9cbcb48
4d6cd41
d4a2244
ac6d473
6cab577
a034dc3
2854015
f21201f
c9d6eff
49efa8b
0cc83e3
996362c
1e6cdcf
4e97666
9d9fa8d
79b8f81
681771e
955308c
341bff3
e2ac9c8
1a47575
65b2ff2
234a26d
8a16fa7
b1f05f5
57e887c
656a58c
61eb6bb
cf70ff6
b77dc8d
548f69e
f152943
1af46cd
dda254c
28798b6
0e9d0e8
cdf23cb
c3bfbf2
987c9cf
e37a46f
eab2f7d
a8051de
3efa868
05b107b
2e7c79f
841165b
64da02e
085103c
90fa76a
ce7706a
efc63be
c02697c
72d6730
20a2212
bc89e7f
7b2bafc
cdf5d13
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,19 +5,32 @@ | |
import * as fs from 'fs-extra' | ||
import { ToolkitError } from '../../shared/errors' | ||
import { ChildProcess } from '../../shared/utilities/childProcess' | ||
import { Timeout } from '../../shared/utilities/timeoutUtils' | ||
|
||
export class SshKeyPair { | ||
private publicKeyPath: string | ||
private constructor(keyPath: string) { | ||
private lifeTimeout: Timeout | ||
private deleted: boolean = false | ||
|
||
private constructor( | ||
private keyPath: string, | ||
lifetime: number | ||
) { | ||
this.publicKeyPath = `${keyPath}.pub` | ||
this.lifeTimeout = new Timeout(lifetime) | ||
|
||
this.lifeTimeout.onCompletion(async () => { | ||
await this.delete() | ||
justinmk3 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}) | ||
} | ||
|
||
public static async getSshKeyPair(keyPath: string) { | ||
const keyExists = await fs.pathExists(keyPath) | ||
if (!keyExists) { | ||
await SshKeyPair.generateSshKeyPair(keyPath) | ||
public static async getSshKeyPair(keyPath: string, lifetime: number) { | ||
// Overwrite key if already exists | ||
if (await fs.pathExists(keyPath)) { | ||
await fs.remove(keyPath) | ||
} | ||
return new SshKeyPair(keyPath) | ||
await SshKeyPair.generateSshKeyPair(keyPath) | ||
return new SshKeyPair(keyPath, lifetime) | ||
} | ||
|
||
public static async generateSshKeyPair(keyPath: string): Promise<void> { | ||
|
@@ -26,14 +39,38 @@ export class SshKeyPair { | |
if (result.exitCode !== 0) { | ||
throw new ToolkitError('ec2: Failed to generate ssh key', { details: { stdout: result.stdout } }) | ||
} | ||
await fs.chmod(keyPath, 0o600) | ||
} | ||
|
||
public getPublicKeyPath(): string { | ||
return this.publicKeyPath | ||
} | ||
|
||
public getPrivateKeyPath(): string { | ||
return this.keyPath | ||
} | ||
|
||
public async getPublicKey(): Promise<string> { | ||
const contents = await fs.readFile(this.publicKeyPath, 'utf-8') | ||
return contents | ||
} | ||
|
||
public async delete(): Promise<void> { | ||
await fs.remove(this.publicKeyPath) | ||
await fs.remove(this.keyPath) | ||
|
||
if (!this.lifeTimeout.completed) { | ||
this.lifeTimeout.cancel() | ||
} | ||
|
||
this.deleted = true | ||
} | ||
|
||
public isDeleted(): boolean { | ||
return this.deleted | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can this extra state be avoided? usually better to check "reality", i.e. check the filesystem. after all, we're already storing the filepath. intermediate state can drift, and leads to "coherency" bugs. even very simple state. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So in that case, replace the |
||
} | ||
|
||
public timeAlive(): number { | ||
return this.lifeTimeout.elapsedTime | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it's not much trouble, it would be helpful to remove this and use our fs.ts module instead. We want to eliminate 'fs-extra'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I couldn't figure out how to set file permission (
fs.chmod
equivalent) throughfs.ts
. While ssh-keygen seems to do this for me locally, it fails in CI without this. Should I keep fs-extra for this until achmod
equivalent exists infs.ts
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would you mind adding a stub for chmod in fs.ts? it can just do nothing for
if (isWeb())
. and for non-web, it can use node's chmod (from 'fs', instead of 'fs-extra)this could be a followup PR. for now in this PR, if it's easy, try using 'fs' instead of 'fs-extra'.