blob: 1365bd930e63638286919e1f6773d614c54a701f [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">
9 {{ $t('pageSslCertificates.alert.certificatesExpiredMessage') }}
10 </template>
11 <template v-else>
12 {{
13 $t('pageSslCertificates.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">
22 {{ $t('pageSslCertificates.alert.certificatesExpiringMessage') }}
23 </template>
24 <template v-else>
25 {{
26 $t('pageSslCertificates.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
38 data-test-id="sslCertificates-button-generateCsr"
39 variant="link"
40 >
Yoshie Muranaka532a4b02020-03-27 11:00:50 -070041 <icon-add />
42 {{ $t('pageSslCertificates.generateCsr') }}
43 </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 />
50 {{ $t('pageSslCertificates.addNewCertificate') }}
51 </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
SurenNeware307382e2020-07-27 20:45:14 +053060 :fields="fields"
61 :items="tableItems"
62 :empty-text="$t('global.table.emptyMessage')"
63 >
Derick Montague602e98a2020-10-21 16:20:00 -050064 <template #cell(validFrom)="{ value }">
Yoshie Muranaka37393812020-03-24 15:25:24 -070065 {{ value | formatDate }}
66 </template>
67
Derick Montague602e98a2020-10-21 16:20:00 -050068 <template #cell(validUntil)="{ value }">
Yoshie Muranakae45f54b2020-03-26 15:23:34 -070069 <status-icon
70 v-if="getDaysUntilExpired(value) < 31"
71 :status="getIconStatus(value)"
72 />
Yoshie Muranaka37393812020-03-24 15:25:24 -070073 {{ value | formatDate }}
74 </template>
75
Derick Montague602e98a2020-10-21 16:20:00 -050076 <template #cell(actions)="{ value, item }">
Yoshie Muranaka37393812020-03-24 15:25:24 -070077 <table-row-action
78 v-for="(action, index) in value"
79 :key="index"
80 :value="action.value"
81 :title="action.title"
82 :enabled="action.enabled"
Sukanya Pandeyedb8a772020-10-29 11:33:42 +053083 @click-table-action="onTableRowAction($event, item)"
Yoshie Muranaka37393812020-03-24 15:25:24 -070084 >
Derick Montague602e98a2020-10-21 16:20:00 -050085 <template #icon>
Yoshie Muranaka37393812020-03-24 15:25:24 -070086 <icon-replace v-if="action.value === 'replace'" />
87 <icon-trashcan v-if="action.value === 'delete'" />
88 </template>
89 </table-row-action>
90 </template>
91 </b-table>
92 </b-col>
93 </b-row>
94
95 <!-- Modals -->
96 <modal-upload-certificate :certificate="modalCertificate" @ok="onModalOk" />
Yoshie Muranaka532a4b02020-03-27 11:00:50 -070097 <modal-generate-csr />
Yoshie Muranaka37393812020-03-24 15:25:24 -070098 </b-container>
99</template>
100
101<script>
102import IconAdd from '@carbon/icons-vue/es/add--alt/20';
103import IconReplace from '@carbon/icons-vue/es/renew/20';
104import IconTrashcan from '@carbon/icons-vue/es/trash-can/20';
105
Yoshie Muranaka532a4b02020-03-27 11:00:50 -0700106import ModalGenerateCsr from './ModalGenerateCsr';
Yoshie Muranaka37393812020-03-24 15:25:24 -0700107import ModalUploadCertificate from './ModalUploadCertificate';
SurenNeware5e25e282020-07-08 15:57:23 +0530108import PageTitle from '@/components/Global/PageTitle';
109import TableRowAction from '@/components/Global/TableRowAction';
110import StatusIcon from '@/components/Global/StatusIcon';
111import Alert from '@/components/Global/Alert';
Yoshie Muranaka37393812020-03-24 15:25:24 -0700112
SurenNeware5e25e282020-07-08 15:57:23 +0530113import BVToastMixin from '@/components/Mixins/BVToastMixin';
Yoshie Muranakae5be9ba2020-04-30 10:13:40 -0700114import LoadingBarMixin from '@/components/Mixins/LoadingBarMixin';
Yoshie Muranaka37393812020-03-24 15:25:24 -0700115
116export default {
117 name: 'SslCertificates',
118 components: {
Yoshie Muranakae45f54b2020-03-26 15:23:34 -0700119 Alert,
Yoshie Muranaka37393812020-03-24 15:25:24 -0700120 IconAdd,
121 IconReplace,
122 IconTrashcan,
Yoshie Muranaka532a4b02020-03-27 11:00:50 -0700123 ModalGenerateCsr,
Yoshie Muranaka37393812020-03-24 15:25:24 -0700124 ModalUploadCertificate,
125 PageTitle,
Yoshie Muranakae45f54b2020-03-26 15:23:34 -0700126 StatusIcon,
Derick Montague602e98a2020-10-21 16:20:00 -0500127 TableRowAction,
Yoshie Muranaka37393812020-03-24 15:25:24 -0700128 },
Yoshie Muranakae5be9ba2020-04-30 10:13:40 -0700129 mixins: [BVToastMixin, LoadingBarMixin],
Derick Montague602e98a2020-10-21 16:20:00 -0500130 beforeRouteLeave(to, from, next) {
131 this.hideLoader();
132 next();
133 },
Yoshie Muranaka37393812020-03-24 15:25:24 -0700134 data() {
135 return {
136 modalCertificate: null,
137 fields: [
138 {
139 key: 'certificate',
Derick Montague602e98a2020-10-21 16:20:00 -0500140 label: this.$t('pageSslCertificates.table.certificate'),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700141 },
142 {
143 key: 'issuedBy',
Derick Montague602e98a2020-10-21 16:20:00 -0500144 label: this.$t('pageSslCertificates.table.issuedBy'),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700145 },
146 {
147 key: 'issuedTo',
Derick Montague602e98a2020-10-21 16:20:00 -0500148 label: this.$t('pageSslCertificates.table.issuedTo'),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700149 },
150 {
151 key: 'validFrom',
Derick Montague602e98a2020-10-21 16:20:00 -0500152 label: this.$t('pageSslCertificates.table.validFrom'),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700153 },
154 {
155 key: 'validUntil',
Derick Montague602e98a2020-10-21 16:20:00 -0500156 label: this.$t('pageSslCertificates.table.validUntil'),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700157 },
158 {
159 key: 'actions',
160 label: '',
Derick Montague602e98a2020-10-21 16:20:00 -0500161 tdClass: 'text-right text-nowrap',
162 },
163 ],
Yoshie Muranaka37393812020-03-24 15:25:24 -0700164 };
165 },
166 computed: {
167 certificates() {
168 return this.$store.getters['sslCertificates/allCertificates'];
169 },
170 tableItems() {
Derick Montague602e98a2020-10-21 16:20:00 -0500171 return this.certificates.map((certificate) => {
Yoshie Muranaka37393812020-03-24 15:25:24 -0700172 return {
173 ...certificate,
174 actions: [
175 {
176 value: 'replace',
Derick Montague602e98a2020-10-21 16:20:00 -0500177 title: this.$t('pageSslCertificates.replaceCertificate'),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700178 },
179 {
180 value: 'delete',
181 title: this.$t('pageSslCertificates.deleteCertificate'),
182 enabled:
Derick Montague602e98a2020-10-21 16:20:00 -0500183 certificate.type === 'TrustStore Certificate' ? true : false,
184 },
185 ],
Yoshie Muranaka37393812020-03-24 15:25:24 -0700186 };
187 });
188 },
189 certificatesForUpload() {
190 return this.$store.getters['sslCertificates/availableUploadTypes'];
Yoshie Muranakae45f54b2020-03-26 15:23:34 -0700191 },
192 bmcTime() {
193 return this.$store.getters['global/bmcTime'];
194 },
195 expiredCertificateTypes() {
196 return this.certificates.reduce((acc, val) => {
197 const daysUntilExpired = this.getDaysUntilExpired(val.validUntil);
198 if (daysUntilExpired < 1) {
199 acc.push(val.certificate);
200 }
201 return acc;
202 }, []);
203 },
204 expiringCertificateTypes() {
205 return this.certificates.reduce((acc, val) => {
206 const daysUntilExpired = this.getDaysUntilExpired(val.validUntil);
207 if (daysUntilExpired < 31 && daysUntilExpired > 0) {
208 acc.push(val.certificate);
209 }
210 return acc;
211 }, []);
Derick Montague602e98a2020-10-21 16:20:00 -0500212 },
Yoshie Muranaka37393812020-03-24 15:25:24 -0700213 },
Yoshie Muranakae5be9ba2020-04-30 10:13:40 -0700214 async created() {
215 this.startLoader();
216 await this.$store.dispatch('global/getBmcTime');
217 this.$store
218 .dispatch('sslCertificates/getCertificates')
219 .finally(() => this.endLoader());
220 },
Yoshie Muranaka37393812020-03-24 15:25:24 -0700221 methods: {
222 onTableRowAction(event, rowItem) {
223 switch (event) {
224 case 'replace':
225 this.initModalUploadCertificate(rowItem);
226 break;
227 case 'delete':
228 this.initModalDeleteCertificate(rowItem);
229 break;
230 default:
231 break;
232 }
233 },
234 initModalUploadCertificate(certificate = null) {
235 this.modalCertificate = certificate;
236 this.$bvModal.show('upload-certificate');
237 },
238 initModalDeleteCertificate(certificate) {
239 this.$bvModal
240 .msgBoxConfirm(
241 this.$t('pageSslCertificates.modal.deleteConfirmMessage', {
242 issuedBy: certificate.issuedBy,
Derick Montague602e98a2020-10-21 16:20:00 -0500243 certificate: certificate.certificate,
Yoshie Muranaka37393812020-03-24 15:25:24 -0700244 }),
245 {
246 title: this.$t('pageSslCertificates.deleteCertificate'),
Derick Montague602e98a2020-10-21 16:20:00 -0500247 okTitle: this.$t('global.action.delete'),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700248 }
249 )
Derick Montague602e98a2020-10-21 16:20:00 -0500250 .then((deleteConfirmed) => {
Yoshie Muranaka37393812020-03-24 15:25:24 -0700251 if (deleteConfirmed) this.deleteCertificate(certificate);
252 });
253 },
254 onModalOk({ addNew, file, type, location }) {
255 if (addNew) {
256 // Upload a new certificate
257 this.addNewCertificate(file, type);
258 } else {
259 // Replace an existing certificate
260 this.replaceCertificate(file, type, location);
261 }
262 },
263 addNewCertificate(file, type) {
Yoshie Muranakae5be9ba2020-04-30 10:13:40 -0700264 this.startLoader();
Yoshie Muranaka37393812020-03-24 15:25:24 -0700265 this.$store
266 .dispatch('sslCertificates/addNewCertificate', { file, type })
Derick Montague602e98a2020-10-21 16:20:00 -0500267 .then((success) => this.successToast(success))
Yoshie Muranakae5be9ba2020-04-30 10:13:40 -0700268 .catch(({ message }) => this.errorToast(message))
269 .finally(() => this.endLoader());
Yoshie Muranaka37393812020-03-24 15:25:24 -0700270 },
271 replaceCertificate(file, type, location) {
Yoshie Muranakae5be9ba2020-04-30 10:13:40 -0700272 this.startLoader();
Yoshie Muranaka37393812020-03-24 15:25:24 -0700273 const reader = new FileReader();
274 reader.readAsBinaryString(file);
Derick Montague602e98a2020-10-21 16:20:00 -0500275 reader.onloadend = (event) => {
Yoshie Muranaka37393812020-03-24 15:25:24 -0700276 const certificateString = event.target.result;
277 this.$store
278 .dispatch('sslCertificates/replaceCertificate', {
279 certificateString,
280 type,
Derick Montague602e98a2020-10-21 16:20:00 -0500281 location,
Yoshie Muranaka37393812020-03-24 15:25:24 -0700282 })
Derick Montague602e98a2020-10-21 16:20:00 -0500283 .then((success) => this.successToast(success))
Yoshie Muranakae5be9ba2020-04-30 10:13:40 -0700284 .catch(({ message }) => this.errorToast(message))
285 .finally(() => this.endLoader());
Yoshie Muranaka37393812020-03-24 15:25:24 -0700286 };
287 },
288 deleteCertificate({ type, location }) {
Yoshie Muranakae5be9ba2020-04-30 10:13:40 -0700289 this.startLoader();
Yoshie Muranaka37393812020-03-24 15:25:24 -0700290 this.$store
291 .dispatch('sslCertificates/deleteCertificate', {
292 type,
Derick Montague602e98a2020-10-21 16:20:00 -0500293 location,
Yoshie Muranaka37393812020-03-24 15:25:24 -0700294 })
Derick Montague602e98a2020-10-21 16:20:00 -0500295 .then((success) => this.successToast(success))
Yoshie Muranakae5be9ba2020-04-30 10:13:40 -0700296 .catch(({ message }) => this.errorToast(message))
297 .finally(() => this.endLoader());
Yoshie Muranakae45f54b2020-03-26 15:23:34 -0700298 },
299 getDaysUntilExpired(date) {
300 if (this.bmcTime) {
301 const validUntilMs = date.getTime();
302 const currentBmcTimeMs = this.bmcTime.getTime();
303 const oneDayInMs = 24 * 60 * 60 * 1000;
304 return Math.round((validUntilMs - currentBmcTimeMs) / oneDayInMs);
305 }
Yoshie Muranakae5be9ba2020-04-30 10:13:40 -0700306 return new Date();
Yoshie Muranakae45f54b2020-03-26 15:23:34 -0700307 },
308 getIconStatus(date) {
309 const daysUntilExpired = this.getDaysUntilExpired(date);
310 if (daysUntilExpired < 1) {
311 return 'danger';
312 } else if (daysUntilExpired < 31) {
313 return 'warning';
314 }
Derick Montague602e98a2020-10-21 16:20:00 -0500315 },
316 },
Yoshie Muranaka37393812020-03-24 15:25:24 -0700317};
318</script>