Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 1 | <template> |
Yoshie Muranaka | 3111b6f | 2020-04-21 19:48:38 -0700 | [diff] [blame] | 2 | <b-container fluid="xl"> |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 3 | <page-title /> |
| 4 | <b-row> |
Yoshie Muranaka | 3111b6f | 2020-04-21 19:48:38 -0700 | [diff] [blame] | 5 | <b-col xl="11"> |
Yoshie Muranaka | e45f54b | 2020-03-26 15:23:34 -0700 | [diff] [blame] | 6 | <!-- 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', { |
| 14 | certificate: expiredCertificateTypes[0] |
| 15 | }) |
| 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', { |
| 27 | certificate: expiringCertificateTypes[0] |
| 28 | }) |
| 29 | }} |
| 30 | </template> |
| 31 | </alert> |
| 32 | </b-col> |
| 33 | </b-row> |
| 34 | <b-row> |
Yoshie Muranaka | 3111b6f | 2020-04-21 19:48:38 -0700 | [diff] [blame] | 35 | <b-col xl="11" class="text-right"> |
SurenNeware | d0df7d2 | 2020-07-22 16:41:20 +0530 | [diff] [blame] | 36 | <b-button |
| 37 | v-b-modal.generate-csr |
| 38 | data-test-id="sslCertificates-button-generateCsr" |
| 39 | variant="link" |
| 40 | > |
Yoshie Muranaka | 532a4b0 | 2020-03-27 11:00:50 -0700 | [diff] [blame] | 41 | <icon-add /> |
| 42 | {{ $t('pageSslCertificates.generateCsr') }} |
| 43 | </b-button> |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 44 | <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 Muranaka | 3111b6f | 2020-04-21 19:48:38 -0700 | [diff] [blame] | 55 | <b-col xl="11"> |
SurenNeware | 307382e | 2020-07-27 20:45:14 +0530 | [diff] [blame] | 56 | <b-table |
| 57 | responsive="md" |
| 58 | show-empty |
Sukanya Pandey | fde429e | 2020-09-14 20:48:39 +0530 | [diff] [blame^] | 59 | hover |
SurenNeware | 307382e | 2020-07-27 20:45:14 +0530 | [diff] [blame] | 60 | :fields="fields" |
| 61 | :items="tableItems" |
| 62 | :empty-text="$t('global.table.emptyMessage')" |
| 63 | > |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 64 | <template v-slot:cell(validFrom)="{ value }"> |
| 65 | {{ value | formatDate }} |
| 66 | </template> |
| 67 | |
| 68 | <template v-slot:cell(validUntil)="{ value }"> |
Yoshie Muranaka | e45f54b | 2020-03-26 15:23:34 -0700 | [diff] [blame] | 69 | <status-icon |
| 70 | v-if="getDaysUntilExpired(value) < 31" |
| 71 | :status="getIconStatus(value)" |
| 72 | /> |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 73 | {{ value | formatDate }} |
| 74 | </template> |
| 75 | |
| 76 | <template v-slot:cell(actions)="{ value, item }"> |
| 77 | <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" |
| 83 | @click:tableAction="onTableRowAction($event, item)" |
| 84 | > |
| 85 | <template v-slot:icon> |
| 86 | <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 Muranaka | 532a4b0 | 2020-03-27 11:00:50 -0700 | [diff] [blame] | 97 | <modal-generate-csr /> |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 98 | </b-container> |
| 99 | </template> |
| 100 | |
| 101 | <script> |
| 102 | import IconAdd from '@carbon/icons-vue/es/add--alt/20'; |
| 103 | import IconReplace from '@carbon/icons-vue/es/renew/20'; |
| 104 | import IconTrashcan from '@carbon/icons-vue/es/trash-can/20'; |
| 105 | |
Yoshie Muranaka | 532a4b0 | 2020-03-27 11:00:50 -0700 | [diff] [blame] | 106 | import ModalGenerateCsr from './ModalGenerateCsr'; |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 107 | import ModalUploadCertificate from './ModalUploadCertificate'; |
SurenNeware | 5e25e28 | 2020-07-08 15:57:23 +0530 | [diff] [blame] | 108 | import PageTitle from '@/components/Global/PageTitle'; |
| 109 | import TableRowAction from '@/components/Global/TableRowAction'; |
| 110 | import StatusIcon from '@/components/Global/StatusIcon'; |
| 111 | import Alert from '@/components/Global/Alert'; |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 112 | |
SurenNeware | 5e25e28 | 2020-07-08 15:57:23 +0530 | [diff] [blame] | 113 | import BVToastMixin from '@/components/Mixins/BVToastMixin'; |
Yoshie Muranaka | e5be9ba | 2020-04-30 10:13:40 -0700 | [diff] [blame] | 114 | import LoadingBarMixin from '@/components/Mixins/LoadingBarMixin'; |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 115 | |
| 116 | export default { |
| 117 | name: 'SslCertificates', |
| 118 | components: { |
Yoshie Muranaka | e45f54b | 2020-03-26 15:23:34 -0700 | [diff] [blame] | 119 | Alert, |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 120 | IconAdd, |
| 121 | IconReplace, |
| 122 | IconTrashcan, |
Yoshie Muranaka | 532a4b0 | 2020-03-27 11:00:50 -0700 | [diff] [blame] | 123 | ModalGenerateCsr, |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 124 | ModalUploadCertificate, |
| 125 | PageTitle, |
Yoshie Muranaka | e45f54b | 2020-03-26 15:23:34 -0700 | [diff] [blame] | 126 | StatusIcon, |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 127 | TableRowAction |
| 128 | }, |
Yoshie Muranaka | e5be9ba | 2020-04-30 10:13:40 -0700 | [diff] [blame] | 129 | mixins: [BVToastMixin, LoadingBarMixin], |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 130 | data() { |
| 131 | return { |
| 132 | modalCertificate: null, |
| 133 | fields: [ |
| 134 | { |
| 135 | key: 'certificate', |
| 136 | label: this.$t('pageSslCertificates.table.certificate') |
| 137 | }, |
| 138 | { |
| 139 | key: 'issuedBy', |
| 140 | label: this.$t('pageSslCertificates.table.issuedBy') |
| 141 | }, |
| 142 | { |
| 143 | key: 'issuedTo', |
| 144 | label: this.$t('pageSslCertificates.table.issuedTo') |
| 145 | }, |
| 146 | { |
| 147 | key: 'validFrom', |
| 148 | label: this.$t('pageSslCertificates.table.validFrom') |
| 149 | }, |
| 150 | { |
| 151 | key: 'validUntil', |
| 152 | label: this.$t('pageSslCertificates.table.validUntil') |
| 153 | }, |
| 154 | { |
| 155 | key: 'actions', |
| 156 | label: '', |
SurenNeware | 5e25e28 | 2020-07-08 15:57:23 +0530 | [diff] [blame] | 157 | tdClass: 'text-right text-nowrap' |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 158 | } |
| 159 | ] |
| 160 | }; |
| 161 | }, |
| 162 | computed: { |
| 163 | certificates() { |
| 164 | return this.$store.getters['sslCertificates/allCertificates']; |
| 165 | }, |
| 166 | tableItems() { |
| 167 | return this.certificates.map(certificate => { |
| 168 | return { |
| 169 | ...certificate, |
| 170 | actions: [ |
| 171 | { |
| 172 | value: 'replace', |
| 173 | title: this.$t('pageSslCertificates.replaceCertificate') |
| 174 | }, |
| 175 | { |
| 176 | value: 'delete', |
| 177 | title: this.$t('pageSslCertificates.deleteCertificate'), |
| 178 | enabled: |
| 179 | certificate.type === 'TrustStore Certificate' ? true : false |
| 180 | } |
| 181 | ] |
| 182 | }; |
| 183 | }); |
| 184 | }, |
| 185 | certificatesForUpload() { |
| 186 | return this.$store.getters['sslCertificates/availableUploadTypes']; |
Yoshie Muranaka | e45f54b | 2020-03-26 15:23:34 -0700 | [diff] [blame] | 187 | }, |
| 188 | bmcTime() { |
| 189 | return this.$store.getters['global/bmcTime']; |
| 190 | }, |
| 191 | expiredCertificateTypes() { |
| 192 | return this.certificates.reduce((acc, val) => { |
| 193 | const daysUntilExpired = this.getDaysUntilExpired(val.validUntil); |
| 194 | if (daysUntilExpired < 1) { |
| 195 | acc.push(val.certificate); |
| 196 | } |
| 197 | return acc; |
| 198 | }, []); |
| 199 | }, |
| 200 | expiringCertificateTypes() { |
| 201 | return this.certificates.reduce((acc, val) => { |
| 202 | const daysUntilExpired = this.getDaysUntilExpired(val.validUntil); |
| 203 | if (daysUntilExpired < 31 && daysUntilExpired > 0) { |
| 204 | acc.push(val.certificate); |
| 205 | } |
| 206 | return acc; |
| 207 | }, []); |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 208 | } |
| 209 | }, |
Yoshie Muranaka | e5be9ba | 2020-04-30 10:13:40 -0700 | [diff] [blame] | 210 | async created() { |
| 211 | this.startLoader(); |
| 212 | await this.$store.dispatch('global/getBmcTime'); |
| 213 | this.$store |
| 214 | .dispatch('sslCertificates/getCertificates') |
| 215 | .finally(() => this.endLoader()); |
| 216 | }, |
| 217 | beforeRouteLeave(to, from, next) { |
| 218 | this.hideLoader(); |
| 219 | next(); |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 220 | }, |
| 221 | 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, |
| 243 | certificate: certificate.certificate |
| 244 | }), |
| 245 | { |
| 246 | title: this.$t('pageSslCertificates.deleteCertificate'), |
| 247 | okTitle: this.$t('global.action.delete') |
| 248 | } |
| 249 | ) |
| 250 | .then(deleteConfirmed => { |
| 251 | 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 Muranaka | e5be9ba | 2020-04-30 10:13:40 -0700 | [diff] [blame] | 264 | this.startLoader(); |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 265 | this.$store |
| 266 | .dispatch('sslCertificates/addNewCertificate', { file, type }) |
| 267 | .then(success => this.successToast(success)) |
Yoshie Muranaka | e5be9ba | 2020-04-30 10:13:40 -0700 | [diff] [blame] | 268 | .catch(({ message }) => this.errorToast(message)) |
| 269 | .finally(() => this.endLoader()); |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 270 | }, |
| 271 | replaceCertificate(file, type, location) { |
Yoshie Muranaka | e5be9ba | 2020-04-30 10:13:40 -0700 | [diff] [blame] | 272 | this.startLoader(); |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 273 | const reader = new FileReader(); |
| 274 | reader.readAsBinaryString(file); |
| 275 | reader.onloadend = event => { |
| 276 | const certificateString = event.target.result; |
| 277 | this.$store |
| 278 | .dispatch('sslCertificates/replaceCertificate', { |
| 279 | certificateString, |
| 280 | type, |
| 281 | location |
| 282 | }) |
| 283 | .then(success => this.successToast(success)) |
Yoshie Muranaka | e5be9ba | 2020-04-30 10:13:40 -0700 | [diff] [blame] | 284 | .catch(({ message }) => this.errorToast(message)) |
| 285 | .finally(() => this.endLoader()); |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 286 | }; |
| 287 | }, |
| 288 | deleteCertificate({ type, location }) { |
Yoshie Muranaka | e5be9ba | 2020-04-30 10:13:40 -0700 | [diff] [blame] | 289 | this.startLoader(); |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 290 | this.$store |
| 291 | .dispatch('sslCertificates/deleteCertificate', { |
| 292 | type, |
| 293 | location |
| 294 | }) |
| 295 | .then(success => this.successToast(success)) |
Yoshie Muranaka | e5be9ba | 2020-04-30 10:13:40 -0700 | [diff] [blame] | 296 | .catch(({ message }) => this.errorToast(message)) |
| 297 | .finally(() => this.endLoader()); |
Yoshie Muranaka | e45f54b | 2020-03-26 15:23:34 -0700 | [diff] [blame] | 298 | }, |
| 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 Muranaka | e5be9ba | 2020-04-30 10:13:40 -0700 | [diff] [blame] | 306 | return new Date(); |
Yoshie Muranaka | e45f54b | 2020-03-26 15:23:34 -0700 | [diff] [blame] | 307 | }, |
| 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 | } |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | }; |
| 318 | </script> |