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