blob: 752c212429c6c8e4c9351bfec103713a197ab0ff [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/',
Derick Montague602e98a2020-10-21 16:20:00 -05008 label: i18n.t('pageSslCertificates.httpsCertificate'),
Yoshie Muranaka37393812020-03-24 15:25:24 -07009 },
10 {
11 type: 'LDAP Certificate',
12 location: '/redfish/v1/AccountService/LDAP/Certificates/',
Derick Montague602e98a2020-10-21 16:20:00 -050013 label: i18n.t('pageSslCertificates.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
Derick Montague602e98a2020-10-21 16:20:00 -050021 label: i18n.t('pageSslCertificates.caCertificate'),
22 },
Yoshie Muranaka37393812020-03-24 15:25:24 -070023];
24
25const getCertificateProp = (type, prop) => {
26 const certificate = CERTIFICATE_TYPES.find(
Derick Montague602e98a2020-10-21 16:20:00 -050027 (certificate) => certificate.type === type
Yoshie Muranaka37393812020-03-24 15:25:24 -070028 );
29 return certificate ? certificate[prop] : null;
30};
31
32const SslCertificatesStore = {
33 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')
54 .then(({ data: { Links: { Certificates } } }) =>
Derick Montague602e98a2020-10-21 16:20:00 -050055 Certificates.map((certificate) => certificate['@odata.id'])
Yoshie Muranaka37393812020-03-24 15:25:24 -070056 )
Derick Montague602e98a2020-10-21 16:20:00 -050057 .then((certificateLocations) => {
58 const promises = certificateLocations.map((location) =>
Yoshie Muranaka37393812020-03-24 15:25:24 -070059 api.get(location)
60 );
61 api.all(promises).then(
62 api.spread((...responses) => {
63 const certificates = responses.map(({ data }) => {
64 const {
65 Name,
66 ValidNotAfter,
67 ValidNotBefore,
68 Issuer = {},
Derick Montague602e98a2020-10-21 16:20:00 -050069 Subject = {},
Yoshie Muranaka37393812020-03-24 15:25:24 -070070 } = data;
71 return {
72 type: Name,
73 location: data['@odata.id'],
74 certificate: getCertificateProp(Name, 'label'),
75 issuedBy: Issuer.CommonName,
76 issuedTo: Subject.CommonName,
77 validFrom: new Date(ValidNotBefore),
Derick Montague602e98a2020-10-21 16:20:00 -050078 validUntil: new Date(ValidNotAfter),
Yoshie Muranaka37393812020-03-24 15:25:24 -070079 };
80 });
81 const availableUploadTypes = CERTIFICATE_TYPES.filter(
82 ({ type }) =>
83 !certificates
Derick Montague602e98a2020-10-21 16:20:00 -050084 .map((certificate) => certificate.type)
Yoshie Muranaka37393812020-03-24 15:25:24 -070085 .includes(type)
86 );
87
88 commit('setCertificates', certificates);
89 commit('setAvailableUploadTypes', availableUploadTypes);
90 })
91 );
92 });
93 },
94 async addNewCertificate({ dispatch }, { file, type }) {
95 return await api
96 .post(getCertificateProp(type, 'location'), file, {
Derick Montague602e98a2020-10-21 16:20:00 -050097 headers: { 'Content-Type': 'application/x-pem-file' },
Yoshie Muranaka37393812020-03-24 15:25:24 -070098 })
99 .then(() => dispatch('getCertificates'))
100 .then(() =>
101 i18n.t('pageSslCertificates.toast.successAddCertificate', {
Derick Montague602e98a2020-10-21 16:20:00 -0500102 certificate: getCertificateProp(type, 'label'),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700103 })
104 )
Derick Montague602e98a2020-10-21 16:20:00 -0500105 .catch((error) => {
Yoshie Muranaka37393812020-03-24 15:25:24 -0700106 console.log(error);
107 throw new Error(
108 i18n.t('pageSslCertificates.toast.errorAddCertificate')
109 );
110 });
111 },
112 async replaceCertificate(
113 { dispatch },
114 { certificateString, location, type }
115 ) {
116 const data = {};
117 data.CertificateString = certificateString;
118 data.CertificateType = 'PEM';
119 data.CertificateUri = { '@odata.id': location };
120
121 return await api
122 .post(
123 '/redfish/v1/CertificateService/Actions/CertificateService.ReplaceCertificate',
124 data
125 )
126 .then(() => dispatch('getCertificates'))
127 .then(() =>
128 i18n.t('pageSslCertificates.toast.successReplaceCertificate', {
Derick Montague602e98a2020-10-21 16:20:00 -0500129 certificate: getCertificateProp(type, 'label'),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700130 })
131 )
Derick Montague602e98a2020-10-21 16:20:00 -0500132 .catch((error) => {
Yoshie Muranaka37393812020-03-24 15:25:24 -0700133 console.log(error);
134 throw new Error(
135 i18n.t('pageSslCertificates.toast.errorReplaceCertificate')
136 );
137 });
138 },
139 async deleteCertificate({ dispatch }, { type, location }) {
140 return await api
141 .delete(location)
142 .then(() => dispatch('getCertificates'))
143 .then(() =>
144 i18n.t('pageSslCertificates.toast.successDeleteCertificate', {
Derick Montague602e98a2020-10-21 16:20:00 -0500145 certificate: getCertificateProp(type, 'label'),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700146 })
147 )
Derick Montague602e98a2020-10-21 16:20:00 -0500148 .catch((error) => {
Yoshie Muranaka37393812020-03-24 15:25:24 -0700149 console.log(error);
150 throw new Error(
151 i18n.t('pageSslCertificates.toast.errorDeleteCertificate')
152 );
153 });
Yoshie Muranaka532a4b02020-03-27 11:00:50 -0700154 },
155 async generateCsr(_, userData) {
156 const {
157 certificateType,
158 country,
159 state,
160 city,
161 companyName,
162 companyUnit,
163 commonName,
164 keyPairAlgorithm,
165 keyBitLength,
166 keyCurveId,
167 challengePassword,
168 contactPerson,
169 emailAddress,
Derick Montague602e98a2020-10-21 16:20:00 -0500170 alternateName,
Yoshie Muranaka532a4b02020-03-27 11:00:50 -0700171 } = userData;
172 const data = {};
173
174 data.CertificateCollection = {
Derick Montague602e98a2020-10-21 16:20:00 -0500175 '@odata.id': getCertificateProp(certificateType, 'location'),
Yoshie Muranaka532a4b02020-03-27 11:00:50 -0700176 };
177 data.Country = country;
178 data.State = state;
179 data.City = city;
180 data.Organization = companyName;
181 data.OrganizationalUnit = companyUnit;
182 data.CommonName = commonName;
183 data.KeyPairAlgorithm = keyPairAlgorithm;
184 data.AlternativeNames = alternateName;
185
186 if (keyCurveId) data.KeyCurveId = keyCurveId;
187 if (keyBitLength) data.KeyBitLength = keyBitLength;
188 if (challengePassword) data.ChallengePassword = challengePassword;
189 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',
195 data
196 )
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
204export default SslCertificatesStore;