This repository has been archived on 2020-03-24. You can view files and clone it, but cannot push or open issues or pull requests.
quay/static/js/directives/ui/credentials-dialog.js

210 lines
5.9 KiB
JavaScript
Raw Normal View History

2016-04-29 23:37:35 +00:00
/**
* An element which displays a credentials dialog.
*/
angular.module('quay').directive('credentialsDialog', function () {
var directiveDefinitionObject = {
priority: 0,
templateUrl: '/static/directives/credentials-dialog.html',
replace: false,
transclude: true,
restrict: 'C',
scope: {
'credentials': '=credentials',
'secretTitle': '@secretTitle',
'entityTitle': '@entityTitle',
'entityIcon': '@entityIcon'
},
controller: function($scope, $element, $rootScope, Config) {
$scope.Config = Config;
$scope.k8s = {};
$scope.rkt = {};
$scope.docker = {};
$scope.$on('$destroy', function() {
2016-05-12 20:54:05 +00:00
if ($scope.inBody) {
document.body.removeChild($element[0]);
}
});
2016-04-29 23:37:35 +00:00
// Generate a unique ID for the dialog.
if (!$rootScope.credentialsDialogCounter) {
$rootScope.credentialsDialogCounter = 0;
}
$rootScope.credentialsDialogCounter++;
$scope.dialogID = $rootScope.credentialsDialogCounter;
$scope.hide = function() {
$element.find('.modal').modal('hide');
};
$scope.show = function() {
$element.find('.modal').modal({});
// Move the dialog to the body to prevent it from being affected
// by being placed inside other tables.
2016-05-12 20:54:05 +00:00
$scope.inBody = true;
document.body.appendChild($element[0]);
2016-04-29 23:37:35 +00:00
};
$scope.$watch('credentials', function(credentials) {
if (!credentials) {
$scope.hide();
return;
}
$scope.show();
});
$scope.downloadFile = function(info) {
var blob = new Blob([info.contents]);
FileSaver.saveAs(blob, info.filename);
2016-04-29 23:37:35 +00:00
};
$scope.viewFile = function(context) {
context.viewingFile = true;
};
$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.getNamespace = function(credentials) {
if (!credentials || !credentials.username) {
return '';
}
if (credentials.namespace) {
return credentials.namespace;
}
2016-04-29 23:37:35 +00:00
return credentials.username.split('+')[0];
};
$scope.getMesosFilename = function(credentials) {
return $scope.getSuffixedFilename(credentials, 'auth.tar.gz');
};
$scope.getMesosFile = function(credentials) {
var tarFile = new Tar();
tarFile.append('.docker/config.json', $scope.getDockerConfig(credentials), {});
contents = (new Zlib.Gzip(tarFile.getData())).compress();
return {
'filename': $scope.getMesosFilename(credentials),
'contents': contents
}
};
$scope.getDockerConfig = function(credentials) {
var auths = {};
auths[Config['SERVER_HOSTNAME']] = {
2016-05-16 20:30:56 +00:00
'auth': $.base64.encode(credentials.username + ":" + credentials.password),
2016-04-29 23:37:35 +00:00
'email': ''
};
var config = {
'auths': auths
};
return JSON.stringify(config, null, ' ');
};
$scope.getDockerFile = function(credentials) {
return {
'filename': $scope.getRktFilename(credentials),
'contents': $scope.getDockerConfig(credentials)
}
};
$scope.getDockerLogin = function(credentials) {
if (!credentials || !credentials.username) {
return '';
}
var escape = function(v) {
if (!v) { return v; }
return v.replace('$', '\\$');
};
return 'docker login -u="' + escape(credentials.username) + '" -p="' + credentials.password + '" ' + Config['SERVER_HOSTNAME'];
2016-04-29 23:37:35 +00:00
};
$scope.getDockerFilename = function(credentials) {
return $scope.getSuffixedFilename(credentials, 'auth.json')
};
$scope.getRktFile = function(credentials) {
var config = {
'rktKind': 'auth',
'rktVersion': 'v1',
'domains': [Config['SERVER_HOSTNAME']],
'type': 'basic',
'credentials': {
'user': credentials['username'],
'password': credentials['password']
}
};
var contents = JSON.stringify(config, null, ' ');
return {
'filename': $scope.getRktFilename(credentials),
'contents': contents
}
};
$scope.getRktFilename = function(credentials) {
return $scope.getSuffixedFilename(credentials, 'auth.json')
};
$scope.getKubernetesSecretName = function(credentials) {
if (!credentials || !credentials.username) {
return '';
}
2017-05-15 15:10:21 +00:00
return $scope.getEscapedUsername(credentials).toLowerCase() + '-pull-secret';
2016-04-29 23:37:35 +00:00
};
$scope.getKubernetesFile = function(credentials) {
var dockerConfigJson = $scope.getDockerConfig(credentials);
var contents = 'apiVersion: v1\n' +
'kind: Secret\n' +
'metadata:\n' +
' name: ' + $scope.getKubernetesSecretName(credentials) + '\n' +
'data:\n' +
' .dockerconfigjson: ' + $.base64.encode(dockerConfigJson) + '\n' +
'type: kubernetes.io/dockerconfigjson'
return {
'filename': $scope.getKubernetesFilename(credentials),
'contents': contents
}
};
$scope.getKubernetesFilename = function(credentials) {
return $scope.getSuffixedFilename(credentials, 'secret.yml')
};
$scope.getEscapedUsername = function(credentials) {
return credentials.username.replace(/[^a-zA-Z0-9]/g, '-');
};
2016-04-29 23:37:35 +00:00
$scope.getSuffixedFilename = function(credentials, suffix) {
if (!credentials || !credentials.username) {
return '';
}
return $scope.getEscapedUsername(credentials) + '-' + suffix;
2016-04-29 23:37:35 +00:00
};
}
};
return directiveDefinitionObject;
});