blob: 06caab6237b04516141457dd636b97fbffd346fd [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 }">
Yoshie Muranaka37393812020-03-24 15:25:24 -070066 {{ value | formatDate }}
67 </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 />
Yoshie Muranaka37393812020-03-24 15:25:24 -070074 {{ value | formatDate }}
75 </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';
Yoshie Muranaka37393812020-03-24 15:25:24 -0700116
117export default {
Sandeepa Singhb4406162021-07-26 15:05:39 +0530118 name: 'Certificates',
Yoshie Muranaka37393812020-03-24 15:25:24 -0700119 components: {
Yoshie Muranakae45f54b2020-03-26 15:23:34 -0700120 Alert,
Yoshie Muranaka37393812020-03-24 15:25:24 -0700121 IconAdd,
122 IconReplace,
123 IconTrashcan,
Yoshie Muranaka532a4b02020-03-27 11:00:50 -0700124 ModalGenerateCsr,
Yoshie Muranaka37393812020-03-24 15:25:24 -0700125 ModalUploadCertificate,
126 PageTitle,
Yoshie Muranakae45f54b2020-03-26 15:23:34 -0700127 StatusIcon,
Derick Montague602e98a2020-10-21 16:20:00 -0500128 TableRowAction,
Yoshie Muranaka37393812020-03-24 15:25:24 -0700129 },
Yoshie Muranakae5be9ba2020-04-30 10:13:40 -0700130 mixins: [BVToastMixin, LoadingBarMixin],
Derick Montague602e98a2020-10-21 16:20:00 -0500131 beforeRouteLeave(to, from, next) {
132 this.hideLoader();
133 next();
134 },
Yoshie Muranaka37393812020-03-24 15:25:24 -0700135 data() {
136 return {
Kenneth Fullbright41057852021-12-27 16:19:37 -0600137 isBusy: true,
Yoshie Muranaka37393812020-03-24 15:25:24 -0700138 modalCertificate: null,
Damian Celico31fb2b92022-06-27 16:43:21 +0200139 fileTypeCorrect: undefined,
Yoshie Muranaka37393812020-03-24 15:25:24 -0700140 fields: [
141 {
142 key: 'certificate',
Sandeepa Singhb4406162021-07-26 15:05:39 +0530143 label: this.$t('pageCertificates.table.certificate'),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700144 },
145 {
146 key: 'issuedBy',
Sandeepa Singhb4406162021-07-26 15:05:39 +0530147 label: this.$t('pageCertificates.table.issuedBy'),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700148 },
149 {
150 key: 'issuedTo',
Sandeepa Singhb4406162021-07-26 15:05:39 +0530151 label: this.$t('pageCertificates.table.issuedTo'),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700152 },
153 {
154 key: 'validFrom',
Sandeepa Singhb4406162021-07-26 15:05:39 +0530155 label: this.$t('pageCertificates.table.validFrom'),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700156 },
157 {
158 key: 'validUntil',
Sandeepa Singhb4406162021-07-26 15:05:39 +0530159 label: this.$t('pageCertificates.table.validUntil'),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700160 },
161 {
162 key: 'actions',
163 label: '',
Derick Montague602e98a2020-10-21 16:20:00 -0500164 tdClass: 'text-right text-nowrap',
165 },
166 ],
Yoshie Muranaka37393812020-03-24 15:25:24 -0700167 };
168 },
169 computed: {
170 certificates() {
Sandeepa Singhb4406162021-07-26 15:05:39 +0530171 return this.$store.getters['certificates/allCertificates'];
Yoshie Muranaka37393812020-03-24 15:25:24 -0700172 },
173 tableItems() {
Derick Montague602e98a2020-10-21 16:20:00 -0500174 return this.certificates.map((certificate) => {
Yoshie Muranaka37393812020-03-24 15:25:24 -0700175 return {
176 ...certificate,
177 actions: [
178 {
179 value: 'replace',
Sandeepa Singhb4406162021-07-26 15:05:39 +0530180 title: this.$t('pageCertificates.replaceCertificate'),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700181 },
182 {
183 value: 'delete',
Sandeepa Singhb4406162021-07-26 15:05:39 +0530184 title: this.$t('pageCertificates.deleteCertificate'),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700185 enabled:
Derick Montague602e98a2020-10-21 16:20:00 -0500186 certificate.type === 'TrustStore Certificate' ? true : false,
187 },
188 ],
Yoshie Muranaka37393812020-03-24 15:25:24 -0700189 };
190 });
191 },
192 certificatesForUpload() {
Sandeepa Singhb4406162021-07-26 15:05:39 +0530193 return this.$store.getters['certificates/availableUploadTypes'];
Yoshie Muranakae45f54b2020-03-26 15:23:34 -0700194 },
195 bmcTime() {
196 return this.$store.getters['global/bmcTime'];
197 },
198 expiredCertificateTypes() {
199 return this.certificates.reduce((acc, val) => {
200 const daysUntilExpired = this.getDaysUntilExpired(val.validUntil);
201 if (daysUntilExpired < 1) {
202 acc.push(val.certificate);
203 }
204 return acc;
205 }, []);
206 },
207 expiringCertificateTypes() {
208 return this.certificates.reduce((acc, val) => {
209 const daysUntilExpired = this.getDaysUntilExpired(val.validUntil);
210 if (daysUntilExpired < 31 && daysUntilExpired > 0) {
211 acc.push(val.certificate);
212 }
213 return acc;
214 }, []);
Derick Montague602e98a2020-10-21 16:20:00 -0500215 },
Yoshie Muranaka37393812020-03-24 15:25:24 -0700216 },
Yoshie Muranakae5be9ba2020-04-30 10:13:40 -0700217 async created() {
218 this.startLoader();
219 await this.$store.dispatch('global/getBmcTime');
Kenneth Fullbright41057852021-12-27 16:19:37 -0600220 this.$store.dispatch('certificates/getCertificates').finally(() => {
221 this.endLoader();
222 this.isBusy = false;
223 });
Yoshie Muranakae5be9ba2020-04-30 10:13:40 -0700224 },
Yoshie Muranaka37393812020-03-24 15:25:24 -0700225 methods: {
226 onTableRowAction(event, rowItem) {
227 switch (event) {
228 case 'replace':
229 this.initModalUploadCertificate(rowItem);
230 break;
231 case 'delete':
232 this.initModalDeleteCertificate(rowItem);
233 break;
234 default:
235 break;
236 }
237 },
238 initModalUploadCertificate(certificate = null) {
239 this.modalCertificate = certificate;
240 this.$bvModal.show('upload-certificate');
241 },
242 initModalDeleteCertificate(certificate) {
243 this.$bvModal
244 .msgBoxConfirm(
Sandeepa Singhb4406162021-07-26 15:05:39 +0530245 this.$t('pageCertificates.modal.deleteConfirmMessage', {
Yoshie Muranaka37393812020-03-24 15:25:24 -0700246 issuedBy: certificate.issuedBy,
Derick Montague602e98a2020-10-21 16:20:00 -0500247 certificate: certificate.certificate,
Yoshie Muranaka37393812020-03-24 15:25:24 -0700248 }),
249 {
Sandeepa Singhb4406162021-07-26 15:05:39 +0530250 title: this.$t('pageCertificates.deleteCertificate'),
Derick Montague602e98a2020-10-21 16:20:00 -0500251 okTitle: this.$t('global.action.delete'),
Sukanya Pandey38357132020-12-22 13:40:59 +0530252 cancelTitle: this.$t('global.action.cancel'),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700253 }
254 )
Derick Montague602e98a2020-10-21 16:20:00 -0500255 .then((deleteConfirmed) => {
Yoshie Muranaka37393812020-03-24 15:25:24 -0700256 if (deleteConfirmed) this.deleteCertificate(certificate);
257 });
258 },
259 onModalOk({ addNew, file, type, location }) {
260 if (addNew) {
261 // Upload a new certificate
Damian Celico31fb2b92022-06-27 16:43:21 +0200262 this.fileTypeCorrect = this.getIsFileTypeCorrect(file);
263 if (this.fileTypeCorrect) {
264 this.addNewCertificate(file, type);
Damian Celico713dae02022-11-14 23:06:55 +0100265 } else {
266 this.errorToast(
267 this.$t('pageCertificates.alert.incorrectCertificateFileType'),
268 {
269 title: this.$t('pageCertificates.toast.errorAddCertificate'),
270 }
271 );
Damian Celico31fb2b92022-06-27 16:43:21 +0200272 }
Yoshie Muranaka37393812020-03-24 15:25:24 -0700273 } else {
274 // Replace an existing certificate
275 this.replaceCertificate(file, type, location);
276 }
277 },
278 addNewCertificate(file, type) {
Damian Celico31fb2b92022-06-27 16:43:21 +0200279 if (this.fileTypeCorrect === true) {
280 this.startLoader();
281 this.$store
282 .dispatch('certificates/addNewCertificate', { file, type })
283 .then((success) => this.successToast(success))
284 .catch(({ message }) => this.errorToast(message))
285 .finally(() => this.endLoader());
286 }
Yoshie Muranaka37393812020-03-24 15:25:24 -0700287 },
288 replaceCertificate(file, type, location) {
Yoshie Muranakae5be9ba2020-04-30 10:13:40 -0700289 this.startLoader();
Yoshie Muranaka37393812020-03-24 15:25:24 -0700290 const reader = new FileReader();
291 reader.readAsBinaryString(file);
Derick Montague602e98a2020-10-21 16:20:00 -0500292 reader.onloadend = (event) => {
Yoshie Muranaka37393812020-03-24 15:25:24 -0700293 const certificateString = event.target.result;
294 this.$store
Sandeepa Singhb4406162021-07-26 15:05:39 +0530295 .dispatch('certificates/replaceCertificate', {
Yoshie Muranaka37393812020-03-24 15:25:24 -0700296 certificateString,
297 type,
Derick Montague602e98a2020-10-21 16:20:00 -0500298 location,
Yoshie Muranaka37393812020-03-24 15:25:24 -0700299 })
Derick Montague602e98a2020-10-21 16:20:00 -0500300 .then((success) => this.successToast(success))
Yoshie Muranakae5be9ba2020-04-30 10:13:40 -0700301 .catch(({ message }) => this.errorToast(message))
302 .finally(() => this.endLoader());
Yoshie Muranaka37393812020-03-24 15:25:24 -0700303 };
304 },
305 deleteCertificate({ type, location }) {
Yoshie Muranakae5be9ba2020-04-30 10:13:40 -0700306 this.startLoader();
Yoshie Muranaka37393812020-03-24 15:25:24 -0700307 this.$store
Sandeepa Singhb4406162021-07-26 15:05:39 +0530308 .dispatch('certificates/deleteCertificate', {
Yoshie Muranaka37393812020-03-24 15:25:24 -0700309 type,
Derick Montague602e98a2020-10-21 16:20:00 -0500310 location,
Yoshie Muranaka37393812020-03-24 15:25:24 -0700311 })
Derick Montague602e98a2020-10-21 16:20:00 -0500312 .then((success) => this.successToast(success))
Yoshie Muranakae5be9ba2020-04-30 10:13:40 -0700313 .catch(({ message }) => this.errorToast(message))
314 .finally(() => this.endLoader());
Yoshie Muranakae45f54b2020-03-26 15:23:34 -0700315 },
316 getDaysUntilExpired(date) {
317 if (this.bmcTime) {
318 const validUntilMs = date.getTime();
319 const currentBmcTimeMs = this.bmcTime.getTime();
320 const oneDayInMs = 24 * 60 * 60 * 1000;
321 return Math.round((validUntilMs - currentBmcTimeMs) / oneDayInMs);
322 }
Yoshie Muranakae5be9ba2020-04-30 10:13:40 -0700323 return new Date();
Yoshie Muranakae45f54b2020-03-26 15:23:34 -0700324 },
325 getIconStatus(date) {
326 const daysUntilExpired = this.getDaysUntilExpired(date);
327 if (daysUntilExpired < 1) {
328 return 'danger';
329 } else if (daysUntilExpired < 31) {
330 return 'warning';
331 }
Derick Montague602e98a2020-10-21 16:20:00 -0500332 },
Damian Celico31fb2b92022-06-27 16:43:21 +0200333 getIsFileTypeCorrect(file) {
334 const fileTypeExtension = file.name.split('.').pop();
335 return fileTypeExtension === 'pem';
336 },
Derick Montague602e98a2020-10-21 16:20:00 -0500337 },
Yoshie Muranaka37393812020-03-24 15:25:24 -0700338};
339</script>