Skip to content

Commit 3351b38

Browse files
[management] pass config to controller (#4807)
1 parent 05cbead commit 3351b38

File tree

19 files changed

+463
-471
lines changed

19 files changed

+463
-471
lines changed

client/cmd/testutil_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func startManagement(t *testing.T, config *config.Config, testFile string) (*grp
116116
ctx := context.Background()
117117
updateManager := update_channel.NewPeersUpdateManager(metrics)
118118
requestBuffer := mgmt.NewAccountRequestBuffer(ctx, store)
119-
networkMapController := controller.NewController(ctx, store, metrics, updateManager, requestBuffer, mgmt.MockIntegratedValidator{}, settingsMockManager, "netbird.cloud", port_forwarding.NewControllerMock())
119+
networkMapController := controller.NewController(ctx, store, metrics, updateManager, requestBuffer, mgmt.MockIntegratedValidator{}, settingsMockManager, "netbird.cloud", port_forwarding.NewControllerMock(), config)
120120

121121
accountManager, err := mgmt.BuildManager(context.Background(), config, store, networkMapController, nil, "", eventStore, nil, false, iv, metrics, port_forwarding.NewControllerMock(), settingsMockManager, permissionsManagerMock, false)
122122
if err != nil {

client/internal/engine.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,6 +1192,7 @@ func toRouteDomains(myPubKey string, routes []*route.Route) []*dnsfwd.ForwarderE
11921192
}
11931193

11941194
func toDNSConfig(protoDNSConfig *mgmProto.DNSConfig, network netip.Prefix) nbdns.Config {
1195+
//nolint
11951196
forwarderPort := uint16(protoDNSConfig.GetForwarderPort())
11961197
if forwarderPort == 0 {
11971198
forwarderPort = nbdns.ForwarderClientPort

client/internal/engine_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1624,7 +1624,7 @@ func startManagement(t *testing.T, dataDir, testFile string) (*grpc.Server, stri
16241624

16251625
updateManager := update_channel.NewPeersUpdateManager(metrics)
16261626
requestBuffer := server.NewAccountRequestBuffer(context.Background(), store)
1627-
networkMapController := controller.NewController(context.Background(), store, metrics, updateManager, requestBuffer, server.MockIntegratedValidator{}, settingsMockManager, "netbird.selfhosted", port_forwarding.NewControllerMock())
1627+
networkMapController := controller.NewController(context.Background(), store, metrics, updateManager, requestBuffer, server.MockIntegratedValidator{}, settingsMockManager, "netbird.selfhosted", port_forwarding.NewControllerMock(), config)
16281628
accountManager, err := server.BuildManager(context.Background(), config, store, networkMapController, nil, "", eventStore, nil, false, ia, metrics, port_forwarding.NewControllerMock(), settingsMockManager, permissionsManager, false)
16291629
if err != nil {
16301630
return nil, "", err

client/server/server_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ func startManagement(t *testing.T, signalAddr string, counter *int) (*grpc.Serve
316316

317317
requestBuffer := server.NewAccountRequestBuffer(context.Background(), store)
318318
peersUpdateManager := update_channel.NewPeersUpdateManager(metrics)
319-
networkMapController := controller.NewController(context.Background(), store, metrics, peersUpdateManager, requestBuffer, server.MockIntegratedValidator{}, settingsMockManager, "netbird.selfhosted", port_forwarding.NewControllerMock())
319+
networkMapController := controller.NewController(context.Background(), store, metrics, peersUpdateManager, requestBuffer, server.MockIntegratedValidator{}, settingsMockManager, "netbird.selfhosted", port_forwarding.NewControllerMock(), config)
320320
accountManager, err := server.BuildManager(context.Background(), config, store, networkMapController, nil, "", eventStore, nil, false, ia, metrics, port_forwarding.NewControllerMock(), settingsMockManager, permissionsManagerMock, false)
321321
if err != nil {
322322
return nil, "", err

management/internals/controllers/network_map/controller/controller.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
nbdns "github.com/netbirdio/netbird/dns"
2020
"github.com/netbirdio/netbird/management/internals/controllers/network_map"
2121
"github.com/netbirdio/netbird/management/internals/controllers/network_map/controller/cache"
22+
"github.com/netbirdio/netbird/management/internals/server/config"
2223
"github.com/netbirdio/netbird/management/internals/shared/grpc"
2324
"github.com/netbirdio/netbird/management/server/account"
2425
"github.com/netbirdio/netbird/management/server/integrations/integrated_validator"
@@ -47,6 +48,7 @@ type Controller struct {
4748
updateAccountPeersBufferInterval atomic.Int64
4849
// dnsDomain is used for peer resolution. This is appended to the peer's name
4950
dnsDomain string
51+
config *config.Config
5052

5153
requestBuffer account.RequestBuffer
5254

@@ -68,7 +70,7 @@ type bufferUpdate struct {
6870

6971
var _ network_map.Controller = (*Controller)(nil)
7072

71-
func NewController(ctx context.Context, store store.Store, metrics telemetry.AppMetrics, peersUpdateManager network_map.PeersUpdateManager, requestBuffer account.RequestBuffer, integratedPeerValidator integrated_validator.IntegratedValidator, settingsManager settings.Manager, dnsDomain string, proxyController port_forwarding.Controller) *Controller {
73+
func NewController(ctx context.Context, store store.Store, metrics telemetry.AppMetrics, peersUpdateManager network_map.PeersUpdateManager, requestBuffer account.RequestBuffer, integratedPeerValidator integrated_validator.IntegratedValidator, settingsManager settings.Manager, dnsDomain string, proxyController port_forwarding.Controller, config *config.Config) *Controller {
7274
nMetrics, err := newMetrics(metrics.UpdateChannelMetrics())
7375
if err != nil {
7476
log.Fatal(fmt.Errorf("error creating metrics: %w", err))
@@ -95,6 +97,7 @@ func NewController(ctx context.Context, store store.Store, metrics telemetry.App
9597
integratedPeerValidator: integratedPeerValidator,
9698
settingsManager: settingsManager,
9799
dnsDomain: dnsDomain,
100+
config: config,
98101

99102
proxyController: proxyController,
100103

@@ -205,7 +208,7 @@ func (c *Controller) sendUpdateAccountPeers(ctx context.Context, accountID strin
205208

206209
peerGroups := account.GetPeerGroups(p.ID)
207210
start = time.Now()
208-
update := grpc.ToSyncResponse(ctx, nil, p, nil, nil, remotePeerNetworkMap, dnsDomain, postureChecks, dnsCache, account.Settings, extraSetting, maps.Keys(peerGroups), dnsFwdPort)
211+
update := grpc.ToSyncResponse(ctx, nil, c.config.HttpConfig, c.config.DeviceAuthorizationFlow, p, nil, nil, remotePeerNetworkMap, dnsDomain, postureChecks, dnsCache, account.Settings, extraSetting, maps.Keys(peerGroups), dnsFwdPort)
209212
c.metrics.CountToSyncResponseDuration(time.Since(start))
210213

211214
c.peersUpdateManager.SendUpdate(ctx, p.ID, &network_map.UpdateMessage{Update: update})
@@ -323,7 +326,7 @@ func (c *Controller) UpdateAccountPeer(ctx context.Context, accountId string, pe
323326
peerGroups := account.GetPeerGroups(peerId)
324327
dnsFwdPort := computeForwarderPort(maps.Values(account.Peers), network_map.DnsForwarderPortMinVersion)
325328

326-
update := grpc.ToSyncResponse(ctx, nil, peer, nil, nil, remotePeerNetworkMap, dnsDomain, postureChecks, dnsCache, account.Settings, extraSettings, maps.Keys(peerGroups), dnsFwdPort)
329+
update := grpc.ToSyncResponse(ctx, nil, c.config.HttpConfig, c.config.DeviceAuthorizationFlow, peer, nil, nil, remotePeerNetworkMap, dnsDomain, postureChecks, dnsCache, account.Settings, extraSettings, maps.Keys(peerGroups), dnsFwdPort)
327330
c.peersUpdateManager.SendUpdate(ctx, peer.ID, &network_map.UpdateMessage{Update: update})
328331

329332
return nil

management/internals/server/controllers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func (s *BaseServer) EphemeralManager() ephemeral.Manager {
7070

7171
func (s *BaseServer) NetworkMapController() network_map.Controller {
7272
return Create(s, func() *nmapcontroller.Controller {
73-
return nmapcontroller.NewController(context.Background(), s.Store(), s.Metrics(), s.PeersUpdateManager(), s.AccountRequestBuffer(), s.IntegratedValidator(), s.SettingsManager(), s.dnsDomain, s.ProxyController())
73+
return nmapcontroller.NewController(context.Background(), s.Store(), s.Metrics(), s.PeersUpdateManager(), s.AccountRequestBuffer(), s.IntegratedValidator(), s.SettingsManager(), s.dnsDomain, s.ProxyController(), s.config)
7474
})
7575
}
7676

management/internals/shared/grpc/conversion.go

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func toNetbirdConfig(config *nbconfig.Config, turnCredentials *Token, relayToken
8383
return nbConfig
8484
}
8585

86-
func toPeerConfig(peer *nbpeer.Peer, network *types.Network, dnsName string, settings *types.Settings, config *nbconfig.Config) *proto.PeerConfig {
86+
func toPeerConfig(peer *nbpeer.Peer, network *types.Network, dnsName string, settings *types.Settings, httpConfig *nbconfig.HttpServerConfig, deviceFlowConfig *nbconfig.DeviceAuthorizationFlow) *proto.PeerConfig {
8787
netmask, _ := network.Net.Mask.Size()
8888
fqdn := peer.FQDN(dnsName)
8989

@@ -92,7 +92,7 @@ func toPeerConfig(peer *nbpeer.Peer, network *types.Network, dnsName string, set
9292
}
9393

9494
if peer.SSHEnabled {
95-
sshConfig.JwtConfig = buildJWTConfig(config)
95+
sshConfig.JwtConfig = buildJWTConfig(httpConfig, deviceFlowConfig)
9696
}
9797

9898
return &proto.PeerConfig{
@@ -104,9 +104,9 @@ func toPeerConfig(peer *nbpeer.Peer, network *types.Network, dnsName string, set
104104
}
105105
}
106106

107-
func ToSyncResponse(ctx context.Context, config *nbconfig.Config, peer *nbpeer.Peer, turnCredentials *Token, relayCredentials *Token, networkMap *types.NetworkMap, dnsName string, checks []*posture.Checks, dnsCache *cache.DNSConfigCache, settings *types.Settings, extraSettings *types.ExtraSettings, peerGroups []string, dnsFwdPort int64) *proto.SyncResponse {
107+
func ToSyncResponse(ctx context.Context, config *nbconfig.Config, httpConfig *nbconfig.HttpServerConfig, deviceFlowConfig *nbconfig.DeviceAuthorizationFlow, peer *nbpeer.Peer, turnCredentials *Token, relayCredentials *Token, networkMap *types.NetworkMap, dnsName string, checks []*posture.Checks, dnsCache *cache.DNSConfigCache, settings *types.Settings, extraSettings *types.ExtraSettings, peerGroups []string, dnsFwdPort int64) *proto.SyncResponse {
108108
response := &proto.SyncResponse{
109-
PeerConfig: toPeerConfig(peer, networkMap.Network, dnsName, settings, config),
109+
PeerConfig: toPeerConfig(peer, networkMap.Network, dnsName, settings, httpConfig, deviceFlowConfig),
110110
NetworkMap: &proto.NetworkMap{
111111
Serial: networkMap.Network.CurrentSerial(),
112112
Routes: toProtocolRoutes(networkMap.Routes),
@@ -363,35 +363,29 @@ func convertToProtoNameServerGroup(nsGroup *nbdns.NameServerGroup) *proto.NameSe
363363
}
364364

365365
// buildJWTConfig constructs JWT configuration for SSH servers from management server config
366-
func buildJWTConfig(config *nbconfig.Config) *proto.JWTConfig {
367-
if config == nil {
368-
return nil
369-
}
370-
371-
if config.HttpConfig == nil || config.HttpConfig.AuthAudience == "" {
366+
func buildJWTConfig(config *nbconfig.HttpServerConfig, deviceFlowConfig *nbconfig.DeviceAuthorizationFlow) *proto.JWTConfig {
367+
if config == nil || config.AuthAudience == "" {
372368
return nil
373369
}
374370

375-
issuer := strings.TrimSpace(config.HttpConfig.AuthIssuer)
376-
if issuer == "" {
377-
if config.DeviceAuthorizationFlow != nil {
378-
if d := deriveIssuerFromTokenEndpoint(config.DeviceAuthorizationFlow.ProviderConfig.TokenEndpoint); d != "" {
379-
issuer = d
380-
}
371+
issuer := strings.TrimSpace(config.AuthIssuer)
372+
if issuer == "" || deviceFlowConfig != nil {
373+
if d := deriveIssuerFromTokenEndpoint(deviceFlowConfig.ProviderConfig.TokenEndpoint); d != "" {
374+
issuer = d
381375
}
382376
}
383377
if issuer == "" {
384378
return nil
385379
}
386380

387-
keysLocation := strings.TrimSpace(config.HttpConfig.AuthKeysLocation)
381+
keysLocation := strings.TrimSpace(config.AuthKeysLocation)
388382
if keysLocation == "" {
389383
keysLocation = strings.TrimSuffix(issuer, "/") + "/.well-known/jwks.json"
390384
}
391385

392386
return &proto.JWTConfig{
393387
Issuer: issuer,
394-
Audience: config.HttpConfig.AuthAudience,
388+
Audience: config.AuthAudience,
395389
KeysLocation: keysLocation,
396390
}
397391
}

management/internals/shared/grpc/server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ func (s *Server) prepareLoginResponse(ctx context.Context, peer *nbpeer.Peer, ne
646646
// if peer has reached this point then it has logged in
647647
loginResp := &proto.LoginResponse{
648648
NetbirdConfig: toNetbirdConfig(s.config, nil, relayToken, nil),
649-
PeerConfig: toPeerConfig(peer, netMap.Network, s.networkMapController.GetDNSDomain(settings), settings, s.config),
649+
PeerConfig: toPeerConfig(peer, netMap.Network, s.networkMapController.GetDNSDomain(settings), settings, s.config.HttpConfig, s.config.DeviceAuthorizationFlow),
650650
Checks: toProtocolChecks(ctx, postureChecks),
651651
}
652652

@@ -713,7 +713,7 @@ func (s *Server) sendInitialSync(ctx context.Context, peerKey wgtypes.Key, peer
713713
return status.Errorf(codes.Internal, "failed to get peer groups %s", err)
714714
}
715715

716-
plainResp := ToSyncResponse(ctx, s.config, peer, turnToken, relayToken, networkMap, s.networkMapController.GetDNSDomain(settings), postureChecks, nil, settings, settings.Extra, peerGroups, dnsFwdPort)
716+
plainResp := ToSyncResponse(ctx, s.config, s.config.HttpConfig, s.config.DeviceAuthorizationFlow, peer, turnToken, relayToken, networkMap, s.networkMapController.GetDNSDomain(settings), postureChecks, nil, settings, settings.Extra, peerGroups, dnsFwdPort)
717717

718718
encryptedResp, err := encryption.EncryptMessage(peerKey, s.wgKey, plainResp)
719719
if err != nil {

management/server/account_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/netbirdio/netbird/management/internals/controllers/network_map"
2626
"github.com/netbirdio/netbird/management/internals/controllers/network_map/controller"
2727
"github.com/netbirdio/netbird/management/internals/controllers/network_map/update_channel"
28+
"github.com/netbirdio/netbird/management/internals/server/config"
2829
nbAccount "github.com/netbirdio/netbird/management/server/account"
2930
"github.com/netbirdio/netbird/management/server/activity"
3031
"github.com/netbirdio/netbird/management/server/cache"
@@ -2958,7 +2959,7 @@ func createManager(t testing.TB) (*DefaultAccountManager, *update_channel.PeersU
29582959

29592960
updateManager := update_channel.NewPeersUpdateManager(metrics)
29602961
requestBuffer := NewAccountRequestBuffer(ctx, store)
2961-
networkMapController := controller.NewController(ctx, store, metrics, updateManager, requestBuffer, MockIntegratedValidator{}, settingsMockManager, "netbird.cloud", port_forwarding.NewControllerMock())
2962+
networkMapController := controller.NewController(ctx, store, metrics, updateManager, requestBuffer, MockIntegratedValidator{}, settingsMockManager, "netbird.cloud", port_forwarding.NewControllerMock(), &config.Config{})
29622963
manager, err := BuildManager(ctx, nil, store, networkMapController, nil, "", eventStore, nil, false, MockIntegratedValidator{}, metrics, port_forwarding.NewControllerMock(), settingsMockManager, permissionsManager, false)
29632964
if err != nil {
29642965
return nil, nil, err

management/server/dns_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
nbdns "github.com/netbirdio/netbird/dns"
1313
"github.com/netbirdio/netbird/management/internals/controllers/network_map/controller"
1414
"github.com/netbirdio/netbird/management/internals/controllers/network_map/update_channel"
15+
"github.com/netbirdio/netbird/management/internals/server/config"
1516
"github.com/netbirdio/netbird/management/server/integrations/port_forwarding"
1617
"github.com/netbirdio/netbird/management/server/permissions"
1718
"github.com/netbirdio/netbird/management/server/settings"
@@ -222,7 +223,7 @@ func createDNSManager(t *testing.T) (*DefaultAccountManager, error) {
222223
ctx := context.Background()
223224
updateManager := update_channel.NewPeersUpdateManager(metrics)
224225
requestBuffer := NewAccountRequestBuffer(ctx, store)
225-
networkMapController := controller.NewController(ctx, store, metrics, updateManager, requestBuffer, MockIntegratedValidator{}, settingsMockManager, "netbird.test", port_forwarding.NewControllerMock())
226+
networkMapController := controller.NewController(ctx, store, metrics, updateManager, requestBuffer, MockIntegratedValidator{}, settingsMockManager, "netbird.test", port_forwarding.NewControllerMock(), &config.Config{})
226227

227228
return BuildManager(context.Background(), nil, store, networkMapController, nil, "", eventStore, nil, false, MockIntegratedValidator{}, metrics, port_forwarding.NewControllerMock(), settingsMockManager, permissionsManager, false)
228229
}

0 commit comments

Comments
 (0)