File tree Expand file tree Collapse file tree 2 files changed +29
-1
lines changed
test/awsService/sagemaker Expand file tree Collapse file tree 2 files changed +29
-1
lines changed Original file line number Diff line number Diff line change @@ -226,7 +226,13 @@ export async function removeKnownHost(hostname: string): Promise<void> {
226226 throw ToolkitError . chain ( err , 'Failed to read known_hosts file' )
227227 }
228228
229- const updatedLines = lines . filter ( ( line ) => ! line . split ( ' ' ) [ 0 ] . split ( ',' ) . includes ( hostname ) )
229+ const updatedLines = lines . filter ( ( line ) => {
230+ const entryHostname = line . split ( ' ' ) [ 0 ] . split ( ',' )
231+ // Hostnames in the known_hosts file seem to be always lowercase, but keeping the case-sensitive check just in
232+ // case. Originally we were only doing the case-sensitive check which caused users to get a host
233+ // identification error when reconnecting to a Space after it was restarted.
234+ return ! entryHostname . includes ( hostname ) && ! entryHostname . includes ( hostname . toLowerCase ( ) )
235+ } )
230236
231237 if ( updatedLines . length !== lines . length ) {
232238 try {
Original file line number Diff line number Diff line change @@ -211,6 +211,28 @@ describe('SageMaker Model', () => {
211211 assertLogsContain ( `Removed '${ hostname } ' from known_hosts` , false , 'debug' )
212212 } )
213213
214+ it ( 'removes case-sensitive hostname when entry in known_hosts is lowercase' , async function ( ) {
215+ const mixedCaseHostname = 'Test.Host.Com'
216+
217+ sandbox . stub ( fs , 'existsFile' ) . resolves ( true )
218+
219+ const inputContent = `test.host.com ssh-rsa AAAA\nsome.other.com ssh-rsa BBBB`
220+ const expectedOutput = `some.other.com ssh-rsa BBBB`
221+
222+ sandbox . stub ( fs , 'readFileText' ) . resolves ( inputContent )
223+ const writeStub = sandbox . stub ( fs , 'writeFile' ) . resolves ( )
224+
225+ await removeKnownHost ( mixedCaseHostname )
226+
227+ sinon . assert . calledWith (
228+ writeStub ,
229+ path . join ( os . homedir ( ) , '.ssh' , 'known_hosts' ) ,
230+ sinon . match ( ( value : string ) => value . trim ( ) === expectedOutput ) ,
231+ { atomic : true }
232+ )
233+ assertLogsContain ( `Removed '${ mixedCaseHostname } ' from known_hosts` , false , 'debug' )
234+ } )
235+
214236 it ( 'handles hostname in comma-separated list' , async function ( ) {
215237 sandbox . stub ( fs , 'existsFile' ) . resolves ( true )
216238
You can’t perform that action at this time.
0 commit comments