2019-03-21 05:46:49 +00:00
|
|
|
import { AccessControlFormData } from '../../../../portainer/components/accessControlForm/porAccessControlFormModel';
|
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
angular.module('portainer.docker').controller('CreateSecretController', [
|
|
|
|
'$scope',
|
|
|
|
'$state',
|
|
|
|
'Notifications',
|
|
|
|
'SecretService',
|
|
|
|
'LabelHelper',
|
|
|
|
'Authentication',
|
|
|
|
'ResourceControlService',
|
|
|
|
'FormValidator',
|
|
|
|
function ($scope, $state, Notifications, SecretService, LabelHelper, Authentication, ResourceControlService, FormValidator) {
|
|
|
|
$scope.formValues = {
|
|
|
|
Name: '',
|
|
|
|
Data: '',
|
|
|
|
Labels: [],
|
|
|
|
encodeSecret: true,
|
|
|
|
AccessControlData: new AccessControlFormData(),
|
|
|
|
};
|
2017-09-19 15:10:15 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
$scope.state = {
|
|
|
|
formValidationError: '',
|
|
|
|
actionInProgress: false,
|
|
|
|
};
|
2017-09-19 15:10:15 +00:00
|
|
|
|
2022-07-20 00:39:44 +00:00
|
|
|
$scope.handleEncodeSecretChange = handleEncodeSecretChange;
|
|
|
|
|
|
|
|
function handleEncodeSecretChange(checked) {
|
|
|
|
return $scope.$evalAsync(() => {
|
|
|
|
$scope.formValues.encodeSecret = checked;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
$scope.addLabel = function () {
|
|
|
|
$scope.formValues.Labels.push({ key: '', value: '' });
|
|
|
|
};
|
2017-05-27 07:23:49 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
$scope.removeLabel = function (index) {
|
|
|
|
$scope.formValues.Labels.splice(index, 1);
|
|
|
|
};
|
2017-05-27 07:23:49 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
function prepareLabelsConfig(config) {
|
|
|
|
config.Labels = LabelHelper.fromKeyValueToLabelHash($scope.formValues.Labels);
|
|
|
|
}
|
2017-05-27 07:23:49 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
function prepareSecretData(config) {
|
|
|
|
if ($scope.formValues.encodeSecret) {
|
|
|
|
config.Data = btoa(unescape(encodeURIComponent($scope.formValues.Data)));
|
|
|
|
} else {
|
|
|
|
config.Data = $scope.formValues.Data;
|
|
|
|
}
|
2017-05-27 07:23:49 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
function prepareConfiguration() {
|
|
|
|
var config = {};
|
|
|
|
config.Name = $scope.formValues.Name;
|
|
|
|
prepareSecretData(config);
|
|
|
|
prepareLabelsConfig(config);
|
|
|
|
return config;
|
|
|
|
}
|
2017-05-27 07:23:49 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
function validateForm(accessControlData, isAdmin) {
|
|
|
|
$scope.state.formValidationError = '';
|
|
|
|
var error = '';
|
|
|
|
error = FormValidator.validateAccessControl(accessControlData, isAdmin);
|
2017-09-19 15:10:15 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
if (error) {
|
|
|
|
$scope.state.formValidationError = error;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2017-09-19 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
$scope.create = function () {
|
|
|
|
const accessControlData = $scope.formValues.AccessControlData;
|
|
|
|
const userDetails = Authentication.getUserDetails();
|
|
|
|
const isAdmin = Authentication.isAdmin();
|
2017-09-19 15:10:15 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
if (!validateForm(accessControlData, isAdmin)) {
|
|
|
|
return;
|
|
|
|
}
|
2017-09-19 15:10:15 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
$scope.state.actionInProgress = true;
|
|
|
|
var secretConfiguration = prepareConfiguration();
|
|
|
|
SecretService.create(secretConfiguration)
|
|
|
|
.then(function success(data) {
|
|
|
|
const userId = userDetails.ID;
|
|
|
|
const resourceControl = data.Portainer.ResourceControl;
|
|
|
|
return ResourceControlService.applyResourceControl(userId, accessControlData, resourceControl);
|
|
|
|
})
|
|
|
|
.then(function success() {
|
2022-08-10 05:07:35 +00:00
|
|
|
Notifications.success('Success', 'Secret successfully created');
|
2020-04-10 21:54:53 +00:00
|
|
|
$state.go('docker.secrets', {}, { reload: true });
|
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
Notifications.error('Failure', err, 'Unable to create secret');
|
|
|
|
})
|
|
|
|
.finally(function final() {
|
|
|
|
$scope.state.actionInProgress = false;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
},
|
|
|
|
]);
|