certificate: Don't check if certificate id is a number

In phosphor-certificate-manager, only the certificate object paths
implement xyz.openbmc_project.Certs.Certificate and Delete interface.
When reading with GetAll and deleting, these interfaces ensures that
the object path represents a certificate. And checking if the id in
object path is a number is based on a incorrect assumption that cert
id is always a number. This patch removes such unnecessary checks and
uses the standard filename() method to extract id from object paths.

Tested:
* Verified getting and deleting certificates only works on the URL
  mapped to certificate DBus object paths, other URL will give 404
  Not found error.
* Verified uploading certificates responds with correct URL.
* Redfish Service Validator passed.

Change-Id: I7484fa5602afcbe9e0fc76b17483e76e12930dee
Signed-off-by: Jiaqing Zhao <jiaqing.zhao@intel.com>
diff --git a/redfish-core/lib/certificate_service.hpp b/redfish-core/lib/certificate_service.hpp
index 0c1bc62..53c72e6 100644
--- a/redfish-core/lib/certificate_service.hpp
+++ b/redfish-core/lib/certificate_service.hpp
@@ -870,29 +870,21 @@
             boost::beast::http::verb::
                 get)([&app](const crow::Request& req,
                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
-                            const std::string& param) -> void {
+                            const std::string& id) -> void {
             if (!redfish::setUpRedfishRoute(app, req, asyncResp))
             {
                 return;
             }
-            if (param.empty())
-            {
-                messages::internalError(asyncResp->res);
-                return;
-            }
-            long id = getIDFromURL(req.url);
 
-            BMCWEB_LOG_DEBUG << "HTTPSCertificate::doGet ID="
-                             << std::to_string(id);
+            BMCWEB_LOG_DEBUG << "HTTPS Certificate ID=" << id;
             std::string certURL =
                 "/redfish/v1/Managers/bmc/NetworkProtocol/HTTPS/Certificates/" +
-                std::to_string(id);
-            std::string objectPath = certs::httpsObjectPath;
-            objectPath += "/";
-            objectPath += std::to_string(id);
-            getCertificateProperties(
-                asyncResp, objectPath, certs::httpsServiceName,
-                std::to_string(id), certURL, "HTTPS Certificate");
+                id;
+            std::string objPath =
+                sdbusplus::message::object_path(certs::httpsObjectPath) / id;
+            getCertificateProperties(asyncResp, objPath,
+                                     certs::httpsServiceName, id, certURL,
+                                     "HTTPS Certificate");
         });
 }
 
@@ -961,19 +953,15 @@
                 messages::internalError(asyncResp->res);
                 return;
             }
-            long certId = getIDFromURL(objectPath);
-            if (certId < 0)
-            {
-                BMCWEB_LOG_ERROR << "Invalid objectPath value" << objectPath;
-                messages::internalError(asyncResp->res);
-                return;
-            }
+
+            sdbusplus::message::object_path path(objectPath);
+            std::string certId = path.filename();
             std::string certURL =
                 "/redfish/v1/Managers/bmc/NetworkProtocol/HTTPS/Certificates/" +
-                std::to_string(certId);
-            getCertificateProperties(
-                asyncResp, objectPath, certs::httpsServiceName,
-                std::to_string(certId), certURL, "HTTPS Certificate");
+                certId;
+            getCertificateProperties(asyncResp, objectPath,
+                                     certs::httpsServiceName, certId, certURL,
+                                     "HTTPS Certificate");
             BMCWEB_LOG_DEBUG << "HTTPS certificate install file="
                              << certFile->getCertFilePath();
             },
@@ -1072,19 +1060,14 @@
                 messages::internalError(asyncResp->res);
                 return;
             }
-            long certId = getIDFromURL(objectPath);
-            if (certId < 0)
-            {
-                BMCWEB_LOG_ERROR << "Invalid objectPath value" << objectPath;
-                messages::internalError(asyncResp->res);
-                return;
-            }
+
+            sdbusplus::message::object_path path(objectPath);
+            std::string certId = path.filename();
             std::string certURL =
-                "/redfish/v1/AccountService/LDAP/Certificates/" +
-                std::to_string(certId);
-            getCertificateProperties(
-                asyncResp, objectPath, certs::ldapServiceName,
-                std::to_string(certId), certURL, "LDAP Certificate");
+                "/redfish/v1/AccountService/LDAP/Certificates/" + certId;
+            getCertificateProperties(asyncResp, objectPath,
+                                     certs::ldapServiceName, certId, certURL,
+                                     "LDAP Certificate");
             BMCWEB_LOG_DEBUG << "LDAP certificate install file="
                              << certFile->getCertFilePath();
             },
@@ -1104,27 +1087,19 @@
         .methods(boost::beast::http::verb::get)(
             [&app](const crow::Request& req,
                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
-                   const std::string&) {
+                   const std::string& id) {
         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
         {
             return;
         }
-        long id = getIDFromURL(req.url);
-        if (id < 0)
-        {
-            BMCWEB_LOG_ERROR << "Invalid url value" << req.url;
-            messages::internalError(asyncResp->res);
-            return;
-        }
-        BMCWEB_LOG_DEBUG << "LDAP Certificate ID=" << std::to_string(id);
-        std::string certURL = "/redfish/v1/AccountService/LDAP/Certificates/" +
-                              std::to_string(id);
-        std::string objectPath = certs::ldapObjectPath;
-        objectPath += "/";
-        objectPath += std::to_string(id);
-        getCertificateProperties(asyncResp, objectPath, certs::ldapServiceName,
-                                 std::to_string(id), certURL,
-                                 "LDAP Certificate");
+
+        BMCWEB_LOG_DEBUG << "LDAP Certificate ID=" << id;
+        std::string certURL =
+            "/redfish/v1/AccountService/LDAP/Certificates/" + id;
+        std::string objPath =
+            sdbusplus::message::object_path(certs::ldapObjectPath) / id;
+        getCertificateProperties(asyncResp, objPath, certs::ldapServiceName, id,
+                                 certURL, "LDAP Certificate");
         });
 } // requestRoutesLDAPCertificate
 /**
@@ -1184,20 +1159,14 @@
                 messages::internalError(asyncResp->res);
                 return;
             }
-            long certId = getIDFromURL(objectPath);
-            if (certId < 0)
-            {
-                BMCWEB_LOG_ERROR << "Invalid objectPath value" << objectPath;
-                messages::internalError(asyncResp->res);
-                return;
-            }
-            std::string certURL =
-                "/redfish/v1/Managers/bmc/Truststore/Certificates/" +
-                std::to_string(certId);
 
-            getCertificateProperties(
-                asyncResp, objectPath, certs::authorityServiceName,
-                std::to_string(certId), certURL, "TrustStore Certificate");
+            sdbusplus::message::object_path path(objectPath);
+            std::string certId = path.filename();
+            std::string certURL =
+                "/redfish/v1/Managers/bmc/Truststore/Certificates/" + certId;
+            getCertificateProperties(asyncResp, objectPath,
+                                     certs::authorityServiceName, certId,
+                                     certURL, "TrustStore Certificate");
             BMCWEB_LOG_DEBUG << "TrustStore certificate install file="
                              << certFile->getCertFilePath();
             },
@@ -1217,29 +1186,20 @@
         .methods(boost::beast::http::verb::get)(
             [&app](const crow::Request& req,
                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
-                   const std::string&) {
+                   const std::string& id) {
         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
         {
             return;
         }
-        long id = getIDFromURL(req.url);
-        if (id < 0)
-        {
-            BMCWEB_LOG_ERROR << "Invalid url value" << req.url;
-            messages::internalError(asyncResp->res);
-            return;
-        }
-        BMCWEB_LOG_DEBUG << "TrustStoreCertificate::doGet ID="
-                         << std::to_string(id);
+
+        BMCWEB_LOG_DEBUG << "Truststore Certificate ID=" << id;
         std::string certURL =
-            "/redfish/v1/Managers/bmc/Truststore/Certificates/" +
-            std::to_string(id);
-        std::string objectPath = certs::authorityObjectPath;
-        objectPath += "/";
-        objectPath += std::to_string(id);
-        getCertificateProperties(
-            asyncResp, objectPath, certs::authorityServiceName,
-            std::to_string(id), certURL, "TrustStore Certificate");
+            "/redfish/v1/Managers/bmc/Truststore/Certificates/" + id;
+        std::string objPath =
+            sdbusplus::message::object_path(certs::authorityObjectPath) / id;
+        getCertificateProperties(asyncResp, objPath,
+                                 certs::authorityServiceName, id, certURL,
+                                 "TrustStore Certificate");
         });
 
     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/Truststore/Certificates/<str>/")
@@ -1247,44 +1207,28 @@
         .methods(boost::beast::http::verb::delete_)(
             [&app](const crow::Request& req,
                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
-                   const std::string& param) {
+                   const std::string& id) {
         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
         {
             return;
         }
-        if (param.empty())
-        {
-            messages::internalError(asyncResp->res);
-            return;
-        }
 
-        long id = getIDFromURL(req.url);
-        if (id < 0)
-        {
-            BMCWEB_LOG_ERROR << "Invalid url value: " << req.url;
-            messages::resourceNotFound(asyncResp->res, "TrustStore Certificate",
-                                       std::string(req.url));
-            return;
-        }
-        BMCWEB_LOG_DEBUG << "TrustStoreCertificate::doDelete ID="
-                         << std::to_string(id);
-        std::string certPath = certs::authorityObjectPath;
-        certPath += "/";
-        certPath += std::to_string(id);
+        BMCWEB_LOG_DEBUG << "Delete TrustStore Certificate ID=" << id;
+        std::string objPath =
+            sdbusplus::message::object_path(certs::authorityObjectPath) / id;
 
         crow::connections::systemBus->async_method_call(
             [asyncResp, id](const boost::system::error_code ec) {
             if (ec)
             {
                 messages::resourceNotFound(asyncResp->res,
-                                           "TrustStore Certificate",
-                                           std::to_string(id));
+                                           "TrustStore Certificate", id);
                 return;
             }
             BMCWEB_LOG_INFO << "Certificate deleted";
             asyncResp->res.result(boost::beast::http::status::no_content);
             },
-            certs::authorityServiceName, certPath, certs::objDeleteIntf,
+            certs::authorityServiceName, objPath, certs::objDeleteIntf,
             "Delete");
         });
 } // requestRoutesTrustStoreCertificate