Skip to content

Commit

Permalink
client: allow custom identity instead of using mac address by default
Browse files Browse the repository at this point in the history
  • Loading branch information
joelguittet committed May 27, 2024
1 parent 628b33f commit 13f4255
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 30 deletions.
44 changes: 32 additions & 12 deletions core/src/mender-api.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ mender_err_t
mender_api_init(mender_api_config_t *config) {

assert(NULL != config);
assert(NULL != config->mac_address);
assert(NULL != config->identity);
assert(NULL != config->artifact_name);
assert(NULL != config->device_type);
assert(NULL != config->host);
Expand Down Expand Up @@ -130,6 +130,9 @@ mender_err_t
mender_api_perform_authentication(void) {

mender_err_t ret;
cJSON * json_identity = NULL;
char * tmp = NULL;
char * identity = NULL;
char * public_key_pem = NULL;
char * payload = NULL;
char * response = NULL;
Expand All @@ -143,29 +146,40 @@ mender_api_perform_authentication(void) {
goto END;
}

/* Format identity */
if (MENDER_OK != (ret = mender_utils_keystore_to_json(mender_api_config.identity, &json_identity))) {
mender_log_error("Unable to format identity");
goto END;
}
if (NULL == (identity = cJSON_PrintUnformatted(json_identity))) {
mender_log_error("Unable to allocate memory");
ret = MENDER_FAIL;
goto END;
}
if (NULL == (tmp = mender_utils_str_replace(identity, "\"", "\\\""))) {
mender_log_error("Unable to allocate memory");
ret = MENDER_FAIL;
goto END;
}
identity = tmp;

/* Format payload */
if (NULL != mender_api_config.tenant_token) {
if (NULL
== (payload = (char *)malloc(strlen("{ \"id_data\": \"{ \\\"mac\\\": \\\"\\\"}\", \"pubkey\": \"\", \"tenant_token\": \"\" }")
+ strlen(mender_api_config.mac_address) + strlen(public_key_pem) + strlen(mender_api_config.tenant_token) + 1))) {
== (payload = (char *)malloc(strlen("{ \"id_data\": \"\", \"pubkey\": \"\", \"tenant_token\": \"\" }") + strlen(identity) + strlen(public_key_pem)
+ strlen(mender_api_config.tenant_token) + 1))) {
mender_log_error("Unable to allocate memory");
ret = MENDER_FAIL;
goto END;
}
sprintf(payload,
"{ \"id_data\": \"{ \\\"mac\\\": \\\"%s\\\"}\", \"pubkey\": \"%s\", \"tenant_token\": \"%s\" }",
mender_api_config.mac_address,
public_key_pem,
mender_api_config.tenant_token);
sprintf(payload, "{ \"id_data\": \"%s\", \"pubkey\": \"%s\", \"tenant_token\": \"%s\" }", identity, public_key_pem, mender_api_config.tenant_token);
} else {
if (NULL
== (payload = (char *)malloc(strlen("{ \"id_data\": \"{ \\\"mac\\\": \\\"\\\"}\", \"pubkey\": \"\" }") + strlen(mender_api_config.mac_address)
+ strlen(public_key_pem) + 1))) {
if (NULL == (payload = (char *)malloc(strlen("{ \"id_data\": \"\", \"pubkey\": \"\" }") + strlen(identity) + strlen(public_key_pem) + 1))) {
mender_log_error("Unable to allocate memory");
ret = MENDER_FAIL;
goto END;
}
sprintf(payload, "{ \"id_data\": \"{ \\\"mac\\\": \\\"%s\\\"}\", \"pubkey\": \"%s\" }", mender_api_config.mac_address, public_key_pem);
sprintf(payload, "{ \"id_data\": \"%s\", \"pubkey\": \"%s\" }", identity, public_key_pem);
}

/* Sign payload */
Expand Down Expand Up @@ -224,6 +238,12 @@ mender_api_perform_authentication(void) {
if (NULL != public_key_pem) {
free(public_key_pem);
}
if (NULL != identity) {
free(identity);
}
if (NULL != json_identity) {
cJSON_Delete(json_identity);
}

return ret;
}
Expand Down
12 changes: 8 additions & 4 deletions core/src/mender-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -202,15 +202,18 @@ mender_err_t
mender_client_init(mender_client_config_t *config, mender_client_callbacks_t *callbacks) {

assert(NULL != config);
assert(NULL != config->mac_address);
assert(NULL != config->identity);
assert(NULL != config->artifact_name);
assert(NULL != config->device_type);
assert(NULL != callbacks);
assert(NULL != callbacks->restart);
mender_err_t ret;

/* Save configuration */
mender_client_config.mac_address = config->mac_address;
if (MENDER_OK != (ret = mender_utils_keystore_copy(&mender_client_config.identity, config->identity))) {
mender_log_error("Unable to copy identity");
goto END;
}
mender_client_config.artifact_name = config->artifact_name;
mender_client_config.device_type = config->device_type;
if ((NULL != config->host) && (strlen(config->host) > 0)) {
Expand Down Expand Up @@ -269,7 +272,7 @@ mender_client_init(mender_client_config_t *config, mender_client_callbacks_t *ca
goto END;
}
mender_api_config_t mender_api_config = {
.mac_address = mender_client_config.mac_address,
.identity = mender_client_config.identity,
.artifact_name = mender_client_config.artifact_name,
.device_type = mender_client_config.device_type,
.host = mender_client_config.host,
Expand Down Expand Up @@ -380,7 +383,8 @@ mender_client_exit(void) {
mender_scheduler_exit();

/* Release memory */
mender_client_config.mac_address = NULL;
mender_utils_keystore_delete(mender_client_config.identity);
mender_client_config.identity = NULL;
mender_client_config.artifact_name = NULL;
mender_client_config.device_type = NULL;
mender_client_config.host = NULL;
Expand Down
10 changes: 5 additions & 5 deletions include/mender-api.h
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ extern "C" {
* @brief Mender API configuration
*/
typedef struct {
char *mac_address; /**< MAC address of the device */
char *artifact_name; /**< Artifact name */
char *device_type; /**< Device type */
char *host; /**< URL of the mender server */
char *tenant_token; /**< Tenant token used to authenticate on the mender server (optional) */
mender_keystore_t *identity; /**< Identity of the device */
char * artifact_name; /**< Artifact name */
char * device_type; /**< Device type */
char * host; /**< URL of the mender server */
char * tenant_token; /**< Tenant token used to authenticate on the mender server (optional) */
} mender_api_config_t;

/**
Expand Down
16 changes: 8 additions & 8 deletions include/mender-client.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ extern "C" {
* @brief Mender client configuration
*/
typedef struct {
char * mac_address; /**< MAC address of the device */
char * artifact_name; /**< Artifact name */
char * device_type; /**< Device type */
char * host; /**< URL of the mender server */
char * tenant_token; /**< Tenant token used to authenticate on the mender server (optional) */
int32_t authentication_poll_interval; /**< Authentication poll interval, default is 60 seconds, -1 permits to disable periodic execution */
int32_t update_poll_interval; /**< Update poll interval, default is 1800 seconds, -1 permits to disable periodic execution */
bool recommissioning; /**< Used to force creation of new authentication keys */
mender_keystore_t *identity; /**< Identity of the device */
char * artifact_name; /**< Artifact name */
char * device_type; /**< Device type */
char * host; /**< URL of the mender server */
char * tenant_token; /**< Tenant token used to authenticate on the mender server (optional) */
int32_t authentication_poll_interval; /**< Authentication poll interval, default is 60 seconds, -1 permits to disable periodic execution */
int32_t update_poll_interval; /**< Update poll interval, default is 1800 seconds, -1 permits to disable periodic execution */
bool recommissioning; /**< Used to force creation of new authentication keys */
} mender_client_config_t;

/**
Expand Down
3 changes: 2 additions & 1 deletion tests/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,8 @@ main(int argc, char **argv) {
}

/* Initialize mender-client */
mender_client_config_t mender_client_config = { .mac_address = mac_address,
mender_keystore_t identity[] = { { .name = "mac", .value = mac_address }, { .name = NULL, .value = NULL } };
mender_client_config_t mender_client_config = { .identity = identity,
.artifact_name = artifact_name,
.device_type = device_type,
.host = NULL,
Expand Down

0 comments on commit 13f4255

Please sign in to comment.