blob: 304c37068f6388ffc505901ab4c78aa6763f43dc [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/`,
Surya Vde23ea22024-07-11 15:19:46 +053042 label: i18n.global.t('pageCertificates.httpsCertificate'),
Sean Zhang8841b7d2024-06-15 08:42:41 +030043 },
44 {
45 type: 'LDAP Certificate',
46 location: '/redfish/v1/AccountService/LDAP/Certificates/',
Surya Vde23ea22024-07-11 15:19:46 +053047 label: i18n.global.t('pageCertificates.ldapCertificate'),
Sean Zhang8841b7d2024-06-15 08:42:41 +030048 },
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
Surya Vde23ea22024-07-11 15:19:46 +053057 label: i18n.global.t('pageCertificates.caCertificate'),
Sean Zhang8841b7d2024-06-15 08:42:41 +030058 },
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(() =>
Surya Vde23ea22024-07-11 15:19:46 +0530125 i18n.global.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);
Surya Vde23ea22024-07-11 15:19:46 +0530135 throw new Error(
136 i18n.global.t('pageCertificates.toast.errorAddCertificate'),
137 );
Yoshie Muranaka37393812020-03-24 15:25:24 -0700138 });
139 },
140 async replaceCertificate(
Sean Zhang8841b7d2024-06-15 08:42:41 +0300141 { dispatch, getters },
Ed Tanous81323992024-02-27 11:26:24 -0800142 { certificateString, location, type },
Yoshie Muranaka37393812020-03-24 15:25:24 -0700143 ) {
144 const data = {};
145 data.CertificateString = certificateString;
146 data.CertificateType = 'PEM';
147 data.CertificateUri = { '@odata.id': location };
148
149 return await api
150 .post(
151 '/redfish/v1/CertificateService/Actions/CertificateService.ReplaceCertificate',
Ed Tanous81323992024-02-27 11:26:24 -0800152 data,
Yoshie Muranaka37393812020-03-24 15:25:24 -0700153 )
154 .then(() => dispatch('getCertificates'))
155 .then(() =>
Surya Vde23ea22024-07-11 15:19:46 +0530156 i18n.global.t('pageCertificates.toast.successReplaceCertificate', {
Sean Zhang8841b7d2024-06-15 08:42:41 +0300157 certificate: getCertificateProp(
158 getters['certificateTypes'],
159 type,
160 'label',
161 ),
Ed Tanous81323992024-02-27 11:26:24 -0800162 }),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700163 )
Derick Montague602e98a2020-10-21 16:20:00 -0500164 .catch((error) => {
Yoshie Muranaka37393812020-03-24 15:25:24 -0700165 console.log(error);
166 throw new Error(
Surya Vde23ea22024-07-11 15:19:46 +0530167 i18n.global.t('pageCertificates.toast.errorReplaceCertificate'),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700168 );
169 });
170 },
Sean Zhang8841b7d2024-06-15 08:42:41 +0300171 async deleteCertificate({ dispatch, getters }, { type, location }) {
Yoshie Muranaka37393812020-03-24 15:25:24 -0700172 return await api
173 .delete(location)
174 .then(() => dispatch('getCertificates'))
175 .then(() =>
Surya Vde23ea22024-07-11 15:19:46 +0530176 i18n.global.t('pageCertificates.toast.successDeleteCertificate', {
Sean Zhang8841b7d2024-06-15 08:42:41 +0300177 certificate: getCertificateProp(
178 getters['certificateTypes'],
179 type,
180 'label',
181 ),
Ed Tanous81323992024-02-27 11:26:24 -0800182 }),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700183 )
Derick Montague602e98a2020-10-21 16:20:00 -0500184 .catch((error) => {
Yoshie Muranaka37393812020-03-24 15:25:24 -0700185 console.log(error);
186 throw new Error(
Surya Vde23ea22024-07-11 15:19:46 +0530187 i18n.global.t('pageCertificates.toast.errorDeleteCertificate'),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700188 );
189 });
Yoshie Muranaka532a4b02020-03-27 11:00:50 -0700190 },
Sean Zhang8841b7d2024-06-15 08:42:41 +0300191 async generateCsr({ getters }, userData) {
Yoshie Muranaka532a4b02020-03-27 11:00:50 -0700192 const {
193 certificateType,
194 country,
195 state,
196 city,
197 companyName,
198 companyUnit,
199 commonName,
200 keyPairAlgorithm,
201 keyBitLength,
202 keyCurveId,
Yoshie Muranaka532a4b02020-03-27 11:00:50 -0700203 contactPerson,
204 emailAddress,
Derick Montague602e98a2020-10-21 16:20:00 -0500205 alternateName,
Yoshie Muranaka532a4b02020-03-27 11:00:50 -0700206 } = userData;
207 const data = {};
208
209 data.CertificateCollection = {
Sean Zhang8841b7d2024-06-15 08:42:41 +0300210 '@odata.id': getCertificateProp(
211 getters['certificateTypes'],
212 certificateType,
213 'location',
214 ),
Yoshie Muranaka532a4b02020-03-27 11:00:50 -0700215 };
216 data.Country = country;
217 data.State = state;
218 data.City = city;
219 data.Organization = companyName;
220 data.OrganizationalUnit = companyUnit;
221 data.CommonName = commonName;
222 data.KeyPairAlgorithm = keyPairAlgorithm;
223 data.AlternativeNames = alternateName;
224
225 if (keyCurveId) data.KeyCurveId = keyCurveId;
226 if (keyBitLength) data.KeyBitLength = keyBitLength;
Yoshie Muranaka532a4b02020-03-27 11:00:50 -0700227 if (contactPerson) data.ContactPerson = contactPerson;
228 if (emailAddress) data.Email = emailAddress;
229
230 return await api
231 .post(
232 '/redfish/v1/CertificateService/Actions/CertificateService.GenerateCSR',
Ed Tanous81323992024-02-27 11:26:24 -0800233 data,
Yoshie Muranaka532a4b02020-03-27 11:00:50 -0700234 )
235 //TODO: Success response also throws error so
236 // can't accurately show legitimate error in UI
Derick Montague602e98a2020-10-21 16:20:00 -0500237 .catch((error) => console.log(error));
238 },
239 },
Yoshie Muranaka37393812020-03-24 15:25:24 -0700240};
241
Sandeepa Singhb4406162021-07-26 15:05:39 +0530242export default CertificatesStore;