2016-04-01 17:55:29 +00:00
|
|
|
/**
|
|
|
|
* An element which displays a panel for managing keys for external services.
|
|
|
|
*/
|
|
|
|
angular.module('quay').directive('serviceKeysManager', function () {
|
|
|
|
var directiveDefinitionObject = {
|
|
|
|
priority: 0,
|
|
|
|
templateUrl: '/static/directives/service-keys-manager.html',
|
|
|
|
replace: false,
|
|
|
|
transclude: true,
|
|
|
|
restrict: 'C',
|
|
|
|
scope: {
|
|
|
|
'isEnabled': '=isEnabled'
|
|
|
|
},
|
2016-04-27 21:44:44 +00:00
|
|
|
controller: function($scope, $element, ApiService, TableService, UIService) {
|
2016-04-01 17:55:29 +00:00
|
|
|
$scope.options = {
|
|
|
|
'filter': null,
|
|
|
|
'predicate': 'expiration_datetime',
|
|
|
|
'reverse': false,
|
|
|
|
};
|
|
|
|
|
2016-04-27 21:44:44 +00:00
|
|
|
$scope.deleteKeysInfo = null;
|
|
|
|
$scope.approveKeysInfo = null;
|
|
|
|
$scope.changeKeysInfo = null;
|
|
|
|
|
|
|
|
$scope.checkedKeys = UIService.createCheckStateController([], 'kid');
|
|
|
|
|
2016-04-01 17:55:29 +00:00
|
|
|
$scope.TableService = TableService;
|
|
|
|
$scope.newKey = null;
|
|
|
|
$scope.creatingKey = false;
|
|
|
|
$scope.context = {
|
|
|
|
'expirationChangeInfo': null
|
|
|
|
};
|
|
|
|
|
|
|
|
var buildOrderedKeys = function() {
|
|
|
|
if (!$scope.keys) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var keys = $scope.keys.map(function(key) {
|
|
|
|
var expiration_datetime = -Number.MAX_VALUE;
|
2016-04-12 23:17:19 +00:00
|
|
|
if (key.rotation_duration) {
|
|
|
|
expiration_datetime = -(Number.MAX_VALUE/2);
|
|
|
|
} else if (key.expiration_date) {
|
2016-04-01 17:55:29 +00:00
|
|
|
expiration_datetime = new Date(key.expiration_date).valueOf() * (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $.extend(key, {
|
|
|
|
'creation_datetime': new Date(key.creation_date).valueOf() * (-1),
|
|
|
|
'expiration_datetime': expiration_datetime,
|
|
|
|
'expanded': false
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
$scope.orderedKeys = TableService.buildOrderedItems(keys, $scope.options,
|
|
|
|
['name', 'kid', 'service'],
|
|
|
|
['creation_datetime', 'expiration_datetime'])
|
2016-04-27 21:44:44 +00:00
|
|
|
|
|
|
|
$scope.checkedKeys = UIService.createCheckStateController($scope.orderedKeys.visibleEntries, 'kid');
|
2016-04-01 17:55:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
var loadServiceKeys = function() {
|
|
|
|
$scope.options.filter = null;
|
|
|
|
$scope.now = new Date();
|
2016-04-08 21:48:44 +00:00
|
|
|
$scope.keysResource = ApiService.listServiceKeysAsResource().get(function(resp) {
|
2016-04-01 17:55:29 +00:00
|
|
|
$scope.keys = resp['keys'];
|
|
|
|
buildOrderedKeys();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.getKeyTitle = function(key) {
|
|
|
|
if (!key) { return ''; }
|
|
|
|
return key.name || key.kid.substr(0, 12);
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.toggleDetails = function(key) {
|
|
|
|
key.expanded = !key.expanded;
|
|
|
|
};
|
|
|
|
|
2016-04-12 23:17:19 +00:00
|
|
|
$scope.getRotationDate = function(key) {
|
|
|
|
return moment(key.created_date).add(key.rotation_duration, 's').format('LLL');
|
|
|
|
};
|
|
|
|
|
2016-04-01 17:55:29 +00:00
|
|
|
$scope.getExpirationInfo = function(key) {
|
|
|
|
if (!key.expiration_date) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2016-04-12 23:17:19 +00:00
|
|
|
if (key.rotation_duration) {
|
|
|
|
var rotate_date = moment(key.created_date).add(key.rotation_duration, 's')
|
2016-04-01 17:55:29 +00:00
|
|
|
if (moment().isBefore(rotate_date)) {
|
2016-04-12 23:17:19 +00:00
|
|
|
return {'className': 'rotation', 'icon': 'fa-refresh', 'willRotate': true};
|
2016-04-01 17:55:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
expiration_date = moment(key.expiration_date);
|
|
|
|
if (moment().isAfter(expiration_date)) {
|
|
|
|
return {'className': 'expired', 'icon': 'fa-warning'};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (moment().add(1, 'week').isAfter(expiration_date)) {
|
|
|
|
return {'className': 'critical', 'icon': 'fa-warning'};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (moment().add(1, 'month').isAfter(expiration_date)) {
|
|
|
|
return {'className': 'warning', 'icon': 'fa-warning'};
|
|
|
|
}
|
|
|
|
|
2016-04-08 21:48:44 +00:00
|
|
|
return {'className': 'info', 'icon': 'fa-check'};
|
2016-04-01 17:55:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
$scope.showChangeName = function(key) {
|
2016-04-08 21:48:44 +00:00
|
|
|
bootbox.prompt({
|
|
|
|
'size': 'small',
|
|
|
|
'title': 'Enter a friendly name for key ' + $scope.getKeyTitle(key),
|
|
|
|
'value': key.name || '',
|
|
|
|
'callback': function(value) {
|
|
|
|
if (value != null) {
|
|
|
|
var data = {
|
|
|
|
'name': value
|
|
|
|
};
|
|
|
|
|
|
|
|
var params = {
|
|
|
|
'kid': key.kid
|
|
|
|
};
|
|
|
|
|
|
|
|
ApiService.updateServiceKey(data, params).then(function(resp) {
|
|
|
|
loadServiceKeys();
|
|
|
|
}, ApiService.errorDisplay('Could not update service key'));
|
|
|
|
}
|
2016-04-01 17:55:29 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.showChangeExpiration = function(key) {
|
|
|
|
$scope.context.expirationChangeInfo = {
|
|
|
|
'key': key,
|
|
|
|
'expiration_date': key.expiration_date ? (new Date(key.expiration_date).getTime() / 1000) : null
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.changeKeyExpiration = function(changeInfo, callback) {
|
2016-04-27 21:44:44 +00:00
|
|
|
var errorHandler = ApiService.errorDisplay('Could not change expiration on service key', function() {
|
|
|
|
loadServiceKeys();
|
|
|
|
callback(false);
|
|
|
|
});
|
2016-04-01 17:55:29 +00:00
|
|
|
|
|
|
|
var data = {
|
|
|
|
'expiration': changeInfo.expiration_date
|
|
|
|
};
|
|
|
|
|
|
|
|
var params = {
|
|
|
|
'kid': changeInfo.key.kid
|
|
|
|
};
|
|
|
|
|
|
|
|
ApiService.updateServiceKey(data, params).then(function(resp) {
|
|
|
|
loadServiceKeys();
|
|
|
|
callback(true);
|
|
|
|
}, errorHandler);
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.createServiceKey = function() {
|
|
|
|
$scope.creatingKey = true;
|
|
|
|
ApiService.createServiceKey($scope.newKey).then(function(resp) {
|
|
|
|
$scope.creatingKey = false;
|
|
|
|
$('#createKeyModal').modal('hide');
|
|
|
|
$scope.createdKey = resp;
|
|
|
|
$('#createdKeyModal').modal('show');
|
|
|
|
loadServiceKeys();
|
|
|
|
}, ApiService.errorDisplay('Could not create service key'));
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.showApproveKey = function(key) {
|
|
|
|
$scope.approvalKeyInfo = {
|
|
|
|
'key': key,
|
|
|
|
'notes': ''
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.approveKey = function(approvalKeyInfo, callback) {
|
2016-04-27 21:44:44 +00:00
|
|
|
var errorHandler = ApiService.errorDisplay('Could not approve service key', function() {
|
|
|
|
loadServiceKeys();
|
|
|
|
callback(false);
|
|
|
|
});
|
2016-04-01 17:55:29 +00:00
|
|
|
|
|
|
|
var data = {
|
|
|
|
'notes': approvalKeyInfo.notes
|
|
|
|
};
|
|
|
|
|
|
|
|
var params = {
|
|
|
|
'kid': approvalKeyInfo.key.kid
|
|
|
|
};
|
|
|
|
|
|
|
|
ApiService.approveServiceKey(data, params).then(function(resp) {
|
|
|
|
loadServiceKeys();
|
|
|
|
callback(true);
|
|
|
|
}, errorHandler);
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.showCreateKey = function() {
|
|
|
|
$scope.newKey = {
|
|
|
|
'expiration': null
|
|
|
|
};
|
|
|
|
|
|
|
|
$('#createKeyModal').modal('show');
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.showDeleteKey = function(key) {
|
|
|
|
$scope.deleteKeyInfo = {
|
|
|
|
'key': key
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.deleteKey = function(deleteKeyInfo, callback) {
|
2016-04-27 21:44:44 +00:00
|
|
|
var errorHandler = ApiService.errorDisplay('Could not delete service key', function() {
|
|
|
|
loadServiceKeys();
|
|
|
|
callback(false);
|
|
|
|
});
|
2016-04-01 17:55:29 +00:00
|
|
|
|
|
|
|
var params = {
|
|
|
|
'kid': deleteKeyInfo.key.kid
|
|
|
|
};
|
|
|
|
|
|
|
|
ApiService.deleteServiceKey(null, params).then(function(resp) {
|
|
|
|
loadServiceKeys();
|
|
|
|
callback(true);
|
|
|
|
}, errorHandler);
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.isDownloadSupported = function() {
|
|
|
|
var isSafari = /^((?!chrome).)*safari/i.test(navigator.userAgent);
|
|
|
|
if (isSafari) {
|
|
|
|
// Doesn't work properly in Safari, sadly.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
try { return !!new Blob(); } catch(e) {}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.downloadPrivateKey = function(key) {
|
|
|
|
var blob = new Blob([key.private_key]);
|
2017-05-21 09:10:11 +00:00
|
|
|
FileSaver.saveAs(blob, $scope.getKeyTitle(key) + '.pem');
|
2016-04-01 17:55:29 +00:00
|
|
|
};
|
|
|
|
|
2016-04-27 21:44:44 +00:00
|
|
|
$scope.askDeleteMultipleKeys = function(keys) {
|
|
|
|
$scope.deleteKeysInfo = {
|
|
|
|
'keys': keys
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.askApproveMultipleKeys = function(keys) {
|
|
|
|
$scope.approveKeysInfo = {
|
|
|
|
'keys': keys
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.askChangeExpirationMultipleKeys = function(keys) {
|
|
|
|
$scope.changeKeysInfo = {
|
|
|
|
'keys': keys
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.allKeyFilter = function(key) {
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.noKeyFilter = function(key) {
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.unapprovedKeyFilter = function(key) {
|
|
|
|
return !key.approval;
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.expiredKeyFilter = function(key) {
|
|
|
|
return $scope.getExpirationInfo(key)['className'] == 'expired';
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.allRequireApproval = function(keys) {
|
|
|
|
for (var i = 0; i < keys.length; ++i) {
|
|
|
|
if (keys[i].approval) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.allExpired = function(keys) {
|
|
|
|
for (var i = 0; i < keys.length; ++i) {
|
|
|
|
if (!$scope.expiredKeyFilter(keys[i])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
var forAllKeys = function(keys, error_msg, performer, callback) {
|
|
|
|
var counter = 0;
|
|
|
|
var performAction = function() {
|
|
|
|
if (counter >= keys.length) {
|
|
|
|
loadServiceKeys();
|
|
|
|
callback(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var key = keys[counter];
|
|
|
|
var errorHandler = function(resp) {
|
|
|
|
if (resp.status != 404) {
|
|
|
|
bootbox.alert(error_msg);
|
|
|
|
loadServiceKeys();
|
|
|
|
callback(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
performAction();
|
|
|
|
};
|
|
|
|
|
|
|
|
counter++;
|
|
|
|
performer(key).then(performAction, errorHandler);
|
|
|
|
};
|
|
|
|
|
|
|
|
performAction();
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.deleteKeys = function(info, callback) {
|
|
|
|
var performer = function(key) {
|
|
|
|
var params = {
|
|
|
|
'kid': key.kid
|
|
|
|
};
|
|
|
|
|
|
|
|
return ApiService.deleteServiceKey(null, params);
|
|
|
|
};
|
|
|
|
|
|
|
|
forAllKeys(info.keys, 'Could not delete service key', performer, callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.approveKeys = function(info, callback) {
|
|
|
|
var performer = function(key) {
|
|
|
|
var params = {
|
|
|
|
'kid': key.kid
|
|
|
|
};
|
|
|
|
|
|
|
|
var data = {
|
|
|
|
'notes': $scope.approveKeysInfo.notes
|
|
|
|
};
|
|
|
|
|
|
|
|
return ApiService.approveServiceKey(data, params);
|
|
|
|
};
|
|
|
|
|
|
|
|
forAllKeys(info.keys, 'Could not approve service key', performer, callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.changeKeysExpiration = function(info, callback) {
|
|
|
|
var performer = function(key) {
|
|
|
|
var data = {
|
|
|
|
'expiration': info.expiration_date || null
|
|
|
|
};
|
|
|
|
|
|
|
|
var params = {
|
|
|
|
'kid': key.kid
|
|
|
|
};
|
|
|
|
|
|
|
|
return ApiService.updateServiceKey(data, params);
|
|
|
|
};
|
|
|
|
|
|
|
|
forAllKeys(info.keys, 'Could not update service key', performer, callback);
|
|
|
|
};
|
|
|
|
|
2016-04-01 17:55:29 +00:00
|
|
|
$scope.$watch('options.filter', buildOrderedKeys);
|
|
|
|
$scope.$watch('options.predicate', buildOrderedKeys);
|
|
|
|
$scope.$watch('options.reverse', buildOrderedKeys);
|
|
|
|
|
|
|
|
$scope.$watch('isEnabled', function(value) {
|
|
|
|
if (value) {
|
|
|
|
loadServiceKeys();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return directiveDefinitionObject;
|
|
|
|
});
|