Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame^] | 1 | import api from '../../api'; |
| 2 | import i18n from '../../../i18n'; |
| 3 | |
| 4 | const CERTIFICATE_TYPES = [ |
| 5 | { |
| 6 | type: 'HTTPS Certificate', |
| 7 | location: '/redfish/v1/Managers/bmc/NetworkProtocol/HTTPS/Certificates/', |
| 8 | label: i18n.t('pageSslCertificates.httpsCertificate') |
| 9 | }, |
| 10 | { |
| 11 | type: 'LDAP Certificate', |
| 12 | location: '/redfish/v1/AccountService/LDAP/Certificates/', |
| 13 | label: i18n.t('pageSslCertificates.ldapCertificate') |
| 14 | }, |
| 15 | { |
| 16 | type: 'TrustStore Certificate', |
| 17 | location: '/redfish/v1/Managers/bmc/Truststore/Certificates/', |
| 18 | // Web UI will show 'CA Certificate' instead of |
| 19 | // 'TrustStore Certificate' after user testing revealed |
| 20 | // the term 'TrustStore Certificate' wasn't recognized/was unfamilar |
| 21 | label: i18n.t('pageSslCertificates.caCertificate') |
| 22 | } |
| 23 | ]; |
| 24 | |
| 25 | const getCertificateProp = (type, prop) => { |
| 26 | const certificate = CERTIFICATE_TYPES.find( |
| 27 | certificate => certificate.type === type |
| 28 | ); |
| 29 | return certificate ? certificate[prop] : null; |
| 30 | }; |
| 31 | |
| 32 | const SslCertificatesStore = { |
| 33 | namespaced: true, |
| 34 | state: { |
| 35 | allCertificates: [], |
| 36 | availableUploadTypes: [] |
| 37 | }, |
| 38 | getters: { |
| 39 | allCertificates: state => state.allCertificates, |
| 40 | availableUploadTypes: state => state.availableUploadTypes |
| 41 | }, |
| 42 | mutations: { |
| 43 | setCertificates(state, certificates) { |
| 44 | state.allCertificates = certificates; |
| 45 | }, |
| 46 | setAvailableUploadTypes(state, availableUploadTypes) { |
| 47 | state.availableUploadTypes = availableUploadTypes; |
| 48 | } |
| 49 | }, |
| 50 | actions: { |
| 51 | getCertificates({ commit }) { |
| 52 | api |
| 53 | .get('/redfish/v1/CertificateService/CertificateLocations') |
| 54 | .then(({ data: { Links: { Certificates } } }) => |
| 55 | Certificates.map(certificate => certificate['@odata.id']) |
| 56 | ) |
| 57 | .then(certificateLocations => { |
| 58 | const promises = certificateLocations.map(location => |
| 59 | api.get(location) |
| 60 | ); |
| 61 | api.all(promises).then( |
| 62 | api.spread((...responses) => { |
| 63 | const certificates = responses.map(({ data }) => { |
| 64 | const { |
| 65 | Name, |
| 66 | ValidNotAfter, |
| 67 | ValidNotBefore, |
| 68 | Issuer = {}, |
| 69 | Subject = {} |
| 70 | } = data; |
| 71 | return { |
| 72 | type: Name, |
| 73 | location: data['@odata.id'], |
| 74 | certificate: getCertificateProp(Name, 'label'), |
| 75 | issuedBy: Issuer.CommonName, |
| 76 | issuedTo: Subject.CommonName, |
| 77 | validFrom: new Date(ValidNotBefore), |
| 78 | validUntil: new Date(ValidNotAfter) |
| 79 | }; |
| 80 | }); |
| 81 | const availableUploadTypes = CERTIFICATE_TYPES.filter( |
| 82 | ({ type }) => |
| 83 | !certificates |
| 84 | .map(certificate => certificate.type) |
| 85 | .includes(type) |
| 86 | ); |
| 87 | |
| 88 | commit('setCertificates', certificates); |
| 89 | commit('setAvailableUploadTypes', availableUploadTypes); |
| 90 | }) |
| 91 | ); |
| 92 | }); |
| 93 | }, |
| 94 | async addNewCertificate({ dispatch }, { file, type }) { |
| 95 | return await api |
| 96 | .post(getCertificateProp(type, 'location'), file, { |
| 97 | headers: { 'Content-Type': 'application/x-pem-file' } |
| 98 | }) |
| 99 | .then(() => dispatch('getCertificates')) |
| 100 | .then(() => |
| 101 | i18n.t('pageSslCertificates.toast.successAddCertificate', { |
| 102 | certificate: getCertificateProp(type, 'label') |
| 103 | }) |
| 104 | ) |
| 105 | .catch(error => { |
| 106 | console.log(error); |
| 107 | throw new Error( |
| 108 | i18n.t('pageSslCertificates.toast.errorAddCertificate') |
| 109 | ); |
| 110 | }); |
| 111 | }, |
| 112 | async replaceCertificate( |
| 113 | { dispatch }, |
| 114 | { certificateString, location, type } |
| 115 | ) { |
| 116 | const data = {}; |
| 117 | data.CertificateString = certificateString; |
| 118 | data.CertificateType = 'PEM'; |
| 119 | data.CertificateUri = { '@odata.id': location }; |
| 120 | |
| 121 | return await api |
| 122 | .post( |
| 123 | '/redfish/v1/CertificateService/Actions/CertificateService.ReplaceCertificate', |
| 124 | data |
| 125 | ) |
| 126 | .then(() => dispatch('getCertificates')) |
| 127 | .then(() => |
| 128 | i18n.t('pageSslCertificates.toast.successReplaceCertificate', { |
| 129 | certificate: getCertificateProp(type, 'label') |
| 130 | }) |
| 131 | ) |
| 132 | .catch(error => { |
| 133 | console.log(error); |
| 134 | throw new Error( |
| 135 | i18n.t('pageSslCertificates.toast.errorReplaceCertificate') |
| 136 | ); |
| 137 | }); |
| 138 | }, |
| 139 | async deleteCertificate({ dispatch }, { type, location }) { |
| 140 | return await api |
| 141 | .delete(location) |
| 142 | .then(() => dispatch('getCertificates')) |
| 143 | .then(() => |
| 144 | i18n.t('pageSslCertificates.toast.successDeleteCertificate', { |
| 145 | certificate: getCertificateProp(type, 'label') |
| 146 | }) |
| 147 | ) |
| 148 | .catch(error => { |
| 149 | console.log(error); |
| 150 | throw new Error( |
| 151 | i18n.t('pageSslCertificates.toast.errorDeleteCertificate') |
| 152 | ); |
| 153 | }); |
| 154 | } |
| 155 | } |
| 156 | }; |
| 157 | |
| 158 | export default SslCertificatesStore; |