Add consistent error messages to ckm_db_tool 00/239200/8
authorMateusz Cegielka <m.cegielka@samsung.com>
Wed, 22 Jul 2020 09:23:37 +0000 (11:23 +0200)
committerMateusz Cegielka <m.cegielka@samsung.com>
Thu, 27 Aug 2020 07:32:33 +0000 (09:32 +0200)
ckm_db_tool is a set of CLI utilities for debugging key-manager. Various
displayed error messages are inconsistent, do not display all available
information, and contain minor gramatical errors. Also, new interactive
features are planned to be introduced, which will require reading and
writing more information to the terminal.

A simple helper functions has been created for displaying error, warning
and info messages. All error messages have been changed to use them,
received grammar fixes, started displaying APICodeToString result when
possible, and rewritten to follow a consistent style. Finally, warning
and askPassword functions were implemented to prepare for next patches.

Change-Id: Ifd0608637f3f4ef3ce31c2fe7c79074da9a93bbb

misc/ckm_db_tool/CMakeLists.txt
misc/ckm_db_tool/ckm_db_merge.cpp
misc/ckm_db_tool/ckm_db_tool.cpp
misc/ckm_db_tool/db-wrapper.cpp
misc/ckm_db_tool/ui.cpp [new file with mode: 0644]
misc/ckm_db_tool/ui.h [new file with mode: 0644]

index 021b9c5..db6a133 100644 (file)
@@ -20,11 +20,13 @@ INCLUDE_DIRECTORIES(
 SET(CKM_DB_TOOLS_SOURCES
     ${CMAKE_CURRENT_SOURCE_DIR}/ckm-logic-ext.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/db-wrapper.cpp
+    ${CMAKE_CURRENT_SOURCE_DIR}/ui.cpp
     ${KEY_MANAGER_PATH}/common/ckm-error.cpp
     ${KEY_MANAGER_PATH}/crypto/platform/decider.cpp
     ${KEY_MANAGER_PATH}/crypto/sw-backend/internals.cpp
     ${KEY_MANAGER_PATH}/crypto/sw-backend/obj.cpp
     ${KEY_MANAGER_PATH}/crypto/sw-backend/store.cpp
+    ${KEY_MANAGER_PATH}/dpl/core/src/colors.cpp
     ${KEY_MANAGER_PATH}/dpl/db/src/naive_synchronization_object.cpp
     ${KEY_MANAGER_PATH}/dpl/db/src/sql_connection.cpp
     ${KEY_MANAGER_PATH}/initial-values/BufferHandler.cpp
index 5248b4c..44e4226 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.
@@ -29,6 +29,7 @@
 #include <unistd.h>
 
 #include "db-wrapper.h"
+#include "ui.h"
 
 using namespace std;
 using namespace CKM;
@@ -86,11 +87,11 @@ int mergeDatabase(
        int ret;
 
        if (CKM_API_SUCCESS != (ret = source.unlock())) {  // this should migrate database to newest version
-               cerr << "Unlocking source database failed: " << ret << endl;
+               UI::error() << "unlocking source database failed: " << APICodeToString(ret) << endl;
                return 1;
        }
        if (CKM_API_SUCCESS != (ret = target.unlock())) {  // this should migrate database to newest version
-               cerr << "Unlocking target database failed: " << ret << endl;
+               UI::error() << "unlocking target database failed: " << APICodeToString(ret) << endl;
                return 1;
        }
 
@@ -106,7 +107,9 @@ int mergeDatabase(
                        try {
                                target.saveRow(e);
                        } catch (const CKM::DB::SqlConnection::Exception::Base &e) {
-                               cerr << "Sql exception. Migration failed or object already exist in database: " << e.DumpToString() << endl;
+                               UI::error()
+                                       << "sql exception, migration failed or object already exists in the database: "
+                                       << e.DumpToString() << endl;
                        }
                }
        }
@@ -172,21 +175,22 @@ int main(int argc, char *argv[]) {
                }
 
                if (uid1 == uid2) {
-                       cerr << "Source database and target database must be different!" << endl;
+                       UI::error() << "source and target databases must be different" << endl;
                        return 1;
                }
 
                return mergeDatabase(uid1, password1, uid2, password2, filters);
        } catch (const CKM::DB::SqlConnection::Exception::InvalidColumn &e) {
-               cerr << "Invalid Column exception was catched. Probably migration failed. " << e.DumpToString() << endl;
+               UI::error() << "invalid column exception, probably a failed migration: " << e.DumpToString()
+                       << endl;
        } catch (const invalid_argument &e) {
-               cerr << "Argument could not be converted" << endl;
+               UI::error() << "argument could not be converted: " << e.what() << endl;
        } catch (const out_of_range &e) {
-               cerr << "Out of range" << endl;
+               UI::error() << "argument out of range: " << e.what() << endl;
        } catch (const exception &e) {
-               cerr << "Unexpected error: " << e.what() << endl;
+               UI::error() << "unexpected exception: " << e.what() << endl;
        } catch (...) {
-               cerr << "Unknown exception" << endl;
+               UI::error() << "unknown exception" << endl;
        }
 
        return 1;
index e1a4fa6..6c31dc7 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.
@@ -27,6 +27,7 @@
 #include <unistd.h>
 
 #include "db-wrapper.h"
+#include "ui.h"
 
 using namespace std;
 using namespace CKM;
@@ -104,19 +105,19 @@ int main(int argc, char *argv[])
                int retCode;
 
                if (CKM_API_SUCCESS != (retCode = dbw.unlock())) {
-                       cerr << "Unlocking database failed: " << APICodeToString(retCode) << endl;
+                       UI::error() << "unlocking database failed: " << APICodeToString(retCode) << endl;
                        return -1;
                }
 
-               cout << "Database unlocked" << endl;
+               UI::info() << "database unlocked" << endl;
 
                while (true) {
                        string cmd;
 
                        if (argcmd.empty()) {
-                               cout << "> ";
+                               cmd = UI::promptLine(">");
 
-                               if (!getline(cin, cmd)) {
+                               if (!std::cin) {
                                        cout << "exit" << endl;
                                        break; // EOF
                                }
@@ -139,17 +140,17 @@ int main(int argc, char *argv[])
                }
 
                dbw.lock();
-               cout << "Database locked" << endl;
+               UI::info() << "database locked" << endl;
 
                return 0;
-       } catch (const std::invalid_argument &e) {
-               cerr << "Argument could not be converted" << endl;
-       } catch (const std::out_of_range &e) {
-               cerr << "Out of range" << endl;
-       } catch (const std::exception &e) {
-               cerr << "Exception: " << e.what() << endl;
+       } catch (const invalid_argument &e) {
+               UI::error() << "argument could not be converted: " << e.what() << endl;
+       } catch (const out_of_range &e) {
+               UI::error() << "argument out of range: " << e.what() << endl;
+       } catch (const exception &e) {
+               UI::error() << "unexpected error: " << e.what() << endl;
        } catch (...) {
-               cerr << "Unexpected exception occurred" << endl;
+               UI::error() << "unknown exception" << endl;
        }
        return -1;
 }
index aa09810..0d9ba91 100644 (file)
@@ -20,6 +20,7 @@
  * @version    1.0
  */
 #include "db-wrapper.h"
+#include "ui.h"
 
 #include <string>
 #include <algorithm>
@@ -99,13 +100,13 @@ void DbWrapper::process(const std::string &acmd)
                        displayRow(row, trim);
                }
        } catch (const DB::SqlConnection::Exception::Base &e) {
-               std::cerr << e.GetMessage() << std::endl;
+               UI::error() << "sql exception: " << e.GetMessage() << std::endl;
        } catch (const Exc::Exception &e) {
-               std::cerr << e.message() << std::endl;
+               UI::error() << "unexpected exception: " << e.message() << std::endl;
        } catch (const std::exception &e) {
-               std::cerr << e.what() << std::endl;
+               UI::error() << "unexpected exception: " << e.what() << std::endl;
        } catch (...) {
-               std::cerr << "Unexpected exception occurred" << std::endl;
+               UI::error() << "unknown exception" << std::endl;
        }
 }
 
diff --git a/misc/ckm_db_tool/ui.cpp b/misc/ckm_db_tool/ui.cpp
new file mode 100644 (file)
index 0000000..b3f9470
--- /dev/null
@@ -0,0 +1,74 @@
+/*
+ *  Copyright (c) 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.
+ *  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       ui.cpp
+ * @author     Mateusz Cegielka (m.cegielka@samsung.com)
+ * @version    1.0
+ */
+
+#include <dpl/colors.h>
+#include "ui.h"
+
+#include <unistd.h>
+
+namespace CKM {
+
+namespace UI {
+
+std::ostream& info()
+{
+       return std::cerr
+               << Colors::Text::BOLD_GREEN_BEGIN << "info:"
+               << Colors::Text::BOLD_GREEN_END << " ";
+}
+
+std::ostream& warning()
+{
+       return std::cerr
+               << Colors::Text::BOLD_YELLOW_BEGIN << "warning:"
+               << Colors::Text::BOLD_YELLOW_END << " ";
+}
+
+std::ostream& error()
+{
+       return std::cerr
+               << Colors::Text::BOLD_RED_BEGIN << "error:"
+               << Colors::Text::BOLD_RED_END << " ";
+}
+
+std::string promptLine(const std::string &prompt)
+{
+       std::cerr
+               << Colors::Text::BOLD_WHITE_BEGIN << prompt
+               << Colors::Text::BOLD_WHITE_END << " ";
+
+       std::string buf;
+       std::getline(std::cin, buf);
+       return buf;
+}
+
+std::string promptPassword(const std::string &prompt)
+{
+       std::string colorPrompt
+               = Colors::Text::BOLD_WHITE_BEGIN + prompt
+               + Colors::Text::BOLD_WHITE_END + " ";
+       char *password = getpass(colorPrompt.c_str());
+       return password ? password : "";
+}
+
+} // namespace UI
+
+} // namespace CKM
diff --git a/misc/ckm_db_tool/ui.h b/misc/ckm_db_tool/ui.h
new file mode 100644 (file)
index 0000000..a0236df
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ *  Copyright (c) 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.
+ *  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       ui.h
+ * @author     Mateusz Cegielka (m.cegielka@samsung.com)
+ * @version    1.0
+ */
+
+#pragma once
+
+#include <iostream>
+#include <string>
+
+namespace CKM {
+
+namespace UI {
+
+std::ostream& info();
+std::ostream& warning();
+std::ostream& error();
+
+std::string promptLine(const std::string &prompt);
+std::string promptPassword(const std::string &prompt);
+
+} // namespace UI
+
+} // namespace CKM