@@ -9,6 +9,7 @@ export const postKillAgentSetting: string = 'INSTALL_SSH_KEY_KILL_SSH_AGENT_PID'
99export const postDeleteKeySetting : string = 'INSTALL_SSH_KEY_DELETE_KEY' ;
1010export const postKnownHostsContentsSetting : string = 'INSTALL_SSH_KEY_KNOWN_HOSTS_CONTENTS' ;
1111export const postKnownHostsLocationSetting : string = 'INSTALL_SSH_KEY_KNOWN_HOSTS_LOCATION' ;
12+ export const postKnownHostsDeleteFileSetting : string = 'INSTALL_SSH_KEY_KNOWN_HOSTS_FILE_DELETE' ;
1213
1314export const sshAgentPidEnvVariableKey : string = 'SSH_AGENT_PID' ;
1415export const sshAgentSockEnvVariableKey : string = 'SSH_AUTH_SOCK' ;
@@ -72,10 +73,13 @@ export class SshToolRunner {
7273 }
7374
7475 tl . debug ( 'Adding the SSH key to the agent ' + privateKeyLocation ) ;
76+ let oldMode : number = fs . statSync ( privateKeyLocation ) . mode ;
77+ fs . chmodSync ( privateKeyLocation , '600' ) ; // requires user only permissions when adding to agent
7578 results = tl . execSync ( this . getExecutable ( 'ssh-add' ) , privateKeyLocation ) ;
7679 if ( results . error ) {
7780 throw tl . loc ( 'SSHKeyInstallFailed' ) ;
7881 }
82+ fs . chmodSync ( privateKeyLocation , oldMode ) ;
7983 tl . setTaskVariable ( postDeleteKeySetting , privateKeyLocation ) ;
8084
8185 results = tl . execSync ( this . getExecutable ( 'ssh-add' ) , null ) ;
@@ -92,14 +96,20 @@ export class SshToolRunner {
9296
9397export function setKnownHosts ( knownHostsEntry : string ) {
9498 let knownHostsFolder : string = path . join ( os . homedir ( ) , '.ssh' ) ;
99+ let knownHostsFile : string = path . join ( knownHostsFolder , 'known_hosts' ) ;
100+ let knownHostsContent : string = '' ;
101+ let knownHostsDeleteFileOnClose : string = 'true' ;
95102 if ( ! fs . existsSync ( knownHostsFolder ) ) {
96103 fs . mkdirSync ( knownHostsFolder ) ;
104+ } else if ( fs . existsSync ( knownHostsFile ) ) {
105+ tl . debug ( 'Read known_hosts' ) ;
106+ knownHostsDeleteFileOnClose = '' ;
107+ knownHostsContent = fs . readFileSync ( knownHostsFile ) . toString ( ) ;
97108 }
98- let knownHostsFile : string = path . join ( knownHostsFolder , 'known_hosts' ) ;
99109
100- tl . debug ( 'Read known_hosts' ) ;
101- tl . setTaskVariable ( postKnownHostsContentsSetting , fs . readFileSync ( knownHostsFile ) . toString ( ) ) ;
110+ tl . setTaskVariable ( postKnownHostsContentsSetting , knownHostsContent ) ;
102111 tl . setTaskVariable ( postKnownHostsLocationSetting , knownHostsFile ) ;
112+ tl . setTaskVariable ( postKnownHostsDeleteFileSetting , knownHostsDeleteFileOnClose ) ;
103113
104114 tl . debug ( 'Inserting entry into known_hosts' ) ;
105115 fs . writeFileSync ( knownHostsFile , knownHostsEntry + os . EOL ) ;
@@ -108,9 +118,12 @@ export function setKnownHosts(knownHostsEntry: string) {
108118export function tryRestoreKnownHosts ( ) {
109119 let knownHostsContents : string = tl . getTaskVariable ( postKnownHostsContentsSetting ) ;
110120 let knownHostsLocation : string = tl . getTaskVariable ( postKnownHostsLocationSetting ) ;
111-
121+ let knownHostsDeleteFileOnExit : string = tl . getTaskVariable ( postKnownHostsDeleteFileSetting ) ;
122+
112123 tl . debug ( 'Restoring known_hosts' ) ;
113- if ( knownHostsContents && knownHostsLocation ) {
124+ if ( knownHostsDeleteFileOnExit && knownHostsLocation ) {
125+ fs . unlinkSync ( knownHostsLocation ) ;
126+ } else if ( knownHostsContents && knownHostsLocation ) {
114127 fs . writeFileSync ( knownHostsLocation , knownHostsContents ) ;
115128 } else if ( knownHostsLocation || knownHostsContents ) {
116129 tl . warning ( tl . loc ( 'CannotResetKnownHosts' ) ) ;
0 commit comments