@@ -28,7 +28,7 @@ import { showMessageWithCancel } from '../../shared/utilities/messages'
2828import { SshConfig } from '../../shared/sshConfig'
2929import { SshKeyPair } from './sshKeyPair'
3030import { Ec2SessionTracker } from './remoteSessionManager'
31- import { getEc2SsmEnv } from './utils'
31+ import { getEc2SsmEnv , getRemoveLabeledEntriesCommand } from './utils'
3232
3333export type Ec2ConnectErrorCode = 'EC2SSMStatus' | 'EC2SSMPermission' | 'EC2SSMConnect' | 'EC2SSMAgentStatus'
3434
@@ -265,13 +265,17 @@ export class Ec2Connecter implements vscode.Disposable {
265265 remoteUser : string
266266 ) : Promise < void > {
267267 const sshPubKey = await sshKeyPair . getPublicKey ( )
268+ const hintComment = '# Added by AWS Toolkit for VSCode'
268269
269- const remoteAuthorizedKeysPaths = `/home/${ remoteUser } /.ssh/authorized_keys`
270- const command = `echo "${ sshPubKey } " > ${ remoteAuthorizedKeysPaths } `
271- const documentName = 'AWS-RunShellScript'
270+ const remoteAuthorizedKeysPath = `/home/${ remoteUser } /.ssh/authorized_keys`
271+ const deleteExistingKeyCommand = getRemoveLabeledEntriesCommand ( hintComment , remoteAuthorizedKeysPath )
272272
273- await this . ssmClient . sendCommandAndWait ( selection . instanceId , documentName , {
274- commands : [ command ] ,
273+ const appendStr = ( s : string ) => `echo "${ s } " >> ${ remoteAuthorizedKeysPath } `
274+ const writeCommentCommand = appendStr ( hintComment )
275+ const writeKeyCommand = appendStr ( sshPubKey )
276+
277+ await this . ssmClient . sendCommandAndWait ( selection . instanceId , 'AWS-RunShellScript' , {
278+ commands : [ deleteExistingKeyCommand , writeCommentCommand , writeKeyCommand ] ,
275279 } )
276280 }
277281
0 commit comments