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( |
Ed Tanous | 8132399 | 2024-02-27 11:26:24 -0800 | [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') |
Ed Tanous | 8132399 | 2024-02-27 11:26:24 -0800 | [diff] [blame^] | 54 | .then( |
| 55 | ({ |
| 56 | data: { |
| 57 | Links: { Certificates }, |
| 58 | }, |
| 59 | }) => Certificates.map((certificate) => certificate['@odata.id']), |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 60 | ) |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 61 | .then((certificateLocations) => { |
| 62 | const promises = certificateLocations.map((location) => |
Ed Tanous | 8132399 | 2024-02-27 11:26:24 -0800 | [diff] [blame^] | 63 | api.get(location), |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 64 | ); |
| 65 | api.all(promises).then( |
| 66 | api.spread((...responses) => { |
| 67 | const certificates = responses.map(({ data }) => { |
| 68 | const { |
| 69 | Name, |
| 70 | ValidNotAfter, |
| 71 | ValidNotBefore, |
| 72 | Issuer = {}, |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 73 | Subject = {}, |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 74 | } = data; |
| 75 | return { |
| 76 | type: Name, |
| 77 | location: data['@odata.id'], |
| 78 | certificate: getCertificateProp(Name, 'label'), |
| 79 | issuedBy: Issuer.CommonName, |
| 80 | issuedTo: Subject.CommonName, |
| 81 | validFrom: new Date(ValidNotBefore), |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 82 | validUntil: new Date(ValidNotAfter), |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 83 | }; |
| 84 | }); |
| 85 | const availableUploadTypes = CERTIFICATE_TYPES.filter( |
| 86 | ({ type }) => |
| 87 | !certificates |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 88 | .map((certificate) => certificate.type) |
Ed Tanous | 8132399 | 2024-02-27 11:26:24 -0800 | [diff] [blame^] | 89 | .includes(type), |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 90 | ); |
| 91 | |
| 92 | commit('setCertificates', certificates); |
| 93 | commit('setAvailableUploadTypes', availableUploadTypes); |
Ed Tanous | 8132399 | 2024-02-27 11:26:24 -0800 | [diff] [blame^] | 94 | }), |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 95 | ); |
| 96 | }); |
| 97 | }, |
| 98 | async addNewCertificate({ dispatch }, { file, type }) { |
| 99 | return await api |
| 100 | .post(getCertificateProp(type, 'location'), file, { |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 101 | headers: { 'Content-Type': 'application/x-pem-file' }, |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 102 | }) |
| 103 | .then(() => dispatch('getCertificates')) |
| 104 | .then(() => |
Sandeepa Singh | b440616 | 2021-07-26 15:05:39 +0530 | [diff] [blame] | 105 | i18n.t('pageCertificates.toast.successAddCertificate', { |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 106 | certificate: getCertificateProp(type, 'label'), |
Ed Tanous | 8132399 | 2024-02-27 11:26:24 -0800 | [diff] [blame^] | 107 | }), |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 108 | ) |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 109 | .catch((error) => { |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 110 | console.log(error); |
Sandeepa Singh | b440616 | 2021-07-26 15:05:39 +0530 | [diff] [blame] | 111 | throw new Error(i18n.t('pageCertificates.toast.errorAddCertificate')); |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 112 | }); |
| 113 | }, |
| 114 | async replaceCertificate( |
| 115 | { dispatch }, |
Ed Tanous | 8132399 | 2024-02-27 11:26:24 -0800 | [diff] [blame^] | 116 | { certificateString, location, type }, |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 117 | ) { |
| 118 | const data = {}; |
| 119 | data.CertificateString = certificateString; |
| 120 | data.CertificateType = 'PEM'; |
| 121 | data.CertificateUri = { '@odata.id': location }; |
| 122 | |
| 123 | return await api |
| 124 | .post( |
| 125 | '/redfish/v1/CertificateService/Actions/CertificateService.ReplaceCertificate', |
Ed Tanous | 8132399 | 2024-02-27 11:26:24 -0800 | [diff] [blame^] | 126 | data, |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 127 | ) |
| 128 | .then(() => dispatch('getCertificates')) |
| 129 | .then(() => |
Sandeepa Singh | b440616 | 2021-07-26 15:05:39 +0530 | [diff] [blame] | 130 | i18n.t('pageCertificates.toast.successReplaceCertificate', { |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 131 | certificate: getCertificateProp(type, 'label'), |
Ed Tanous | 8132399 | 2024-02-27 11:26:24 -0800 | [diff] [blame^] | 132 | }), |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 133 | ) |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 134 | .catch((error) => { |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 135 | console.log(error); |
| 136 | throw new Error( |
Ed Tanous | 8132399 | 2024-02-27 11:26:24 -0800 | [diff] [blame^] | 137 | i18n.t('pageCertificates.toast.errorReplaceCertificate'), |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 138 | ); |
| 139 | }); |
| 140 | }, |
| 141 | async deleteCertificate({ dispatch }, { type, location }) { |
| 142 | return await api |
| 143 | .delete(location) |
| 144 | .then(() => dispatch('getCertificates')) |
| 145 | .then(() => |
Sandeepa Singh | b440616 | 2021-07-26 15:05:39 +0530 | [diff] [blame] | 146 | i18n.t('pageCertificates.toast.successDeleteCertificate', { |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 147 | certificate: getCertificateProp(type, 'label'), |
Ed Tanous | 8132399 | 2024-02-27 11:26:24 -0800 | [diff] [blame^] | 148 | }), |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 149 | ) |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 150 | .catch((error) => { |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 151 | console.log(error); |
| 152 | throw new Error( |
Ed Tanous | 8132399 | 2024-02-27 11:26:24 -0800 | [diff] [blame^] | 153 | i18n.t('pageCertificates.toast.errorDeleteCertificate'), |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 154 | ); |
| 155 | }); |
Yoshie Muranaka | 532a4b0 | 2020-03-27 11:00:50 -0700 | [diff] [blame] | 156 | }, |
| 157 | async generateCsr(_, userData) { |
| 158 | const { |
| 159 | certificateType, |
| 160 | country, |
| 161 | state, |
| 162 | city, |
| 163 | companyName, |
| 164 | companyUnit, |
| 165 | commonName, |
| 166 | keyPairAlgorithm, |
| 167 | keyBitLength, |
| 168 | keyCurveId, |
| 169 | challengePassword, |
| 170 | contactPerson, |
| 171 | emailAddress, |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 172 | alternateName, |
Yoshie Muranaka | 532a4b0 | 2020-03-27 11:00:50 -0700 | [diff] [blame] | 173 | } = userData; |
| 174 | const data = {}; |
| 175 | |
| 176 | data.CertificateCollection = { |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 177 | '@odata.id': getCertificateProp(certificateType, 'location'), |
Yoshie Muranaka | 532a4b0 | 2020-03-27 11:00:50 -0700 | [diff] [blame] | 178 | }; |
| 179 | data.Country = country; |
| 180 | data.State = state; |
| 181 | data.City = city; |
| 182 | data.Organization = companyName; |
| 183 | data.OrganizationalUnit = companyUnit; |
| 184 | data.CommonName = commonName; |
| 185 | data.KeyPairAlgorithm = keyPairAlgorithm; |
| 186 | data.AlternativeNames = alternateName; |
| 187 | |
| 188 | if (keyCurveId) data.KeyCurveId = keyCurveId; |
| 189 | if (keyBitLength) data.KeyBitLength = keyBitLength; |
| 190 | if (challengePassword) data.ChallengePassword = challengePassword; |
| 191 | if (contactPerson) data.ContactPerson = contactPerson; |
| 192 | if (emailAddress) data.Email = emailAddress; |
| 193 | |
| 194 | return await api |
| 195 | .post( |
| 196 | '/redfish/v1/CertificateService/Actions/CertificateService.GenerateCSR', |
Ed Tanous | 8132399 | 2024-02-27 11:26:24 -0800 | [diff] [blame^] | 197 | data, |
Yoshie Muranaka | 532a4b0 | 2020-03-27 11:00:50 -0700 | [diff] [blame] | 198 | ) |
| 199 | //TODO: Success response also throws error so |
| 200 | // can't accurately show legitimate error in UI |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 201 | .catch((error) => console.log(error)); |
| 202 | }, |
| 203 | }, |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 204 | }; |
| 205 | |
Sandeepa Singh | b440616 | 2021-07-26 15:05:39 +0530 | [diff] [blame] | 206 | export default CertificatesStore; |