blob: 666f5fd5dc51e4537df83c891222a162fe5cea98 [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
Yoshie Muranaka532a4b02020-03-27 11:00:50 -07004export const CERTIFICATE_TYPES = [
Yoshie Muranaka37393812020-03-24 15:25:24 -07005 {
6 type: 'HTTPS Certificate',
7 location: '/redfish/v1/Managers/bmc/NetworkProtocol/HTTPS/Certificates/',
Sandeepa Singhb4406162021-07-26 15:05:39 +05308 label: i18n.t('pageCertificates.httpsCertificate'),
Yoshie Muranaka37393812020-03-24 15:25:24 -07009 },
10 {
11 type: 'LDAP Certificate',
12 location: '/redfish/v1/AccountService/LDAP/Certificates/',
Sandeepa Singhb4406162021-07-26 15:05:39 +053013 label: i18n.t('pageCertificates.ldapCertificate'),
Yoshie Muranaka37393812020-03-24 15:25:24 -070014 },
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 Singhb4406162021-07-26 15:05:39 +053021 label: i18n.t('pageCertificates.caCertificate'),
Derick Montague602e98a2020-10-21 16:20:00 -050022 },
Yoshie Muranaka37393812020-03-24 15:25:24 -070023];
24
25const getCertificateProp = (type, prop) => {
26 const certificate = CERTIFICATE_TYPES.find(
Ed Tanous81323992024-02-27 11:26:24 -080027 (certificate) => certificate.type === type,
Yoshie Muranaka37393812020-03-24 15:25:24 -070028 );
29 return certificate ? certificate[prop] : null;
30};
31
Sandeepa Singhb4406162021-07-26 15:05:39 +053032const CertificatesStore = {
Yoshie Muranaka37393812020-03-24 15:25:24 -070033 namespaced: true,
34 state: {
35 allCertificates: [],
Derick Montague602e98a2020-10-21 16:20:00 -050036 availableUploadTypes: [],
Yoshie Muranaka37393812020-03-24 15:25:24 -070037 },
38 getters: {
Derick Montague602e98a2020-10-21 16:20:00 -050039 allCertificates: (state) => state.allCertificates,
40 availableUploadTypes: (state) => state.availableUploadTypes,
Yoshie Muranaka37393812020-03-24 15:25:24 -070041 },
42 mutations: {
43 setCertificates(state, certificates) {
44 state.allCertificates = certificates;
45 },
46 setAvailableUploadTypes(state, availableUploadTypes) {
47 state.availableUploadTypes = availableUploadTypes;
Derick Montague602e98a2020-10-21 16:20:00 -050048 },
Yoshie Muranaka37393812020-03-24 15:25:24 -070049 },
50 actions: {
Yoshie Muranakae5be9ba2020-04-30 10:13:40 -070051 async getCertificates({ commit }) {
52 return await api
Yoshie Muranaka37393812020-03-24 15:25:24 -070053 .get('/redfish/v1/CertificateService/CertificateLocations')
Ed Tanous81323992024-02-27 11:26:24 -080054 .then(
55 ({
56 data: {
57 Links: { Certificates },
58 },
59 }) => Certificates.map((certificate) => certificate['@odata.id']),
Yoshie Muranaka37393812020-03-24 15:25:24 -070060 )
Derick Montague602e98a2020-10-21 16:20:00 -050061 .then((certificateLocations) => {
62 const promises = certificateLocations.map((location) =>
Ed Tanous81323992024-02-27 11:26:24 -080063 api.get(location),
Yoshie Muranaka37393812020-03-24 15:25:24 -070064 );
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 Montague602e98a2020-10-21 16:20:00 -050073 Subject = {},
Yoshie Muranaka37393812020-03-24 15:25:24 -070074 } = 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 Montague602e98a2020-10-21 16:20:00 -050082 validUntil: new Date(ValidNotAfter),
Yoshie Muranaka37393812020-03-24 15:25:24 -070083 };
84 });
85 const availableUploadTypes = CERTIFICATE_TYPES.filter(
86 ({ type }) =>
87 !certificates
Derick Montague602e98a2020-10-21 16:20:00 -050088 .map((certificate) => certificate.type)
Ed Tanous81323992024-02-27 11:26:24 -080089 .includes(type),
Yoshie Muranaka37393812020-03-24 15:25:24 -070090 );
91
92 commit('setCertificates', certificates);
93 commit('setAvailableUploadTypes', availableUploadTypes);
Ed Tanous81323992024-02-27 11:26:24 -080094 }),
Yoshie Muranaka37393812020-03-24 15:25:24 -070095 );
96 });
97 },
98 async addNewCertificate({ dispatch }, { file, type }) {
99 return await api
100 .post(getCertificateProp(type, 'location'), file, {
Derick Montague602e98a2020-10-21 16:20:00 -0500101 headers: { 'Content-Type': 'application/x-pem-file' },
Yoshie Muranaka37393812020-03-24 15:25:24 -0700102 })
103 .then(() => dispatch('getCertificates'))
104 .then(() =>
Sandeepa Singhb4406162021-07-26 15:05:39 +0530105 i18n.t('pageCertificates.toast.successAddCertificate', {
Derick Montague602e98a2020-10-21 16:20:00 -0500106 certificate: getCertificateProp(type, 'label'),
Ed Tanous81323992024-02-27 11:26:24 -0800107 }),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700108 )
Derick Montague602e98a2020-10-21 16:20:00 -0500109 .catch((error) => {
Yoshie Muranaka37393812020-03-24 15:25:24 -0700110 console.log(error);
Sandeepa Singhb4406162021-07-26 15:05:39 +0530111 throw new Error(i18n.t('pageCertificates.toast.errorAddCertificate'));
Yoshie Muranaka37393812020-03-24 15:25:24 -0700112 });
113 },
114 async replaceCertificate(
115 { dispatch },
Ed Tanous81323992024-02-27 11:26:24 -0800116 { certificateString, location, type },
Yoshie Muranaka37393812020-03-24 15:25:24 -0700117 ) {
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 Tanous81323992024-02-27 11:26:24 -0800126 data,
Yoshie Muranaka37393812020-03-24 15:25:24 -0700127 )
128 .then(() => dispatch('getCertificates'))
129 .then(() =>
Sandeepa Singhb4406162021-07-26 15:05:39 +0530130 i18n.t('pageCertificates.toast.successReplaceCertificate', {
Derick Montague602e98a2020-10-21 16:20:00 -0500131 certificate: getCertificateProp(type, 'label'),
Ed Tanous81323992024-02-27 11:26:24 -0800132 }),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700133 )
Derick Montague602e98a2020-10-21 16:20:00 -0500134 .catch((error) => {
Yoshie Muranaka37393812020-03-24 15:25:24 -0700135 console.log(error);
136 throw new Error(
Ed Tanous81323992024-02-27 11:26:24 -0800137 i18n.t('pageCertificates.toast.errorReplaceCertificate'),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700138 );
139 });
140 },
141 async deleteCertificate({ dispatch }, { type, location }) {
142 return await api
143 .delete(location)
144 .then(() => dispatch('getCertificates'))
145 .then(() =>
Sandeepa Singhb4406162021-07-26 15:05:39 +0530146 i18n.t('pageCertificates.toast.successDeleteCertificate', {
Derick Montague602e98a2020-10-21 16:20:00 -0500147 certificate: getCertificateProp(type, 'label'),
Ed Tanous81323992024-02-27 11:26:24 -0800148 }),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700149 )
Derick Montague602e98a2020-10-21 16:20:00 -0500150 .catch((error) => {
Yoshie Muranaka37393812020-03-24 15:25:24 -0700151 console.log(error);
152 throw new Error(
Ed Tanous81323992024-02-27 11:26:24 -0800153 i18n.t('pageCertificates.toast.errorDeleteCertificate'),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700154 );
155 });
Yoshie Muranaka532a4b02020-03-27 11:00:50 -0700156 },
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,
Yoshie Muranaka532a4b02020-03-27 11:00:50 -0700169 contactPerson,
170 emailAddress,
Derick Montague602e98a2020-10-21 16:20:00 -0500171 alternateName,
Yoshie Muranaka532a4b02020-03-27 11:00:50 -0700172 } = userData;
173 const data = {};
174
175 data.CertificateCollection = {
Derick Montague602e98a2020-10-21 16:20:00 -0500176 '@odata.id': getCertificateProp(certificateType, 'location'),
Yoshie Muranaka532a4b02020-03-27 11:00:50 -0700177 };
178 data.Country = country;
179 data.State = state;
180 data.City = city;
181 data.Organization = companyName;
182 data.OrganizationalUnit = companyUnit;
183 data.CommonName = commonName;
184 data.KeyPairAlgorithm = keyPairAlgorithm;
185 data.AlternativeNames = alternateName;
186
187 if (keyCurveId) data.KeyCurveId = keyCurveId;
188 if (keyBitLength) data.KeyBitLength = keyBitLength;
Yoshie Muranaka532a4b02020-03-27 11:00:50 -0700189 if (contactPerson) data.ContactPerson = contactPerson;
190 if (emailAddress) data.Email = emailAddress;
191
192 return await api
193 .post(
194 '/redfish/v1/CertificateService/Actions/CertificateService.GenerateCSR',
Ed Tanous81323992024-02-27 11:26:24 -0800195 data,
Yoshie Muranaka532a4b02020-03-27 11:00:50 -0700196 )
197 //TODO: Success response also throws error so
198 // can't accurately show legitimate error in UI
Derick Montague602e98a2020-10-21 16:20:00 -0500199 .catch((error) => console.log(error));
200 },
201 },
Yoshie Muranaka37393812020-03-24 15:25:24 -0700202};
203
Sandeepa Singhb4406162021-07-26 15:05:39 +0530204export default CertificatesStore;