Skip to content

Commit adccdd4

Browse files
authored
refactor(ec2): avoid unnecessary state in sshKeyPair aws#5648
`isDeleted` status is determined by looking at the actual file system, rather than maintaining additional state
1 parent 7d6a975 commit adccdd4

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

packages/core/src/awsService/ec2/sshKeyPair.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ type sshKeyType = 'rsa' | 'ed25519'
1414
export class SshKeyPair {
1515
private publicKeyPath: string
1616
private lifeTimeout: Timeout
17-
private deleted: boolean = false
1817

1918
private constructor(
2019
private readonly keyPath: string,
@@ -79,12 +78,12 @@ export class SshKeyPair {
7978
if (!this.lifeTimeout.completed) {
8079
this.lifeTimeout.cancel()
8180
}
82-
83-
this.deleted = true
8481
}
8582

86-
public isDeleted(): boolean {
87-
return this.deleted
83+
public async isDeleted(): Promise<boolean> {
84+
const privateKeyDeleted = !(await fs.existsFile(this.getPrivateKeyPath()))
85+
const publicKeyDeleted = !(await fs.existsFile(this.getPublicKeyPath()))
86+
return privateKeyDeleted || publicKeyDeleted
8887
}
8988

9089
public timeAlive(): number {

packages/core/src/test/awsService/ec2/sshKeyPair.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,32 @@ describe('SshKeyUtility', async function () {
126126
sinon.assert.calledOnce(deleteStub)
127127
sinon.restore()
128128
})
129+
130+
it('determines deleted status based on file system', async function () {
131+
await fs.delete(keyPair.getPrivateKeyPath())
132+
await fs.delete(keyPair.getPublicKeyPath())
133+
134+
assert(keyPair.isDeleted())
135+
})
136+
137+
describe('isDeleted', async function () {
138+
it('returns false if key files exist', async function () {
139+
assert.strictEqual(await keyPair.isDeleted(), false)
140+
})
141+
142+
it('returns true if key files do not exist', async function () {
143+
await keyPair.delete()
144+
assert.strictEqual(await keyPair.isDeleted(), true)
145+
})
146+
147+
it('returns true if private key remains', async function () {
148+
await fs.delete(keyPair.getPublicKeyPath())
149+
assert.strictEqual(await keyPair.isDeleted(), true)
150+
})
151+
152+
it('returns true if public key remains', async function () {
153+
await fs.delete(keyPair.getPrivateKeyPath())
154+
assert.strictEqual(await keyPair.isDeleted(), true)
155+
})
156+
})
129157
})

0 commit comments

Comments
 (0)