mirror of
https://github.com/netbirdio/netbird.git
synced 2026-10-09 23:19:11 +02:00
added allowed_user_ids call
Signed-off-by: Dmitri Dolguikh <dmitri.external@netbird.io>
This commit is contained in:
@@ -33,6 +33,7 @@ type NetworkMapDBStore interface {
|
|||||||
GetAccountSettings(ctx context.Context, accountId string) (nmdata.AccountSettingsInfo, error)
|
GetAccountSettings(ctx context.Context, accountId string) (nmdata.AccountSettingsInfo, error)
|
||||||
GetPostureChecks(ctx context.Context, accountId string) ([]nmdata.PostureChecks, error)
|
GetPostureChecks(ctx context.Context, accountId string) ([]nmdata.PostureChecks, error)
|
||||||
GetNetworkMapData(ctx context.Context, accountId string) (*networkmap.NetworkMapData, error)
|
GetNetworkMapData(ctx context.Context, accountId string) (*networkmap.NetworkMapData, error)
|
||||||
|
GetAllowedUserIds(ctx context.Context, accountId string) (map[string]struct{}, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type NetworkMapDBStoreImpl struct {
|
type NetworkMapDBStoreImpl struct {
|
||||||
|
|||||||
@@ -0,0 +1,42 @@
|
|||||||
|
package networkmap_pgsql
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"github.com/jackc/pgx/v5"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
GetAllowedUserIdsQuery = `
|
||||||
|
select id
|
||||||
|
from users
|
||||||
|
where account_id=$1 and not blocked and not is_service_user
|
||||||
|
`
|
||||||
|
)
|
||||||
|
|
||||||
|
func (pg *PgStore) GetAllowedUserIds(ctx context.Context, accountId string) (map[string]struct{}, error) {
|
||||||
|
c, err := pg.Pool.Acquire(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return GetGetAllowedUserIdsViaPgxConnection(ctx, c.Conn(), accountId)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetGetAllowedUserIdsViaPgxConnection(ctx context.Context, con *pgx.Conn, accountId string) (map[string]struct{}, error) {
|
||||||
|
rows, err := con.Query(ctx, GetAllowedUserIdsQuery, accountId)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
toret := make(map[string]struct{})
|
||||||
|
var id string
|
||||||
|
_, err = pgx.ForEachRow(rows, []any{&id}, func() error {
|
||||||
|
toret[id] = struct{}{}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return toret, nil
|
||||||
|
}
|
||||||
@@ -3,13 +3,11 @@ package networkmap_pgsql
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
_ "embed"
|
_ "embed"
|
||||||
|
|
||||||
"github.com/jackc/pgx/v5/pgxpool"
|
|
||||||
"github.com/netbirdio/netbird/shared/management/networkmap/nmdata"
|
"github.com/netbirdio/netbird/shared/management/networkmap/nmdata"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
@@ -59,7 +57,7 @@ func TestGetPeers(t *testing.T) {
|
|||||||
// )
|
// )
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetPolocies(t *testing.T) {
|
func TestGetPolicies(t *testing.T) {
|
||||||
ctx := context.TODO()
|
ctx := context.TODO()
|
||||||
|
|
||||||
s, err := NewPostgresqlStore(ctx, "postgresql://root:netbird@localhost:5432/netbird")
|
s, err := NewPostgresqlStore(ctx, "postgresql://root:netbird@localhost:5432/netbird")
|
||||||
@@ -256,18 +254,15 @@ func TestGetPostureChecks(t *testing.T) {
|
|||||||
// )
|
// )
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadSQL(ctx context.Context, pool *pgxpool.Pool, initdb string) error {
|
func TestGetGetAllowedUserIds(t *testing.T) {
|
||||||
queries := strings.Split(string(initdb), ";")
|
ctx := context.TODO()
|
||||||
|
|
||||||
for _, query := range queries {
|
s, err := NewPostgresqlStore(ctx, "postgresql://root:netbird@localhost:5432/netbird")
|
||||||
query = strings.TrimSpace(query)
|
assert.NoError(t, err)
|
||||||
if query != "" {
|
// err = loadSQL(ctx, s.pool, initDb)
|
||||||
_, err := pool.Query(ctx, query)
|
//assert.NoError(t, err)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
ids, err := s.GetAllowedUserIds(ctx, "cus73sbl0ubs73cfoo90") //"ckd7ee2fic3c73dtendg")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.NotEmpty(t, ids)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -62,6 +62,10 @@ func (pg *PgStore) GetNetworkMapData(ctx context.Context, accountId string) (*ne
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return rollbackAndReturnError(ctx, tx, err)
|
return rollbackAndReturnError(ctx, tx, err)
|
||||||
}
|
}
|
||||||
|
allowedUserIds, err := GetGetAllowedUserIdsViaPgxConnection(ctx, tx.Conn(), accountId)
|
||||||
|
if err != nil {
|
||||||
|
return rollbackAndReturnError(ctx, tx, err)
|
||||||
|
}
|
||||||
|
|
||||||
resourcePolicies := make(map[string][]*nmdata.Policy)
|
resourcePolicies := make(map[string][]*nmdata.Policy)
|
||||||
for _, resource := range networkResources {
|
for _, resource := range networkResources {
|
||||||
@@ -104,6 +108,7 @@ func (pg *PgStore) GetNetworkMapData(ctx context.Context, accountId string) (*ne
|
|||||||
NameServerGroups: toSliceOfPtrs(nsGroups),
|
NameServerGroups: toSliceOfPtrs(nsGroups),
|
||||||
NetworkResources: toSliceOfPtrs(networkResources),
|
NetworkResources: toSliceOfPtrs(networkResources),
|
||||||
PostureChecks: toMap(postureChecks, func(pc nmdata.PostureChecks) string { return pc.ID }),
|
PostureChecks: toMap(postureChecks, func(pc nmdata.PostureChecks) string { return pc.ID }),
|
||||||
|
AllowedUserIDs: allowedUserIds,
|
||||||
NetworkXIDToPublicID: networkXIDToPublicID, // TODO (dmitri) maybe we can switch to public ids everywhere?
|
NetworkXIDToPublicID: networkXIDToPublicID, // TODO (dmitri) maybe we can switch to public ids everywhere?
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user