Skip to content

Commit

Permalink
[#484] Redo CLI enable/disable feature
Browse files Browse the repository at this point in the history
  • Loading branch information
jesperpedersen committed Nov 10, 2024
1 parent 1e7fbc3 commit 8a661f2
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 56 deletions.
1 change: 1 addition & 0 deletions src/include/pgagroal.h
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,7 @@ struct main_configuration
int management; /**< The management port */
bool gracefully; /**< Is pgagroal in gracefully mode */

bool all_disabled; /**< Are all databases disabled */
char disabled[NUMBER_OF_DISABLED][MAX_DATABASE_LENGTH]; /**< Which databases are disabled */

int pipeline; /**< The pipeline type */
Expand Down
2 changes: 2 additions & 0 deletions src/libpgagroal/configuration.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ pgagroal_init_configuration(void* shm)
config->disconnect_client = 0;
config->disconnect_client_force = false;

config->all_disabled = false;

config->keep_alive = true;
config->nodelay = true;
config->non_blocking = false;
Expand Down
8 changes: 6 additions & 2 deletions src/libpgagroal/security.c
Original file line number Diff line number Diff line change
Expand Up @@ -3096,10 +3096,14 @@ is_disabled(char* database)

config = (struct main_configuration*)shmem;

if (config->all_disabled)
{
return true;
}

for (int i = 0; i < NUMBER_OF_DISABLED; i++)
{
if (!strcmp(config->disabled[i], "*") ||
!strcmp(config->disabled[i], database))
if (!strcmp(config->disabled[i], database))
{
return true;
}
Expand Down
59 changes: 38 additions & 21 deletions src/libpgagroal/status.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,9 @@ status_details(bool details, struct json* response)

if (details)
{
int number_of_disabled = 0;
struct json* limits = NULL;
struct json* databases = NULL;
struct json* connections = NULL;

pgagroal_json_create(&limits);
Expand All @@ -236,35 +238,50 @@ status_details(bool details, struct json* response)

pgagroal_json_put(response, MANAGEMENT_ARGUMENT_LIMITS, (uintptr_t)limits, ValueJSON);

// TODO Databases + Disabled
/* for (int i = 0; i < NUMBER_OF_DISABLED; i++) */
/* { */
/* if (strcmp(disabled[i], "")) */
/* { */
/* if (!strcmp(disabled[i], "*")) */
/* { */
/* cJSON_AddItemToArray(databases_array, cJSON_CreateString("ALL")); */
/* counter = -1; */
/* } */
/* else */
/* { */
/* cJSON_AddItemToArray(databases_array, cJSON_CreateString(disabled[i])); */
/* counter++; */
/* } */
/* } */
/* } */
pgagroal_json_create(&databases);

for (int i = 0; i < NUMBER_OF_DISABLED; i++)
{
struct json* js = NULL;

if (strlen(config->disabled[i]) > 0)
{
pgagroal_json_create(&js);

pgagroal_json_put(js, MANAGEMENT_ARGUMENT_DATABASE, (uintptr_t)config->disabled[i], ValueString);
pgagroal_json_put(js, MANAGEMENT_ARGUMENT_ENABLED, (uintptr_t)false, ValueBool);

pgagroal_json_append(databases, (uintptr_t)js, ValueJSON);

number_of_disabled++;
}
}

if (number_of_disabled == 0)
{
struct json* js = NULL;

pgagroal_json_create(&js);

pgagroal_json_put(js, MANAGEMENT_ARGUMENT_DATABASE, (uintptr_t)"*", ValueString);
pgagroal_json_put(js, MANAGEMENT_ARGUMENT_ENABLED, (uintptr_t)!config->all_disabled, ValueBool);

pgagroal_json_append(databases, (uintptr_t)js, ValueJSON);
}

pgagroal_json_put(response, MANAGEMENT_ARGUMENT_DATABASES, (uintptr_t)databases, ValueJSON);

for (int i = 0; i < config->max_connections; i++)
{
struct json* js = NULL;

pgagroal_json_create(&js);

pgagroal_json_put(js, MANAGEMENT_ARGUMENT_START_TIME, (uintptr_t)config->connections[i].start_time, ValueUInt64);
pgagroal_json_put(js, MANAGEMENT_ARGUMENT_TIMESTAMP, (uintptr_t)config->connections[i].timestamp, ValueUInt64);
pgagroal_json_put(js, MANAGEMENT_ARGUMENT_START_TIME, (uintptr_t)config->connections[i].start_time, ValueInt64);
pgagroal_json_put(js, MANAGEMENT_ARGUMENT_TIMESTAMP, (uintptr_t)config->connections[i].timestamp, ValueInt64);

pgagroal_json_put(js, MANAGEMENT_ARGUMENT_PID, (uintptr_t)config->connections[i].pid, ValueUInt32);
pgagroal_json_put(js, MANAGEMENT_ARGUMENT_FD, (uintptr_t)config->connections[i].fd, ValueUInt32);
pgagroal_json_put(js, MANAGEMENT_ARGUMENT_PID, (uintptr_t)config->connections[i].pid, ValueInt32);
pgagroal_json_put(js, MANAGEMENT_ARGUMENT_FD, (uintptr_t)config->connections[i].fd, ValueInt32);

pgagroal_json_put(js, MANAGEMENT_ARGUMENT_DATABASE, (uintptr_t)config->connections[i].database, ValueString);
pgagroal_json_put(js, MANAGEMENT_ARGUMENT_USERNAME, (uintptr_t)config->connections[i].username, ValueString);
Expand Down
89 changes: 56 additions & 33 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1484,30 +1484,40 @@ accept_mgt_cb(struct ev_loop* loop, struct ev_io* watcher, int revents)
pgagroal_management_create_response(payload, -1, &res);
pgagroal_json_create(&databases);

for (int i = 0; i < NUMBER_OF_DISABLED; i++)
if (!strcmp("*", database))
{
bool found = false;
struct json* js = NULL;

if (!strcmp("*", database))
{
memset(&config->disabled[i], 0, MAX_DATABASE_LENGTH);
found = true;
}
else if (!strcmp(config->disabled[i], database))
{
memset(&config->disabled[i], 0, MAX_DATABASE_LENGTH);
found = true;
}
config->all_disabled = false;
memset(&config->disabled, 0, sizeof(config->disabled));

if (found)
pgagroal_json_create(&js);

pgagroal_json_put(js, MANAGEMENT_ARGUMENT_DATABASE, (uintptr_t)database, ValueString);
pgagroal_json_put(js, MANAGEMENT_ARGUMENT_ENABLED, (uintptr_t)true, ValueBool);

pgagroal_json_append(databases, (uintptr_t)js, ValueJSON);
}
else
{
bool found = false;
for (int i = 0; !found && i < NUMBER_OF_DISABLED; i++)
{
pgagroal_json_create(&js);
struct json* js = NULL;

if (!strcmp(config->disabled[i], database))
{
memset(&config->disabled[i], 0, MAX_DATABASE_LENGTH);

pgagroal_json_put(js, MANAGEMENT_ARGUMENT_DATABASE, (uintptr_t)database, ValueString);
pgagroal_json_put(js, MANAGEMENT_ARGUMENT_ENABLED, (uintptr_t)true, ValueBool);
pgagroal_json_create(&js);

pgagroal_json_append(databases, (uintptr_t)js, ValueJSON);
pgagroal_json_put(js, MANAGEMENT_ARGUMENT_DATABASE, (uintptr_t)database, ValueString);
pgagroal_json_put(js, MANAGEMENT_ARGUMENT_ENABLED, (uintptr_t)true, ValueBool);

pgagroal_json_append(databases, (uintptr_t)js, ValueJSON);

found = true;
}
}
}

Expand Down Expand Up @@ -1535,29 +1545,42 @@ accept_mgt_cb(struct ev_loop* loop, struct ev_io* watcher, int revents)
pgagroal_management_create_response(payload, -1, &res);
pgagroal_json_create(&databases);

for (int i = 0; i < NUMBER_OF_DISABLED; i++)
config->all_disabled = false;

if (!strcmp("*", database))
{
bool found = false;
struct json* js = NULL;

if (!strcmp("*", database))
{
memcpy(&config->disabled[i], database, strlen(database));
}
else if (!strcmp(config->disabled[i], ""))
{
memcpy(&config->disabled[i], database, strlen(database));
break;
}
config->all_disabled = true;
memset(&config->disabled, 0, sizeof(config->disabled));

pgagroal_json_create(&js);

pgagroal_json_put(js, MANAGEMENT_ARGUMENT_DATABASE, (uintptr_t)database, ValueString);
pgagroal_json_put(js, MANAGEMENT_ARGUMENT_ENABLED, (uintptr_t)false, ValueBool);

if (found)
pgagroal_json_append(databases, (uintptr_t)js, ValueJSON);
}
else
{
bool inserted = false;
for (int i = 0; !inserted && i < NUMBER_OF_DISABLED; i++)
{
pgagroal_json_create(&js);
struct json* js = NULL;

if (strlen(config->disabled[i]) == 0)
{
memcpy(&config->disabled[i], database, strlen(database) > MAX_DATABASE_LENGTH ? MAX_DATABASE_LENGTH : strlen(database));

pgagroal_json_create(&js);

pgagroal_json_put(js, MANAGEMENT_ARGUMENT_DATABASE, (uintptr_t)database, ValueString);
pgagroal_json_put(js, MANAGEMENT_ARGUMENT_ENABLED, (uintptr_t)false, ValueBool);
pgagroal_json_put(js, MANAGEMENT_ARGUMENT_DATABASE, (uintptr_t)database, ValueString);
pgagroal_json_put(js, MANAGEMENT_ARGUMENT_ENABLED, (uintptr_t)false, ValueBool);

pgagroal_json_append(databases, (uintptr_t)js, ValueJSON);
pgagroal_json_append(databases, (uintptr_t)js, ValueJSON);

inserted = true;
}
}
}

Expand Down

0 comments on commit 8a661f2

Please sign in to comment.