blob: 0113b80a35c4c7e257d3420aa6d80fab0c148beb [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,
139 fields: [
140 {
141 key: 'certificate',
Sandeepa Singhb4406162021-07-26 15:05:39 +0530142 label: this.$t('pageCertificates.table.certificate'),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700143 },
144 {
145 key: 'issuedBy',
Sandeepa Singhb4406162021-07-26 15:05:39 +0530146 label: this.$t('pageCertificates.table.issuedBy'),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700147 },
148 {
149 key: 'issuedTo',
Sandeepa Singhb4406162021-07-26 15:05:39 +0530150 label: this.$t('pageCertificates.table.issuedTo'),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700151 },
152 {
153 key: 'validFrom',
Sandeepa Singhb4406162021-07-26 15:05:39 +0530154 label: this.$t('pageCertificates.table.validFrom'),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700155 },
156 {
157 key: 'validUntil',
Sandeepa Singhb4406162021-07-26 15:05:39 +0530158 label: this.$t('pageCertificates.table.validUntil'),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700159 },
160 {
161 key: 'actions',
162 label: '',
Derick Montague602e98a2020-10-21 16:20:00 -0500163 tdClass: 'text-right text-nowrap',
164 },
165 ],
Yoshie Muranaka37393812020-03-24 15:25:24 -0700166 };
167 },
168 computed: {
169 certificates() {
Sandeepa Singhb4406162021-07-26 15:05:39 +0530170 return this.$store.getters['certificates/allCertificates'];
Yoshie Muranaka37393812020-03-24 15:25:24 -0700171 },
172 tableItems() {
Derick Montague602e98a2020-10-21 16:20:00 -0500173 return this.certificates.map((certificate) => {
Yoshie Muranaka37393812020-03-24 15:25:24 -0700174 return {
175 ...certificate,
176 actions: [
177 {
178 value: 'replace',
Sandeepa Singhb4406162021-07-26 15:05:39 +0530179 title: this.$t('pageCertificates.replaceCertificate'),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700180 },
181 {
182 value: 'delete',
Sandeepa Singhb4406162021-07-26 15:05:39 +0530183 title: this.$t('pageCertificates.deleteCertificate'),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700184 enabled:
Derick Montague602e98a2020-10-21 16:20:00 -0500185 certificate.type === 'TrustStore Certificate' ? true : false,
186 },
187 ],
Yoshie Muranaka37393812020-03-24 15:25:24 -0700188 };
189 });
190 },
191 certificatesForUpload() {
Sandeepa Singhb4406162021-07-26 15:05:39 +0530192 return this.$store.getters['certificates/availableUploadTypes'];
Yoshie Muranakae45f54b2020-03-26 15:23:34 -0700193 },
194 bmcTime() {
195 return this.$store.getters['global/bmcTime'];
196 },
197 expiredCertificateTypes() {
198 return this.certificates.reduce((acc, val) => {
199 const daysUntilExpired = this.getDaysUntilExpired(val.validUntil);
200 if (daysUntilExpired < 1) {
201 acc.push(val.certificate);
202 }
203 return acc;
204 }, []);
205 },
206 expiringCertificateTypes() {
207 return this.certificates.reduce((acc, val) => {
208 const daysUntilExpired = this.getDaysUntilExpired(val.validUntil);
209 if (daysUntilExpired < 31 && daysUntilExpired > 0) {
210 acc.push(val.certificate);
211 }
212 return acc;
213 }, []);
Derick Montague602e98a2020-10-21 16:20:00 -0500214 },
Yoshie Muranaka37393812020-03-24 15:25:24 -0700215 },
Yoshie Muranakae5be9ba2020-04-30 10:13:40 -0700216 async created() {
217 this.startLoader();
218 await this.$store.dispatch('global/getBmcTime');
Kenneth Fullbright41057852021-12-27 16:19:37 -0600219 this.$store.dispatch('certificates/getCertificates').finally(() => {
220 this.endLoader();
221 this.isBusy = false;
222 });
Yoshie Muranakae5be9ba2020-04-30 10:13:40 -0700223 },
Yoshie Muranaka37393812020-03-24 15:25:24 -0700224 methods: {
225 onTableRowAction(event, rowItem) {
226 switch (event) {
227 case 'replace':
228 this.initModalUploadCertificate(rowItem);
229 break;
230 case 'delete':
231 this.initModalDeleteCertificate(rowItem);
232 break;
233 default:
234 break;
235 }
236 },
237 initModalUploadCertificate(certificate = null) {
238 this.modalCertificate = certificate;
239 this.$bvModal.show('upload-certificate');
240 },
241 initModalDeleteCertificate(certificate) {
242 this.$bvModal
243 .msgBoxConfirm(
Sandeepa Singhb4406162021-07-26 15:05:39 +0530244 this.$t('pageCertificates.modal.deleteConfirmMessage', {
Yoshie Muranaka37393812020-03-24 15:25:24 -0700245 issuedBy: certificate.issuedBy,
Derick Montague602e98a2020-10-21 16:20:00 -0500246 certificate: certificate.certificate,
Yoshie Muranaka37393812020-03-24 15:25:24 -0700247 }),
248 {
Sandeepa Singhb4406162021-07-26 15:05:39 +0530249 title: this.$t('pageCertificates.deleteCertificate'),
Derick Montague602e98a2020-10-21 16:20:00 -0500250 okTitle: this.$t('global.action.delete'),
Sukanya Pandey38357132020-12-22 13:40:59 +0530251 cancelTitle: this.$t('global.action.cancel'),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700252 }
253 )
Derick Montague602e98a2020-10-21 16:20:00 -0500254 .then((deleteConfirmed) => {
Yoshie Muranaka37393812020-03-24 15:25:24 -0700255 if (deleteConfirmed) this.deleteCertificate(certificate);
256 });
257 },
258 onModalOk({ addNew, file, type, location }) {
259 if (addNew) {
260 // Upload a new certificate
261 this.addNewCertificate(file, type);
262 } else {
263 // Replace an existing certificate
264 this.replaceCertificate(file, type, location);
265 }
266 },
267 addNewCertificate(file, type) {
Yoshie Muranakae5be9ba2020-04-30 10:13:40 -0700268 this.startLoader();
Yoshie Muranaka37393812020-03-24 15:25:24 -0700269 this.$store
Sandeepa Singhb4406162021-07-26 15:05:39 +0530270 .dispatch('certificates/addNewCertificate', { file, type })
Derick Montague602e98a2020-10-21 16:20:00 -0500271 .then((success) => this.successToast(success))
Yoshie Muranakae5be9ba2020-04-30 10:13:40 -0700272 .catch(({ message }) => this.errorToast(message))
273 .finally(() => this.endLoader());
Yoshie Muranaka37393812020-03-24 15:25:24 -0700274 },
275 replaceCertificate(file, type, location) {
Yoshie Muranakae5be9ba2020-04-30 10:13:40 -0700276 this.startLoader();
Yoshie Muranaka37393812020-03-24 15:25:24 -0700277 const reader = new FileReader();
278 reader.readAsBinaryString(file);
Derick Montague602e98a2020-10-21 16:20:00 -0500279 reader.onloadend = (event) => {
Yoshie Muranaka37393812020-03-24 15:25:24 -0700280 const certificateString = event.target.result;
281 this.$store
Sandeepa Singhb4406162021-07-26 15:05:39 +0530282 .dispatch('certificates/replaceCertificate', {
Yoshie Muranaka37393812020-03-24 15:25:24 -0700283 certificateString,
284 type,
Derick Montague602e98a2020-10-21 16:20:00 -0500285 location,
Yoshie Muranaka37393812020-03-24 15:25:24 -0700286 })
Derick Montague602e98a2020-10-21 16:20:00 -0500287 .then((success) => this.successToast(success))
Yoshie Muranakae5be9ba2020-04-30 10:13:40 -0700288 .catch(({ message }) => this.errorToast(message))
289 .finally(() => this.endLoader());
Yoshie Muranaka37393812020-03-24 15:25:24 -0700290 };
291 },
292 deleteCertificate({ type, location }) {
Yoshie Muranakae5be9ba2020-04-30 10:13:40 -0700293 this.startLoader();
Yoshie Muranaka37393812020-03-24 15:25:24 -0700294 this.$store
Sandeepa Singhb4406162021-07-26 15:05:39 +0530295 .dispatch('certificates/deleteCertificate', {
Yoshie Muranaka37393812020-03-24 15:25:24 -0700296 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 Muranakae45f54b2020-03-26 15:23:34 -0700302 },
303 getDaysUntilExpired(date) {
304 if (this.bmcTime) {
305 const validUntilMs = date.getTime();
306 const currentBmcTimeMs = this.bmcTime.getTime();
307 const oneDayInMs = 24 * 60 * 60 * 1000;
308 return Math.round((validUntilMs - currentBmcTimeMs) / oneDayInMs);
309 }
Yoshie Muranakae5be9ba2020-04-30 10:13:40 -0700310 return new Date();
Yoshie Muranakae45f54b2020-03-26 15:23:34 -0700311 },
312 getIconStatus(date) {
313 const daysUntilExpired = this.getDaysUntilExpired(date);
314 if (daysUntilExpired < 1) {
315 return 'danger';
316 } else if (daysUntilExpired < 31) {
317 return 'warning';
318 }
Derick Montague602e98a2020-10-21 16:20:00 -0500319 },
320 },
Yoshie Muranaka37393812020-03-24 15:25:24 -0700321};
322</script>