Skip to content

Commit

Permalink
[#392] Add application name and version in management protocol
Browse files Browse the repository at this point in the history
In order to improve debugging, [#392] suggested to write
the sender application name and its version in the
socket over which pgagroal-cli and pgagroal communicate.
  • Loading branch information
EuGig committed May 5, 2024
1 parent 5dd618b commit 07e64b7
Show file tree
Hide file tree
Showing 6 changed files with 350 additions and 23 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ Yongting You <2010youy01@gmail.com>
Ashutosh Sharma <ash2003sharma@gmail.com>
Henrique de Carvalho <decarv.henrique@gmail.com>
Yihe Lu <t1t4m1un@gmail.com>
Eugenio Gigante <giganteeugenio2@gmail.com>
2 changes: 1 addition & 1 deletion src/include/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
* response object
*/
cJSON*
pgagroal_json_create_new_command_object(char* command_name, bool success, char* executable_name);
pgagroal_json_create_new_command_object(char* command_name, bool success, char* executable_name, char* executable_version);

/**
* Utility method to "jump" to the output JSON object wrapped into
Expand Down
19 changes: 19 additions & 0 deletions src/include/management.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,25 @@ extern "C" {
#define COMMAND_OUTPUT_FORMAT_TEXT 'T'
#define COMMAND_OUTPUT_FORMAT_JSON 'J'

/**
* Available applications
*/
#define PGAGROAL_EXECUTABLE 1
#define PGAGROAL_EXECUTABLE_CLI 2
#define PGAGROAL_EXECUTABLE_VAULT 3

/*
* stores the application name and its version
* which are sent through the socket
*/
struct pgagroal_version_info
{
char s[2];
int command;
char v[3];
int version;
};

/**
* Get the frontend password of a user
* @param ssl The SSL connection
Expand Down
2 changes: 1 addition & 1 deletion src/libpgagroal/configuration.c
Original file line number Diff line number Diff line change
Expand Up @@ -3066,7 +3066,7 @@ key_in_section(char* wanted, char* section, char* key, bool global, bool* unknow

// if here there is a match on the key, ensure the section is
// appropriate
if (global && (!strncmp(section, PGAGROAL_MAIN_INI_SECTION, MISC_LENGTH) | !strncmp(section, PGAGROAL_VAULT_INI_SECTION, MISC_LENGTH)))
if (global && (!strncmp(section, PGAGROAL_MAIN_INI_SECTION, MISC_LENGTH) || !strncmp(section, PGAGROAL_VAULT_INI_SECTION, MISC_LENGTH)))
{
return true;
}
Expand Down
22 changes: 17 additions & 5 deletions src/libpgagroal/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@
/* pgagroal */
#include <pgagroal.h>
#include <json.h>
#include <errno.h>

cJSON*
pgagroal_json_create_new_command_object(char* command_name, bool success, char* executable_name)
pgagroal_json_create_new_command_object(char* command_name, bool success, char* executable_name, char* executable_version)
{
// root of the JSON structure
cJSON* json = cJSON_CreateObject();
Expand Down Expand Up @@ -70,11 +71,22 @@ pgagroal_json_create_new_command_object(char* command_name, bool success, char*
goto error;
}

long minor = strtol(&executable_version[2], NULL, 10);
if (errno == ERANGE || minor <= LONG_MIN || minor >= LONG_MAX)
{
goto error;
}
long patch = strtol(&executable_version[5], NULL, 10);
if (errno == ERANGE || patch <= LONG_MIN || patch >= LONG_MAX)
{
goto error;
}

cJSON_AddStringToObject(application, JSON_TAG_APPLICATION_NAME, executable_name);
cJSON_AddNumberToObject(application, JSON_TAG_APPLICATION_VERSION_MAJOR, PGAGROAL_MAJOR_VERSION);
cJSON_AddNumberToObject(application, JSON_TAG_APPLICATION_VERSION_MINOR, PGAGROAL_MINOR_VERSION);
cJSON_AddNumberToObject(application, JSON_TAG_APPLICATION_VERSION_PATCH, PGAGROAL_PATCH_VERSION);
cJSON_AddStringToObject(application, JSON_TAG_APPLICATION_VERSION, PGAGROAL_VERSION);
cJSON_AddNumberToObject(application, JSON_TAG_APPLICATION_VERSION_MAJOR, executable_version[0] - '0');
cJSON_AddNumberToObject(application, JSON_TAG_APPLICATION_VERSION_MINOR, (int)minor);
cJSON_AddNumberToObject(application, JSON_TAG_APPLICATION_VERSION_PATCH, (int)patch);
cJSON_AddStringToObject(application, JSON_TAG_APPLICATION_VERSION, executable_version);

// add objects to the whole json thing
cJSON_AddItemToObject(json, "command", command);
Expand Down

0 comments on commit 07e64b7

Please sign in to comment.