blob: 5c7c36d2efe038589d897ebc2c399c1d52df4065 [file] [log] [blame]
SurenNeware61859092020-10-01 09:37:32 +05301import api from '@/store/api';
2import i18n from '@/i18n';
Yoshie Muranaka37393812020-03-24 15:25:24 -07003
Sean Zhang8841b7d2024-06-15 08:42:41 +03004const getCertificateProp = (certificateTypes, type, prop) => {
5 const certificate = certificateTypes.find(
Ed Tanous81323992024-02-27 11:26:24 -08006 (certificate) => certificate.type === type,
Yoshie Muranaka37393812020-03-24 15:25:24 -07007 );
8 return certificate ? certificate[prop] : null;
9};
10
Sandeepa Singhb4406162021-07-26 15:05:39 +053011const CertificatesStore = {
Yoshie Muranaka37393812020-03-24 15:25:24 -070012 namespaced: true,
13 state: {
14 allCertificates: [],
Derick Montague602e98a2020-10-21 16:20:00 -050015 availableUploadTypes: [],
Sean Zhang8841b7d2024-06-15 08:42:41 +030016 certificateTypes: [],
Yoshie Muranaka37393812020-03-24 15:25:24 -070017 },
18 getters: {
Derick Montague602e98a2020-10-21 16:20:00 -050019 allCertificates: (state) => state.allCertificates,
20 availableUploadTypes: (state) => state.availableUploadTypes,
Sean Zhang8841b7d2024-06-15 08:42:41 +030021 certificateTypes: (state) => state.certificateTypes,
Yoshie Muranaka37393812020-03-24 15:25:24 -070022 },
23 mutations: {
24 setCertificates(state, certificates) {
25 state.allCertificates = certificates;
26 },
27 setAvailableUploadTypes(state, availableUploadTypes) {
28 state.availableUploadTypes = availableUploadTypes;
Derick Montague602e98a2020-10-21 16:20:00 -050029 },
Sean Zhang8841b7d2024-06-15 08:42:41 +030030 setCertificateTypes(state, certificateTypes) {
31 state.certificateTypes = certificateTypes;
32 },
Yoshie Muranaka37393812020-03-24 15:25:24 -070033 },
34 actions: {
Sean Zhang8841b7d2024-06-15 08:42:41 +030035 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 Muranakae5be9ba2020-04-30 10:13:40 -070064 return await api
Yoshie Muranaka37393812020-03-24 15:25:24 -070065 .get('/redfish/v1/CertificateService/CertificateLocations')
Ed Tanous81323992024-02-27 11:26:24 -080066 .then(
67 ({
68 data: {
69 Links: { Certificates },
70 },
71 }) => Certificates.map((certificate) => certificate['@odata.id']),
Yoshie Muranaka37393812020-03-24 15:25:24 -070072 )
Derick Montague602e98a2020-10-21 16:20:00 -050073 .then((certificateLocations) => {
74 const promises = certificateLocations.map((location) =>
Ed Tanous81323992024-02-27 11:26:24 -080075 api.get(location),
Yoshie Muranaka37393812020-03-24 15:25:24 -070076 );
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 Montague602e98a2020-10-21 16:20:00 -050085 Subject = {},
Yoshie Muranaka37393812020-03-24 15:25:24 -070086 } = data;
87 return {
88 type: Name,
89 location: data['@odata.id'],
Sean Zhang8841b7d2024-06-15 08:42:41 +030090 certificate: getCertificateProp(
91 getters['certificateTypes'],
92 Name,
93 'label',
94 ),
Yoshie Muranaka37393812020-03-24 15:25:24 -070095 issuedBy: Issuer.CommonName,
96 issuedTo: Subject.CommonName,
97 validFrom: new Date(ValidNotBefore),
Derick Montague602e98a2020-10-21 16:20:00 -050098 validUntil: new Date(ValidNotAfter),
Yoshie Muranaka37393812020-03-24 15:25:24 -070099 };
100 });
Sean Zhang8841b7d2024-06-15 08:42:41 +0300101 const availableUploadTypes = getters['certificateTypes'].filter(
Yoshie Muranaka37393812020-03-24 15:25:24 -0700102 ({ type }) =>
103 !certificates
Derick Montague602e98a2020-10-21 16:20:00 -0500104 .map((certificate) => certificate.type)
Ed Tanous81323992024-02-27 11:26:24 -0800105 .includes(type),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700106 );
107
108 commit('setCertificates', certificates);
109 commit('setAvailableUploadTypes', availableUploadTypes);
Ed Tanous81323992024-02-27 11:26:24 -0800110 }),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700111 );
112 });
113 },
Sean Zhang8841b7d2024-06-15 08:42:41 +0300114 async addNewCertificate({ dispatch, getters }, { file, type }) {
Yoshie Muranaka37393812020-03-24 15:25:24 -0700115 return await api
Sean Zhang8841b7d2024-06-15 08:42:41 +0300116 .post(
117 getCertificateProp(getters['certificateTypes'], type, 'location'),
118 file,
119 {
120 headers: { 'Content-Type': 'application/x-pem-file' },
121 },
122 )
Yoshie Muranaka37393812020-03-24 15:25:24 -0700123 .then(() => dispatch('getCertificates'))
124 .then(() =>
Sandeepa Singhb4406162021-07-26 15:05:39 +0530125 i18n.t('pageCertificates.toast.successAddCertificate', {
Sean Zhang8841b7d2024-06-15 08:42:41 +0300126 certificate: getCertificateProp(
127 getters['certificateTypes'],
128 type,
129 'label',
130 ),
Ed Tanous81323992024-02-27 11:26:24 -0800131 }),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700132 )
Derick Montague602e98a2020-10-21 16:20:00 -0500133 .catch((error) => {
Yoshie Muranaka37393812020-03-24 15:25:24 -0700134 console.log(error);
Sandeepa Singhb4406162021-07-26 15:05:39 +0530135 throw new Error(i18n.t('pageCertificates.toast.errorAddCertificate'));
Yoshie Muranaka37393812020-03-24 15:25:24 -0700136 });
137 },
138 async replaceCertificate(
Sean Zhang8841b7d2024-06-15 08:42:41 +0300139 { dispatch, getters },
Ed Tanous81323992024-02-27 11:26:24 -0800140 { certificateString, location, type },
Yoshie Muranaka37393812020-03-24 15:25:24 -0700141 ) {
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 Tanous81323992024-02-27 11:26:24 -0800150 data,
Yoshie Muranaka37393812020-03-24 15:25:24 -0700151 )
152 .then(() => dispatch('getCertificates'))
153 .then(() =>
Sandeepa Singhb4406162021-07-26 15:05:39 +0530154 i18n.t('pageCertificates.toast.successReplaceCertificate', {
Sean Zhang8841b7d2024-06-15 08:42:41 +0300155 certificate: getCertificateProp(
156 getters['certificateTypes'],
157 type,
158 'label',
159 ),
Ed Tanous81323992024-02-27 11:26:24 -0800160 }),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700161 )
Derick Montague602e98a2020-10-21 16:20:00 -0500162 .catch((error) => {
Yoshie Muranaka37393812020-03-24 15:25:24 -0700163 console.log(error);
164 throw new Error(
Ed Tanous81323992024-02-27 11:26:24 -0800165 i18n.t('pageCertificates.toast.errorReplaceCertificate'),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700166 );
167 });
168 },
Sean Zhang8841b7d2024-06-15 08:42:41 +0300169 async deleteCertificate({ dispatch, getters }, { type, location }) {
Yoshie Muranaka37393812020-03-24 15:25:24 -0700170 return await api
171 .delete(location)
172 .then(() => dispatch('getCertificates'))
173 .then(() =>
Sandeepa Singhb4406162021-07-26 15:05:39 +0530174 i18n.t('pageCertificates.toast.successDeleteCertificate', {
Sean Zhang8841b7d2024-06-15 08:42:41 +0300175 certificate: getCertificateProp(
176 getters['certificateTypes'],
177 type,
178 'label',
179 ),
Ed Tanous81323992024-02-27 11:26:24 -0800180 }),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700181 )
Derick Montague602e98a2020-10-21 16:20:00 -0500182 .catch((error) => {
Yoshie Muranaka37393812020-03-24 15:25:24 -0700183 console.log(error);
184 throw new Error(
Ed Tanous81323992024-02-27 11:26:24 -0800185 i18n.t('pageCertificates.toast.errorDeleteCertificate'),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700186 );
187 });
Yoshie Muranaka532a4b02020-03-27 11:00:50 -0700188 },
Sean Zhang8841b7d2024-06-15 08:42:41 +0300189 async generateCsr({ getters }, userData) {
Yoshie Muranaka532a4b02020-03-27 11:00:50 -0700190 const {
191 certificateType,
192 country,
193 state,
194 city,
195 companyName,
196 companyUnit,
197 commonName,
198 keyPairAlgorithm,
199 keyBitLength,
200 keyCurveId,
Yoshie Muranaka532a4b02020-03-27 11:00:50 -0700201 contactPerson,
202 emailAddress,
Derick Montague602e98a2020-10-21 16:20:00 -0500203 alternateName,
Yoshie Muranaka532a4b02020-03-27 11:00:50 -0700204 } = userData;
205 const data = {};
206
207 data.CertificateCollection = {
Sean Zhang8841b7d2024-06-15 08:42:41 +0300208 '@odata.id': getCertificateProp(
209 getters['certificateTypes'],
210 certificateType,
211 'location',
212 ),
Yoshie Muranaka532a4b02020-03-27 11:00:50 -0700213 };
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 Muranaka532a4b02020-03-27 11:00:50 -0700225 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 Tanous81323992024-02-27 11:26:24 -0800231 data,
Yoshie Muranaka532a4b02020-03-27 11:00:50 -0700232 )
233 //TODO: Success response also throws error so
234 // can't accurately show legitimate error in UI
Derick Montague602e98a2020-10-21 16:20:00 -0500235 .catch((error) => console.log(error));
236 },
237 },
Yoshie Muranaka37393812020-03-24 15:25:24 -0700238};
239
Sandeepa Singhb4406162021-07-26 15:05:39 +0530240export default CertificatesStore;