blob: 677d19d95db3fa8f6de51af6e1fbddf4cf78f061 [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>
Damian Celico31fb2b92022-06-27 16:43:21 +020032 <!-- Wrong file type banner -->
33 <alert :show="fileTypeCorrect === false" variant="danger">
34 <template v-if="fileTypeCorrect === false">
35 {{ $t('pageCertificates.alert.incorrectCertificateFileType') }}
36 </template>
37 </alert>
Yoshie Muranakae45f54b2020-03-26 15:23:34 -070038 </b-col>
39 </b-row>
40 <b-row>
Yoshie Muranaka3111b6f2020-04-21 19:48:38 -070041 <b-col xl="11" class="text-right">
SurenNewared0df7d22020-07-22 16:41:20 +053042 <b-button
43 v-b-modal.generate-csr
Sandeepa Singhb4406162021-07-26 15:05:39 +053044 data-test-id="certificates-button-generateCsr"
SurenNewared0df7d22020-07-22 16:41:20 +053045 variant="link"
46 >
Yoshie Muranaka532a4b02020-03-27 11:00:50 -070047 <icon-add />
Sandeepa Singhb4406162021-07-26 15:05:39 +053048 {{ $t('pageCertificates.generateCsr') }}
Yoshie Muranaka532a4b02020-03-27 11:00:50 -070049 </b-button>
Yoshie Muranaka37393812020-03-24 15:25:24 -070050 <b-button
51 variant="primary"
52 :disabled="certificatesForUpload.length === 0"
53 @click="initModalUploadCertificate(null)"
54 >
55 <icon-add />
Sandeepa Singhb4406162021-07-26 15:05:39 +053056 {{ $t('pageCertificates.addNewCertificate') }}
Yoshie Muranaka37393812020-03-24 15:25:24 -070057 </b-button>
58 </b-col>
59 </b-row>
60 <b-row>
Yoshie Muranaka3111b6f2020-04-21 19:48:38 -070061 <b-col xl="11">
SurenNeware307382e2020-07-27 20:45:14 +053062 <b-table
63 responsive="md"
64 show-empty
Sukanya Pandeyfde429e2020-09-14 20:48:39 +053065 hover
Kenneth Fullbright41057852021-12-27 16:19:37 -060066 :busy="isBusy"
SurenNeware307382e2020-07-27 20:45:14 +053067 :fields="fields"
68 :items="tableItems"
69 :empty-text="$t('global.table.emptyMessage')"
70 >
Derick Montague602e98a2020-10-21 16:20:00 -050071 <template #cell(validFrom)="{ value }">
Yoshie Muranaka37393812020-03-24 15:25:24 -070072 {{ value | formatDate }}
73 </template>
74
Derick Montague602e98a2020-10-21 16:20:00 -050075 <template #cell(validUntil)="{ value }">
Yoshie Muranakae45f54b2020-03-26 15:23:34 -070076 <status-icon
77 v-if="getDaysUntilExpired(value) < 31"
78 :status="getIconStatus(value)"
79 />
Yoshie Muranaka37393812020-03-24 15:25:24 -070080 {{ value | formatDate }}
81 </template>
82
Derick Montague602e98a2020-10-21 16:20:00 -050083 <template #cell(actions)="{ value, item }">
Yoshie Muranaka37393812020-03-24 15:25:24 -070084 <table-row-action
85 v-for="(action, index) in value"
86 :key="index"
87 :value="action.value"
88 :title="action.title"
89 :enabled="action.enabled"
Sukanya Pandeyedb8a772020-10-29 11:33:42 +053090 @click-table-action="onTableRowAction($event, item)"
Yoshie Muranaka37393812020-03-24 15:25:24 -070091 >
Derick Montague602e98a2020-10-21 16:20:00 -050092 <template #icon>
Yoshie Muranaka37393812020-03-24 15:25:24 -070093 <icon-replace v-if="action.value === 'replace'" />
94 <icon-trashcan v-if="action.value === 'delete'" />
95 </template>
96 </table-row-action>
97 </template>
98 </b-table>
99 </b-col>
100 </b-row>
101
102 <!-- Modals -->
103 <modal-upload-certificate :certificate="modalCertificate" @ok="onModalOk" />
Yoshie Muranaka532a4b02020-03-27 11:00:50 -0700104 <modal-generate-csr />
Yoshie Muranaka37393812020-03-24 15:25:24 -0700105 </b-container>
106</template>
107
108<script>
109import IconAdd from '@carbon/icons-vue/es/add--alt/20';
110import IconReplace from '@carbon/icons-vue/es/renew/20';
111import IconTrashcan from '@carbon/icons-vue/es/trash-can/20';
112
Yoshie Muranaka532a4b02020-03-27 11:00:50 -0700113import ModalGenerateCsr from './ModalGenerateCsr';
Yoshie Muranaka37393812020-03-24 15:25:24 -0700114import ModalUploadCertificate from './ModalUploadCertificate';
SurenNeware5e25e282020-07-08 15:57:23 +0530115import PageTitle from '@/components/Global/PageTitle';
116import TableRowAction from '@/components/Global/TableRowAction';
117import StatusIcon from '@/components/Global/StatusIcon';
118import Alert from '@/components/Global/Alert';
Yoshie Muranaka37393812020-03-24 15:25:24 -0700119
SurenNeware5e25e282020-07-08 15:57:23 +0530120import BVToastMixin from '@/components/Mixins/BVToastMixin';
Yoshie Muranakae5be9ba2020-04-30 10:13:40 -0700121import LoadingBarMixin from '@/components/Mixins/LoadingBarMixin';
Yoshie Muranaka37393812020-03-24 15:25:24 -0700122
123export default {
Sandeepa Singhb4406162021-07-26 15:05:39 +0530124 name: 'Certificates',
Yoshie Muranaka37393812020-03-24 15:25:24 -0700125 components: {
Yoshie Muranakae45f54b2020-03-26 15:23:34 -0700126 Alert,
Yoshie Muranaka37393812020-03-24 15:25:24 -0700127 IconAdd,
128 IconReplace,
129 IconTrashcan,
Yoshie Muranaka532a4b02020-03-27 11:00:50 -0700130 ModalGenerateCsr,
Yoshie Muranaka37393812020-03-24 15:25:24 -0700131 ModalUploadCertificate,
132 PageTitle,
Yoshie Muranakae45f54b2020-03-26 15:23:34 -0700133 StatusIcon,
Derick Montague602e98a2020-10-21 16:20:00 -0500134 TableRowAction,
Yoshie Muranaka37393812020-03-24 15:25:24 -0700135 },
Yoshie Muranakae5be9ba2020-04-30 10:13:40 -0700136 mixins: [BVToastMixin, LoadingBarMixin],
Derick Montague602e98a2020-10-21 16:20:00 -0500137 beforeRouteLeave(to, from, next) {
138 this.hideLoader();
139 next();
140 },
Yoshie Muranaka37393812020-03-24 15:25:24 -0700141 data() {
142 return {
Kenneth Fullbright41057852021-12-27 16:19:37 -0600143 isBusy: true,
Yoshie Muranaka37393812020-03-24 15:25:24 -0700144 modalCertificate: null,
Damian Celico31fb2b92022-06-27 16:43:21 +0200145 fileTypeCorrect: undefined,
Yoshie Muranaka37393812020-03-24 15:25:24 -0700146 fields: [
147 {
148 key: 'certificate',
Sandeepa Singhb4406162021-07-26 15:05:39 +0530149 label: this.$t('pageCertificates.table.certificate'),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700150 },
151 {
152 key: 'issuedBy',
Sandeepa Singhb4406162021-07-26 15:05:39 +0530153 label: this.$t('pageCertificates.table.issuedBy'),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700154 },
155 {
156 key: 'issuedTo',
Sandeepa Singhb4406162021-07-26 15:05:39 +0530157 label: this.$t('pageCertificates.table.issuedTo'),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700158 },
159 {
160 key: 'validFrom',
Sandeepa Singhb4406162021-07-26 15:05:39 +0530161 label: this.$t('pageCertificates.table.validFrom'),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700162 },
163 {
164 key: 'validUntil',
Sandeepa Singhb4406162021-07-26 15:05:39 +0530165 label: this.$t('pageCertificates.table.validUntil'),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700166 },
167 {
168 key: 'actions',
169 label: '',
Derick Montague602e98a2020-10-21 16:20:00 -0500170 tdClass: 'text-right text-nowrap',
171 },
172 ],
Yoshie Muranaka37393812020-03-24 15:25:24 -0700173 };
174 },
175 computed: {
176 certificates() {
Sandeepa Singhb4406162021-07-26 15:05:39 +0530177 return this.$store.getters['certificates/allCertificates'];
Yoshie Muranaka37393812020-03-24 15:25:24 -0700178 },
179 tableItems() {
Derick Montague602e98a2020-10-21 16:20:00 -0500180 return this.certificates.map((certificate) => {
Yoshie Muranaka37393812020-03-24 15:25:24 -0700181 return {
182 ...certificate,
183 actions: [
184 {
185 value: 'replace',
Sandeepa Singhb4406162021-07-26 15:05:39 +0530186 title: this.$t('pageCertificates.replaceCertificate'),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700187 },
188 {
189 value: 'delete',
Sandeepa Singhb4406162021-07-26 15:05:39 +0530190 title: this.$t('pageCertificates.deleteCertificate'),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700191 enabled:
Derick Montague602e98a2020-10-21 16:20:00 -0500192 certificate.type === 'TrustStore Certificate' ? true : false,
193 },
194 ],
Yoshie Muranaka37393812020-03-24 15:25:24 -0700195 };
196 });
197 },
198 certificatesForUpload() {
Sandeepa Singhb4406162021-07-26 15:05:39 +0530199 return this.$store.getters['certificates/availableUploadTypes'];
Yoshie Muranakae45f54b2020-03-26 15:23:34 -0700200 },
201 bmcTime() {
202 return this.$store.getters['global/bmcTime'];
203 },
204 expiredCertificateTypes() {
205 return this.certificates.reduce((acc, val) => {
206 const daysUntilExpired = this.getDaysUntilExpired(val.validUntil);
207 if (daysUntilExpired < 1) {
208 acc.push(val.certificate);
209 }
210 return acc;
211 }, []);
212 },
213 expiringCertificateTypes() {
214 return this.certificates.reduce((acc, val) => {
215 const daysUntilExpired = this.getDaysUntilExpired(val.validUntil);
216 if (daysUntilExpired < 31 && daysUntilExpired > 0) {
217 acc.push(val.certificate);
218 }
219 return acc;
220 }, []);
Derick Montague602e98a2020-10-21 16:20:00 -0500221 },
Yoshie Muranaka37393812020-03-24 15:25:24 -0700222 },
Yoshie Muranakae5be9ba2020-04-30 10:13:40 -0700223 async created() {
224 this.startLoader();
225 await this.$store.dispatch('global/getBmcTime');
Kenneth Fullbright41057852021-12-27 16:19:37 -0600226 this.$store.dispatch('certificates/getCertificates').finally(() => {
227 this.endLoader();
228 this.isBusy = false;
229 });
Yoshie Muranakae5be9ba2020-04-30 10:13:40 -0700230 },
Yoshie Muranaka37393812020-03-24 15:25:24 -0700231 methods: {
232 onTableRowAction(event, rowItem) {
233 switch (event) {
234 case 'replace':
235 this.initModalUploadCertificate(rowItem);
236 break;
237 case 'delete':
238 this.initModalDeleteCertificate(rowItem);
239 break;
240 default:
241 break;
242 }
243 },
244 initModalUploadCertificate(certificate = null) {
245 this.modalCertificate = certificate;
246 this.$bvModal.show('upload-certificate');
247 },
248 initModalDeleteCertificate(certificate) {
249 this.$bvModal
250 .msgBoxConfirm(
Sandeepa Singhb4406162021-07-26 15:05:39 +0530251 this.$t('pageCertificates.modal.deleteConfirmMessage', {
Yoshie Muranaka37393812020-03-24 15:25:24 -0700252 issuedBy: certificate.issuedBy,
Derick Montague602e98a2020-10-21 16:20:00 -0500253 certificate: certificate.certificate,
Yoshie Muranaka37393812020-03-24 15:25:24 -0700254 }),
255 {
Sandeepa Singhb4406162021-07-26 15:05:39 +0530256 title: this.$t('pageCertificates.deleteCertificate'),
Derick Montague602e98a2020-10-21 16:20:00 -0500257 okTitle: this.$t('global.action.delete'),
Sukanya Pandey38357132020-12-22 13:40:59 +0530258 cancelTitle: this.$t('global.action.cancel'),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700259 }
260 )
Derick Montague602e98a2020-10-21 16:20:00 -0500261 .then((deleteConfirmed) => {
Yoshie Muranaka37393812020-03-24 15:25:24 -0700262 if (deleteConfirmed) this.deleteCertificate(certificate);
263 });
264 },
265 onModalOk({ addNew, file, type, location }) {
266 if (addNew) {
267 // Upload a new certificate
Damian Celico31fb2b92022-06-27 16:43:21 +0200268 this.fileTypeCorrect = this.getIsFileTypeCorrect(file);
269 if (this.fileTypeCorrect) {
270 this.addNewCertificate(file, type);
271 }
Yoshie Muranaka37393812020-03-24 15:25:24 -0700272 } else {
273 // Replace an existing certificate
274 this.replaceCertificate(file, type, location);
275 }
276 },
277 addNewCertificate(file, type) {
Damian Celico31fb2b92022-06-27 16:43:21 +0200278 if (this.fileTypeCorrect === true) {
279 this.startLoader();
280 this.$store
281 .dispatch('certificates/addNewCertificate', { file, type })
282 .then((success) => this.successToast(success))
283 .catch(({ message }) => this.errorToast(message))
284 .finally(() => this.endLoader());
285 }
Yoshie Muranaka37393812020-03-24 15:25:24 -0700286 },
287 replaceCertificate(file, type, location) {
Yoshie Muranakae5be9ba2020-04-30 10:13:40 -0700288 this.startLoader();
Yoshie Muranaka37393812020-03-24 15:25:24 -0700289 const reader = new FileReader();
290 reader.readAsBinaryString(file);
Derick Montague602e98a2020-10-21 16:20:00 -0500291 reader.onloadend = (event) => {
Yoshie Muranaka37393812020-03-24 15:25:24 -0700292 const certificateString = event.target.result;
293 this.$store
Sandeepa Singhb4406162021-07-26 15:05:39 +0530294 .dispatch('certificates/replaceCertificate', {
Yoshie Muranaka37393812020-03-24 15:25:24 -0700295 certificateString,
296 type,
Derick Montague602e98a2020-10-21 16:20:00 -0500297 location,
Yoshie Muranaka37393812020-03-24 15:25:24 -0700298 })
Derick Montague602e98a2020-10-21 16:20:00 -0500299 .then((success) => this.successToast(success))
Yoshie Muranakae5be9ba2020-04-30 10:13:40 -0700300 .catch(({ message }) => this.errorToast(message))
301 .finally(() => this.endLoader());
Yoshie Muranaka37393812020-03-24 15:25:24 -0700302 };
303 },
304 deleteCertificate({ type, location }) {
Yoshie Muranakae5be9ba2020-04-30 10:13:40 -0700305 this.startLoader();
Yoshie Muranaka37393812020-03-24 15:25:24 -0700306 this.$store
Sandeepa Singhb4406162021-07-26 15:05:39 +0530307 .dispatch('certificates/deleteCertificate', {
Yoshie Muranaka37393812020-03-24 15:25:24 -0700308 type,
Derick Montague602e98a2020-10-21 16:20:00 -0500309 location,
Yoshie Muranaka37393812020-03-24 15:25:24 -0700310 })
Derick Montague602e98a2020-10-21 16:20:00 -0500311 .then((success) => this.successToast(success))
Yoshie Muranakae5be9ba2020-04-30 10:13:40 -0700312 .catch(({ message }) => this.errorToast(message))
313 .finally(() => this.endLoader());
Yoshie Muranakae45f54b2020-03-26 15:23:34 -0700314 },
315 getDaysUntilExpired(date) {
316 if (this.bmcTime) {
317 const validUntilMs = date.getTime();
318 const currentBmcTimeMs = this.bmcTime.getTime();
319 const oneDayInMs = 24 * 60 * 60 * 1000;
320 return Math.round((validUntilMs - currentBmcTimeMs) / oneDayInMs);
321 }
Yoshie Muranakae5be9ba2020-04-30 10:13:40 -0700322 return new Date();
Yoshie Muranakae45f54b2020-03-26 15:23:34 -0700323 },
324 getIconStatus(date) {
325 const daysUntilExpired = this.getDaysUntilExpired(date);
326 if (daysUntilExpired < 1) {
327 return 'danger';
328 } else if (daysUntilExpired < 31) {
329 return 'warning';
330 }
Derick Montague602e98a2020-10-21 16:20:00 -0500331 },
Damian Celico31fb2b92022-06-27 16:43:21 +0200332 getIsFileTypeCorrect(file) {
333 const fileTypeExtension = file.name.split('.').pop();
334 return fileTypeExtension === 'pem';
335 },
Derick Montague602e98a2020-10-21 16:20:00 -0500336 },
Yoshie Muranaka37393812020-03-24 15:25:24 -0700337};
338</script>