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