xref: /openbmc/webui-vue/src/store/modules/SecurityAndAccess/CertificatesStore.js (revision de23ea23d88451a2fa2774ec72053772603c23ae)
1b440616cSSandeepa Singhimport api from '@/store/api';
2b440616cSSandeepa Singhimport i18n from '@/i18n';
3b440616cSSandeepa Singh
48841b7d4SSean Zhangconst getCertificateProp = (certificateTypes, type, prop) => {
58841b7d4SSean 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: [],
168841b7d4SSean Zhang    certificateTypes: [],
17b440616cSSandeepa Singh  },
18b440616cSSandeepa Singh  getters: {
19b440616cSSandeepa Singh    allCertificates: (state) => state.allCertificates,
20b440616cSSandeepa Singh    availableUploadTypes: (state) => state.availableUploadTypes,
218841b7d4SSean 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    },
308841b7d4SSean Zhang    setCertificateTypes(state, certificateTypes) {
318841b7d4SSean Zhang      state.certificateTypes = certificateTypes;
328841b7d4SSean Zhang    },
33b440616cSSandeepa Singh  },
34b440616cSSandeepa Singh  actions: {
358841b7d4SSean Zhang    async getCertificateTypes({ commit }) {
368841b7d4SSean Zhang      const certificateTypes = [
378841b7d4SSean Zhang        {
388841b7d4SSean Zhang          type: 'HTTPS Certificate',
398841b7d4SSean Zhang          location: `${await this.dispatch(
408841b7d4SSean Zhang            'global/getBmcPath',
418841b7d4SSean Zhang          )}/NetworkProtocol/HTTPS/Certificates/`,
42*de23ea23SSurya V          label: i18n.global.t('pageCertificates.httpsCertificate'),
438841b7d4SSean Zhang        },
448841b7d4SSean Zhang        {
458841b7d4SSean Zhang          type: 'LDAP Certificate',
468841b7d4SSean Zhang          location: '/redfish/v1/AccountService/LDAP/Certificates/',
47*de23ea23SSurya V          label: i18n.global.t('pageCertificates.ldapCertificate'),
488841b7d4SSean Zhang        },
498841b7d4SSean Zhang        {
508841b7d4SSean Zhang          type: 'TrustStore Certificate',
518841b7d4SSean Zhang          location: `${await this.dispatch(
528841b7d4SSean Zhang            'global/getBmcPath',
538841b7d4SSean Zhang          )}/Truststore/Certificates/`,
548841b7d4SSean Zhang          // Web UI will show 'CA Certificate' instead of
558841b7d4SSean Zhang          // 'TrustStore Certificate' after user testing revealed
568841b7d4SSean Zhang          // the term 'TrustStore Certificate' wasn't recognized/was unfamilar
57*de23ea23SSurya V          label: i18n.global.t('pageCertificates.caCertificate'),
588841b7d4SSean Zhang        },
598841b7d4SSean Zhang      ];
608841b7d4SSean Zhang      await commit('setCertificateTypes', certificateTypes);
618841b7d4SSean Zhang    },
628841b7d4SSean Zhang    async getCertificates({ dispatch, getters, commit }) {
638841b7d4SSean 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'],
908841b7d4SSean Zhang                  certificate: getCertificateProp(
918841b7d4SSean Zhang                    getters['certificateTypes'],
928841b7d4SSean Zhang                    Name,
938841b7d4SSean Zhang                    'label',
948841b7d4SSean 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              });
1018841b7d4SSean 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    },
1148841b7d4SSean Zhang    async addNewCertificate({ dispatch, getters }, { file, type }) {
115b440616cSSandeepa Singh      return await api
1168841b7d4SSean Zhang        .post(
1178841b7d4SSean Zhang          getCertificateProp(getters['certificateTypes'], type, 'location'),
1188841b7d4SSean Zhang          file,
1198841b7d4SSean Zhang          {
120b440616cSSandeepa Singh            headers: { 'Content-Type': 'application/x-pem-file' },
1218841b7d4SSean Zhang          },
1228841b7d4SSean Zhang        )
123b440616cSSandeepa Singh        .then(() => dispatch('getCertificates'))
124b440616cSSandeepa Singh        .then(() =>
125*de23ea23SSurya V          i18n.global.t('pageCertificates.toast.successAddCertificate', {
1268841b7d4SSean Zhang            certificate: getCertificateProp(
1278841b7d4SSean Zhang              getters['certificateTypes'],
1288841b7d4SSean Zhang              type,
1298841b7d4SSean Zhang              'label',
1308841b7d4SSean Zhang            ),
1318132399cSEd Tanous          }),
132b440616cSSandeepa Singh        )
133b440616cSSandeepa Singh        .catch((error) => {
134b440616cSSandeepa Singh          console.log(error);
135*de23ea23SSurya V          throw new Error(
136*de23ea23SSurya V            i18n.global.t('pageCertificates.toast.errorAddCertificate'),
137*de23ea23SSurya V          );
138b440616cSSandeepa Singh        });
139b440616cSSandeepa Singh    },
140b440616cSSandeepa Singh    async replaceCertificate(
1418841b7d4SSean Zhang      { dispatch, getters },
1428132399cSEd Tanous      { certificateString, location, type },
143b440616cSSandeepa Singh    ) {
144b440616cSSandeepa Singh      const data = {};
145b440616cSSandeepa Singh      data.CertificateString = certificateString;
146b440616cSSandeepa Singh      data.CertificateType = 'PEM';
147b440616cSSandeepa Singh      data.CertificateUri = { '@odata.id': location };
148b440616cSSandeepa Singh
149b440616cSSandeepa Singh      return await api
150b440616cSSandeepa Singh        .post(
151b440616cSSandeepa Singh          '/redfish/v1/CertificateService/Actions/CertificateService.ReplaceCertificate',
1528132399cSEd Tanous          data,
153b440616cSSandeepa Singh        )
154b440616cSSandeepa Singh        .then(() => dispatch('getCertificates'))
155b440616cSSandeepa Singh        .then(() =>
156*de23ea23SSurya V          i18n.global.t('pageCertificates.toast.successReplaceCertificate', {
1578841b7d4SSean Zhang            certificate: getCertificateProp(
1588841b7d4SSean Zhang              getters['certificateTypes'],
1598841b7d4SSean Zhang              type,
1608841b7d4SSean Zhang              'label',
1618841b7d4SSean Zhang            ),
1628132399cSEd Tanous          }),
163b440616cSSandeepa Singh        )
164b440616cSSandeepa Singh        .catch((error) => {
165b440616cSSandeepa Singh          console.log(error);
166b440616cSSandeepa Singh          throw new Error(
167*de23ea23SSurya V            i18n.global.t('pageCertificates.toast.errorReplaceCertificate'),
168b440616cSSandeepa Singh          );
169b440616cSSandeepa Singh        });
170b440616cSSandeepa Singh    },
1718841b7d4SSean Zhang    async deleteCertificate({ dispatch, getters }, { type, location }) {
172b440616cSSandeepa Singh      return await api
173b440616cSSandeepa Singh        .delete(location)
174b440616cSSandeepa Singh        .then(() => dispatch('getCertificates'))
175b440616cSSandeepa Singh        .then(() =>
176*de23ea23SSurya V          i18n.global.t('pageCertificates.toast.successDeleteCertificate', {
1778841b7d4SSean Zhang            certificate: getCertificateProp(
1788841b7d4SSean Zhang              getters['certificateTypes'],
1798841b7d4SSean Zhang              type,
1808841b7d4SSean Zhang              'label',
1818841b7d4SSean Zhang            ),
1828132399cSEd Tanous          }),
183b440616cSSandeepa Singh        )
184b440616cSSandeepa Singh        .catch((error) => {
185b440616cSSandeepa Singh          console.log(error);
186b440616cSSandeepa Singh          throw new Error(
187*de23ea23SSurya V            i18n.global.t('pageCertificates.toast.errorDeleteCertificate'),
188b440616cSSandeepa Singh          );
189b440616cSSandeepa Singh        });
190b440616cSSandeepa Singh    },
1918841b7d4SSean Zhang    async generateCsr({ getters }, userData) {
192b440616cSSandeepa Singh      const {
193b440616cSSandeepa Singh        certificateType,
194b440616cSSandeepa Singh        country,
195b440616cSSandeepa Singh        state,
196b440616cSSandeepa Singh        city,
197b440616cSSandeepa Singh        companyName,
198b440616cSSandeepa Singh        companyUnit,
199b440616cSSandeepa Singh        commonName,
200b440616cSSandeepa Singh        keyPairAlgorithm,
201b440616cSSandeepa Singh        keyBitLength,
202b440616cSSandeepa Singh        keyCurveId,
203b440616cSSandeepa Singh        contactPerson,
204b440616cSSandeepa Singh        emailAddress,
205b440616cSSandeepa Singh        alternateName,
206b440616cSSandeepa Singh      } = userData;
207b440616cSSandeepa Singh      const data = {};
208b440616cSSandeepa Singh
209b440616cSSandeepa Singh      data.CertificateCollection = {
2108841b7d4SSean Zhang        '@odata.id': getCertificateProp(
2118841b7d4SSean Zhang          getters['certificateTypes'],
2128841b7d4SSean Zhang          certificateType,
2138841b7d4SSean Zhang          'location',
2148841b7d4SSean Zhang        ),
215b440616cSSandeepa Singh      };
216b440616cSSandeepa Singh      data.Country = country;
217b440616cSSandeepa Singh      data.State = state;
218b440616cSSandeepa Singh      data.City = city;
219b440616cSSandeepa Singh      data.Organization = companyName;
220b440616cSSandeepa Singh      data.OrganizationalUnit = companyUnit;
221b440616cSSandeepa Singh      data.CommonName = commonName;
222b440616cSSandeepa Singh      data.KeyPairAlgorithm = keyPairAlgorithm;
223b440616cSSandeepa Singh      data.AlternativeNames = alternateName;
224b440616cSSandeepa Singh
225b440616cSSandeepa Singh      if (keyCurveId) data.KeyCurveId = keyCurveId;
226b440616cSSandeepa Singh      if (keyBitLength) data.KeyBitLength = keyBitLength;
227b440616cSSandeepa Singh      if (contactPerson) data.ContactPerson = contactPerson;
228b440616cSSandeepa Singh      if (emailAddress) data.Email = emailAddress;
229b440616cSSandeepa Singh
230b440616cSSandeepa Singh      return await api
231b440616cSSandeepa Singh        .post(
232b440616cSSandeepa Singh          '/redfish/v1/CertificateService/Actions/CertificateService.GenerateCSR',
2338132399cSEd Tanous          data,
234b440616cSSandeepa Singh        )
235b440616cSSandeepa Singh        //TODO: Success response also throws error so
236b440616cSSandeepa Singh        // can't accurately show legitimate error in UI
237b440616cSSandeepa Singh        .catch((error) => console.log(error));
238b440616cSSandeepa Singh    },
239b440616cSSandeepa Singh  },
240b440616cSSandeepa Singh};
241b440616cSSandeepa Singh
242b440616cSSandeepa Singhexport default CertificatesStore;
243