mirror of
https://github.com/netbirdio/netbird.git
synced 2026-05-06 17:08:53 +00:00
Generate ssh_config independently of ssh server
This commit is contained in:
@@ -24,7 +24,6 @@ type sshServer interface {
|
||||
RemoveAuthorizedKey(peer string)
|
||||
AddAuthorizedKey(peer, newKey string) error
|
||||
SetSocketFilter(ifIdx int)
|
||||
SetupSSHClientConfigWithPeers(peerKeys []sshconfig.PeerHostKey) error
|
||||
}
|
||||
|
||||
func (e *Engine) setupSSHPortRedirection() error {
|
||||
@@ -183,11 +182,8 @@ func (e *Engine) updateKnownHostsFile(peerKeys []sshconfig.PeerHostKey) error {
|
||||
|
||||
// updateSSHClientConfig updates SSH client configuration with peer hostnames
|
||||
func (e *Engine) updateSSHClientConfig(peerKeys []sshconfig.PeerHostKey) {
|
||||
if e.sshServer == nil {
|
||||
return
|
||||
}
|
||||
|
||||
if err := e.sshServer.SetupSSHClientConfigWithPeers(peerKeys); err != nil {
|
||||
configMgr := sshconfig.NewManager()
|
||||
if err := configMgr.SetupSSHClientConfig(peerKeys); err != nil {
|
||||
log.Warnf("failed to update SSH client config with peer hostnames: %v", err)
|
||||
} else {
|
||||
log.Debugf("updated SSH client config with %d peer hostnames", len(peerKeys))
|
||||
@@ -271,9 +267,6 @@ func (e *Engine) startSSHServer() error {
|
||||
return fmt.Errorf("start SSH server: %w", err)
|
||||
}
|
||||
|
||||
if err := server.SetupSSHClientConfig(); err != nil {
|
||||
log.Warnf("failed to setup SSH client config: %v", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -162,13 +162,8 @@ func getWindowsSSHPaths() (configDir, knownHostsDir string) {
|
||||
return configDir, knownHostsDir
|
||||
}
|
||||
|
||||
// SetupSSHClientConfig creates SSH client configuration for NetBird domains
|
||||
func (m *Manager) SetupSSHClientConfig(domains []string) error {
|
||||
return m.SetupSSHClientConfigWithPeers(domains, nil)
|
||||
}
|
||||
|
||||
// SetupSSHClientConfigWithPeers creates SSH client configuration for peer hostnames
|
||||
func (m *Manager) SetupSSHClientConfigWithPeers(domains []string, peerKeys []PeerHostKey) error {
|
||||
// SetupSSHClientConfig creates SSH client configuration for NetBird peers
|
||||
func (m *Manager) SetupSSHClientConfig(peerKeys []PeerHostKey) error {
|
||||
if !shouldGenerateSSHConfig(len(peerKeys)) {
|
||||
m.logSkipReason(len(peerKeys))
|
||||
return nil
|
||||
@@ -176,7 +171,7 @@ func (m *Manager) SetupSSHClientConfigWithPeers(domains []string, peerKeys []Pee
|
||||
|
||||
knownHostsPath := m.getKnownHostsPath()
|
||||
sshConfig := m.buildSSHConfig(peerKeys, knownHostsPath)
|
||||
return m.writeSSHConfig(sshConfig, domains)
|
||||
return m.writeSSHConfig(sshConfig)
|
||||
}
|
||||
|
||||
func (m *Manager) logSkipReason(peerCount int) {
|
||||
@@ -255,17 +250,17 @@ func (m *Manager) buildHostKeyConfig(knownHostsPath string) string {
|
||||
fmt.Sprintf(" UserKnownHostsFile %s\n", knownHostsPath)
|
||||
}
|
||||
|
||||
func (m *Manager) writeSSHConfig(sshConfig string, domains []string) error {
|
||||
func (m *Manager) writeSSHConfig(sshConfig string) error {
|
||||
sshConfigPath := filepath.Join(m.sshConfigDir, m.sshConfigFile)
|
||||
|
||||
if err := os.MkdirAll(m.sshConfigDir, 0755); err != nil {
|
||||
log.Warnf("Failed to create SSH config directory %s: %v", m.sshConfigDir, err)
|
||||
return m.setupUserConfig(sshConfig, domains)
|
||||
return m.setupUserConfig(sshConfig)
|
||||
}
|
||||
|
||||
if err := writeFileWithTimeout(sshConfigPath, []byte(sshConfig), 0644); err != nil {
|
||||
log.Warnf("Failed to write SSH config file %s: %v", sshConfigPath, err)
|
||||
return m.setupUserConfig(sshConfig, domains)
|
||||
return m.setupUserConfig(sshConfig)
|
||||
}
|
||||
|
||||
log.Infof("Created NetBird SSH client config: %s", sshConfigPath)
|
||||
@@ -273,7 +268,7 @@ func (m *Manager) writeSSHConfig(sshConfig string, domains []string) error {
|
||||
}
|
||||
|
||||
// setupUserConfig creates SSH config in user's directory as fallback
|
||||
func (m *Manager) setupUserConfig(sshConfig string, domains []string) error {
|
||||
func (m *Manager) setupUserConfig(sshConfig string) error {
|
||||
homeDir, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return fmt.Errorf("get user home directory: %w", err)
|
||||
|
||||
@@ -100,9 +100,8 @@ func TestManager_SetupSSHClientConfig(t *testing.T) {
|
||||
userKnownHosts: "known_hosts_netbird",
|
||||
}
|
||||
|
||||
// Test SSH config generation
|
||||
domains := []string{"example.nb.internal", "test.nb.internal"}
|
||||
err = manager.SetupSSHClientConfig(domains)
|
||||
// Test SSH config generation with empty peer keys
|
||||
err = manager.SetupSSHClientConfig(nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Read generated config
|
||||
@@ -275,7 +274,7 @@ func TestManager_PeerLimit(t *testing.T) {
|
||||
}
|
||||
|
||||
// Test that SSH config generation is skipped when too many peers
|
||||
err = manager.SetupSSHClientConfigWithPeers([]string{"nb.internal"}, peerKeys)
|
||||
err = manager.SetupSSHClientConfig(peerKeys)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Config should not be created due to peer limit
|
||||
@@ -328,7 +327,7 @@ func TestManager_ForcedSSHConfig(t *testing.T) {
|
||||
}
|
||||
|
||||
// Test that SSH config generation is forced despite many peers
|
||||
err = manager.SetupSSHClientConfigWithPeers([]string{"nb.internal"}, peerKeys)
|
||||
err = manager.SetupSSHClientConfig(peerKeys)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Config should be created despite peer limit due to force flag
|
||||
|
||||
@@ -14,7 +14,6 @@ import (
|
||||
"golang.zx2c4.com/wireguard/tun/netstack"
|
||||
|
||||
"github.com/netbirdio/netbird/client/iface/wgaddr"
|
||||
sshconfig "github.com/netbirdio/netbird/client/ssh/config"
|
||||
)
|
||||
|
||||
// DefaultSSHPort is the default SSH port of the NetBird's embedded SSH server
|
||||
@@ -255,26 +254,6 @@ func (s *Server) SetSocketFilter(ifIdx int) {
|
||||
s.ifIdx = ifIdx
|
||||
}
|
||||
|
||||
// SetupSSHClientConfig configures SSH client settings
|
||||
func (s *Server) SetupSSHClientConfig() error {
|
||||
return s.SetupSSHClientConfigWithPeers(nil)
|
||||
}
|
||||
|
||||
// SetupSSHClientConfigWithPeers configures SSH client settings for peer hostnames
|
||||
func (s *Server) SetupSSHClientConfigWithPeers(peerKeys []sshconfig.PeerHostKey) error {
|
||||
configMgr := sshconfig.NewManager()
|
||||
if err := configMgr.SetupSSHClientConfigWithPeers(nil, peerKeys); err != nil {
|
||||
return fmt.Errorf("setup SSH client config: %w", err)
|
||||
}
|
||||
|
||||
peerCount := len(peerKeys)
|
||||
if peerCount > 0 {
|
||||
log.Debugf("SSH client config setup completed for %d peer hostnames", peerCount)
|
||||
} else {
|
||||
log.Debugf("SSH client config setup completed with no peers")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) publicKeyHandler(ctx ssh.Context, key ssh.PublicKey) bool {
|
||||
s.mu.RLock()
|
||||
|
||||
Reference in New Issue
Block a user