blob: ad9a0600aaa8fbe685fcc7c31373d92486916d04 [file] [log] [blame]
beccabroek309b5da2018-11-07 12:22:31 -06001/**
2 * Controller for Certificate Management
3 *
miramurali23afc8a792019-06-17 13:07:24 -05004 * @module app/access-control
beccabroek309b5da2018-11-07 12:22:31 -06005 * @exports certificateController
6 * @name certificateController
7 */
8
9window.angular && (function(angular) {
10 'use strict';
11
Dixsie Wolmersc15f66b2019-09-11 15:26:38 -050012 angular.module('app.configuration').controller('certificateController', [
13 '$scope', 'APIUtils', '$q', 'Constants', 'toastService', '$timeout',
14 '$uibModal',
15 function(
16 $scope, APIUtils, $q, Constants, toastService, $timeout, $uibModal) {
beccabroek309b5da2018-11-07 12:22:31 -060017 $scope.loading = false;
18 $scope.certificates = [];
19 $scope.availableCertificateTypes = [];
miramurali235e8785d2019-06-10 15:09:27 -050020 $scope.allCertificateTypes = Constants.CERTIFICATE_TYPES;
beccabroek309b5da2018-11-07 12:22:31 -060021 $scope.newCertificate = {};
miramurali235e8785d2019-06-10 15:09:27 -050022 $scope.newCSR = {};
miramurali235e8785d2019-06-10 15:09:27 -050023 $scope.keyBitLength = Constants.CERTIFICATE.KEY_BIT_LENGTH;
24 $scope.keyPairAlgorithm = Constants.CERTIFICATE.KEY_PAIR_ALGORITHM;
25 $scope.keyCurveId = Constants.CERTIFICATE.KEY_CURVE_ID;
26 $scope.countryList = Constants.COUNTRIES;
27
miramurali23219738d2019-08-15 16:22:06 -040028 $scope.$on('$viewContentLoaded', () => {
29 getBmcTime();
30 })
31
beccabroek309b5da2018-11-07 12:22:31 -060032 $scope.loadCertificates = function() {
33 $scope.certificates = [];
34 $scope.availableCertificateTypes = Constants.CERTIFICATE_TYPES;
35 $scope.loading = true;
36 // Use Certificate Service to get the locations of all the certificates,
37 // then add a promise for fetching each certificate
38 APIUtils.getCertificateLocations().then(
39 function(data) {
40 var promises = [];
41 var locations = data.Links.Certificates;
42 for (var i in locations) {
43 var location = locations[i];
44 promises.push(getCertificatePromise(location['@odata.id']));
45 }
46 $q.all(promises)
47 .catch(function(error) {
48 toastService.error('Failed to load certificates.');
49 console.log(JSON.stringify(error));
50 })
51 .finally(function() {
52 $scope.loading = false;
53 });
54 },
55 function(error) {
56 $scope.loading = false;
57 $scope.availableCertificateTypes = [];
58 toastService.error('Failed to load certificates.');
59 console.log(JSON.stringify(error));
60 });
61 };
62
63 $scope.uploadCertificate = function() {
64 if ($scope.newCertificate.file.name.split('.').pop() !== 'pem') {
65 toastService.error('Certificate must be a .pem file.');
66 return;
67 }
beccabroek309b5da2018-11-07 12:22:31 -060068 APIUtils
69 .addNewCertificate(
70 $scope.newCertificate.file, $scope.newCertificate.selectedType)
71 .then(
72 function(data) {
73 toastService.success(
Yoshie Muranakadbafdc52019-06-28 09:41:19 -050074 $scope.newCertificate.selectedType.name +
beccabroek309b5da2018-11-07 12:22:31 -060075 ' was uploaded.');
76 $scope.newCertificate = {};
77 $scope.loadCertificates();
78 },
79 function(error) {
80 toastService.error(
Yoshie Muranakadbafdc52019-06-28 09:41:19 -050081 $scope.newCertificate.selectedType.name +
beccabroek309b5da2018-11-07 12:22:31 -060082 ' failed upload.');
83 console.log(JSON.stringify(error));
84 });
85 };
86
87 var getCertificatePromise = function(url) {
88 var promise = APIUtils.getCertificate(url).then(function(data) {
89 var certificate = data;
90 isExpiring(certificate);
91 updateAvailableTypes(certificate);
92 $scope.certificates.push(certificate);
93 });
94 return promise;
95 };
96
97 var isExpiring = function(certificate) {
miramurali23219738d2019-08-15 16:22:06 -040098 // convert certificate time to epoch time
99 // if ValidNotAfter is less than or equal to 30 days from bmc time
beccabroek309b5da2018-11-07 12:22:31 -0600100 // (2592000000), isExpiring. If less than or equal to 0, is expired.
miramurali23219738d2019-08-15 16:22:06 -0400101 // dividing bmc time by 1000 converts epoch milliseconds to seconds
102 var difference = (new Date(certificate.ValidNotAfter).getTime()) -
103 ($scope.bmcTime) / 1000;
beccabroek309b5da2018-11-07 12:22:31 -0600104 if (difference <= 0) {
105 certificate.isExpired = true;
106 } else if (difference <= 2592000000) {
107 certificate.isExpiring = true;
108 } else {
109 certificate.isExpired = false;
110 certificate.isExpiring = false;
111 }
112 };
113
miramurali235e8785d2019-06-10 15:09:27 -0500114 // add optional name
115 $scope.names = [];
116 $scope.addOptionalRow = function() {
117 $scope.names.push({Value: ''})
118 };
119
120 // remove optional name row
121 $scope.deleteOptionalRow = function(index) {
122 $scope.names.splice(index, 1);
123 if ($scope.names.length == 0) {
124 $scope.names = [];
125 }
126 };
127
miramurali235e8785d2019-06-10 15:09:27 -0500128 // create a CSR object to send to the backend
129 $scope.getCSRCode = function() {
130 var addCSR = {};
131 let alternativeNames = $scope.names.map(name => name.Value);
132
133 // if user provided a first alternative name then push to alternative
134 // names array
135 $scope.newCSR.firstAlternativeName ?
136 alternativeNames.push($scope.newCSR.firstAlternativeName) :
137 $scope.newCSR.firstAlternativeName = '';
138
miramurali235e8785d2019-06-10 15:09:27 -0500139 addCSR.CertificateCollection = {
140 '@odata.id': $scope.newCSR.certificateCollection.location
141 };
142 addCSR.CommonName = $scope.newCSR.commonName;
143 addCSR.ContactPerson = $scope.newCSR.contactPerson || '';
144 addCSR.City = $scope.newCSR.city;
145 addCSR.AlternativeNames = alternativeNames || [];
146 addCSR.ChallengePassword = $scope.newCSR.challengePassword || '';
147 addCSR.Email = $scope.newCSR.emailAddress || '';
148 addCSR.Country = $scope.newCSR.countryCode.code;
149 addCSR.Organization = $scope.newCSR.organization;
150 addCSR.OrganizationalUnit = $scope.newCSR.companyUnit;
151 addCSR.KeyCurveId = $scope.newCSR.keyCurveId || '';
152 addCSR.KeyBitLength = $scope.newCSR.keyBitLength
153 addCSR.KeyPairAlgorithm = $scope.newCSR.keyPairAlgorithm || '';
154 addCSR.State = $scope.newCSR.state;
155
156 APIUtils.createCSRCertificate(addCSR).then(
157 function(data) {
miramurali235e8785d2019-06-10 15:09:27 -0500158 $scope.csrCode = data;
Dixsie Wolmersc15f66b2019-09-11 15:26:38 -0500159 openDownloadCsrModal();
miramurali235e8785d2019-06-10 15:09:27 -0500160 },
161 function(error) {
miramurali235e8785d2019-06-10 15:09:27 -0500162 toastService.error('Unable to generate CSR. Try again.');
163 console.log(JSON.stringify(error));
164 })
165 };
166
Dixsie Wolmersc15f66b2019-09-11 15:26:38 -0500167 function openDownloadCsrModal() {
168 const modalTemplateCsrDownload =
169 require('./certificate-modal-csr-download.html');
170 $uibModal
171 .open({
172 template: modalTemplateCsrDownload,
173 windowTopClass: 'uib-modal',
174 scope: $scope,
175 ariaLabelledBy: 'modal_label',
176 size: 'lg',
177 })
178 .result.catch(function() {
179 resetCSRModal();
180 });
181 };
182
183 $scope.addCertModal = function() {
184 openAddCertModal();
185 };
186
187 function openAddCertModal() {
188 const modalTemplateAddCert =
189 require('./certificate-modal-add-cert.html');
190 $uibModal
191 .open({
192 template: modalTemplateAddCert,
193 windowTopClass: 'uib-modal',
194 scope: $scope,
195 ariaLabelledBy: 'modal_label',
196 })
197 .result.catch(function() {
198 // do nothing
199 });
200 };
201
202 $scope.addCsrModal = function() {
203 openCsrModal();
204 };
205
206 function openCsrModal() {
207 const modalTemplateCsrGen = require('./certificate-modal-csr-gen.html');
208 $uibModal
209 .open({
210 template: modalTemplateCsrGen,
211 windowTopClass: 'uib-modal',
212 scope: $scope,
213 ariaLabelledBy: 'modal_label',
214 size: 'lg',
215 })
216 .result.catch(function() {
217 resetCSRModal();
218 });
219 };
220
miramurali235e8785d2019-06-10 15:09:27 -0500221 // resetting the modal when user clicks cancel/closes the
222 // modal
Dixsie Wolmersc15f66b2019-09-11 15:26:38 -0500223 const resetCSRModal = function() {
miramurali235e8785d2019-06-10 15:09:27 -0500224 $scope.newCSR.certificateCollection = $scope.selectOption;
225 $scope.newCSR.commonName = '';
226 $scope.newCSR.contactPerson = '';
227 $scope.newCSR.city = '';
228 $scope.names = [];
229 $scope.newCSR.challengePassword = '';
230 $scope.newCSR.emailAddress = '';
231 $scope.newCSR.countryCode = '';
232 $scope.newCSR.keyCurveId = '';
233 $scope.newCSR.firstAlternativeName = '';
234 $scope.newCSR.keyBitLength = $scope.selectOption;
235 $scope.newCSR.keyPairAlgorithm = $scope.selectOption;
236 $scope.newCSR.organization = '';
237 $scope.newCSR.companyUnit = '';
238 $scope.newCSR.state = '';
239 };
240
241 // copies the CSR code
242 $scope.copySuccess = function(event) {
243 $scope.copied = true;
244 $timeout(function() {
245 $scope.copied = false;
246 }, 5000);
247 };
248 $scope.copyFailed = function(err) {
249 console.log(JSON.stringify(err));
250 };
251
252
miramurali23219738d2019-08-15 16:22:06 -0400253 var getBmcTime = function() {
254 APIUtils.getBMCTime().then(function(data) {
255 $scope.bmcTime = data.data.Elapsed;
256 });
257
258 return $scope.bmcTime;
259 };
260
beccabroek309b5da2018-11-07 12:22:31 -0600261 var updateAvailableTypes = function(certificate) {
beccabroek309b5da2018-11-07 12:22:31 -0600262 $scope.availableCertificateTypes =
263 $scope.availableCertificateTypes.filter(function(type) {
Zbigniew Kurzynskibb3714e2019-07-18 14:03:47 +0200264 if (type.Description == 'TrustStore Certificate') {
265 return true;
266 }
beccabroek309b5da2018-11-07 12:22:31 -0600267 return type.Description !== certificate.Description;
268 });
269 };
270
271 $scope.getDays = function(endDate) {
272 // finds number of days until certificate expiration
miramurali23219738d2019-08-15 16:22:06 -0400273 // dividing bmc time by 1000 converts milliseconds to seconds
274 var ms = (new Date(endDate).getTime()) - ($scope.bmcTime) / 1000;
beccabroek309b5da2018-11-07 12:22:31 -0600275 return Math.floor(ms / (24 * 60 * 60 * 1000));
276 };
277
278 $scope.loadCertificates();
279 }
280 ]);
281})(angular);