blob: a55b66ccdba3206aa6a719f7b11ea84c21090603 [file] [log] [blame]
Yoshie Muranaka37393812020-03-24 15:25:24 -07001<template>
Yoshie Muranaka3111b6f2020-04-21 19:48:38 -07002 <b-container fluid="xl">
Yoshie Muranaka37393812020-03-24 15:25:24 -07003 <page-title />
4 <b-row>
Yoshie Muranaka3111b6f2020-04-21 19:48:38 -07005 <b-col xl="11">
Yoshie Muranakae45f54b2020-03-26 15:23:34 -07006 <!-- Expired certificates banner -->
7 <alert :show="expiredCertificateTypes.length > 0" variant="danger">
8 <template v-if="expiredCertificateTypes.length > 1">
Sandeepa Singhb4406162021-07-26 15:05:39 +05309 {{ $t('pageCertificates.alert.certificatesExpiredMessage') }}
Yoshie Muranakae45f54b2020-03-26 15:23:34 -070010 </template>
11 <template v-else>
12 {{
Sandeepa Singhb4406162021-07-26 15:05:39 +053013 $t('pageCertificates.alert.certificateExpiredMessage', {
Derick Montague602e98a2020-10-21 16:20:00 -050014 certificate: expiredCertificateTypes[0],
Yoshie Muranakae45f54b2020-03-26 15:23:34 -070015 })
16 }}
17 </template>
18 </alert>
19 <!-- Expiring certificates banner -->
20 <alert :show="expiringCertificateTypes.length > 0" variant="warning">
21 <template v-if="expiringCertificateTypes.length > 1">
Sandeepa Singhb4406162021-07-26 15:05:39 +053022 {{ $t('pageCertificates.alert.certificatesExpiringMessage') }}
Yoshie Muranakae45f54b2020-03-26 15:23:34 -070023 </template>
24 <template v-else>
25 {{
Sandeepa Singhb4406162021-07-26 15:05:39 +053026 $t('pageCertificates.alert.certificateExpiringMessage', {
Derick Montague602e98a2020-10-21 16:20:00 -050027 certificate: expiringCertificateTypes[0],
Yoshie Muranakae45f54b2020-03-26 15:23:34 -070028 })
29 }}
30 </template>
31 </alert>
32 </b-col>
33 </b-row>
34 <b-row>
Yoshie Muranaka3111b6f2020-04-21 19:48:38 -070035 <b-col xl="11" class="text-right">
SurenNewared0df7d22020-07-22 16:41:20 +053036 <b-button
37 v-b-modal.generate-csr
Sandeepa Singhb4406162021-07-26 15:05:39 +053038 data-test-id="certificates-button-generateCsr"
SurenNewared0df7d22020-07-22 16:41:20 +053039 variant="link"
40 >
Yoshie Muranaka532a4b02020-03-27 11:00:50 -070041 <icon-add />
Sandeepa Singhb4406162021-07-26 15:05:39 +053042 {{ $t('pageCertificates.generateCsr') }}
Yoshie Muranaka532a4b02020-03-27 11:00:50 -070043 </b-button>
Yoshie Muranaka37393812020-03-24 15:25:24 -070044 <b-button
45 variant="primary"
46 :disabled="certificatesForUpload.length === 0"
47 @click="initModalUploadCertificate(null)"
48 >
49 <icon-add />
Sandeepa Singhb4406162021-07-26 15:05:39 +053050 {{ $t('pageCertificates.addNewCertificate') }}
Yoshie Muranaka37393812020-03-24 15:25:24 -070051 </b-button>
52 </b-col>
53 </b-row>
54 <b-row>
Yoshie Muranaka3111b6f2020-04-21 19:48:38 -070055 <b-col xl="11">
SurenNeware307382e2020-07-27 20:45:14 +053056 <b-table
57 responsive="md"
58 show-empty
Sukanya Pandeyfde429e2020-09-14 20:48:39 +053059 hover
Kenneth Fullbright41057852021-12-27 16:19:37 -060060 :busy="isBusy"
SurenNeware307382e2020-07-27 20:45:14 +053061 :fields="fields"
62 :items="tableItems"
63 :empty-text="$t('global.table.emptyMessage')"
64 >
Derick Montague602e98a2020-10-21 16:20:00 -050065 <template #cell(validFrom)="{ value }">
Surya Vde23ea22024-07-11 15:19:46 +053066 {{ $filters.formatDate(value) }}
Yoshie Muranaka37393812020-03-24 15:25:24 -070067 </template>
68
Derick Montague602e98a2020-10-21 16:20:00 -050069 <template #cell(validUntil)="{ value }">
Yoshie Muranakae45f54b2020-03-26 15:23:34 -070070 <status-icon
71 v-if="getDaysUntilExpired(value) < 31"
72 :status="getIconStatus(value)"
73 />
Surya Vde23ea22024-07-11 15:19:46 +053074 {{ $filters.formatDate(value) }}
Yoshie Muranaka37393812020-03-24 15:25:24 -070075 </template>
76
Derick Montague602e98a2020-10-21 16:20:00 -050077 <template #cell(actions)="{ value, item }">
Yoshie Muranaka37393812020-03-24 15:25:24 -070078 <table-row-action
79 v-for="(action, index) in value"
80 :key="index"
81 :value="action.value"
82 :title="action.title"
83 :enabled="action.enabled"
Sukanya Pandeyedb8a772020-10-29 11:33:42 +053084 @click-table-action="onTableRowAction($event, item)"
Yoshie Muranaka37393812020-03-24 15:25:24 -070085 >
Derick Montague602e98a2020-10-21 16:20:00 -050086 <template #icon>
Yoshie Muranaka37393812020-03-24 15:25:24 -070087 <icon-replace v-if="action.value === 'replace'" />
88 <icon-trashcan v-if="action.value === 'delete'" />
89 </template>
90 </table-row-action>
91 </template>
92 </b-table>
93 </b-col>
94 </b-row>
95
96 <!-- Modals -->
97 <modal-upload-certificate :certificate="modalCertificate" @ok="onModalOk" />
Yoshie Muranaka532a4b02020-03-27 11:00:50 -070098 <modal-generate-csr />
Yoshie Muranaka37393812020-03-24 15:25:24 -070099 </b-container>
100</template>
101
102<script>
103import IconAdd from '@carbon/icons-vue/es/add--alt/20';
104import IconReplace from '@carbon/icons-vue/es/renew/20';
105import IconTrashcan from '@carbon/icons-vue/es/trash-can/20';
106
Yoshie Muranaka532a4b02020-03-27 11:00:50 -0700107import ModalGenerateCsr from './ModalGenerateCsr';
Yoshie Muranaka37393812020-03-24 15:25:24 -0700108import ModalUploadCertificate from './ModalUploadCertificate';
SurenNeware5e25e282020-07-08 15:57:23 +0530109import PageTitle from '@/components/Global/PageTitle';
110import TableRowAction from '@/components/Global/TableRowAction';
111import StatusIcon from '@/components/Global/StatusIcon';
112import Alert from '@/components/Global/Alert';
Yoshie Muranaka37393812020-03-24 15:25:24 -0700113
SurenNeware5e25e282020-07-08 15:57:23 +0530114import BVToastMixin from '@/components/Mixins/BVToastMixin';
Yoshie Muranakae5be9ba2020-04-30 10:13:40 -0700115import LoadingBarMixin from '@/components/Mixins/LoadingBarMixin';
Surya Vde23ea22024-07-11 15:19:46 +0530116import { useI18n } from 'vue-i18n';
117import i18n from '@/i18n';
Yoshie Muranaka37393812020-03-24 15:25:24 -0700118
119export default {
Sandeepa Singhb4406162021-07-26 15:05:39 +0530120 name: 'Certificates',
Yoshie Muranaka37393812020-03-24 15:25:24 -0700121 components: {
Yoshie Muranakae45f54b2020-03-26 15:23:34 -0700122 Alert,
Yoshie Muranaka37393812020-03-24 15:25:24 -0700123 IconAdd,
124 IconReplace,
125 IconTrashcan,
Yoshie Muranaka532a4b02020-03-27 11:00:50 -0700126 ModalGenerateCsr,
Yoshie Muranaka37393812020-03-24 15:25:24 -0700127 ModalUploadCertificate,
128 PageTitle,
Yoshie Muranakae45f54b2020-03-26 15:23:34 -0700129 StatusIcon,
Derick Montague602e98a2020-10-21 16:20:00 -0500130 TableRowAction,
Yoshie Muranaka37393812020-03-24 15:25:24 -0700131 },
Yoshie Muranakae5be9ba2020-04-30 10:13:40 -0700132 mixins: [BVToastMixin, LoadingBarMixin],
Derick Montague602e98a2020-10-21 16:20:00 -0500133 beforeRouteLeave(to, from, next) {
134 this.hideLoader();
135 next();
136 },
Yoshie Muranaka37393812020-03-24 15:25:24 -0700137 data() {
138 return {
Surya Vde23ea22024-07-11 15:19:46 +0530139 $t: useI18n().t,
Kenneth Fullbright41057852021-12-27 16:19:37 -0600140 isBusy: true,
Yoshie Muranaka37393812020-03-24 15:25:24 -0700141 modalCertificate: null,
Damian Celico31fb2b92022-06-27 16:43:21 +0200142 fileTypeCorrect: undefined,
Yoshie Muranaka37393812020-03-24 15:25:24 -0700143 fields: [
144 {
145 key: 'certificate',
Surya Vde23ea22024-07-11 15:19:46 +0530146 label: i18n.global.t('pageCertificates.table.certificate'),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700147 },
148 {
149 key: 'issuedBy',
Surya Vde23ea22024-07-11 15:19:46 +0530150 label: i18n.global.t('pageCertificates.table.issuedBy'),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700151 },
152 {
153 key: 'issuedTo',
Surya Vde23ea22024-07-11 15:19:46 +0530154 label: i18n.global.t('pageCertificates.table.issuedTo'),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700155 },
156 {
157 key: 'validFrom',
Surya Vde23ea22024-07-11 15:19:46 +0530158 label: i18n.global.t('pageCertificates.table.validFrom'),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700159 },
160 {
161 key: 'validUntil',
Surya Vde23ea22024-07-11 15:19:46 +0530162 label: i18n.global.t('pageCertificates.table.validUntil'),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700163 },
164 {
165 key: 'actions',
166 label: '',
Derick Montague602e98a2020-10-21 16:20:00 -0500167 tdClass: 'text-right text-nowrap',
168 },
169 ],
Yoshie Muranaka37393812020-03-24 15:25:24 -0700170 };
171 },
172 computed: {
173 certificates() {
Sandeepa Singhb4406162021-07-26 15:05:39 +0530174 return this.$store.getters['certificates/allCertificates'];
Yoshie Muranaka37393812020-03-24 15:25:24 -0700175 },
176 tableItems() {
Derick Montague602e98a2020-10-21 16:20:00 -0500177 return this.certificates.map((certificate) => {
Yoshie Muranaka37393812020-03-24 15:25:24 -0700178 return {
179 ...certificate,
180 actions: [
181 {
182 value: 'replace',
Surya Vde23ea22024-07-11 15:19:46 +0530183 title: i18n.global.t('pageCertificates.replaceCertificate'),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700184 },
185 {
186 value: 'delete',
Surya Vde23ea22024-07-11 15:19:46 +0530187 title: i18n.global.t('pageCertificates.deleteCertificate'),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700188 enabled:
Derick Montague602e98a2020-10-21 16:20:00 -0500189 certificate.type === 'TrustStore Certificate' ? true : false,
190 },
191 ],
Yoshie Muranaka37393812020-03-24 15:25:24 -0700192 };
193 });
194 },
195 certificatesForUpload() {
Sandeepa Singhb4406162021-07-26 15:05:39 +0530196 return this.$store.getters['certificates/availableUploadTypes'];
Yoshie Muranakae45f54b2020-03-26 15:23:34 -0700197 },
198 bmcTime() {
199 return this.$store.getters['global/bmcTime'];
200 },
201 expiredCertificateTypes() {
202 return this.certificates.reduce((acc, val) => {
203 const daysUntilExpired = this.getDaysUntilExpired(val.validUntil);
204 if (daysUntilExpired < 1) {
205 acc.push(val.certificate);
206 }
207 return acc;
208 }, []);
209 },
210 expiringCertificateTypes() {
211 return this.certificates.reduce((acc, val) => {
212 const daysUntilExpired = this.getDaysUntilExpired(val.validUntil);
213 if (daysUntilExpired < 31 && daysUntilExpired > 0) {
214 acc.push(val.certificate);
215 }
216 return acc;
217 }, []);
Derick Montague602e98a2020-10-21 16:20:00 -0500218 },
Yoshie Muranaka37393812020-03-24 15:25:24 -0700219 },
Yoshie Muranakae5be9ba2020-04-30 10:13:40 -0700220 async created() {
221 this.startLoader();
222 await this.$store.dispatch('global/getBmcTime');
Kenneth Fullbright41057852021-12-27 16:19:37 -0600223 this.$store.dispatch('certificates/getCertificates').finally(() => {
224 this.endLoader();
225 this.isBusy = false;
226 });
Yoshie Muranakae5be9ba2020-04-30 10:13:40 -0700227 },
Yoshie Muranaka37393812020-03-24 15:25:24 -0700228 methods: {
229 onTableRowAction(event, rowItem) {
230 switch (event) {
231 case 'replace':
232 this.initModalUploadCertificate(rowItem);
233 break;
234 case 'delete':
235 this.initModalDeleteCertificate(rowItem);
236 break;
237 default:
238 break;
239 }
240 },
241 initModalUploadCertificate(certificate = null) {
242 this.modalCertificate = certificate;
243 this.$bvModal.show('upload-certificate');
244 },
245 initModalDeleteCertificate(certificate) {
246 this.$bvModal
247 .msgBoxConfirm(
Surya Vde23ea22024-07-11 15:19:46 +0530248 i18n.global.t('pageCertificates.modal.deleteConfirmMessage', {
Yoshie Muranaka37393812020-03-24 15:25:24 -0700249 issuedBy: certificate.issuedBy,
Derick Montague602e98a2020-10-21 16:20:00 -0500250 certificate: certificate.certificate,
Yoshie Muranaka37393812020-03-24 15:25:24 -0700251 }),
252 {
Surya Vde23ea22024-07-11 15:19:46 +0530253 title: i18n.global.t('pageCertificates.deleteCertificate'),
254 okTitle: i18n.global.t('global.action.delete'),
255 cancelTitle: i18n.global.t('global.action.cancel'),
Paul Fertserd1ef18e2024-04-06 08:04:14 +0000256 autoFocusButton: 'ok',
Ed Tanous81323992024-02-27 11:26:24 -0800257 },
Yoshie Muranaka37393812020-03-24 15:25:24 -0700258 )
Derick Montague602e98a2020-10-21 16:20:00 -0500259 .then((deleteConfirmed) => {
Yoshie Muranaka37393812020-03-24 15:25:24 -0700260 if (deleteConfirmed) this.deleteCertificate(certificate);
261 });
262 },
263 onModalOk({ addNew, file, type, location }) {
264 if (addNew) {
265 // Upload a new certificate
Damian Celico31fb2b92022-06-27 16:43:21 +0200266 this.fileTypeCorrect = this.getIsFileTypeCorrect(file);
267 if (this.fileTypeCorrect) {
268 this.addNewCertificate(file, type);
Damian Celico713dae02022-11-14 23:06:55 +0100269 } else {
270 this.errorToast(
Surya Vde23ea22024-07-11 15:19:46 +0530271 i18n.global.t(
272 'pageCertificates.alert.incorrectCertificateFileType',
273 ),
Damian Celico713dae02022-11-14 23:06:55 +0100274 {
Surya Vde23ea22024-07-11 15:19:46 +0530275 title: i18n.global.t(
276 'pageCertificates.toast.errorAddCertificate',
277 ),
Ed Tanous81323992024-02-27 11:26:24 -0800278 },
Damian Celico713dae02022-11-14 23:06:55 +0100279 );
Damian Celico31fb2b92022-06-27 16:43:21 +0200280 }
Yoshie Muranaka37393812020-03-24 15:25:24 -0700281 } else {
282 // Replace an existing certificate
283 this.replaceCertificate(file, type, location);
284 }
285 },
286 addNewCertificate(file, type) {
Damian Celico31fb2b92022-06-27 16:43:21 +0200287 if (this.fileTypeCorrect === true) {
288 this.startLoader();
289 this.$store
290 .dispatch('certificates/addNewCertificate', { file, type })
291 .then((success) => this.successToast(success))
292 .catch(({ message }) => this.errorToast(message))
293 .finally(() => this.endLoader());
294 }
Yoshie Muranaka37393812020-03-24 15:25:24 -0700295 },
296 replaceCertificate(file, type, location) {
Yoshie Muranakae5be9ba2020-04-30 10:13:40 -0700297 this.startLoader();
Yoshie Muranaka37393812020-03-24 15:25:24 -0700298 const reader = new FileReader();
299 reader.readAsBinaryString(file);
Derick Montague602e98a2020-10-21 16:20:00 -0500300 reader.onloadend = (event) => {
Yoshie Muranaka37393812020-03-24 15:25:24 -0700301 const certificateString = event.target.result;
302 this.$store
Sandeepa Singhb4406162021-07-26 15:05:39 +0530303 .dispatch('certificates/replaceCertificate', {
Yoshie Muranaka37393812020-03-24 15:25:24 -0700304 certificateString,
305 type,
Derick Montague602e98a2020-10-21 16:20:00 -0500306 location,
Yoshie Muranaka37393812020-03-24 15:25:24 -0700307 })
Derick Montague602e98a2020-10-21 16:20:00 -0500308 .then((success) => this.successToast(success))
Yoshie Muranakae5be9ba2020-04-30 10:13:40 -0700309 .catch(({ message }) => this.errorToast(message))
310 .finally(() => this.endLoader());
Yoshie Muranaka37393812020-03-24 15:25:24 -0700311 };
312 },
313 deleteCertificate({ type, location }) {
Yoshie Muranakae5be9ba2020-04-30 10:13:40 -0700314 this.startLoader();
Yoshie Muranaka37393812020-03-24 15:25:24 -0700315 this.$store
Sandeepa Singhb4406162021-07-26 15:05:39 +0530316 .dispatch('certificates/deleteCertificate', {
Yoshie Muranaka37393812020-03-24 15:25:24 -0700317 type,
Derick Montague602e98a2020-10-21 16:20:00 -0500318 location,
Yoshie Muranaka37393812020-03-24 15:25:24 -0700319 })
Derick Montague602e98a2020-10-21 16:20:00 -0500320 .then((success) => this.successToast(success))
Yoshie Muranakae5be9ba2020-04-30 10:13:40 -0700321 .catch(({ message }) => this.errorToast(message))
322 .finally(() => this.endLoader());
Yoshie Muranakae45f54b2020-03-26 15:23:34 -0700323 },
324 getDaysUntilExpired(date) {
325 if (this.bmcTime) {
326 const validUntilMs = date.getTime();
327 const currentBmcTimeMs = this.bmcTime.getTime();
328 const oneDayInMs = 24 * 60 * 60 * 1000;
329 return Math.round((validUntilMs - currentBmcTimeMs) / oneDayInMs);
330 }
Yoshie Muranakae5be9ba2020-04-30 10:13:40 -0700331 return new Date();
Yoshie Muranakae45f54b2020-03-26 15:23:34 -0700332 },
333 getIconStatus(date) {
334 const daysUntilExpired = this.getDaysUntilExpired(date);
335 if (daysUntilExpired < 1) {
336 return 'danger';
337 } else if (daysUntilExpired < 31) {
338 return 'warning';
339 }
Derick Montague602e98a2020-10-21 16:20:00 -0500340 },
Damian Celico31fb2b92022-06-27 16:43:21 +0200341 getIsFileTypeCorrect(file) {
342 const fileTypeExtension = file.name.split('.').pop();
343 return fileTypeExtension === 'pem';
344 },
Derick Montague602e98a2020-10-21 16:20:00 -0500345 },
Yoshie Muranaka37393812020-03-24 15:25:24 -0700346};
347</script>