Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 1 | #include "certs_manager.hpp" |
| 2 | |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 3 | #include <phosphor-logging/elog-errors.hpp> |
| 4 | #include <xyz/openbmc_project/Certs/Install/error.hpp> |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 5 | #include <xyz/openbmc_project/Common/error.hpp> |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 6 | namespace phosphor |
| 7 | { |
| 8 | namespace certs |
| 9 | { |
| 10 | |
Marri Devender Rao | 1396511 | 2019-02-27 08:47:12 -0600 | [diff] [blame^] | 11 | using InternalFailure = |
| 12 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 13 | |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 14 | /** @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 | */ |
| 21 | Manager::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 Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 27 | { |
Marri Devender Rao | 1396511 | 2019-02-27 08:47:12 -0600 | [diff] [blame^] | 28 | using InvalidCertificate = sdbusplus::xyz::openbmc_project::Certs::Install:: |
| 29 | Error::InvalidCertificate; |
| 30 | using Reason = |
| 31 | xyz::openbmc_project::Certs::Install::InvalidCertificate::REASON; |
Marri Devender Rao | bf7c588 | 2019-02-27 08:41:07 -0600 | [diff] [blame] | 32 | 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 Rao | bf7c588 | 2019-02-27 08:41:07 -0600 | [diff] [blame] | 46 | report<InternalFailure>(); |
| 47 | } |
| 48 | catch (const InvalidCertificate& e) |
| 49 | { |
Marri Devender Rao | bf7c588 | 2019-02-27 08:41:07 -0600 | [diff] [blame] | 50 | report<InvalidCertificate>( |
| 51 | Reason("Existing certificate file is corrupted")); |
| 52 | } |
| 53 | } |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 54 | } |
| 55 | |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 56 | void Manager::install(const std::string filePath) |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 57 | { |
Marri Devender Rao | 1396511 | 2019-02-27 08:47:12 -0600 | [diff] [blame^] | 58 | 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 Othayoth | 589159f | 2018-09-28 08:32:39 -0500 | [diff] [blame] | 72 | } |
Deepak Kodihalli | ae70b3d | 2018-09-30 05:42:00 -0500 | [diff] [blame] | 73 | |
| 74 | void Manager::delete_() |
| 75 | { |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 76 | // 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 Kodihalli | ae70b3d | 2018-09-30 05:42:00 -0500 | [diff] [blame] | 82 | { |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 83 | certificatePtr.reset(nullptr); |
Deepak Kodihalli | ae70b3d | 2018-09-30 05:42:00 -0500 | [diff] [blame] | 84 | } |
| 85 | } |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 86 | } // namespace certs |
| 87 | } // namespace phosphor |