Skip to content

Commit

Permalink
Cchanged the logic. now it is based on obtaining a list of group memb…
Browse files Browse the repository at this point in the history
…ers, and not on whether the participant belongs to a specific group
  • Loading branch information
NikitaUnisikhin committed May 1, 2024
1 parent 3800860 commit 40cbb0f
Show file tree
Hide file tree
Showing 10 changed files with 194 additions and 136 deletions.
4 changes: 2 additions & 2 deletions docker/group/config.conf
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ database "group_db" {

pool_routing "internal"
pool "session"
group_query "select pg_has_role('%s', 'group1', 'MEMBER')"
group_query "SELECT rolname FROM pg_roles WHERE pg_has_role(rolname, 'group1', 'member');"
}

user "group_user2" {
Expand All @@ -52,7 +52,7 @@ database "group_db" {

pool_routing "internal"
pool "session"
group_query "select pg_has_role('%s', 'group2', 'MEMBER')"
group_query "SELECT rolname FROM pg_roles WHERE pg_has_role(rolname, 'group2', 'member');"
}

user "group_user4" {
Expand Down
17 changes: 11 additions & 6 deletions docker/group/test_group.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,17 @@ for user in "${users[@]}"; do
}
done

psql -h localhost -p 6432 -U postgres -c "GRANT group1 TO group_user1;" group_db
ody-stop

psql -h localhost -p 5432 -U postgres -c "GRANT group1 TO group_user2;" group_db
psql -h localhost -p 5432 -U postgres -c "GRANT group1 TO group_user4;" group_db
psql -h localhost -p 5432 -U postgres -c "GRANT group2 TO group_user4;" group_db
psql -h localhost -p 5432 -U postgres -c "GRANT group1 TO group_user1;" group_db

/usr/bin/odyssey /group/config.conf

sleep 1

psql -h localhost -p 6432 -U group_user1 -c "SELECT 1" group_db >/dev/null 2>&1 || {
echo "ERROR: group auth apply for over user at config"

Expand All @@ -33,8 +42,6 @@ psql -h localhost -p 6432 -U group_user1 -c "SELECT 1" group_db >/dev/null 2>&1
exit 1
}

psql -h localhost -p 6432 -U postgres -c "GRANT group1 TO group_user2;" group_db
sleep 1
psql -h localhost -p 6432 -U group_user2 -c "SELECT 1" group_db >/dev/null 2>&1 && {
echo "ERROR: group auth not apply"

Expand All @@ -47,9 +54,6 @@ psql -h localhost -p 6432 -U group_user2 -c "SELECT 1" group_db >/dev/null 2>&1
exit 1
}

psql -h localhost -p 6432 -U postgres -c "GRANT group1 TO group_user4;" group_db
psql -h localhost -p 6432 -U postgres -c "GRANT group2 TO group_user4;" group_db
sleep 1
PGPASSWORD=password1 psql -h localhost -p 6432 -U group_user4 -c "SELECT 1" group_db >/dev/null 2>&1 && {
echo "ERROR: group auth not accepted down group"

Expand All @@ -61,6 +65,7 @@ PGPASSWORD=password1 psql -h localhost -p 6432 -U group_user4 -c "SELECT 1" grou

exit 1
}

PGPASSWORD=password2 psql -h localhost -p 6432 -U group_user4 -c "SELECT 1" group_db >/dev/null 2>&1 || {
echo "ERROR: group auth not apply"

Expand Down
6 changes: 4 additions & 2 deletions sources/config_reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -1893,6 +1893,8 @@ static int od_config_reader_group(od_config_reader_t *reader, char *db_name,
if (!od_config_reader_string(reader, &group_name))
return NOT_OK_RESPONSE;

// TODO: need to find a way to create internal rules for a specific database

char route_usr[strlen("group_") + strlen(group_name) + 1];
char route_db[strlen("group_") + strlen(group_name) + 1];
snprintf(route_usr, sizeof route_usr, "%s%s", "group_", group_name);
Expand Down Expand Up @@ -1945,7 +1947,7 @@ static int od_config_reader_group(od_config_reader_t *reader, char *db_name,
// force several settings
group->storage_db = rule->storage_db;
group->storage_user = rule->storage_user;
rule->pool->routing = OD_RULE_POOL_INTERVAL;
rule->pool->routing = OD_RULE_POOL_INTERNAL;

return OK_RESPONSE;

Expand Down Expand Up @@ -2004,7 +2006,7 @@ static inline int od_config_reader_watchdog(od_config_reader_t *reader,
// force several settings
watchdog->storage_db = rule->storage_db;
watchdog->storage_user = rule->storage_user;
rule->pool->routing = OD_RULE_POOL_INTERVAL;
rule->pool->routing = OD_RULE_POOL_INTERNAL;

return OK_RESPONSE;
}
Expand Down
30 changes: 14 additions & 16 deletions sources/group.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,7 @@ int od_group_free(od_group_t *group)
return OK_RESPONSE;
}

void od_group_qry_format(char *qry, char *fmt, ...)
{
va_list args;
va_start(args, fmt);
int len = od_vsnprintf(qry, OD_QRY_MAX_SZ, fmt, args);
va_end(args);
}

int od_group_parse_val_datarow(machine_msg_t *msg, int *is_group_member)
int od_group_parse_val_datarow(machine_msg_t *msg, char **group_member)
{
char *pos = (char *)machine_msg_data(msg) + 1;
uint32_t pos_size = machine_msg_size(msg) - 1;
Expand Down Expand Up @@ -66,15 +58,21 @@ int od_group_parse_val_datarow(machine_msg_t *msg, int *is_group_member)
goto error;
}

if (strcmp(pos, "f") == 0) {
*is_group_member = 0;
} else if (strcmp(pos, "t") == 0) {
*is_group_member = 1;
} else {
goto error;
}
*group_member = strdup(pos);

return OK_RESPONSE;
error:
return NOT_OK_RESPONSE;
}

od_group_member_name_item_t *od_group_member_name_item_add(od_list_t *members)
{
od_group_member_name_item_t *item;
item = (od_group_member_name_item_t *)malloc(sizeof(*item));
if (item == NULL)
return NULL;
memset(item, 0, sizeof(*item));
od_list_init(&item->link);
od_list_append(members, &item->link);
return item;
}
12 changes: 10 additions & 2 deletions sources/group.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,16 @@ struct od_group {
od_list_t link;
};

typedef struct od_group_member_name_item od_group_member_name_item_t;

struct od_group_member_name_item {
char *value;
int is_checked;
od_list_t link;
};

int od_group_free(od_group_t *);
void od_group_qry_format(char *, char *, ...);
int od_group_parse_val_datarow(machine_msg_t *, int *);
int od_group_parse_val_datarow(machine_msg_t *, char **);
od_group_member_name_item_t *od_group_member_name_item_add(od_list_t *);

#endif /* ODYSSEY_GROUP_CHECK_ITER_INTERVAL */
2 changes: 1 addition & 1 deletion sources/pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ int od_rule_matches_client(od_rule_pool_t *pool, od_pool_client_type_t t)
{
switch (t) {
case OD_POOL_CLIENT_INTERNAL:
return pool->routing == OD_RULE_POOL_INTERVAL;
return pool->routing == OD_RULE_POOL_INTERNAL;
case OD_POOL_CLIENT_EXTERNAL:
return pool->routing == OD_RULE_POOL_CLIENT_VISIBLE;
default:
Expand Down
2 changes: 1 addition & 1 deletion sources/pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ typedef enum {
} od_rule_pool_type_t;

typedef enum {
OD_RULE_POOL_INTERVAL,
OD_RULE_POOL_INTERNAL,
OD_RULE_POOL_CLIENT_VISIBLE,
} od_rule_routing_type_t;

Expand Down
4 changes: 1 addition & 3 deletions sources/router.c
Original file line number Diff line number Diff line change
Expand Up @@ -358,12 +358,10 @@ od_router_status_t od_router_route(od_router_t *router, od_client_t *client)
int salen;
struct sockaddr *saddr;
int rc;
od_address_range_t default_address_range;
switch (client->type) {
case OD_POOL_CLIENT_INTERNAL:
default_address_range = od_address_range_create_default();
rule = od_rules_forward(&router->rules, startup->database.value,
startup->user.value, &default_address_range, 1);
startup->user.value, NULL, 1);
break;
case OD_POOL_CLIENT_EXTERNAL:
salen = sizeof(sa);
Expand Down
Loading

0 comments on commit 40cbb0f

Please sign in to comment.