@@ -259,22 +259,34 @@ export class Ec2Connecter implements vscode.Disposable {
259259 return keyPair
260260 }
261261
262+ private async attemptToCleanKeys ( instanceId : string , hintComment : string , remoteAuthorizedKeysPath : string ) {
263+ try {
264+ const deleteExistingKeyCommand = getRemoveLinesCommand ( hintComment , remoteAuthorizedKeysPath )
265+ const result = await this . ssmClient . sendCommandAndWait ( instanceId , 'AWS-RunShellScript' , {
266+ commands : [ deleteExistingKeyCommand ] ,
267+ } )
268+ console . log ( result )
269+ } catch ( e ) {
270+ getLogger ( ) . warn ( `ec2: failed to clean keys: %O` , e )
271+ }
272+ }
273+
262274 public async sendSshKeyToInstance (
263275 selection : Ec2Selection ,
264276 sshKeyPair : SshKeyPair ,
265277 remoteUser : string
266278 ) : Promise < void > {
267279 const sshPubKey = await sshKeyPair . getPublicKey ( )
268- const hintComment = '# Added by AWS Toolkit for VSCode '
280+ const hintComment = '#AWSToolkitForVSCode '
269281
270282 const remoteAuthorizedKeysPath = `/home/${ remoteUser } /.ssh/authorized_keys`
271- const deleteExistingKeyCommand = getRemoveLinesCommand ( hintComment , remoteAuthorizedKeysPath )
272283
273284 const appendStr = ( s : string ) => `echo "${ s } " >> ${ remoteAuthorizedKeysPath } `
274- const writeKeyCommand = appendStr ( [ sshPubKey , hintComment ] . join ( ' ' ) )
285+ const writeKeyCommand = appendStr ( [ sshPubKey . replace ( '\n' , '' ) , hintComment ] . join ( ' ' ) )
275286
287+ await this . attemptToCleanKeys ( selection . instanceId , hintComment , remoteAuthorizedKeysPath )
276288 await this . ssmClient . sendCommandAndWait ( selection . instanceId , 'AWS-RunShellScript' , {
277- commands : [ deleteExistingKeyCommand , writeKeyCommand ] ,
289+ commands : [ writeKeyCommand ] ,
278290 } )
279291 }
280292
0 commit comments