Skip to content

Commit b72e3fa

Browse files
authored
Merge pull request #4286 from Microsoft/users/lukillgo/sshkey
#939802: Fix mac / linux failures
2 parents c129150 + 1b120c3 commit b72e3fa

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

Tasks/InstallSSHKey/Tests/L0StartAgent.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ process.env['AGENT_HOMEDIRECTORY'] = '';
1818
let secureFileHelperMock = require('./secure-files-mock.js');
1919
tr.registerMock('securefiles-common/securefiles-common', secureFileHelperMock);
2020

21+
class MockStats {
22+
mode = 600;
23+
};
2124
tr.registerMock('fs', {
2225
writeFileSync: function (filePath, contents) {
2326
},
@@ -26,6 +29,13 @@ tr.registerMock('fs', {
2629
},
2730
readFileSync: function (filePath) {
2831
return 'contents';
32+
},
33+
statSync: function (filePath) {
34+
let s : MockStats = new MockStats();
35+
return s;
36+
},
37+
chmodSync: function (filePath, string) {
38+
2939
}
3040
});
3141

Tasks/InstallSSHKey/installsshkey-util.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export const postKillAgentSetting: string = 'INSTALL_SSH_KEY_KILL_SSH_AGENT_PID'
99
export const postDeleteKeySetting: string = 'INSTALL_SSH_KEY_DELETE_KEY';
1010
export const postKnownHostsContentsSetting: string = 'INSTALL_SSH_KEY_KNOWN_HOSTS_CONTENTS';
1111
export const postKnownHostsLocationSetting: string = 'INSTALL_SSH_KEY_KNOWN_HOSTS_LOCATION';
12+
export const postKnownHostsDeleteFileSetting: string = 'INSTALL_SSH_KEY_KNOWN_HOSTS_FILE_DELETE';
1213

1314
export const sshAgentPidEnvVariableKey: string = 'SSH_AGENT_PID';
1415
export 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

9397
export 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) {
108118
export 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

Comments
 (0)