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 | 5e25e28 | 2020-07-08 15:57:23 +0530 | [diff] [blame] | 56 | <b-table responsive="md" :fields="fields" :items="tableItems"> |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 57 | <template v-slot:cell(validFrom)="{ value }"> |
| 58 | {{ value | formatDate }} |
| 59 | </template> |
| 60 | |
| 61 | <template v-slot:cell(validUntil)="{ value }"> |
Yoshie Muranaka | e45f54b | 2020-03-26 15:23:34 -0700 | [diff] [blame] | 62 | <status-icon |
| 63 | v-if="getDaysUntilExpired(value) < 31" |
| 64 | :status="getIconStatus(value)" |
| 65 | /> |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 66 | {{ value | formatDate }} |
| 67 | </template> |
| 68 | |
| 69 | <template v-slot:cell(actions)="{ value, item }"> |
| 70 | <table-row-action |
| 71 | v-for="(action, index) in value" |
| 72 | :key="index" |
| 73 | :value="action.value" |
| 74 | :title="action.title" |
| 75 | :enabled="action.enabled" |
| 76 | @click:tableAction="onTableRowAction($event, item)" |
| 77 | > |
| 78 | <template v-slot:icon> |
| 79 | <icon-replace v-if="action.value === 'replace'" /> |
| 80 | <icon-trashcan v-if="action.value === 'delete'" /> |
| 81 | </template> |
| 82 | </table-row-action> |
| 83 | </template> |
| 84 | </b-table> |
| 85 | </b-col> |
| 86 | </b-row> |
| 87 | |
| 88 | <!-- Modals --> |
| 89 | <modal-upload-certificate :certificate="modalCertificate" @ok="onModalOk" /> |
Yoshie Muranaka | 532a4b0 | 2020-03-27 11:00:50 -0700 | [diff] [blame] | 90 | <modal-generate-csr /> |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 91 | </b-container> |
| 92 | </template> |
| 93 | |
| 94 | <script> |
| 95 | import IconAdd from '@carbon/icons-vue/es/add--alt/20'; |
| 96 | import IconReplace from '@carbon/icons-vue/es/renew/20'; |
| 97 | import IconTrashcan from '@carbon/icons-vue/es/trash-can/20'; |
| 98 | |
Yoshie Muranaka | 532a4b0 | 2020-03-27 11:00:50 -0700 | [diff] [blame] | 99 | import ModalGenerateCsr from './ModalGenerateCsr'; |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 100 | import ModalUploadCertificate from './ModalUploadCertificate'; |
SurenNeware | 5e25e28 | 2020-07-08 15:57:23 +0530 | [diff] [blame] | 101 | import PageTitle from '@/components/Global/PageTitle'; |
| 102 | import TableRowAction from '@/components/Global/TableRowAction'; |
| 103 | import StatusIcon from '@/components/Global/StatusIcon'; |
| 104 | import Alert from '@/components/Global/Alert'; |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 105 | |
SurenNeware | 5e25e28 | 2020-07-08 15:57:23 +0530 | [diff] [blame] | 106 | import BVToastMixin from '@/components/Mixins/BVToastMixin'; |
Yoshie Muranaka | e5be9ba | 2020-04-30 10:13:40 -0700 | [diff] [blame] | 107 | import LoadingBarMixin from '@/components/Mixins/LoadingBarMixin'; |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 108 | |
| 109 | export default { |
| 110 | name: 'SslCertificates', |
| 111 | components: { |
Yoshie Muranaka | e45f54b | 2020-03-26 15:23:34 -0700 | [diff] [blame] | 112 | Alert, |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 113 | IconAdd, |
| 114 | IconReplace, |
| 115 | IconTrashcan, |
Yoshie Muranaka | 532a4b0 | 2020-03-27 11:00:50 -0700 | [diff] [blame] | 116 | ModalGenerateCsr, |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 117 | ModalUploadCertificate, |
| 118 | PageTitle, |
Yoshie Muranaka | e45f54b | 2020-03-26 15:23:34 -0700 | [diff] [blame] | 119 | StatusIcon, |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 120 | TableRowAction |
| 121 | }, |
Yoshie Muranaka | e5be9ba | 2020-04-30 10:13:40 -0700 | [diff] [blame] | 122 | mixins: [BVToastMixin, LoadingBarMixin], |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 123 | data() { |
| 124 | return { |
| 125 | modalCertificate: null, |
| 126 | fields: [ |
| 127 | { |
| 128 | key: 'certificate', |
| 129 | label: this.$t('pageSslCertificates.table.certificate') |
| 130 | }, |
| 131 | { |
| 132 | key: 'issuedBy', |
| 133 | label: this.$t('pageSslCertificates.table.issuedBy') |
| 134 | }, |
| 135 | { |
| 136 | key: 'issuedTo', |
| 137 | label: this.$t('pageSslCertificates.table.issuedTo') |
| 138 | }, |
| 139 | { |
| 140 | key: 'validFrom', |
| 141 | label: this.$t('pageSslCertificates.table.validFrom') |
| 142 | }, |
| 143 | { |
| 144 | key: 'validUntil', |
| 145 | label: this.$t('pageSslCertificates.table.validUntil') |
| 146 | }, |
| 147 | { |
| 148 | key: 'actions', |
| 149 | label: '', |
SurenNeware | 5e25e28 | 2020-07-08 15:57:23 +0530 | [diff] [blame] | 150 | tdClass: 'text-right text-nowrap' |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 151 | } |
| 152 | ] |
| 153 | }; |
| 154 | }, |
| 155 | computed: { |
| 156 | certificates() { |
| 157 | return this.$store.getters['sslCertificates/allCertificates']; |
| 158 | }, |
| 159 | tableItems() { |
| 160 | return this.certificates.map(certificate => { |
| 161 | return { |
| 162 | ...certificate, |
| 163 | actions: [ |
| 164 | { |
| 165 | value: 'replace', |
| 166 | title: this.$t('pageSslCertificates.replaceCertificate') |
| 167 | }, |
| 168 | { |
| 169 | value: 'delete', |
| 170 | title: this.$t('pageSslCertificates.deleteCertificate'), |
| 171 | enabled: |
| 172 | certificate.type === 'TrustStore Certificate' ? true : false |
| 173 | } |
| 174 | ] |
| 175 | }; |
| 176 | }); |
| 177 | }, |
| 178 | certificatesForUpload() { |
| 179 | return this.$store.getters['sslCertificates/availableUploadTypes']; |
Yoshie Muranaka | e45f54b | 2020-03-26 15:23:34 -0700 | [diff] [blame] | 180 | }, |
| 181 | bmcTime() { |
| 182 | return this.$store.getters['global/bmcTime']; |
| 183 | }, |
| 184 | expiredCertificateTypes() { |
| 185 | return this.certificates.reduce((acc, val) => { |
| 186 | const daysUntilExpired = this.getDaysUntilExpired(val.validUntil); |
| 187 | if (daysUntilExpired < 1) { |
| 188 | acc.push(val.certificate); |
| 189 | } |
| 190 | return acc; |
| 191 | }, []); |
| 192 | }, |
| 193 | expiringCertificateTypes() { |
| 194 | return this.certificates.reduce((acc, val) => { |
| 195 | const daysUntilExpired = this.getDaysUntilExpired(val.validUntil); |
| 196 | if (daysUntilExpired < 31 && daysUntilExpired > 0) { |
| 197 | acc.push(val.certificate); |
| 198 | } |
| 199 | return acc; |
| 200 | }, []); |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 201 | } |
| 202 | }, |
Yoshie Muranaka | e5be9ba | 2020-04-30 10:13:40 -0700 | [diff] [blame] | 203 | async created() { |
| 204 | this.startLoader(); |
| 205 | await this.$store.dispatch('global/getBmcTime'); |
| 206 | this.$store |
| 207 | .dispatch('sslCertificates/getCertificates') |
| 208 | .finally(() => this.endLoader()); |
| 209 | }, |
| 210 | beforeRouteLeave(to, from, next) { |
| 211 | this.hideLoader(); |
| 212 | next(); |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 213 | }, |
| 214 | methods: { |
| 215 | onTableRowAction(event, rowItem) { |
| 216 | switch (event) { |
| 217 | case 'replace': |
| 218 | this.initModalUploadCertificate(rowItem); |
| 219 | break; |
| 220 | case 'delete': |
| 221 | this.initModalDeleteCertificate(rowItem); |
| 222 | break; |
| 223 | default: |
| 224 | break; |
| 225 | } |
| 226 | }, |
| 227 | initModalUploadCertificate(certificate = null) { |
| 228 | this.modalCertificate = certificate; |
| 229 | this.$bvModal.show('upload-certificate'); |
| 230 | }, |
| 231 | initModalDeleteCertificate(certificate) { |
| 232 | this.$bvModal |
| 233 | .msgBoxConfirm( |
| 234 | this.$t('pageSslCertificates.modal.deleteConfirmMessage', { |
| 235 | issuedBy: certificate.issuedBy, |
| 236 | certificate: certificate.certificate |
| 237 | }), |
| 238 | { |
| 239 | title: this.$t('pageSslCertificates.deleteCertificate'), |
| 240 | okTitle: this.$t('global.action.delete') |
| 241 | } |
| 242 | ) |
| 243 | .then(deleteConfirmed => { |
| 244 | if (deleteConfirmed) this.deleteCertificate(certificate); |
| 245 | }); |
| 246 | }, |
| 247 | onModalOk({ addNew, file, type, location }) { |
| 248 | if (addNew) { |
| 249 | // Upload a new certificate |
| 250 | this.addNewCertificate(file, type); |
| 251 | } else { |
| 252 | // Replace an existing certificate |
| 253 | this.replaceCertificate(file, type, location); |
| 254 | } |
| 255 | }, |
| 256 | addNewCertificate(file, type) { |
Yoshie Muranaka | e5be9ba | 2020-04-30 10:13:40 -0700 | [diff] [blame] | 257 | this.startLoader(); |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 258 | this.$store |
| 259 | .dispatch('sslCertificates/addNewCertificate', { file, type }) |
| 260 | .then(success => this.successToast(success)) |
Yoshie Muranaka | e5be9ba | 2020-04-30 10:13:40 -0700 | [diff] [blame] | 261 | .catch(({ message }) => this.errorToast(message)) |
| 262 | .finally(() => this.endLoader()); |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 263 | }, |
| 264 | replaceCertificate(file, type, location) { |
Yoshie Muranaka | e5be9ba | 2020-04-30 10:13:40 -0700 | [diff] [blame] | 265 | this.startLoader(); |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 266 | const reader = new FileReader(); |
| 267 | reader.readAsBinaryString(file); |
| 268 | reader.onloadend = event => { |
| 269 | const certificateString = event.target.result; |
| 270 | this.$store |
| 271 | .dispatch('sslCertificates/replaceCertificate', { |
| 272 | certificateString, |
| 273 | type, |
| 274 | location |
| 275 | }) |
| 276 | .then(success => this.successToast(success)) |
Yoshie Muranaka | e5be9ba | 2020-04-30 10:13:40 -0700 | [diff] [blame] | 277 | .catch(({ message }) => this.errorToast(message)) |
| 278 | .finally(() => this.endLoader()); |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 279 | }; |
| 280 | }, |
| 281 | deleteCertificate({ type, location }) { |
Yoshie Muranaka | e5be9ba | 2020-04-30 10:13:40 -0700 | [diff] [blame] | 282 | this.startLoader(); |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 283 | this.$store |
| 284 | .dispatch('sslCertificates/deleteCertificate', { |
| 285 | type, |
| 286 | location |
| 287 | }) |
| 288 | .then(success => this.successToast(success)) |
Yoshie Muranaka | e5be9ba | 2020-04-30 10:13:40 -0700 | [diff] [blame] | 289 | .catch(({ message }) => this.errorToast(message)) |
| 290 | .finally(() => this.endLoader()); |
Yoshie Muranaka | e45f54b | 2020-03-26 15:23:34 -0700 | [diff] [blame] | 291 | }, |
| 292 | getDaysUntilExpired(date) { |
| 293 | if (this.bmcTime) { |
| 294 | const validUntilMs = date.getTime(); |
| 295 | const currentBmcTimeMs = this.bmcTime.getTime(); |
| 296 | const oneDayInMs = 24 * 60 * 60 * 1000; |
| 297 | return Math.round((validUntilMs - currentBmcTimeMs) / oneDayInMs); |
| 298 | } |
Yoshie Muranaka | e5be9ba | 2020-04-30 10:13:40 -0700 | [diff] [blame] | 299 | return new Date(); |
Yoshie Muranaka | e45f54b | 2020-03-26 15:23:34 -0700 | [diff] [blame] | 300 | }, |
| 301 | getIconStatus(date) { |
| 302 | const daysUntilExpired = this.getDaysUntilExpired(date); |
| 303 | if (daysUntilExpired < 1) { |
| 304 | return 'danger'; |
| 305 | } else if (daysUntilExpired < 31) { |
| 306 | return 'warning'; |
| 307 | } |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 308 | } |
| 309 | } |
| 310 | }; |
| 311 | </script> |