mirror of
https://github.com/netbirdio/netbird.git
synced 2026-10-27 14:59:09 +01:00
stub out management proxy server database connection
This commit is contained in:
@@ -2,8 +2,11 @@ package grpc
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"github.com/netbirdio/netbird/management/internals/modules/services"
|
||||||
|
"github.com/netbirdio/netbird/management/server/store"
|
||||||
"github.com/netbirdio/netbird/shared/management/proto"
|
"github.com/netbirdio/netbird/shared/management/proto"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"google.golang.org/grpc/codes"
|
"google.golang.org/grpc/codes"
|
||||||
@@ -74,7 +77,7 @@ func (s *ProxyServiceServer) GetMappingUpdate(req *proto.GetMappingUpdateRequest
|
|||||||
log.Infof("Proxy %s disconnected", proxyID)
|
log.Infof("Proxy %s disconnected", proxyID)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
if err := s.sendSnapshot(conn); err != nil {
|
if err := s.sendSnapshot(ctx, conn); err != nil {
|
||||||
log.Errorf("Failed to send snapshot to proxy %s: %v", proxyID, err)
|
log.Errorf("Failed to send snapshot to proxy %s: %v", proxyID, err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -90,39 +93,96 @@ func (s *ProxyServiceServer) GetMappingUpdate(req *proto.GetMappingUpdateRequest
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type serviceStore interface {
|
||||||
|
GetAccountServices(ctx context.Context, lockStrength store.LockingStrength, accountID string) ([]*services.Service, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// stubstore implements a serviceStore using stubbed out data rather than a real database connection.
|
||||||
|
type stubstore struct{}
|
||||||
|
|
||||||
|
func (stubstore) GetAccountServices(_ context.Context, _ store.LockingStrength, _ string) ([]*services.Service, error) {
|
||||||
|
return []*services.Service{
|
||||||
|
{
|
||||||
|
ID: "test",
|
||||||
|
Domain: "test.netbird.io",
|
||||||
|
Targets: []services.Target{
|
||||||
|
{
|
||||||
|
Enabled: true,
|
||||||
|
Path: "/",
|
||||||
|
Host: "100.116.118.156:8181",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Enabled: true,
|
||||||
|
Exposed: true,
|
||||||
|
AuthBearerEnabled: true,
|
||||||
|
},
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
// sendSnapshot sends the initial snapshot of all services to proxy
|
// sendSnapshot sends the initial snapshot of all services to proxy
|
||||||
func (s *ProxyServiceServer) sendSnapshot(conn *proxyConnection) error {
|
func (s *ProxyServiceServer) sendSnapshot(ctx context.Context, conn *proxyConnection) error {
|
||||||
// TODO: Get actual services from database/store
|
svcs, err := stubstore{}.GetAccountServices(ctx, store.LockingStrengthNone, conn.proxyID) // TODO: check locking strength and accountID. Use an actual database connection here!
|
||||||
// For now, sending test service
|
if err != nil {
|
||||||
testService := &proto.ProxyMapping{
|
// TODO: something
|
||||||
Id: "test",
|
return fmt.Errorf("get account services from store: %w", err)
|
||||||
Domain: "test.netbird.io",
|
}
|
||||||
Path: []*proto.PathMapping{
|
|
||||||
{
|
for _, svc := range svcs {
|
||||||
Path: "/",
|
if !svc.Enabled || !svc.Exposed {
|
||||||
Target: "100.116.118.156:8181",
|
// We don't care about disabled services for snapshots.
|
||||||
},
|
continue
|
||||||
},
|
}
|
||||||
SetupKey: "some-key",
|
|
||||||
Auth: &proto.Authentication{
|
// Fill auth values.
|
||||||
Oidc: &proto.OIDC{
|
// TODO: This will be removed soon as the management server should be handling authentication rather than the proxy, so probably not much use in fleshing this out too much.
|
||||||
|
auth := &proto.Authentication{}
|
||||||
|
if svc.AuthBearerEnabled {
|
||||||
|
auth.Oidc = &proto.OIDC{
|
||||||
Enabled: true,
|
Enabled: true,
|
||||||
|
// TODO: fill other OIDC fields from account OIDC settings.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if svc.AuthBasicPassword != "" {
|
||||||
|
auth.Basic = &proto.HTTPBasic{
|
||||||
|
Enabled: true,
|
||||||
|
Username: svc.AuthBasicUsername,
|
||||||
|
Password: svc.AuthBasicPassword,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if svc.AuthPINValue != "" {
|
||||||
|
auth.Pin = &proto.Pin{
|
||||||
|
Enabled: true,
|
||||||
|
Pin: svc.AuthPINValue,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var paths []*proto.PathMapping
|
||||||
|
for _, t := range svc.Targets {
|
||||||
|
if !t.Enabled {
|
||||||
|
// We don't care about disabled service targets for snapshots.
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
paths = append(paths, &proto.PathMapping{
|
||||||
|
Path: t.Path,
|
||||||
|
Target: t.Host,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := conn.stream.Send(&proto.GetMappingUpdateResponse{
|
||||||
|
Mapping: []*proto.ProxyMapping{
|
||||||
|
{
|
||||||
|
Type: proto.ProxyMappingUpdateType_UPDATE_TYPE_CREATED, // Initial snapshot, all records are "new" for the proxy.
|
||||||
|
Id: svc.ID,
|
||||||
|
Domain: svc.Domain,
|
||||||
|
Path: paths,
|
||||||
|
SetupKey: "", // TODO: get the setup key.
|
||||||
|
Auth: auth,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
}); err != nil {
|
||||||
}
|
// TODO: log the error, maybe retry?
|
||||||
|
continue
|
||||||
snapshot := []*proto.ProxyMapping{
|
}
|
||||||
testService,
|
|
||||||
}
|
|
||||||
|
|
||||||
msg := &proto.GetMappingUpdateResponse{
|
|
||||||
Mapping: snapshot,
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Infof("Sending snapshot to proxy %s with %d services", conn.proxyID, len(snapshot))
|
|
||||||
|
|
||||||
if err := conn.stream.Send(msg); err != nil {
|
|
||||||
return status.Errorf(codes.Internal, "failed to send snapshot: %v", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
Reference in New Issue
Block a user