Skip to content

Commit

Permalink
client: improve management of server host ant tenant token
Browse files Browse the repository at this point in the history
  • Loading branch information
joelguittet committed May 25, 2024
1 parent f127d8e commit 34e9fe1
Showing 1 changed file with 35 additions and 15 deletions.
50 changes: 35 additions & 15 deletions core/src/mender-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,24 @@ mender_client_init(mender_client_config_t *config, mender_client_callbacks_t *ca
mender_client_config.device_type = config->device_type;
if ((NULL != config->host) && (strlen(config->host) > 0)) {
mender_client_config.host = config->host;
} else {
} else if ((NULL != CONFIG_MENDER_SERVER_HOST) && (strlen(CONFIG_MENDER_SERVER_HOST) > 0)) {
mender_client_config.host = CONFIG_MENDER_SERVER_HOST;
} else {
mender_log_error("Invalid server host configuration");
ret = MENDER_FAIL;
goto END;
}
if ('/' == mender_client_config.host[strlen(mender_client_config.host) - 1]) {
mender_log_error("Invalid server host configuration, trailing '/' is not allowed");
ret = MENDER_FAIL;
goto END;
}
if ((NULL != config->tenant_token) && (strlen(config->tenant_token) > 0)) {
mender_client_config.tenant_token = config->tenant_token;
} else {
} else if ((NULL != CONFIG_MENDER_SERVER_TENANT_TOKEN) && (strlen(CONFIG_MENDER_SERVER_TENANT_TOKEN) > 0)) {
mender_client_config.tenant_token = CONFIG_MENDER_SERVER_TENANT_TOKEN;
} else {
mender_client_config.tenant_token = NULL;
}
if (0 != config->authentication_poll_interval) {
mender_client_config.authentication_poll_interval = config->authentication_poll_interval;
Expand All @@ -241,19 +252,19 @@ mender_client_init(mender_client_config_t *config, mender_client_callbacks_t *ca
/* Initializations */
if (MENDER_OK != (ret = mender_scheduler_init())) {
mender_log_error("Unable to initialize scheduler");
return ret;
goto END;
}
if (MENDER_OK != (ret = mender_log_init())) {
mender_log_error("Unable to initialize log");
return ret;
goto END;
}
if (MENDER_OK != (ret = mender_storage_init())) {
mender_log_error("Unable to initialize storage");
return ret;
goto END;
}
if (MENDER_OK != (ret = mender_tls_init())) {
mender_log_error("Unable to initialize TLS");
return ret;
goto END;
}
mender_api_config_t mender_api_config = {
.mac_address = mender_client_config.mac_address,
Expand All @@ -264,14 +275,14 @@ mender_client_init(mender_client_config_t *config, mender_client_callbacks_t *ca
};
if (MENDER_OK != (ret = mender_api_init(&mender_api_config))) {
mender_log_error("Unable to initialize API");
return ret;
goto END;
}

/* Register rootfs-image artifact type */
if (MENDER_OK
!= (ret = mender_client_register_artifact_type("rootfs-image", &mender_client_download_artifact_flash_callback, true, config->artifact_name))) {
mender_log_error("Unable to register 'rootfs-image' artifact type");
return ret;
goto END;
}

/* Create mender client work */
Expand All @@ -281,15 +292,17 @@ mender_client_init(mender_client_config_t *config, mender_client_callbacks_t *ca
update_work_params.name = "mender_client_update";
if (MENDER_OK != (ret = mender_scheduler_work_create(&update_work_params, &mender_client_work_handle))) {
mender_log_error("Unable to create update work");
return ret;
goto END;
}

/* Activate update work */
if (MENDER_OK != (ret = mender_scheduler_work_activate(mender_client_work_handle))) {
mender_log_error("Unable to activate update work");
return ret;
goto END;
}

END:

return ret;
}

Expand Down Expand Up @@ -339,10 +352,12 @@ mender_client_execute(void) {
/* Trigger execution of the work */
if (MENDER_OK != (ret = mender_scheduler_work_execute(mender_client_work_handle))) {
mender_log_error("Unable to trigger update work");
return ret;
goto END;
}

return MENDER_OK;
END:

return ret;
}

mender_err_t
Expand Down Expand Up @@ -432,26 +447,31 @@ mender_client_initialization_work_function(void) {
/* Retrieve or generate authentication keys */
if (MENDER_OK != (ret = mender_tls_init_authentication_keys(mender_client_config.recommissioning))) {
mender_log_error("Unable to retrieve or generate authentication keys");
return ret;
goto END;
}

/* Retrieve deployment data if it is found (following an update) */
if (MENDER_OK != (ret = mender_storage_get_deployment_data(&deployment_data))) {
if (MENDER_NOT_FOUND != ret) {
mender_log_error("Unable to get deployment data");
return ret;
goto END;
}
}
if (NULL != deployment_data) {
if (NULL == (mender_client_deployment_data = cJSON_Parse(deployment_data))) {
mender_log_error("Unable to allocate memory");
free(deployment_data);
return MENDER_FAIL;
ret = MENDER_FAIL;
goto END;
}
free(deployment_data);
}

return MENDER_DONE;

END:

return ret;
}

static mender_err_t
Expand Down

0 comments on commit 34e9fe1

Please sign in to comment.