-
Notifications
You must be signed in to change notification settings - Fork 14
main: refactor lmp-device-register #41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| /* | ||
| * Copyright (c) 2023 Foundries.io | ||
| * | ||
| * SPDX-License-Identifier: MIT | ||
| */ | ||
|
|
||
| #ifndef CURL_H | ||
| #define CURL_H | ||
|
|
||
| #include <boost/algorithm/string.hpp> | ||
| #include <boost/beast/core/detail/base64.hpp> | ||
| #include <boost/filesystem.hpp> | ||
| #include <boost/iostreams/device/file_descriptor.hpp> | ||
| #include <boost/iostreams/stream.hpp> | ||
| #include <boost/program_options.hpp> | ||
| #include <boost/property_tree/json_parser.hpp> | ||
| #include <boost/property_tree/ini_parser.hpp> | ||
| #include <boost/property_tree/ptree.hpp> | ||
| #include <boost/uuid/uuid_generators.hpp> | ||
| #include <boost/uuid/uuid_io.hpp> | ||
| #include <boost/interprocess/sync/file_lock.hpp> | ||
|
|
||
| #include <curl/curl.h> | ||
|
|
||
| using boost::property_tree::ptree; | ||
| using std::stringstream; | ||
| using std::string; | ||
| using std::cerr; | ||
| using std::cout; | ||
| using std::endl; | ||
|
|
||
| static size_t write_sstream(void *buf, size_t size, size_t nmemb, void *userp) | ||
| { | ||
| auto *body = static_cast<stringstream *>(userp); | ||
|
|
||
| body->write(static_cast<const char *>(buf), size * nmemb); | ||
|
|
||
| return size * nmemb; | ||
| } | ||
|
|
||
| class Curl { | ||
| private: | ||
| string _url; | ||
| public: | ||
| Curl(const string &url) | ||
| { | ||
| _url = url; | ||
| curl_global_init(CURL_GLOBAL_DEFAULT); | ||
| curl = curl_easy_init(); | ||
| if (curl != nullptr) { | ||
| curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); | ||
| } | ||
| } | ||
| ~Curl() | ||
| { | ||
| if (curl != nullptr) { | ||
| curl_easy_cleanup(curl); | ||
| } | ||
| curl_global_cleanup(); | ||
| } | ||
| void ParseResponse(stringstream &body, ptree &resp) | ||
| { | ||
| try { | ||
| read_json(body, resp); | ||
| } catch (const boost::property_tree::json_parser::json_parser_error &e) { | ||
| cerr << "Unable to parse response from: " << _url << " Error is:" << endl; | ||
| cerr << " " << e.message() << endl; | ||
| body.seekg(0); | ||
| cerr << "Raw response was: " << body.str() << endl; | ||
| } | ||
| } | ||
| std::tuple<bool, string> PingEndpoint() | ||
| { | ||
| curl_easy_setopt(curl, CURLOPT_NOBODY, 1L); | ||
| CURLcode res = curl_easy_perform(curl); | ||
| if (res != CURLE_OK) { | ||
| return { false, | ||
| "Unable to reach the device registration endpoint " + _url + "; err: " + curl_easy_strerror(res) }; | ||
| } | ||
| gint64 code = 0; | ||
| CURLcode get_info_res = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code); | ||
| if (get_info_res != CURLE_OK) { | ||
| return { false, | ||
| "Error while checking the device registration endpoint; err: unable to get curl info: " + string(curl_easy_strerror(get_info_res)) }; | ||
| } | ||
| if (code >= 500) { | ||
| // 401 or 400 is returned under normal circumstances what indicates that the OTA backend is reachable and functional | ||
| return { false, | ||
| "The device registration endpoint is not healthy" + _url + "; status code: " + std::to_string(code) }; | ||
| } | ||
| return { true, "" }; | ||
| } | ||
| gint64 Post(const http_headers &headers, const string &data, ptree &resp) | ||
| { | ||
| stringstream body; | ||
| curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &write_sstream); | ||
| curl_easy_setopt(curl, CURLOPT_WRITEDATA, &body); | ||
| curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str()); | ||
|
|
||
| struct curl_slist *chunk = nullptr; | ||
| for (auto item : headers) { | ||
| string header = item.first + ": " + item.second; | ||
| chunk = curl_slist_append(chunk, header.c_str()); | ||
| } | ||
|
|
||
| if (chunk != nullptr) { | ||
| curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk); | ||
| } | ||
|
|
||
| CURLcode res = curl_easy_perform(curl); | ||
| if (res != CURLE_OK) { | ||
| cerr << "Unable to post to " << _url << ": " << curl_easy_strerror(res) << endl; | ||
| exit(1); | ||
| } | ||
|
|
||
| if (chunk != nullptr) { | ||
| curl_slist_free_all(chunk); | ||
| } | ||
|
|
||
| gint64 code = 0; | ||
| res = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code); | ||
| if (res != CURLE_OK) { | ||
| cerr << "Unable to get curl info: " << curl_easy_strerror(res) << endl; | ||
| exit(1); | ||
| } | ||
| ParseResponse(body, resp); | ||
| return code; | ||
| } | ||
| private: | ||
| CURL *curl = nullptr; | ||
| }; | ||
|
|
||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| /* | ||
| * Copyright (c) 2023 Foundries.io | ||
| * | ||
| * SPDX-License-Identifier: MIT | ||
| */ | ||
|
|
||
| #ifndef DEVICE_REGISTER_H | ||
| #define DEVICE_REGISTER_H | ||
|
|
||
| #include <exception> | ||
| #include <fcntl.h> | ||
| #include <glib.h> | ||
| #include <iostream> | ||
| #include <regex> | ||
| #include <sys/mman.h> | ||
| #include <sys/stat.h> | ||
| #include <stdio.h> | ||
| #include <string> | ||
| #include <sstream> | ||
| #include <unistd.h> | ||
|
|
||
| #include <openssl/pem.h> | ||
| #include <openssl/evp.h> | ||
| #include <openssl/encoder.h> | ||
| #include <openssl/x509.h> | ||
| #include <openssl/x509v3.h> | ||
| #include <openssl/err.h> | ||
| #include <openssl/buffer.h> | ||
|
|
||
| #include <boost/algorithm/string.hpp> | ||
| #include <boost/beast/core/detail/base64.hpp> | ||
| #include <boost/filesystem.hpp> | ||
| #include <boost/iostreams/device/file_descriptor.hpp> | ||
| #include <boost/iostreams/stream.hpp> | ||
| #include <boost/program_options.hpp> | ||
| #include <boost/property_tree/json_parser.hpp> | ||
| #include <boost/property_tree/ini_parser.hpp> | ||
| #include <boost/property_tree/ptree.hpp> | ||
| #include <boost/uuid/uuid_generators.hpp> | ||
| #include <boost/uuid/uuid_io.hpp> | ||
| #include <boost/interprocess/sync/file_lock.hpp> | ||
|
|
||
| #define __weak __attribute__((weak)) | ||
|
|
||
| /* OS definitions in os-release */ | ||
| #define LMP_OS_STR "/etc/os-release" | ||
| #define OS_FACTORY_TAG "LMP_FACTORY_TAG" | ||
| #define OS_FACTORY "LMP_FACTORY" | ||
|
|
||
| /* Environment Variables */ | ||
| #define ENV_DEVICE_FACTORY "DEVICE_FACTORY" | ||
| #define ENV_PRODUCTION "PRODUCTION" | ||
| #define ENV_OAUTH_BASE "OAUTH_BASE" | ||
| #define ENV_DEVICE_API "DEVICE_API" | ||
|
|
||
| /* HSM defitions */ | ||
| #define HSM_TOKEN_STR "aktualizr" | ||
| #define HSM_TLS_STR "tls" | ||
| #define HSM_TLS_ID_STR "01" | ||
| #define HSM_CRT_STR "client" | ||
| #define HSM_CRT_ID 3 | ||
| #define HSM_CRT_ID_STR "03" | ||
|
|
||
| /* Files */ | ||
| #define AKLITE_LOCK "/var/lock/aklite.lock" | ||
| #define SOTA_DIR "/var/sota" | ||
| #define SOTA_PEM "/client.pem" | ||
| #define SOTA_SQL "/sql.db" | ||
|
|
||
| using boost::property_tree::ptree; | ||
| using std::stringstream; | ||
| using std::string; | ||
| using std::cerr; | ||
| using std::cout; | ||
| using std::endl; | ||
|
|
||
| struct lmp_options { | ||
| string api_token_header; | ||
| string device_group; | ||
| string api_token; | ||
| string factory; | ||
| string hwid; | ||
| string uuid; | ||
| string name; | ||
| string hsm_module; | ||
| string hsm_so_pin; | ||
| string hsm_pin; | ||
| string sota_dir; | ||
| string pacman_tags; | ||
| bool start_daemon; | ||
| bool use_server; | ||
| bool production; | ||
| bool mlock; | ||
| #if defined DOCKER_COMPOSE_APP | ||
| string apps; | ||
| string restorable_apps; | ||
| #endif | ||
| }; | ||
|
|
||
| typedef std::map<std::string, string> http_headers; | ||
|
|
||
| int auth_register_device(http_headers &headers, ptree &device, ptree &resp); | ||
| void auth_get_http_headers(lmp_options &opt, http_headers &headers); | ||
| int auth_ping_server(void); | ||
|
|
||
| int options_parse(int argc, char **argv, lmp_options &options); | ||
|
|
||
| int openssl_create_csr(const lmp_options &options, string &key, string &csr); | ||
| int openssl_gen_csr(const lmp_options &options, EVP_PKEY *pub, EVP_PKEY *priv, | ||
| string &csr); | ||
|
|
||
| int pkcs11_create_csr(const lmp_options &options, string &key, string &csr); | ||
| int pkcs11_store_cert(lmp_options &opt, X509 *cert); | ||
| int pkcs11_get_uuid(lmp_options &options); | ||
| #endif |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can use the libp11.pc pkgconfig provide in the project. With this we can remove the
cmake-modules/FindLibP11.cmakeThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that would be great. let me test this.