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