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