Skip to content

Commit

Permalink
update gerrit auth provider to add credentials for gerrit event-streams
Browse files Browse the repository at this point in the history
  • Loading branch information
ankul-shippable committed Sep 4, 2018
1 parent 26d33b0 commit 7685b1b
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 14 deletions.
45 changes: 39 additions & 6 deletions api/systemIntegrations/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ module.exports = self;
var async = require('async');
var mongoose = require('mongoose');
var _ = require('underscore');
var tmp = require('tmp');
var sshKeyGen = require('ssh-keygen');

var APIAdapter = require('../../common/APIAdapter.js');
var envHandler = require('../../common/envHandler.js');
Expand Down Expand Up @@ -33,6 +35,7 @@ function post(req, res) {
_getMasterIntegrationFields.bind(null, bag),
_validateMasterIntegrationFields.bind(null, bag),
_postProvider.bind(null, bag),
_genSshKeys.bind(null, bag),
_createSystemIntegration.bind(null, bag),
_getSystemIntegration.bind(null, bag),
_postSystemIntegrationFieldsToVault.bind(null, bag)
Expand Down Expand Up @@ -256,12 +259,6 @@ function _postProvider(bag, next) {
var who = bag.who + '|' + _postProvider.name;
logger.verbose(who, 'Inside');

if (!bag.reqBody.data.url) {
logger.debug(
'No provider available for system integration: ' + bag.reqBody.name);
return next();
}

// Strip 'Keys' and 'BasicAuth' from the end of the name
// This is to deal with the fact that providers will be dynamically created
// for auth providers when the system integration is created. The system
Expand All @@ -274,6 +271,14 @@ function _postProvider(bag, next) {
name = name.replace('Keys', '');
else if (name.endsWith('BasicAuth'))
name = name.replace('BasicAuth', '');
bag.providerName = name;

if (!bag.reqBody.data.url) {
logger.debug(
'No provider available for system integration: ' + bag.reqBody.name);
return next();
}

var provider = {
url: bag.reqBody.data.url,
name: name
Expand All @@ -293,6 +298,34 @@ function _postProvider(bag, next) {
);
}

function _genSshKeys(bag, next) {
if (bag.providerName !== 'gerrit') return next();

var who = bag.who + '|' + _genSshKeys.name;
logger.verbose(who, 'Inside');

tmp.file(
function (err, path) {
sshKeyGen({
location: path,
comment: 'Shippable',
read: true
},
function (err, out) {
if (err)
return next(
new ActErr(who, ActErr.OperationFailed, 'SSH KeyGen Failed', err)
);

bag.reqBody.data.privateKey = out.key;
bag.reqBody.data.publicKey = out.pubKey;
return next();
}
);
}
);
}

function _createSystemIntegration(bag, next) {
var who = bag.who + '|' + _createSystemIntegration.name;
logger.verbose(who, 'Inside');
Expand Down
46 changes: 40 additions & 6 deletions api/systemIntegrations/put.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ module.exports = self;

var async = require('async');
var _ = require('underscore');
var tmp = require('tmp');
var sshKeyGen = require('ssh-keygen');

var APIAdapter = require('../../common/APIAdapter.js');
var envHandler = require('../../common/envHandler.js');
Expand Down Expand Up @@ -32,6 +34,7 @@ function put(req, res) {
_getMasterIntegrationFields.bind(null, bag),
_validateMasterIntegrationFields.bind(null, bag),
_postProvider.bind(null, bag),
_genSshKeys.bind(null, bag),
_put.bind(null, bag),
_getUpdatedSystemIntegration.bind(null, bag),
_postSystemIntegrationFieldsToVault.bind(null, bag),
Expand Down Expand Up @@ -228,12 +231,6 @@ function _postProvider(bag, next) {
var who = bag.who + '|' + _postProvider.name;
logger.verbose(who, 'Inside');

if (!bag.reqBody.data.url) {
logger.debug(
'No provider available for system integration: ' + bag.reqBody.name);
return next();
}

// Strip 'Keys' and 'BasicAuth' from the end of the name
// This is to deal with the fact that providers will be dynamically created
// for auth providers when the system integration is created. The system
Expand All @@ -246,6 +243,14 @@ function _postProvider(bag, next) {
name = name.replace('Keys', '');
else if (name.endsWith('BasicAuth'))
name = name.replace('BasicAuth', '');
bag.providerName = name;

if (!bag.reqBody.data.url) {
logger.debug(
'No provider available for system integration: ' + bag.reqBody.name);
return next();
}

var provider = {
url: bag.reqBody.data.url,
name: name
Expand All @@ -265,6 +270,35 @@ function _postProvider(bag, next) {
);
}

function _genSshKeys(bag, next) {
if (bag.providerName !== 'gerrit') return next();
if (bag.reqBody.data.privateKey && bag.reqBody.data.publicKey) return next();

var who = bag.who + '|' + _genSshKeys.name;
logger.verbose(who, 'Inside');

tmp.file(
function (err, path) {
sshKeyGen({
location: path,
comment: 'Shippable',
read: true
},
function (err, out) {
if (err)
return next(
new ActErr(who, ActErr.OperationFailed, 'SSH KeyGen Failed', err)
);

bag.reqBody.data.privateKey = out.key;
bag.reqBody.data.publicKey = out.pubKey;
return next();
}
);
}
);
}

function _put(bag, next) {
var who = bag.who + '|' + _put.name;
logger.verbose(who, 'Inside');
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"remove-markdown": "0.1.0",
"request": "2.73.0",
"serve-favicon": "^2.4.1",
"ssh-keygen": "0.2.1",
"tmp": "0.0.28",
"underscore": "1.8.3",
"ursa": "0.9.4",
Expand Down
28 changes: 28 additions & 0 deletions static/scripts/dashboard/dashboardNew.html
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,34 @@ <h4 class="m-t-0 header-title"><b>Gerrit</b></h4>
<span>Duplicate URLs found. Multiple authorizations to a provider with same URLs are not allowed.</span>
</dd>
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label"></label>
<div class="col-md-6">
Allow Shippable to access event-streams
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label">Username</label>
<div class="col-md-6">
<input type="text" class="form-control" placeholder="shippable" name="gerritBasicUserName"
ng-model="gerritBasicAuth.sysInt.username" ng-disabled="vm.initializing || vm.installing || vm.saving"/>
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label">SSH Port</label>
<div class="col-md-6">
<input type="text" class="form-control" placeholder="29418" name="gerritBasicSshPort"
ng-model="gerritBasicAuth.sysInt.sshPort" ng-disabled="vm.initializing || vm.installing || vm.saving"/>
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label">Public Key</label>
<div class="col-sm-6">
<textarea rows="6" style="resize: both" class="form-control" placeholder="" name="gerritBasicPublicKey"
ng-model="gerritBasicAuth.sysInt.publicKey" ng-disabled="vm.initializing || vm.installing || vm.saving" readonly />
</textarea>
</div>
<button class="btn btn-danger"
ng-click="vm.deleteAuthencation('gerritBasicAuth', gerritBasicAuth.systemIntegrationId)"
ng-disabled="vm.installForm.auth.gerritBasicAuth.isDeletingAuth || vm.initializing || vm.installing || vm.saving">
Expand Down
9 changes: 7 additions & 2 deletions static/scripts/dashboard/dashboardNewCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,11 @@
sysInt: {
customName: '',
wwwUrl: '',
url: ''
url: '',
username: 'shippable',
sshPort: '29418',
privateKey: '',
publicKey: ''
},
callbackUrl: '',
systemIntegrationId: '',
Expand Down Expand Up @@ -1454,6 +1458,7 @@
systemIntegration.data
);
} else {
console.log('systemIntegration: ', systemIntegration);
var auth = {
sysInt: systemIntegration.data,
systemIntegrationId: systemIntegration.id,
Expand Down Expand Up @@ -1556,7 +1561,7 @@
}
}
);

console.log('auth: ', $scope.vm.installForm.auth);
return next();
}
);
Expand Down

0 comments on commit 7685b1b

Please sign in to comment.