Remove CryptoExt class in favor of friendship 49/239049/10
authorMateusz Cegielka <m.cegielka@samsung.com>
Mon, 20 Jul 2020 15:59:15 +0000 (17:59 +0200)
committerMateusz Cegielka <m.cegielka@samsung.com>
Thu, 27 Aug 2020 07:32:33 +0000 (09:32 +0200)
In the ckm_db_tool CLI helper project, CKMLogicExt and CryptoExt classes
are responsible for breaking encapsulation of CKMLogic and Crypto
classes. However, code used for extracting a Crypto member and casting
it to the CryptoExt type is repeated two times (soon three), and rather
dangerous.

This refactor makes CKMLogicExt a friend of the Crypto class. This makes
it possible to implement additional methods directly in CKMLogicExt
without doing dangerous slicing object casts.

Change-Id: Ice7261b76f46f9a6206f7ae1faded1f3d8e359cb

misc/ckm_db_tool/CMakeLists.txt
misc/ckm_db_tool/ckm-logic-ext.cpp
misc/ckm_db_tool/db-crypto-ext.cpp [deleted file]
misc/ckm_db_tool/db-crypto-ext.h [deleted file]
src/manager/service/db-crypto.h

index 117cc80..021b9c5 100644 (file)
@@ -18,7 +18,6 @@ INCLUDE_DIRECTORIES(
     )
 
 SET(CKM_DB_TOOLS_SOURCES
-    ${CMAKE_CURRENT_SOURCE_DIR}/db-crypto-ext.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/ckm-logic-ext.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/db-wrapper.cpp
     ${KEY_MANAGER_PATH}/common/ckm-error.cpp
index 2eba078..3b035fc 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2000 - 2017 Samsung Electronics Co., Ltd All Rights Reserved
+ *  Copyright (c) 2000 - 2020 Samsung Electronics Co., Ltd All Rights Reserved
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
 
 #include "ckm-logic-ext.h"
 
-#include "db-crypto-ext.h"
+#include <dpl/db/sql_connection.h>
+
+namespace {
+
+const char *DB_CMD_OBJECT_SELECT = "SELECT * FROM [join_name_object_tables];";
+
+} // anonymous namespace
 
 namespace CKM {
 
-DB::SqlConnection::Output CKMLogicExt::Execute(uid_t user,
-               const std::string &cmd)
+DB::SqlConnection::Output CKMLogicExt::Execute(uid_t user, const std::string &cmd)
 {
-       /*
-        * We need to access to DB::Crypto::m_connection to call Execute() on it. We don't want to mess
-        * with DB::Crypto too much so adding a friend and extending public interface was not an option.
-        * That's why we need a derived class DB::CryptoExt. m_userDataMap must be left unchanged after
-        * this operation but DB::Crypto can't be copied. According to C++ standard static casting
-        * DB::Crypto pointer to DB::CryptoExt pointer is UB. Therefore DB::Crypto is temporarily moved
-        * into DB::CryptoExt and moved back to m_userDataMap after the call to Execute().
-        */
-       DB::CryptoExt db(std::move(m_userDataMap[user].database));
-
-       try {
-               DB::SqlConnection::Output output = db.Execute(cmd);
-               m_userDataMap[user].database = std::move(*static_cast<DB::Crypto *>(&db));
-               return output;
-       } catch (const DB::SqlConnection::Exception::Base &e) {
-               m_userDataMap[user].database = std::move(*static_cast<DB::Crypto *>(&db));
-               throw;
-       }
+       DB::SqlConnection::Output out;
+       m_userDataMap[user].database.m_connection->ExecCommand(&out, "%s", cmd.c_str());
+       return out;
 }
 
 DB::RowVector CKMLogicExt::getRows(uid_t user)
 {
-       DB::CryptoExt db(std::move(m_userDataMap[user].database));
-
-       try {
-               DB::RowVector output = db.getRows();
-               m_userDataMap[user].database = std::move(*static_cast<DB::Crypto *>(&db));
-               return output;
-       } catch (const DB::SqlConnection::Exception::Base &e) {
-               m_userDataMap[user].database = std::move(*static_cast<DB::Crypto *>(&db));
-               throw;
-       }
+       DB::Crypto& database = m_userDataMap[user].database;
+       DB::SqlConnection::DataCommandUniquePtr selectCommand =
+               database.m_connection->PrepareDataCommand(DB_CMD_OBJECT_SELECT);
+
+       DB::RowVector rows;
+       while (selectCommand->Step())
+               rows.push_back(database.getRow(selectCommand));
+       return rows;
 }
 
 void CKMLogicExt::saveRow(uid_t user, const DB::Row &row)
diff --git a/misc/ckm_db_tool/db-crypto-ext.cpp b/misc/ckm_db_tool/db-crypto-ext.cpp
deleted file mode 100644 (file)
index df28078..0000000
+++ /dev/null
@@ -1,71 +0,0 @@
-/*
- * Copyright (c) 2014-2017 Samsung Electronics Co., Ltd All Rights Reserved
- *
- *    Licensed under the Apache License, Version 2.0 (the "License");
- *    you may not use this file except in compliance with the License.
- *    You may obtain a copy of the License at
- *
- *        http://www.apache.org/licenses/LICENSE-2.0
- *
- *    Unless required by applicable law or agreed to in writing, software
- *    distributed under the License is distributed on an "AS IS" BASIS,
- *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *    See the License for the specific language governing permissions and
- *    limitations under the License.
- */
-/*
- * @file        db-crypto-ext.cpp
- * @author      Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
- * @version     1.0
- * @brief       Limited implementation of encrypted db access layer
- */
-
-#include "db-crypto-ext.h"
-
-#include <dpl/exception.h>
-
-namespace CKM {
-namespace DB {
-
-const char *DB_CMD_OBJECT_SELECT =
-       "SELECT * FROM [join_name_object_tables];";
-
-SqlConnection::Output CryptoExt::Execute(const std::string &cmd)
-{
-       SqlConnection::Output out;
-
-       if (!m_connection) {
-               ThrowMsg(SqlConnection::Exception::ConnectionBroken,
-                                "Not connected to database");
-       }
-
-       m_connection->ExecCommand(&out, "%s", cmd.c_str());
-       return out;
-}
-
-RowVector CryptoExt::getRows()
-{
-       try {
-               RowVector output;
-               SqlConnection::DataCommandUniquePtr selectCommand =
-                       m_connection->PrepareDataCommand(DB_CMD_OBJECT_SELECT);
-
-               while (selectCommand->Step()) {
-                       // extract data
-                       output.push_back(getRow(selectCommand));
-               }
-
-               return output;
-       } catch (const SqlConnection::Exception::InvalidColumn &) {
-               LogError("Select statement invalid column error");
-       } catch (const SqlConnection::Exception::SyntaxError &) {
-               LogError("Couldn't prepare select statement");
-       } catch (const SqlConnection::Exception::InternalError &) {
-               LogError("Couldn't execute select statement");
-       }
-
-       ThrowErr(Exc::DatabaseFailed, "Couldn't get row from database");
-}
-
-} // namespace DB
-} // namespace CKM
diff --git a/misc/ckm_db_tool/db-crypto-ext.h b/misc/ckm_db_tool/db-crypto-ext.h
deleted file mode 100644 (file)
index bbf7476..0000000
+++ /dev/null
@@ -1,41 +0,0 @@
-/*
- * Copyright (c) 2015-2019 Samsung Electronics Co., Ltd. All rights reserved
- *
- *    Licensed under the Apache License, Version 2.0 (the "License");
- *    you may not use this file except in compliance with the License.
- *    You may obtain a copy of the License at
- *
- *        http://www.apache.org/licenses/LICENSE-2.0
- *
- *    Unless required by applicable law or agreed to in writing, software
- *    distributed under the License is distributed on an "AS IS" BASIS,
- *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *    See the License for the specific language governing permissions and
- *    limitations under the License.
- */
-/*
- * @file        db-crypto-ext.h
- * @author      Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
- * @version     1.0
- * @brief       Header of encrypted db access layer
- */
-
-#pragma once
-
-#include <db-crypto.h>
-#include <string>
-#include <utility>
-#include <dpl/db/sql_connection.h>
-
-namespace CKM {
-namespace DB {
-struct CryptoExt : public Crypto {
-       explicit CryptoExt(Crypto orig) : Crypto(std::move(orig)) {}
-
-       SqlConnection::Output Execute(const std::string &cmd);
-       RowVector getRows();
-};
-
-} // namespace DB
-} // namespace CKM
-
index 3c3c206..6fd3a39 100644 (file)
@@ -37,6 +37,9 @@
 #pragma GCC diagnostic warning "-Wdeprecated-declarations"
 
 namespace CKM {
+
+class CKMLogicExt;
+
 namespace DB {
 class Crypto {
 public:
@@ -202,6 +205,8 @@ protected:
 private:
        bool m_inUserTransaction;
 
+       friend CKMLogicExt;
+
        void resetDB();
        void initDatabase();
        void createDBSchema();
@@ -288,6 +293,7 @@ public:
 };
 
 } // namespace DB
+
 } // namespace CKM
 
 #pragma GCC diagnostic pop