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 | |
Sean Zhang | 8841b7d | 2024-06-15 08:42:41 +0300 | [diff] [blame^] | 4 | const getCertificateProp = (certificateTypes, type, prop) => { |
| 5 | const certificate = certificateTypes.find( |
Ed Tanous | 8132399 | 2024-02-27 11:26:24 -0800 | [diff] [blame] | 6 | (certificate) => certificate.type === type, |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 7 | ); |
| 8 | return certificate ? certificate[prop] : null; |
| 9 | }; |
| 10 | |
Sandeepa Singh | b440616 | 2021-07-26 15:05:39 +0530 | [diff] [blame] | 11 | const CertificatesStore = { |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 12 | namespaced: true, |
| 13 | state: { |
| 14 | allCertificates: [], |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 15 | availableUploadTypes: [], |
Sean Zhang | 8841b7d | 2024-06-15 08:42:41 +0300 | [diff] [blame^] | 16 | certificateTypes: [], |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 17 | }, |
| 18 | getters: { |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 19 | allCertificates: (state) => state.allCertificates, |
| 20 | availableUploadTypes: (state) => state.availableUploadTypes, |
Sean Zhang | 8841b7d | 2024-06-15 08:42:41 +0300 | [diff] [blame^] | 21 | certificateTypes: (state) => state.certificateTypes, |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 22 | }, |
| 23 | mutations: { |
| 24 | setCertificates(state, certificates) { |
| 25 | state.allCertificates = certificates; |
| 26 | }, |
| 27 | setAvailableUploadTypes(state, availableUploadTypes) { |
| 28 | state.availableUploadTypes = availableUploadTypes; |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 29 | }, |
Sean Zhang | 8841b7d | 2024-06-15 08:42:41 +0300 | [diff] [blame^] | 30 | setCertificateTypes(state, certificateTypes) { |
| 31 | state.certificateTypes = certificateTypes; |
| 32 | }, |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 33 | }, |
| 34 | actions: { |
Sean Zhang | 8841b7d | 2024-06-15 08:42:41 +0300 | [diff] [blame^] | 35 | async getCertificateTypes({ commit }) { |
| 36 | const certificateTypes = [ |
| 37 | { |
| 38 | type: 'HTTPS Certificate', |
| 39 | location: `${await this.dispatch( |
| 40 | 'global/getBmcPath', |
| 41 | )}/NetworkProtocol/HTTPS/Certificates/`, |
| 42 | label: i18n.t('pageCertificates.httpsCertificate'), |
| 43 | }, |
| 44 | { |
| 45 | type: 'LDAP Certificate', |
| 46 | location: '/redfish/v1/AccountService/LDAP/Certificates/', |
| 47 | label: i18n.t('pageCertificates.ldapCertificate'), |
| 48 | }, |
| 49 | { |
| 50 | type: 'TrustStore Certificate', |
| 51 | location: `${await this.dispatch( |
| 52 | 'global/getBmcPath', |
| 53 | )}/Truststore/Certificates/`, |
| 54 | // Web UI will show 'CA Certificate' instead of |
| 55 | // 'TrustStore Certificate' after user testing revealed |
| 56 | // the term 'TrustStore Certificate' wasn't recognized/was unfamilar |
| 57 | label: i18n.t('pageCertificates.caCertificate'), |
| 58 | }, |
| 59 | ]; |
| 60 | await commit('setCertificateTypes', certificateTypes); |
| 61 | }, |
| 62 | async getCertificates({ dispatch, getters, commit }) { |
| 63 | await dispatch('getCertificateTypes'); |
Yoshie Muranaka | e5be9ba | 2020-04-30 10:13:40 -0700 | [diff] [blame] | 64 | return await api |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 65 | .get('/redfish/v1/CertificateService/CertificateLocations') |
Ed Tanous | 8132399 | 2024-02-27 11:26:24 -0800 | [diff] [blame] | 66 | .then( |
| 67 | ({ |
| 68 | data: { |
| 69 | Links: { Certificates }, |
| 70 | }, |
| 71 | }) => Certificates.map((certificate) => certificate['@odata.id']), |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 72 | ) |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 73 | .then((certificateLocations) => { |
| 74 | const promises = certificateLocations.map((location) => |
Ed Tanous | 8132399 | 2024-02-27 11:26:24 -0800 | [diff] [blame] | 75 | api.get(location), |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 76 | ); |
| 77 | api.all(promises).then( |
| 78 | api.spread((...responses) => { |
| 79 | const certificates = responses.map(({ data }) => { |
| 80 | const { |
| 81 | Name, |
| 82 | ValidNotAfter, |
| 83 | ValidNotBefore, |
| 84 | Issuer = {}, |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 85 | Subject = {}, |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 86 | } = data; |
| 87 | return { |
| 88 | type: Name, |
| 89 | location: data['@odata.id'], |
Sean Zhang | 8841b7d | 2024-06-15 08:42:41 +0300 | [diff] [blame^] | 90 | certificate: getCertificateProp( |
| 91 | getters['certificateTypes'], |
| 92 | Name, |
| 93 | 'label', |
| 94 | ), |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 95 | issuedBy: Issuer.CommonName, |
| 96 | issuedTo: Subject.CommonName, |
| 97 | validFrom: new Date(ValidNotBefore), |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 98 | validUntil: new Date(ValidNotAfter), |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 99 | }; |
| 100 | }); |
Sean Zhang | 8841b7d | 2024-06-15 08:42:41 +0300 | [diff] [blame^] | 101 | const availableUploadTypes = getters['certificateTypes'].filter( |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 102 | ({ type }) => |
| 103 | !certificates |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 104 | .map((certificate) => certificate.type) |
Ed Tanous | 8132399 | 2024-02-27 11:26:24 -0800 | [diff] [blame] | 105 | .includes(type), |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 106 | ); |
| 107 | |
| 108 | commit('setCertificates', certificates); |
| 109 | commit('setAvailableUploadTypes', availableUploadTypes); |
Ed Tanous | 8132399 | 2024-02-27 11:26:24 -0800 | [diff] [blame] | 110 | }), |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 111 | ); |
| 112 | }); |
| 113 | }, |
Sean Zhang | 8841b7d | 2024-06-15 08:42:41 +0300 | [diff] [blame^] | 114 | async addNewCertificate({ dispatch, getters }, { file, type }) { |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 115 | return await api |
Sean Zhang | 8841b7d | 2024-06-15 08:42:41 +0300 | [diff] [blame^] | 116 | .post( |
| 117 | getCertificateProp(getters['certificateTypes'], type, 'location'), |
| 118 | file, |
| 119 | { |
| 120 | headers: { 'Content-Type': 'application/x-pem-file' }, |
| 121 | }, |
| 122 | ) |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 123 | .then(() => dispatch('getCertificates')) |
| 124 | .then(() => |
Sandeepa Singh | b440616 | 2021-07-26 15:05:39 +0530 | [diff] [blame] | 125 | i18n.t('pageCertificates.toast.successAddCertificate', { |
Sean Zhang | 8841b7d | 2024-06-15 08:42:41 +0300 | [diff] [blame^] | 126 | certificate: getCertificateProp( |
| 127 | getters['certificateTypes'], |
| 128 | type, |
| 129 | 'label', |
| 130 | ), |
Ed Tanous | 8132399 | 2024-02-27 11:26:24 -0800 | [diff] [blame] | 131 | }), |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 132 | ) |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 133 | .catch((error) => { |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 134 | console.log(error); |
Sandeepa Singh | b440616 | 2021-07-26 15:05:39 +0530 | [diff] [blame] | 135 | throw new Error(i18n.t('pageCertificates.toast.errorAddCertificate')); |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 136 | }); |
| 137 | }, |
| 138 | async replaceCertificate( |
Sean Zhang | 8841b7d | 2024-06-15 08:42:41 +0300 | [diff] [blame^] | 139 | { dispatch, getters }, |
Ed Tanous | 8132399 | 2024-02-27 11:26:24 -0800 | [diff] [blame] | 140 | { certificateString, location, type }, |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 141 | ) { |
| 142 | const data = {}; |
| 143 | data.CertificateString = certificateString; |
| 144 | data.CertificateType = 'PEM'; |
| 145 | data.CertificateUri = { '@odata.id': location }; |
| 146 | |
| 147 | return await api |
| 148 | .post( |
| 149 | '/redfish/v1/CertificateService/Actions/CertificateService.ReplaceCertificate', |
Ed Tanous | 8132399 | 2024-02-27 11:26:24 -0800 | [diff] [blame] | 150 | data, |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 151 | ) |
| 152 | .then(() => dispatch('getCertificates')) |
| 153 | .then(() => |
Sandeepa Singh | b440616 | 2021-07-26 15:05:39 +0530 | [diff] [blame] | 154 | i18n.t('pageCertificates.toast.successReplaceCertificate', { |
Sean Zhang | 8841b7d | 2024-06-15 08:42:41 +0300 | [diff] [blame^] | 155 | certificate: getCertificateProp( |
| 156 | getters['certificateTypes'], |
| 157 | type, |
| 158 | 'label', |
| 159 | ), |
Ed Tanous | 8132399 | 2024-02-27 11:26:24 -0800 | [diff] [blame] | 160 | }), |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 161 | ) |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 162 | .catch((error) => { |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 163 | console.log(error); |
| 164 | throw new Error( |
Ed Tanous | 8132399 | 2024-02-27 11:26:24 -0800 | [diff] [blame] | 165 | i18n.t('pageCertificates.toast.errorReplaceCertificate'), |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 166 | ); |
| 167 | }); |
| 168 | }, |
Sean Zhang | 8841b7d | 2024-06-15 08:42:41 +0300 | [diff] [blame^] | 169 | async deleteCertificate({ dispatch, getters }, { type, location }) { |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 170 | return await api |
| 171 | .delete(location) |
| 172 | .then(() => dispatch('getCertificates')) |
| 173 | .then(() => |
Sandeepa Singh | b440616 | 2021-07-26 15:05:39 +0530 | [diff] [blame] | 174 | i18n.t('pageCertificates.toast.successDeleteCertificate', { |
Sean Zhang | 8841b7d | 2024-06-15 08:42:41 +0300 | [diff] [blame^] | 175 | certificate: getCertificateProp( |
| 176 | getters['certificateTypes'], |
| 177 | type, |
| 178 | 'label', |
| 179 | ), |
Ed Tanous | 8132399 | 2024-02-27 11:26:24 -0800 | [diff] [blame] | 180 | }), |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 181 | ) |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 182 | .catch((error) => { |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 183 | console.log(error); |
| 184 | throw new Error( |
Ed Tanous | 8132399 | 2024-02-27 11:26:24 -0800 | [diff] [blame] | 185 | i18n.t('pageCertificates.toast.errorDeleteCertificate'), |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 186 | ); |
| 187 | }); |
Yoshie Muranaka | 532a4b0 | 2020-03-27 11:00:50 -0700 | [diff] [blame] | 188 | }, |
Sean Zhang | 8841b7d | 2024-06-15 08:42:41 +0300 | [diff] [blame^] | 189 | async generateCsr({ getters }, userData) { |
Yoshie Muranaka | 532a4b0 | 2020-03-27 11:00:50 -0700 | [diff] [blame] | 190 | const { |
| 191 | certificateType, |
| 192 | country, |
| 193 | state, |
| 194 | city, |
| 195 | companyName, |
| 196 | companyUnit, |
| 197 | commonName, |
| 198 | keyPairAlgorithm, |
| 199 | keyBitLength, |
| 200 | keyCurveId, |
Yoshie Muranaka | 532a4b0 | 2020-03-27 11:00:50 -0700 | [diff] [blame] | 201 | contactPerson, |
| 202 | emailAddress, |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 203 | alternateName, |
Yoshie Muranaka | 532a4b0 | 2020-03-27 11:00:50 -0700 | [diff] [blame] | 204 | } = userData; |
| 205 | const data = {}; |
| 206 | |
| 207 | data.CertificateCollection = { |
Sean Zhang | 8841b7d | 2024-06-15 08:42:41 +0300 | [diff] [blame^] | 208 | '@odata.id': getCertificateProp( |
| 209 | getters['certificateTypes'], |
| 210 | certificateType, |
| 211 | 'location', |
| 212 | ), |
Yoshie Muranaka | 532a4b0 | 2020-03-27 11:00:50 -0700 | [diff] [blame] | 213 | }; |
| 214 | data.Country = country; |
| 215 | data.State = state; |
| 216 | data.City = city; |
| 217 | data.Organization = companyName; |
| 218 | data.OrganizationalUnit = companyUnit; |
| 219 | data.CommonName = commonName; |
| 220 | data.KeyPairAlgorithm = keyPairAlgorithm; |
| 221 | data.AlternativeNames = alternateName; |
| 222 | |
| 223 | if (keyCurveId) data.KeyCurveId = keyCurveId; |
| 224 | if (keyBitLength) data.KeyBitLength = keyBitLength; |
Yoshie Muranaka | 532a4b0 | 2020-03-27 11:00:50 -0700 | [diff] [blame] | 225 | if (contactPerson) data.ContactPerson = contactPerson; |
| 226 | if (emailAddress) data.Email = emailAddress; |
| 227 | |
| 228 | return await api |
| 229 | .post( |
| 230 | '/redfish/v1/CertificateService/Actions/CertificateService.GenerateCSR', |
Ed Tanous | 8132399 | 2024-02-27 11:26:24 -0800 | [diff] [blame] | 231 | data, |
Yoshie Muranaka | 532a4b0 | 2020-03-27 11:00:50 -0700 | [diff] [blame] | 232 | ) |
| 233 | //TODO: Success response also throws error so |
| 234 | // can't accurately show legitimate error in UI |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 235 | .catch((error) => console.log(error)); |
| 236 | }, |
| 237 | }, |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 238 | }; |
| 239 | |
Sandeepa Singh | b440616 | 2021-07-26 15:05:39 +0530 | [diff] [blame] | 240 | export default CertificatesStore; |