blob: b55363f1009137052057b46ac0286f692f502ff9 [file] [log] [blame]
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -05001#include "certs_manager.hpp"
2
Marri Devender Rao6ceec402019-02-01 03:15:19 -06003#include <phosphor-logging/elog-errors.hpp>
4#include <xyz/openbmc_project/Certs/Install/error.hpp>
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -05005#include <xyz/openbmc_project/Common/error.hpp>
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -05006namespace phosphor
7{
8namespace certs
9{
10
Marri Devender Rao13965112019-02-27 08:47:12 -060011using InternalFailure =
12 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050013
Marri Devender Rao6ceec402019-02-01 03:15:19 -060014/** @brief Constructor to put object onto bus at a dbus path.
15 * @param[in] bus - Bus to attach to.
16 * @param[in] path - Path to attach at.
17 * @param[in] type - Type of the certificate.
18 * @param[in] unit - Unit consumed by this certificate.
19 * @param[in] installPath - Certificate installation path.
20 */
21Manager::Manager(sdbusplus::bus::bus& bus, const char* path,
22 const CertificateType& type, UnitsToRestart&& unit,
23 CertInstallPath&& installPath) :
24 Ifaces(bus, path),
25 bus(bus), objectPath(path), certType(type), unitToRestart(std::move(unit)),
26 certInstallPath(std::move(installPath))
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050027{
Marri Devender Rao13965112019-02-27 08:47:12 -060028 using InvalidCertificate = sdbusplus::xyz::openbmc_project::Certs::Install::
29 Error::InvalidCertificate;
30 using Reason =
31 xyz::openbmc_project::Certs::Install::InvalidCertificate::REASON;
Marri Devender Raobf7c5882019-02-27 08:41:07 -060032 if (fs::exists(certInstallPath))
33 {
34 try
35 {
36 // TODO: Issue#3 At present supporting only one certificate to be
37 // uploaded this need to be revisited to support multiple
38 // certificates
39 auto certObjectPath = objectPath + '/' + '1';
40 certificatePtr = std::make_unique<Certificate>(
41 bus, certObjectPath, certType, unitToRestart, certInstallPath,
42 certInstallPath);
43 }
44 catch (const InternalFailure& e)
45 {
Marri Devender Raobf7c5882019-02-27 08:41:07 -060046 report<InternalFailure>();
47 }
48 catch (const InvalidCertificate& e)
49 {
Marri Devender Raobf7c5882019-02-27 08:41:07 -060050 report<InvalidCertificate>(
51 Reason("Existing certificate file is corrupted"));
52 }
53 }
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050054}
55
Marri Devender Rao6ceec402019-02-01 03:15:19 -060056void Manager::install(const std::string filePath)
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050057{
Marri Devender Rao13965112019-02-27 08:47:12 -060058 using NotAllowed =
59 sdbusplus::xyz::openbmc_project::Common::Error::NotAllowed;
60 using Reason = xyz::openbmc_project::Common::NotAllowed::REASON;
61 // TODO: Issue#3 At present supporting only one certificate to be
62 // uploaded this need to be revisited to support multiple
63 // certificates
64 if (certificatePtr != nullptr)
65 {
66 elog<NotAllowed>(Reason("Certificate already exist"));
67 }
68 auto certObjectPath = objectPath + '/' + '1';
69 certificatePtr =
70 std::make_unique<Certificate>(bus, certObjectPath, certType,
71 unitToRestart, certInstallPath, filePath);
Jayanth Othayoth589159f2018-09-28 08:32:39 -050072}
Deepak Kodihalliae70b3d2018-09-30 05:42:00 -050073
74void Manager::delete_()
75{
Marri Devender Rao6ceec402019-02-01 03:15:19 -060076 // TODO: #Issue 4 when a certificate is deleted system auto generates
77 // certificate file. At present we are not supporting creation of
78 // certificate object for the auto-generated certificate file as
79 // deletion if only applicable for REST server and Bmcweb does not allow
80 // deletion of certificates
81 if (certificatePtr != nullptr)
Deepak Kodihalliae70b3d2018-09-30 05:42:00 -050082 {
Marri Devender Rao6ceec402019-02-01 03:15:19 -060083 certificatePtr.reset(nullptr);
Deepak Kodihalliae70b3d2018-09-30 05:42:00 -050084 }
85}
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050086} // namespace certs
87} // namespace phosphor