Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Users/lukillgo/installsshkey #4258

Merged
merged 8 commits into from
May 8, 2017
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"loc.friendlyName": "Install SSH Key (Preview)",
"loc.helpMarkDown": "",
"loc.description": "Install an SSH key prior to a build",
"loc.instanceNameFormat": "Install an SSH key",
"loc.input.label.hostName": "Known hosts entry",
"loc.input.help.hostName": "The entry for this SSH key for the known_hosts file.",
"loc.input.label.sshPublicKey": "SSH Public Key",
"loc.input.help.sshPublicKey": "The contents of the public SSH key.",
"loc.input.label.sshKeySecureFile": "SSH Key",
"loc.input.help.sshKeySecureFile": "Select the SSH key that was uploaded to `Secure Files` to install on the build agent.",
"loc.messages.SSHKeyAlreadyInstalled": "The SSH Key is already installed.",
"loc.messages.SSHPublicKeyMalformed": "Could not get the base64 portion of the public SSH key.",
"loc.messages.SSHKeyInstallFailed": "Failed to install the SSH key."
}
55 changes: 55 additions & 0 deletions Tasks/InstallSSHKey/Tests/L0.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import fs = require('fs');
import assert = require('assert');
import path = require('path');
import * as ttm from 'vsts-task-lib/mock-test';

describe('InstallSSHKey Suite', function () {
this.timeout(20000);
before(() => {
});

after(() => {
});

it('Start ssh-agent', (done: MochaDone) => {
this.timeout(1000);

let tp: string = path.join(__dirname, 'L0StartAgent.js');
let tr: ttm.MockTestRunner = new ttm.MockTestRunner(tp);

tr.run();

assert(tr.stderr.length === 0, 'should not have written to stderr');
assert(tr.succeeded, 'task should have succeeded');

done();
});

it('SSH key already installed', (done: MochaDone) => {
this.timeout(1000);

let tp: string = path.join(__dirname, 'L0KeyAlreadyInstalled.js');
let tr: ttm.MockTestRunner = new ttm.MockTestRunner(tp);

tr.run();

assert(tr.failed, 'task should have failed');
assert(tr.stdOutContained('loc_mock_SSHKeyAlreadyInstalled'), 'expected error: SSH key already installed');

done();
});

it('SSH key malformed', (done: MochaDone) => {
this.timeout(1000);

let tp: string = path.join(__dirname, 'L0KeyMalformed.js');
let tr: ttm.MockTestRunner = new ttm.MockTestRunner(tp);

tr.run();

assert(tr.failed, 'task should have failed' + ' std=' + tr.stdout + ' err=' + tr.stderr);
assert(tr.stdOutContained('loc_mock_SSHPublicKeyMalformed'), 'expected error: SSH key malformed' + ' std=' + tr.stdout + ' err=' + tr.stderr);

done();
});
});
47 changes: 47 additions & 0 deletions Tasks/InstallSSHKey/Tests/L0AddToAgent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import ma = require('vsts-task-lib/mock-answer');
import tmrm = require('vsts-task-lib/mock-run');
import path = require('path');
import fs = require('fs');

let taskPath = path.join(__dirname, '..', 'preinstallsshkey.js');
let tr: tmrm.TaskMockRunner = new tmrm.TaskMockRunner(taskPath);

tr.setInput('provProfileSecureFile', 'mySecureFileId');

process.env['AGENT_VERSION'] = '2.117.0';
process.env['AGENT_TEMPDIRECTORY'] = '/build/temp';

let secureFileHelperMock = require('securefiles-common/securefiles-common-mock');
tr.registerMock('securefiles-common/securefiles-common', secureFileHelperMock);

tr.registerMock('fs', {
writeFileSync: function (filePath, contents) {
}
});

// provide answers for task mock
let a: ma.TaskLibAnswers = <ma.TaskLibAnswers>{
"which": {
"security": "/usr/bin/security",
"rm": "/bin/rm",
"cp": "/bin/cp"
},
"checkPath": {
"/usr/bin/security": true,
"/bin/rm": true,
"/bin/cp": true
},
"exist": {
"/build/temp/mySecureFileId.filename": true
},
"exec": {
"/usr/bin/security cms -D -i /build/temp/mySecureFileId.filename": {
"code": 0,
"stdout": "ssh key details here"
},
}
};
tr.setAnswers(a);

tr.run();

66 changes: 66 additions & 0 deletions Tasks/InstallSSHKey/Tests/L0KeyAlreadyInstalled.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import ma = require('vsts-task-lib/mock-answer');
import tmrm = require('vsts-task-lib/mock-run');
import path = require('path');
import fs = require('fs');

let taskPath = path.join(__dirname, '..', 'preinstallsshkey.js');
let tr: tmrm.TaskMockRunner = new tmrm.TaskMockRunner(taskPath);

let sshPublicKey: string = 'ssh-rsa KEYINFORMATIONHERE [email protected]'
tr.setInput('sshKeySecureFile', 'mySecureFileId');
tr.setInput('sshPublicKey', sshPublicKey);
tr.setInput('hostName', 'host name entry');

process.env['AGENT_VERSION'] = '2.117.0';
process.env['AGENT_TEMPDIRECTORY'] = '/build/temp';

let secureFileHelperMock = require('securefiles-common/securefiles-common-mock');
tr.registerMock('securefiles-common/securefiles-common', secureFileHelperMock);

tr.registerMock('fs', {
writeFileSync: function (filePath, contents) {
}
});

// provide answers for task mock
let a: ma.TaskLibAnswers = <ma.TaskLibAnswers>{
"which": {
"security": "/usr/bin/security",
"ssh-agent": "/usr/bin/ssh-agent",
"ssh-add": "/usr/bin/ssh-add",
"rm": "/bin/rm",
"cp": "/bin/cp"
},
"checkPath": {
"/usr/bin/security": true,
"/usr/bin/ssh-agent": true,
"/usr/bin/ssh-add": true,
"/bin/rm": true,
"/bin/cp": true
},
"exist": {
"/build/temp/mySecureFileId.filename": true
},
"exec": {
"/usr/bin/security cms -D -i /build/temp/mySecureFileId.filename": {
"code": 0,
"stdout": "ssh key details here"
},
"/usr/bin/ssh-agent": {
"code": 0,
"stdout": "SSH_AUTH_SOCK=/tmp/ssh-XVblDhTvcbC3/agent.24196; export SSH_AUTH_SOCK; SSH_AGENT_PID=4644; export SSH_AGENT_PID; echo Agent pid 4644;"
},
"/usr/bin/ssh-add": {
"code": 0,
"stdout": ""
},
"/usr/bin/ssh-add -L": {
"code": 0,
"stdout": sshPublicKey
},
}
};
tr.setAnswers(a);

tr.run();

76 changes: 76 additions & 0 deletions Tasks/InstallSSHKey/Tests/L0KeyMalformed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import ma = require('vsts-task-lib/mock-answer');
import tmrm = require('vsts-task-lib/mock-run');
import path = require('path');
import fs = require('fs');

let taskPath = path.join(__dirname, '..', 'preinstallsshkey.js');
let tr: tmrm.TaskMockRunner = new tmrm.TaskMockRunner(taskPath);

let sshPublicKey: string = '[email protected]'
tr.setInput('sshKeySecureFile', 'mySecureFileId');
tr.setInput('sshPublicKey', sshPublicKey);
tr.setInput('hostName', 'host name entry');

process.env['AGENT_VERSION'] = '2.117.0';
process.env['AGENT_TEMPDIRECTORY'] = '/build/temp';

let secureFileHelperMock = require('./secure-files-mock.js');
tr.registerMock('securefiles-common/securefiles-common', secureFileHelperMock);

tr.registerMock('fs', {
writeFileSync: function (filePath, contents) {
},
existsSync: function (filePath, contents) {
return true;
},
readFileSync: function (filePath) {
return 'contents';
}
});

// provide answers for task mock
let a: ma.TaskLibAnswers = <ma.TaskLibAnswers>{
"which": {
"security": "/usr/bin/security",
"ssh-agent": "/usr/bin/ssh-agent",
"ssh-add": "/usr/bin/ssh-add",
"rm": "/bin/rm",
"cp": "/bin/cp"
},
"checkPath": {
"/usr/bin/security": true,
"/usr/bin/ssh-agent": true,
"/usr/bin/ssh-add": true,
"/bin/rm": true,
"/bin/cp": true
},
"exist": {
"/build/temp/mySecureFileId.filename": true
},
"exec": {
"/usr/bin/security cms -D -i /build/temp/mySecureFileId.filename": {
"code": 0,
"stdout": "ssh key details here"
},
"/usr/bin/ssh-agent": {
"code": 0,
"stdout": "SSH_AUTH_SOCK=/tmp/ssh-XVblDhTvcbC3/agent.24196; export SSH_AUTH_SOCK; SSH_AGENT_PID=4644; export SSH_AGENT_PID; echo Agent pid 4644;"
},
"/usr/bin/ssh-add": {
"code": 0,
"stdout": ""
},
"/usr/bin/ssh-add -L": {
"code": 0,
"stdout": "No keys"
},
"/usr/bin/ssh-add /build/temp/mySecureFileId.filename": {
"code": 0,
"stdout": ""
},
}
};
tr.setAnswers(a);

tr.run();

76 changes: 76 additions & 0 deletions Tasks/InstallSSHKey/Tests/L0StartAgent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import ma = require('vsts-task-lib/mock-answer');
import tmrm = require('vsts-task-lib/mock-run');
import path = require('path');
import fs = require('fs');

let taskPath = path.join(__dirname, '..', 'preinstallsshkey.js');
let tr: tmrm.TaskMockRunner = new tmrm.TaskMockRunner(taskPath);

let sshPublicKey: string = 'ssh-rsa KEYINFORMATIONHERE [email protected]'
tr.setInput('sshKeySecureFile', 'mySecureFileId');
tr.setInput('sshPublicKey', sshPublicKey);
tr.setInput('hostName', 'host name entry');

process.env['AGENT_VERSION'] = '2.117.0';
process.env['AGENT_TEMPDIRECTORY'] = '/build/temp';

let secureFileHelperMock = require('./secure-files-mock.js');
tr.registerMock('securefiles-common/securefiles-common', secureFileHelperMock);

tr.registerMock('fs', {
writeFileSync: function (filePath, contents) {
},
existsSync: function (filePath, contents) {
return true;
},
readFileSync: function (filePath) {
return 'contents';
}
});

// provide answers for task mock
let a: ma.TaskLibAnswers = <ma.TaskLibAnswers>{
"which": {
"security": "/usr/bin/security",
"ssh-agent": "/usr/bin/ssh-agent",
"ssh-add": "/usr/bin/ssh-add",
"rm": "/bin/rm",
"cp": "/bin/cp"
},
"checkPath": {
"/usr/bin/security": true,
"/usr/bin/ssh-agent": true,
"/usr/bin/ssh-add": true,
"/bin/rm": true,
"/bin/cp": true
},
"exist": {
"/build/temp/mySecureFileId.filename": true
},
"exec": {
"/usr/bin/security cms -D -i /build/temp/mySecureFileId.filename": {
"code": 0,
"stdout": "ssh key details here"
},
"/usr/bin/ssh-agent": {
"code": 0,
"stdout": "SSH_AUTH_SOCK=/tmp/ssh-XVblDhTvcbC3/agent.24196; export SSH_AUTH_SOCK; SSH_AGENT_PID=4644; export SSH_AGENT_PID; echo Agent pid 4644;"
},
"/usr/bin/ssh-add": {
"code": 0,
"stdout": ""
},
"/usr/bin/ssh-add -L": {
"code": 0,
"stdout": "No keys"
},
"/usr/bin/ssh-add /build/temp/mySecureFileId.filename": {
"code": 0,
"stdout": ""
},
}
};
tr.setAnswers(a);

tr.run();

19 changes: 19 additions & 0 deletions Tasks/InstallSSHKey/Tests/secure-files-mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import * as tl from "vsts-task-lib/task";

export class SecureFileHelpers {

constructor() {
tl.debug('Mock SecureFileHelpers constructor');
}

async downloadSecureFile(secureFileId: string) {
tl.debug('Mock downloadSecureFile with id = ' + secureFileId);
let fileName: string = secureFileId + '.filename';
let tempDownloadPath: string = tl.getVariable('Agent.TempDirectory') + '/' + fileName;
return tempDownloadPath;
}

deleteSecureFile(secureFileId: string) {
tl.debug('Mock deleteSecureFile with id = ' + secureFileId);
}
}
Binary file added Tasks/InstallSSHKey/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading