| SurenNeware | 6185909 | 2020-10-01 09:37:32 +0530 | [diff] [blame] | 1 | import api from '@/store/api'; | 
|  | 2 | import i18n from '@/i18n'; | 
| Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 3 |  | 
| Yoshie Muranaka | 532a4b0 | 2020-03-27 11:00:50 -0700 | [diff] [blame] | 4 | export const CERTIFICATE_TYPES = [ | 
| Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 5 | { | 
|  | 6 | type: 'HTTPS Certificate', | 
|  | 7 | location: '/redfish/v1/Managers/bmc/NetworkProtocol/HTTPS/Certificates/', | 
| Sandeepa Singh | b440616 | 2021-07-26 15:05:39 +0530 | [diff] [blame] | 8 | label: i18n.t('pageCertificates.httpsCertificate'), | 
| Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 9 | }, | 
|  | 10 | { | 
|  | 11 | type: 'LDAP Certificate', | 
|  | 12 | location: '/redfish/v1/AccountService/LDAP/Certificates/', | 
| Sandeepa Singh | b440616 | 2021-07-26 15:05:39 +0530 | [diff] [blame] | 13 | label: i18n.t('pageCertificates.ldapCertificate'), | 
| Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 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 | 
| Sandeepa Singh | b440616 | 2021-07-26 15:05:39 +0530 | [diff] [blame] | 21 | label: i18n.t('pageCertificates.caCertificate'), | 
| Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 22 | }, | 
| Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 23 | ]; | 
|  | 24 |  | 
|  | 25 | const getCertificateProp = (type, prop) => { | 
|  | 26 | const certificate = CERTIFICATE_TYPES.find( | 
| Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 27 | (certificate) => certificate.type === type | 
| Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 28 | ); | 
|  | 29 | return certificate ? certificate[prop] : null; | 
|  | 30 | }; | 
|  | 31 |  | 
| Sandeepa Singh | b440616 | 2021-07-26 15:05:39 +0530 | [diff] [blame] | 32 | const CertificatesStore = { | 
| Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 33 | namespaced: true, | 
|  | 34 | state: { | 
|  | 35 | allCertificates: [], | 
| Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 36 | availableUploadTypes: [], | 
| Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 37 | }, | 
|  | 38 | getters: { | 
| Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 39 | allCertificates: (state) => state.allCertificates, | 
|  | 40 | availableUploadTypes: (state) => state.availableUploadTypes, | 
| Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 41 | }, | 
|  | 42 | mutations: { | 
|  | 43 | setCertificates(state, certificates) { | 
|  | 44 | state.allCertificates = certificates; | 
|  | 45 | }, | 
|  | 46 | setAvailableUploadTypes(state, availableUploadTypes) { | 
|  | 47 | state.availableUploadTypes = availableUploadTypes; | 
| Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 48 | }, | 
| Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 49 | }, | 
|  | 50 | actions: { | 
| Yoshie Muranaka | e5be9ba | 2020-04-30 10:13:40 -0700 | [diff] [blame] | 51 | async getCertificates({ commit }) { | 
|  | 52 | return await api | 
| Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 53 | .get('/redfish/v1/CertificateService/CertificateLocations') | 
|  | 54 | .then(({ data: { Links: { Certificates } } }) => | 
| Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 55 | Certificates.map((certificate) => certificate['@odata.id']) | 
| Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 56 | ) | 
| Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 57 | .then((certificateLocations) => { | 
|  | 58 | const promises = certificateLocations.map((location) => | 
| Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 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 = {}, | 
| Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 69 | Subject = {}, | 
| Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 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), | 
| Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 78 | validUntil: new Date(ValidNotAfter), | 
| Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 79 | }; | 
|  | 80 | }); | 
|  | 81 | const availableUploadTypes = CERTIFICATE_TYPES.filter( | 
|  | 82 | ({ type }) => | 
|  | 83 | !certificates | 
| Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 84 | .map((certificate) => certificate.type) | 
| Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 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, { | 
| Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 97 | headers: { 'Content-Type': 'application/x-pem-file' }, | 
| Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 98 | }) | 
|  | 99 | .then(() => dispatch('getCertificates')) | 
|  | 100 | .then(() => | 
| Sandeepa Singh | b440616 | 2021-07-26 15:05:39 +0530 | [diff] [blame] | 101 | i18n.t('pageCertificates.toast.successAddCertificate', { | 
| Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 102 | certificate: getCertificateProp(type, 'label'), | 
| Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 103 | }) | 
|  | 104 | ) | 
| Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 105 | .catch((error) => { | 
| Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 106 | console.log(error); | 
| Sandeepa Singh | b440616 | 2021-07-26 15:05:39 +0530 | [diff] [blame] | 107 | throw new Error(i18n.t('pageCertificates.toast.errorAddCertificate')); | 
| Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 108 | }); | 
|  | 109 | }, | 
|  | 110 | async replaceCertificate( | 
|  | 111 | { dispatch }, | 
|  | 112 | { certificateString, location, type } | 
|  | 113 | ) { | 
|  | 114 | const data = {}; | 
|  | 115 | data.CertificateString = certificateString; | 
|  | 116 | data.CertificateType = 'PEM'; | 
|  | 117 | data.CertificateUri = { '@odata.id': location }; | 
|  | 118 |  | 
|  | 119 | return await api | 
|  | 120 | .post( | 
|  | 121 | '/redfish/v1/CertificateService/Actions/CertificateService.ReplaceCertificate', | 
|  | 122 | data | 
|  | 123 | ) | 
|  | 124 | .then(() => dispatch('getCertificates')) | 
|  | 125 | .then(() => | 
| Sandeepa Singh | b440616 | 2021-07-26 15:05:39 +0530 | [diff] [blame] | 126 | i18n.t('pageCertificates.toast.successReplaceCertificate', { | 
| Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 127 | certificate: getCertificateProp(type, 'label'), | 
| Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 128 | }) | 
|  | 129 | ) | 
| Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 130 | .catch((error) => { | 
| Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 131 | console.log(error); | 
|  | 132 | throw new Error( | 
| Sandeepa Singh | b440616 | 2021-07-26 15:05:39 +0530 | [diff] [blame] | 133 | i18n.t('pageCertificates.toast.errorReplaceCertificate') | 
| Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 134 | ); | 
|  | 135 | }); | 
|  | 136 | }, | 
|  | 137 | async deleteCertificate({ dispatch }, { type, location }) { | 
|  | 138 | return await api | 
|  | 139 | .delete(location) | 
|  | 140 | .then(() => dispatch('getCertificates')) | 
|  | 141 | .then(() => | 
| Sandeepa Singh | b440616 | 2021-07-26 15:05:39 +0530 | [diff] [blame] | 142 | i18n.t('pageCertificates.toast.successDeleteCertificate', { | 
| Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 143 | certificate: getCertificateProp(type, 'label'), | 
| Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 144 | }) | 
|  | 145 | ) | 
| Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 146 | .catch((error) => { | 
| Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 147 | console.log(error); | 
|  | 148 | throw new Error( | 
| Sandeepa Singh | b440616 | 2021-07-26 15:05:39 +0530 | [diff] [blame] | 149 | i18n.t('pageCertificates.toast.errorDeleteCertificate') | 
| Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 150 | ); | 
|  | 151 | }); | 
| Yoshie Muranaka | 532a4b0 | 2020-03-27 11:00:50 -0700 | [diff] [blame] | 152 | }, | 
|  | 153 | async generateCsr(_, userData) { | 
|  | 154 | const { | 
|  | 155 | certificateType, | 
|  | 156 | country, | 
|  | 157 | state, | 
|  | 158 | city, | 
|  | 159 | companyName, | 
|  | 160 | companyUnit, | 
|  | 161 | commonName, | 
|  | 162 | keyPairAlgorithm, | 
|  | 163 | keyBitLength, | 
|  | 164 | keyCurveId, | 
|  | 165 | challengePassword, | 
|  | 166 | contactPerson, | 
|  | 167 | emailAddress, | 
| Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 168 | alternateName, | 
| Yoshie Muranaka | 532a4b0 | 2020-03-27 11:00:50 -0700 | [diff] [blame] | 169 | } = userData; | 
|  | 170 | const data = {}; | 
|  | 171 |  | 
|  | 172 | data.CertificateCollection = { | 
| Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 173 | '@odata.id': getCertificateProp(certificateType, 'location'), | 
| Yoshie Muranaka | 532a4b0 | 2020-03-27 11:00:50 -0700 | [diff] [blame] | 174 | }; | 
|  | 175 | data.Country = country; | 
|  | 176 | data.State = state; | 
|  | 177 | data.City = city; | 
|  | 178 | data.Organization = companyName; | 
|  | 179 | data.OrganizationalUnit = companyUnit; | 
|  | 180 | data.CommonName = commonName; | 
|  | 181 | data.KeyPairAlgorithm = keyPairAlgorithm; | 
|  | 182 | data.AlternativeNames = alternateName; | 
|  | 183 |  | 
|  | 184 | if (keyCurveId) data.KeyCurveId = keyCurveId; | 
|  | 185 | if (keyBitLength) data.KeyBitLength = keyBitLength; | 
|  | 186 | if (challengePassword) data.ChallengePassword = challengePassword; | 
|  | 187 | if (contactPerson) data.ContactPerson = contactPerson; | 
|  | 188 | if (emailAddress) data.Email = emailAddress; | 
|  | 189 |  | 
|  | 190 | return await api | 
|  | 191 | .post( | 
|  | 192 | '/redfish/v1/CertificateService/Actions/CertificateService.GenerateCSR', | 
|  | 193 | data | 
|  | 194 | ) | 
|  | 195 | //TODO: Success response also throws error so | 
|  | 196 | // can't accurately show legitimate error in UI | 
| Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 197 | .catch((error) => console.log(error)); | 
|  | 198 | }, | 
|  | 199 | }, | 
| Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 200 | }; | 
|  | 201 |  | 
| Sandeepa Singh | b440616 | 2021-07-26 15:05:39 +0530 | [diff] [blame] | 202 | export default CertificatesStore; |