2021-07-17 14:38:59 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2022-10-16 13:33:46 +02:00
|
|
|
"context"
|
2021-09-25 19:22:49 +02:00
|
|
|
"crypto/tls"
|
2022-08-23 15:46:12 +02:00
|
|
|
"encoding/json"
|
2022-05-22 18:53:47 +02:00
|
|
|
"errors"
|
2021-07-17 14:38:59 +02:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
2022-05-13 14:11:21 +02:00
|
|
|
"io"
|
2022-05-22 18:53:47 +02:00
|
|
|
"io/fs"
|
2022-05-13 14:11:21 +02:00
|
|
|
"net"
|
2022-07-29 20:37:09 +02:00
|
|
|
"net/http"
|
2024-01-31 16:02:24 +01:00
|
|
|
"net/netip"
|
2022-08-23 15:46:12 +02:00
|
|
|
"net/url"
|
2022-05-13 14:11:21 +02:00
|
|
|
"os"
|
|
|
|
"path"
|
2024-02-08 14:40:40 +01:00
|
|
|
"slices"
|
2022-07-29 20:37:09 +02:00
|
|
|
"strings"
|
2022-05-13 14:11:21 +02:00
|
|
|
"time"
|
|
|
|
|
2023-02-03 21:47:20 +01:00
|
|
|
"github.com/google/uuid"
|
2024-07-03 11:33:02 +02:00
|
|
|
grpcMiddleware "github.com/grpc-ecosystem/go-grpc-middleware/v2"
|
2023-02-03 21:47:20 +01:00
|
|
|
"github.com/miekg/dns"
|
2023-08-30 11:42:03 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/spf13/cobra"
|
2023-03-30 10:54:09 +02:00
|
|
|
"golang.org/x/crypto/acme/autocert"
|
|
|
|
"golang.org/x/net/http2"
|
|
|
|
"golang.org/x/net/http2/h2c"
|
2023-08-30 11:42:03 +02:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/credentials"
|
|
|
|
"google.golang.org/grpc/keepalive"
|
2023-03-30 10:54:09 +02:00
|
|
|
|
2024-01-31 16:02:24 +01:00
|
|
|
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/realip"
|
2024-03-27 18:48:48 +01:00
|
|
|
|
2024-02-20 09:59:56 +01:00
|
|
|
"github.com/netbirdio/management-integrations/integrations"
|
|
|
|
|
2023-08-30 11:42:03 +02:00
|
|
|
"github.com/netbirdio/netbird/encryption"
|
2024-07-03 11:33:02 +02:00
|
|
|
"github.com/netbirdio/netbird/formatter"
|
2023-08-30 11:42:03 +02:00
|
|
|
mgmtProto "github.com/netbirdio/netbird/management/proto"
|
|
|
|
"github.com/netbirdio/netbird/management/server"
|
2024-07-03 11:33:02 +02:00
|
|
|
nbContext "github.com/netbirdio/netbird/management/server/context"
|
2024-02-20 09:59:56 +01:00
|
|
|
"github.com/netbirdio/netbird/management/server/geolocation"
|
2023-02-03 21:47:20 +01:00
|
|
|
httpapi "github.com/netbirdio/netbird/management/server/http"
|
2023-08-30 11:42:03 +02:00
|
|
|
"github.com/netbirdio/netbird/management/server/idp"
|
2023-03-30 10:54:09 +02:00
|
|
|
"github.com/netbirdio/netbird/management/server/jwtclaims"
|
2023-02-03 21:47:20 +01:00
|
|
|
"github.com/netbirdio/netbird/management/server/metrics"
|
|
|
|
"github.com/netbirdio/netbird/management/server/telemetry"
|
2022-03-26 12:08:54 +01:00
|
|
|
"github.com/netbirdio/netbird/util"
|
2024-02-29 16:05:47 +01:00
|
|
|
"github.com/netbirdio/netbird/version"
|
2021-07-17 14:38:59 +02:00
|
|
|
)
|
|
|
|
|
2022-07-29 20:37:09 +02:00
|
|
|
// ManagementLegacyPort is the port that was used before by the Management gRPC server.
|
|
|
|
// It is used for backward compatibility now.
|
|
|
|
const ManagementLegacyPort = 33073
|
|
|
|
|
2021-07-17 14:38:59 +02:00
|
|
|
var (
|
2022-10-19 17:43:28 +02:00
|
|
|
mgmtPort int
|
2022-10-21 16:24:13 +02:00
|
|
|
mgmtMetricsPort int
|
2022-10-19 17:43:28 +02:00
|
|
|
mgmtLetsencryptDomain string
|
|
|
|
mgmtSingleAccModeDomain string
|
|
|
|
certFile string
|
|
|
|
certKey string
|
|
|
|
config *server.Config
|
2021-07-17 14:51:16 +02:00
|
|
|
|
|
|
|
kaep = keepalive.EnforcementPolicy{
|
2021-07-22 10:28:00 +02:00
|
|
|
MinTime: 15 * time.Second,
|
2021-07-17 14:51:16 +02:00
|
|
|
PermitWithoutStream: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
kasp = keepalive.ServerParameters{
|
|
|
|
MaxConnectionIdle: 15 * time.Second,
|
|
|
|
MaxConnectionAgeGrace: 5 * time.Second,
|
|
|
|
Time: 5 * time.Second,
|
|
|
|
Timeout: 2 * time.Second,
|
|
|
|
}
|
2021-07-17 14:38:59 +02:00
|
|
|
|
|
|
|
mgmtCmd = &cobra.Command{
|
|
|
|
Use: "management",
|
2022-07-29 20:37:09 +02:00
|
|
|
Short: "start NetBird Management Server",
|
|
|
|
PreRunE: func(cmd *cobra.Command, args []string) error {
|
2023-08-30 11:42:03 +02:00
|
|
|
flag.Parse()
|
2024-07-03 11:33:02 +02:00
|
|
|
|
|
|
|
//nolint
|
|
|
|
ctx := context.WithValue(cmd.Context(), formatter.ExecutionContextKey, formatter.SystemSource)
|
|
|
|
|
2023-08-30 11:42:03 +02:00
|
|
|
err := util.InitLog(logLevel, logFile)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed initializing log %v", err)
|
|
|
|
}
|
|
|
|
|
2022-07-29 20:37:09 +02:00
|
|
|
// detect whether user specified a port
|
|
|
|
userPort := cmd.Flag("port").Changed
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
config, err = loadMgmtConfig(ctx, mgmtConfig)
|
2021-09-07 09:53:18 +02:00
|
|
|
if err != nil {
|
2022-07-29 20:37:09 +02:00
|
|
|
return fmt.Errorf("failed reading provided config file: %s: %v", mgmtConfig, err)
|
2021-09-07 09:53:18 +02:00
|
|
|
}
|
2023-12-12 14:56:27 +01:00
|
|
|
|
|
|
|
if cmd.Flag(idpSignKeyRefreshEnabledFlagName).Changed {
|
|
|
|
config.HttpConfig.IdpSignKeyRefreshEnabled = idpSignKeyRefreshEnabled
|
|
|
|
}
|
2021-07-17 14:38:59 +02:00
|
|
|
|
2022-07-29 20:37:09 +02:00
|
|
|
tlsEnabled := false
|
|
|
|
if mgmtLetsencryptDomain != "" || (config.HttpConfig.CertFile != "" && config.HttpConfig.CertKey != "") {
|
|
|
|
tlsEnabled = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if !userPort {
|
|
|
|
// different defaults for port when tls enabled/disabled
|
|
|
|
if tlsEnabled {
|
|
|
|
mgmtPort = 443
|
|
|
|
} else {
|
|
|
|
mgmtPort = 80
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-07 15:38:21 +01:00
|
|
|
_, valid := dns.IsDomainName(dnsDomain)
|
|
|
|
if !valid || len(dnsDomain) > 192 {
|
2023-11-01 17:11:16 +01:00
|
|
|
return fmt.Errorf("failed parsing the provided dns-domain. Valid status: %t, Length: %d", valid, len(dnsDomain))
|
2022-11-07 15:38:21 +01:00
|
|
|
}
|
|
|
|
|
2022-07-29 20:37:09 +02:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2024-09-09 18:27:42 +02:00
|
|
|
flag.Parse()
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
ctx, cancel := context.WithCancel(cmd.Context())
|
|
|
|
defer cancel()
|
|
|
|
//nolint
|
|
|
|
ctx = context.WithValue(ctx, formatter.ExecutionContextKey, formatter.SystemSource)
|
|
|
|
|
2023-08-30 11:42:03 +02:00
|
|
|
err := handleRebrand(cmd)
|
2021-07-30 17:46:38 +02:00
|
|
|
if err != nil {
|
2022-07-29 20:37:09 +02:00
|
|
|
return fmt.Errorf("failed to migrate files %v", err)
|
2021-07-30 17:46:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = os.Stat(config.Datadir); os.IsNotExist(err) {
|
2024-01-29 14:21:45 +01:00
|
|
|
err = os.MkdirAll(config.Datadir, 0755)
|
2021-07-17 17:26:51 +02:00
|
|
|
if err != nil {
|
2022-07-29 20:37:09 +02:00
|
|
|
return fmt.Errorf("failed creating datadir: %s: %v", config.Datadir, err)
|
2021-07-17 17:26:51 +02:00
|
|
|
}
|
2021-07-17 14:38:59 +02:00
|
|
|
}
|
2022-10-22 13:29:39 +02:00
|
|
|
appMetrics, err := telemetry.NewDefaultAppMetrics(cmd.Context())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-07-03 11:33:02 +02:00
|
|
|
err = appMetrics.Expose(ctx, mgmtMetricsPort, "/metrics")
|
2022-10-22 13:29:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-07-03 11:33:02 +02:00
|
|
|
store, err := server.NewStore(ctx, config.StoreConfig.Engine, config.Datadir, appMetrics)
|
2023-05-19 11:42:25 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed creating Store: %s: %v", config.Datadir, err)
|
|
|
|
}
|
2023-11-16 18:21:52 +01:00
|
|
|
peersUpdateManager := server.NewPeersUpdateManager(appMetrics)
|
2022-10-22 13:29:39 +02:00
|
|
|
|
2022-02-17 19:18:46 +01:00
|
|
|
var idpManager idp.Manager
|
|
|
|
if config.IdpManagerConfig != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
idpManager, err = idp.NewManager(ctx, *config.IdpManagerConfig, appMetrics)
|
2022-02-17 19:18:46 +01:00
|
|
|
if err != nil {
|
2022-07-29 20:37:09 +02:00
|
|
|
return fmt.Errorf("failed retrieving a new idp manager with err: %v", err)
|
2022-02-17 19:18:46 +01:00
|
|
|
}
|
2022-01-24 11:21:30 +01:00
|
|
|
}
|
2022-02-17 19:18:46 +01:00
|
|
|
|
2022-10-19 17:43:28 +02:00
|
|
|
if disableSingleAccMode {
|
|
|
|
mgmtSingleAccModeDomain = ""
|
|
|
|
}
|
2024-07-03 11:33:02 +02:00
|
|
|
eventStore, key, err := integrations.InitEventStore(ctx, config.Datadir, config.DataStoreEncryptionKey)
|
2023-01-02 15:11:32 +01:00
|
|
|
if err != nil {
|
2023-09-19 18:08:40 +02:00
|
|
|
return fmt.Errorf("failed to initialize database: %s", err)
|
2023-01-02 15:11:32 +01:00
|
|
|
}
|
2023-09-19 18:08:40 +02:00
|
|
|
|
2023-09-27 14:48:40 +02:00
|
|
|
if config.DataStoreEncryptionKey != key {
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Infof("update config with activity store key")
|
2023-09-19 18:08:40 +02:00
|
|
|
config.DataStoreEncryptionKey = key
|
2024-07-03 11:33:02 +02:00
|
|
|
err := updateMgmtConfig(ctx, mgmtConfig, config)
|
2023-09-19 18:08:40 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to write out store encryption key: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-09 18:27:42 +02:00
|
|
|
geo, err := geolocation.NewGeolocation(ctx, config.Datadir, !disableGeoliteUpdate)
|
2024-02-20 09:59:56 +01:00
|
|
|
if err != nil {
|
2024-09-09 18:27:42 +02:00
|
|
|
log.WithContext(ctx).Warnf("could not initialize geolocation service. proceeding without geolocation support: %v", err)
|
2024-02-20 09:59:56 +01:00
|
|
|
} else {
|
2024-09-09 18:27:42 +02:00
|
|
|
log.WithContext(ctx).Infof("geolocation service has been initialized from %s", config.Datadir)
|
2024-02-20 09:59:56 +01:00
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
integratedPeerValidator, err := integrations.NewIntegratedValidator(ctx, eventStore)
|
2024-03-27 18:48:48 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to initialize integrated peer validator: %v", err)
|
|
|
|
}
|
2024-07-03 11:33:02 +02:00
|
|
|
accountManager, err := server.BuildManager(ctx, store, peersUpdateManager, idpManager, mgmtSingleAccModeDomain,
|
2024-08-07 10:52:31 +02:00
|
|
|
dnsDomain, eventStore, geo, userDeleteFromIDPEnabled, integratedPeerValidator, appMetrics)
|
2022-05-21 15:21:39 +02:00
|
|
|
if err != nil {
|
2022-07-29 20:37:09 +02:00
|
|
|
return fmt.Errorf("failed to build default manager: %v", err)
|
2022-05-21 15:21:39 +02:00
|
|
|
}
|
2021-08-12 12:49:10 +02:00
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
secretsManager := server.NewTimeBasedAuthSecretsManager(peersUpdateManager, config.TURNConfig, config.Relay)
|
2021-07-17 14:51:16 +02:00
|
|
|
|
2024-02-08 14:40:40 +01:00
|
|
|
trustedPeers := config.ReverseProxy.TrustedPeers
|
|
|
|
defaultTrustedPeers := []netip.Prefix{netip.MustParsePrefix("0.0.0.0/0"), netip.MustParsePrefix("::/0")}
|
|
|
|
if len(trustedPeers) == 0 || slices.Equal[[]netip.Prefix](trustedPeers, defaultTrustedPeers) {
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Warn("TrustedPeers are configured to default value '0.0.0.0/0', '::/0'. This allows connection IP spoofing.")
|
2024-02-08 14:40:40 +01:00
|
|
|
trustedPeers = defaultTrustedPeers
|
|
|
|
}
|
|
|
|
trustedHTTPProxies := config.ReverseProxy.TrustedHTTPProxies
|
|
|
|
trustedProxiesCount := config.ReverseProxy.TrustedHTTPProxiesCount
|
|
|
|
if len(trustedHTTPProxies) > 0 && trustedProxiesCount > 0 {
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Warn("TrustedHTTPProxies and TrustedHTTPProxiesCount both are configured. " +
|
2024-02-08 14:40:40 +01:00
|
|
|
"This is not recommended way to extract X-Forwarded-For. Consider using one of these options.")
|
|
|
|
}
|
2024-02-20 09:59:56 +01:00
|
|
|
realipOpts := []realip.Option{
|
|
|
|
realip.WithTrustedPeers(trustedPeers),
|
|
|
|
realip.WithTrustedProxies(trustedHTTPProxies),
|
|
|
|
realip.WithTrustedProxiesCount(trustedProxiesCount),
|
|
|
|
realip.WithHeaders([]string{realip.XForwardedFor, realip.XRealIp}),
|
2024-01-31 16:02:24 +01:00
|
|
|
}
|
|
|
|
gRPCOpts := []grpc.ServerOption{
|
|
|
|
grpc.KeepaliveEnforcementPolicy(kaep),
|
|
|
|
grpc.KeepaliveParams(kasp),
|
2024-07-03 11:33:02 +02:00
|
|
|
grpc.ChainUnaryInterceptor(realip.UnaryServerInterceptorOpts(realipOpts...), unaryInterceptor),
|
|
|
|
grpc.ChainStreamInterceptor(realip.StreamServerInterceptorOpts(realipOpts...), streamInterceptor),
|
2024-01-31 16:02:24 +01:00
|
|
|
}
|
2024-02-08 14:40:40 +01:00
|
|
|
|
2022-07-29 20:37:09 +02:00
|
|
|
var certManager *autocert.Manager
|
|
|
|
var tlsConfig *tls.Config
|
|
|
|
tlsEnabled := false
|
2021-08-07 13:35:52 +02:00
|
|
|
if config.HttpConfig.LetsEncryptDomain != "" {
|
2022-07-29 20:37:09 +02:00
|
|
|
certManager, err = encryption.CreateCertManager(config.Datadir, config.HttpConfig.LetsEncryptDomain)
|
2022-07-25 19:55:38 +02:00
|
|
|
if err != nil {
|
2022-07-29 20:37:09 +02:00
|
|
|
return fmt.Errorf("failed creating LetsEncrypt cert manager: %v", err)
|
2022-07-25 19:55:38 +02:00
|
|
|
}
|
2021-08-07 12:26:07 +02:00
|
|
|
transportCredentials := credentials.NewTLS(certManager.TLSConfig())
|
2022-07-29 20:37:09 +02:00
|
|
|
gRPCOpts = append(gRPCOpts, grpc.Creds(transportCredentials))
|
|
|
|
tlsEnabled = true
|
2021-09-25 19:22:49 +02:00
|
|
|
} else if config.HttpConfig.CertFile != "" && config.HttpConfig.CertKey != "" {
|
2022-07-29 20:37:09 +02:00
|
|
|
tlsConfig, err = loadTLSConfig(config.HttpConfig.CertFile, config.HttpConfig.CertKey)
|
2021-09-25 19:22:49 +02:00
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Errorf("cannot load TLS credentials: %v", err)
|
2022-07-29 20:37:09 +02:00
|
|
|
return err
|
2021-09-25 19:22:49 +02:00
|
|
|
}
|
|
|
|
transportCredentials := credentials.NewTLS(tlsConfig)
|
2022-07-29 20:37:09 +02:00
|
|
|
gRPCOpts = append(gRPCOpts, grpc.Creds(transportCredentials))
|
|
|
|
tlsEnabled = true
|
2021-07-17 14:38:59 +02:00
|
|
|
}
|
2022-10-21 16:24:13 +02:00
|
|
|
|
2023-03-30 10:54:09 +02:00
|
|
|
jwtValidator, err := jwtclaims.NewJWTValidator(
|
2024-07-03 11:33:02 +02:00
|
|
|
ctx,
|
2023-03-30 10:54:09 +02:00
|
|
|
config.HttpConfig.AuthIssuer,
|
2023-04-04 16:40:56 +02:00
|
|
|
config.GetAuthAudiences(),
|
2023-03-30 10:54:09 +02:00
|
|
|
config.HttpConfig.AuthKeysLocation,
|
2023-04-15 02:44:42 +02:00
|
|
|
config.HttpConfig.IdpSignKeyRefreshEnabled,
|
2023-03-30 10:54:09 +02:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed creating JWT validator: %v", err)
|
|
|
|
}
|
|
|
|
|
2023-02-03 21:47:20 +01:00
|
|
|
httpAPIAuthCfg := httpapi.AuthCfg{
|
|
|
|
Issuer: config.HttpConfig.AuthIssuer,
|
|
|
|
Audience: config.HttpConfig.AuthAudience,
|
|
|
|
UserIDClaim: config.HttpConfig.AuthUserIDClaim,
|
|
|
|
KeysLocation: config.HttpConfig.AuthKeysLocation,
|
|
|
|
}
|
2024-02-22 12:27:08 +01:00
|
|
|
|
2024-04-15 12:08:38 +02:00
|
|
|
httpAPIHandler, err := httpapi.APIHandler(ctx, accountManager, geo, *jwtValidator, appMetrics, httpAPIAuthCfg, integratedPeerValidator)
|
2021-07-17 14:38:59 +02:00
|
|
|
if err != nil {
|
2022-07-29 20:37:09 +02:00
|
|
|
return fmt.Errorf("failed creating HTTP API handler: %v", err)
|
2021-07-17 14:38:59 +02:00
|
|
|
}
|
2021-07-17 14:51:16 +02:00
|
|
|
|
2023-09-04 11:37:39 +02:00
|
|
|
ephemeralManager := server.NewEphemeralManager(store, accountManager)
|
2024-07-03 11:33:02 +02:00
|
|
|
ephemeralManager.LoadInitialPeers(ctx)
|
2023-09-04 11:37:39 +02:00
|
|
|
|
2022-07-29 20:37:09 +02:00
|
|
|
gRPCAPIHandler := grpc.NewServer(gRPCOpts...)
|
2024-09-08 12:06:14 +02:00
|
|
|
srv, err := server.NewServer(ctx, config, accountManager, peersUpdateManager, secretsManager, appMetrics, ephemeralManager)
|
2021-07-17 14:51:16 +02:00
|
|
|
if err != nil {
|
2022-07-29 20:37:09 +02:00
|
|
|
return fmt.Errorf("failed creating gRPC API handler: %v", err)
|
2021-07-17 14:51:16 +02:00
|
|
|
}
|
2022-07-29 20:37:09 +02:00
|
|
|
mgmtProto.RegisterManagementServiceServer(gRPCAPIHandler, srv)
|
2021-07-17 14:51:16 +02:00
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
installationID, err := getInstallationID(ctx, store)
|
2022-10-16 13:33:46 +02:00
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Errorf("cannot load TLS credentials: %v", err)
|
2022-10-16 13:33:46 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !disableMetrics {
|
2023-07-22 19:30:59 +02:00
|
|
|
idpManager := "disabled"
|
|
|
|
if config.IdpManagerConfig != nil && config.IdpManagerConfig.ManagerType != "" {
|
|
|
|
idpManager = config.IdpManagerConfig.ManagerType
|
|
|
|
}
|
|
|
|
metricsWorker := metrics.NewWorker(ctx, installationID, store, peersUpdateManager, idpManager)
|
2024-07-03 11:33:02 +02:00
|
|
|
go metricsWorker.Run(ctx)
|
2022-10-16 13:33:46 +02:00
|
|
|
}
|
|
|
|
|
2022-07-29 20:37:09 +02:00
|
|
|
var compatListener net.Listener
|
|
|
|
if mgmtPort != ManagementLegacyPort {
|
|
|
|
// The Management gRPC server was running on port 33073 previously. Old agents that are already connected to it
|
|
|
|
// are using port 33073. For compatibility purposes we keep running a 2nd gRPC server on port 33073.
|
2024-07-03 11:33:02 +02:00
|
|
|
compatListener, err = serveGRPC(ctx, gRPCAPIHandler, ManagementLegacyPort)
|
2022-07-29 20:37:09 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-07-17 14:38:59 +02:00
|
|
|
}
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Infof("running gRPC backward compatibility server: %s", compatListener.Addr().String())
|
2022-07-29 20:37:09 +02:00
|
|
|
}
|
2021-07-17 14:38:59 +02:00
|
|
|
|
2022-07-29 20:37:09 +02:00
|
|
|
rootHandler := handlerFunc(gRPCAPIHandler, httpAPIHandler)
|
|
|
|
var listener net.Listener
|
|
|
|
if certManager != nil {
|
|
|
|
// a call to certManager.Listener() always creates a new listener so we do it once
|
|
|
|
cml := certManager.Listener()
|
|
|
|
if mgmtPort == 443 {
|
|
|
|
// CertManager, HTTP and gRPC API all on the same port
|
|
|
|
rootHandler = certManager.HTTPHandler(rootHandler)
|
|
|
|
listener = cml
|
|
|
|
} else {
|
|
|
|
listener, err = tls.Listen("tcp", fmt.Sprintf(":%d", mgmtPort), certManager.TLSConfig())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed creating TLS listener on port %d: %v", mgmtPort, err)
|
|
|
|
}
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Infof("running HTTP server (LetsEncrypt challenge handler): %s", cml.Addr().String())
|
|
|
|
serveHTTP(ctx, cml, certManager.HTTPHandler(nil))
|
2022-07-29 20:37:09 +02:00
|
|
|
}
|
|
|
|
} else if tlsConfig != nil {
|
|
|
|
listener, err = tls.Listen("tcp", fmt.Sprintf(":%d", mgmtPort), tlsConfig)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed creating TLS listener on port %d: %v", mgmtPort, err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
listener, err = net.Listen("tcp", fmt.Sprintf(":%d", mgmtPort))
|
2021-08-07 12:26:07 +02:00
|
|
|
if err != nil {
|
2022-07-29 20:37:09 +02:00
|
|
|
return fmt.Errorf("failed creating TCP listener on port %d: %v", mgmtPort, err)
|
2021-08-07 12:26:07 +02:00
|
|
|
}
|
2022-07-29 20:37:09 +02:00
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Infof("management server version %s", version.NetbirdVersion())
|
|
|
|
log.WithContext(ctx).Infof("running HTTP server and gRPC server on the same port: %s", listener.Addr().String())
|
|
|
|
serveGRPCWithHTTP(ctx, listener, rootHandler, tlsEnabled)
|
2021-08-07 12:26:07 +02:00
|
|
|
|
2021-07-17 14:38:59 +02:00
|
|
|
SetupCloseHandler()
|
2022-07-29 20:37:09 +02:00
|
|
|
|
2021-07-17 14:38:59 +02:00
|
|
|
<-stopCh
|
2024-07-03 11:33:02 +02:00
|
|
|
integratedPeerValidator.Stop(ctx)
|
2024-02-20 09:59:56 +01:00
|
|
|
if geo != nil {
|
|
|
|
_ = geo.Stop()
|
|
|
|
}
|
2023-09-04 11:37:39 +02:00
|
|
|
ephemeralManager.Stop()
|
2022-10-22 11:50:21 +02:00
|
|
|
_ = appMetrics.Close()
|
2022-07-29 20:37:09 +02:00
|
|
|
_ = listener.Close()
|
|
|
|
if certManager != nil {
|
|
|
|
_ = certManager.Listener().Close()
|
2021-08-07 12:26:07 +02:00
|
|
|
}
|
2022-07-29 20:37:09 +02:00
|
|
|
gRPCAPIHandler.Stop()
|
2024-07-03 11:33:02 +02:00
|
|
|
_ = store.Close(ctx)
|
|
|
|
_ = eventStore.Close(ctx)
|
|
|
|
log.WithContext(ctx).Infof("stopped Management Service")
|
2021-08-07 12:26:07 +02:00
|
|
|
|
2022-07-29 20:37:09 +02:00
|
|
|
return nil
|
2021-07-17 14:38:59 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
func unaryInterceptor(
|
|
|
|
ctx context.Context,
|
|
|
|
req interface{},
|
|
|
|
info *grpc.UnaryServerInfo,
|
|
|
|
handler grpc.UnaryHandler,
|
|
|
|
) (interface{}, error) {
|
|
|
|
reqID := uuid.New().String()
|
|
|
|
//nolint
|
|
|
|
ctx = context.WithValue(ctx, formatter.ExecutionContextKey, formatter.GRPCSource)
|
|
|
|
//nolint
|
|
|
|
ctx = context.WithValue(ctx, nbContext.RequestIDKey, reqID)
|
|
|
|
return handler(ctx, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
func streamInterceptor(
|
|
|
|
srv interface{},
|
|
|
|
ss grpc.ServerStream,
|
|
|
|
info *grpc.StreamServerInfo,
|
|
|
|
handler grpc.StreamHandler,
|
|
|
|
) error {
|
|
|
|
reqID := uuid.New().String()
|
|
|
|
wrapped := grpcMiddleware.WrapServerStream(ss)
|
|
|
|
//nolint
|
|
|
|
ctx := context.WithValue(ss.Context(), formatter.ExecutionContextKey, formatter.GRPCSource)
|
|
|
|
//nolint
|
|
|
|
wrapped.WrappedContext = context.WithValue(ctx, nbContext.RequestIDKey, reqID)
|
|
|
|
return handler(srv, wrapped)
|
|
|
|
}
|
|
|
|
|
|
|
|
func notifyStop(ctx context.Context, msg string) {
|
2022-07-29 20:37:09 +02:00
|
|
|
select {
|
|
|
|
case stopCh <- 1:
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Error(msg)
|
2022-07-29 20:37:09 +02:00
|
|
|
default:
|
|
|
|
// stop has been already called, nothing to report
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
func getInstallationID(ctx context.Context, store server.Store) (string, error) {
|
2022-10-16 13:33:46 +02:00
|
|
|
installationID := store.GetInstallationID()
|
|
|
|
if installationID != "" {
|
|
|
|
return installationID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
installationID = strings.ToUpper(uuid.New().String())
|
2024-07-03 11:33:02 +02:00
|
|
|
err := store.SaveInstallationID(ctx, installationID)
|
2022-10-16 13:33:46 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return installationID, nil
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
func serveGRPC(ctx context.Context, grpcServer *grpc.Server, port int) (net.Listener, error) {
|
2022-07-29 20:37:09 +02:00
|
|
|
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
err := grpcServer.Serve(listener)
|
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
notifyStop(ctx, fmt.Sprintf("failed running gRPC server on port %d: %v", port, err))
|
2022-07-29 20:37:09 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
return listener, nil
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
func serveHTTP(ctx context.Context, httpListener net.Listener, handler http.Handler) {
|
2022-07-29 20:37:09 +02:00
|
|
|
go func() {
|
|
|
|
err := http.Serve(httpListener, handler)
|
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
notifyStop(ctx, fmt.Sprintf("failed running HTTP server: %v", err))
|
2022-07-29 20:37:09 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
func serveGRPCWithHTTP(ctx context.Context, listener net.Listener, handler http.Handler, tlsEnabled bool) {
|
2022-07-29 20:37:09 +02:00
|
|
|
go func() {
|
|
|
|
var err error
|
|
|
|
if tlsEnabled {
|
|
|
|
err = http.Serve(listener, handler)
|
|
|
|
} else {
|
|
|
|
// the following magic is needed to support HTTP2 without TLS
|
|
|
|
// and still share a single port between gRPC and HTTP APIs
|
|
|
|
h1s := &http.Server{
|
|
|
|
Handler: h2c.NewHandler(handler, &http2.Server{}),
|
|
|
|
}
|
|
|
|
err = h1s.Serve(listener)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
select {
|
|
|
|
case stopCh <- 1:
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Errorf("failed to serve HTTP and gRPC server: %v", err)
|
2022-07-29 20:37:09 +02:00
|
|
|
default:
|
|
|
|
// stop has been already called, nothing to report
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func handlerFunc(gRPCHandler *grpc.Server, httpHandler http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
|
|
|
grpcHeader := strings.HasPrefix(request.Header.Get("Content-Type"), "application/grpc") ||
|
|
|
|
strings.HasPrefix(request.Header.Get("Content-Type"), "application/grpc+proto")
|
|
|
|
if request.ProtoMajor == 2 && grpcHeader {
|
|
|
|
gRPCHandler.ServeHTTP(writer, request)
|
|
|
|
} else {
|
|
|
|
httpHandler.ServeHTTP(writer, request)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
func loadMgmtConfig(ctx context.Context, mgmtConfigPath string) (*server.Config, error) {
|
2023-07-31 14:22:38 +02:00
|
|
|
loadedConfig := &server.Config{}
|
|
|
|
_, err := util.ReadJson(mgmtConfigPath, loadedConfig)
|
2021-07-30 17:46:38 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if mgmtLetsencryptDomain != "" {
|
2023-07-31 14:22:38 +02:00
|
|
|
loadedConfig.HttpConfig.LetsEncryptDomain = mgmtLetsencryptDomain
|
2021-07-30 17:46:38 +02:00
|
|
|
}
|
|
|
|
if mgmtDataDir != "" {
|
2023-07-31 14:22:38 +02:00
|
|
|
loadedConfig.Datadir = mgmtDataDir
|
2021-07-30 17:46:38 +02:00
|
|
|
}
|
|
|
|
|
2021-09-25 19:22:49 +02:00
|
|
|
if certKey != "" && certFile != "" {
|
2023-07-31 14:22:38 +02:00
|
|
|
loadedConfig.HttpConfig.CertFile = certFile
|
|
|
|
loadedConfig.HttpConfig.CertKey = certKey
|
2021-09-25 19:22:49 +02:00
|
|
|
}
|
|
|
|
|
2023-07-31 14:22:38 +02:00
|
|
|
oidcEndpoint := loadedConfig.HttpConfig.OIDCConfigEndpoint
|
2022-08-23 15:46:12 +02:00
|
|
|
if oidcEndpoint != "" {
|
|
|
|
// if OIDCConfigEndpoint is specified, we can load DeviceAuthEndpoint and TokenEndpoint automatically
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Infof("loading OIDC configuration from the provided IDP configuration endpoint %s", oidcEndpoint)
|
|
|
|
oidcConfig, err := fetchOIDCConfig(ctx, oidcEndpoint)
|
2022-08-23 15:46:12 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Infof("loaded OIDC configuration from the provided IDP configuration endpoint: %s", oidcEndpoint)
|
2022-08-23 15:46:12 +02:00
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Infof("overriding HttpConfig.AuthIssuer with a new value %s, previously configured value: %s",
|
2023-07-31 14:22:38 +02:00
|
|
|
oidcConfig.Issuer, loadedConfig.HttpConfig.AuthIssuer)
|
|
|
|
loadedConfig.HttpConfig.AuthIssuer = oidcConfig.Issuer
|
2022-08-23 15:46:12 +02:00
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Infof("overriding HttpConfig.AuthKeysLocation (JWT certs) with a new value %s, previously configured value: %s",
|
2023-07-31 14:22:38 +02:00
|
|
|
oidcConfig.JwksURI, loadedConfig.HttpConfig.AuthKeysLocation)
|
|
|
|
loadedConfig.HttpConfig.AuthKeysLocation = oidcConfig.JwksURI
|
2022-08-23 15:46:12 +02:00
|
|
|
|
2023-07-31 14:22:38 +02:00
|
|
|
if !(loadedConfig.DeviceAuthorizationFlow == nil || strings.ToLower(loadedConfig.DeviceAuthorizationFlow.Provider) == string(server.NONE)) {
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Infof("overriding DeviceAuthorizationFlow.TokenEndpoint with a new value: %s, previously configured value: %s",
|
2023-07-31 14:22:38 +02:00
|
|
|
oidcConfig.TokenEndpoint, loadedConfig.DeviceAuthorizationFlow.ProviderConfig.TokenEndpoint)
|
|
|
|
loadedConfig.DeviceAuthorizationFlow.ProviderConfig.TokenEndpoint = oidcConfig.TokenEndpoint
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Infof("overriding DeviceAuthorizationFlow.DeviceAuthEndpoint with a new value: %s, previously configured value: %s",
|
2023-07-31 14:22:38 +02:00
|
|
|
oidcConfig.DeviceAuthEndpoint, loadedConfig.DeviceAuthorizationFlow.ProviderConfig.DeviceAuthEndpoint)
|
|
|
|
loadedConfig.DeviceAuthorizationFlow.ProviderConfig.DeviceAuthEndpoint = oidcConfig.DeviceAuthEndpoint
|
2022-08-23 15:46:12 +02:00
|
|
|
|
|
|
|
u, err := url.Parse(oidcEndpoint)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Infof("overriding DeviceAuthorizationFlow.ProviderConfig.Domain with a new value: %s, previously configured value: %s",
|
2023-07-31 14:22:38 +02:00
|
|
|
u.Host, loadedConfig.DeviceAuthorizationFlow.ProviderConfig.Domain)
|
|
|
|
loadedConfig.DeviceAuthorizationFlow.ProviderConfig.Domain = u.Host
|
2023-04-05 17:46:34 +02:00
|
|
|
|
2023-07-31 14:22:38 +02:00
|
|
|
if loadedConfig.DeviceAuthorizationFlow.ProviderConfig.Scope == "" {
|
|
|
|
loadedConfig.DeviceAuthorizationFlow.ProviderConfig.Scope = server.DefaultDeviceAuthFlowScope
|
2023-04-05 17:46:34 +02:00
|
|
|
}
|
2022-08-23 15:46:12 +02:00
|
|
|
}
|
2023-07-27 11:31:07 +02:00
|
|
|
|
2023-07-31 14:22:38 +02:00
|
|
|
if loadedConfig.PKCEAuthorizationFlow != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Infof("overriding PKCEAuthorizationFlow.TokenEndpoint with a new value: %s, previously configured value: %s",
|
2023-07-31 14:22:38 +02:00
|
|
|
oidcConfig.TokenEndpoint, loadedConfig.PKCEAuthorizationFlow.ProviderConfig.TokenEndpoint)
|
|
|
|
loadedConfig.PKCEAuthorizationFlow.ProviderConfig.TokenEndpoint = oidcConfig.TokenEndpoint
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Infof("overriding PKCEAuthorizationFlow.AuthorizationEndpoint with a new value: %s, previously configured value: %s",
|
2023-07-31 14:22:38 +02:00
|
|
|
oidcConfig.AuthorizationEndpoint, loadedConfig.PKCEAuthorizationFlow.ProviderConfig.AuthorizationEndpoint)
|
|
|
|
loadedConfig.PKCEAuthorizationFlow.ProviderConfig.AuthorizationEndpoint = oidcConfig.AuthorizationEndpoint
|
2023-07-27 11:31:07 +02:00
|
|
|
}
|
2022-08-23 15:46:12 +02:00
|
|
|
}
|
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
if loadedConfig.Relay != nil {
|
|
|
|
log.Infof("Relay addresses: %v", loadedConfig.Relay.Addresses)
|
|
|
|
}
|
|
|
|
|
2023-07-31 14:22:38 +02:00
|
|
|
return loadedConfig, err
|
2021-07-30 17:46:38 +02:00
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
func updateMgmtConfig(ctx context.Context, path string, config *server.Config) error {
|
|
|
|
return util.DirectWriteJson(ctx, path, config)
|
2023-09-19 18:08:40 +02:00
|
|
|
}
|
|
|
|
|
2022-08-23 15:46:12 +02:00
|
|
|
// OIDCConfigResponse used for parsing OIDC config response
|
|
|
|
type OIDCConfigResponse struct {
|
2023-07-27 11:31:07 +02:00
|
|
|
Issuer string `json:"issuer"`
|
|
|
|
TokenEndpoint string `json:"token_endpoint"`
|
|
|
|
DeviceAuthEndpoint string `json:"device_authorization_endpoint"`
|
|
|
|
JwksURI string `json:"jwks_uri"`
|
|
|
|
AuthorizationEndpoint string `json:"authorization_endpoint"`
|
2022-08-23 15:46:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// fetchOIDCConfig fetches OIDC configuration from the IDP
|
2024-07-03 11:33:02 +02:00
|
|
|
func fetchOIDCConfig(ctx context.Context, oidcEndpoint string) (OIDCConfigResponse, error) {
|
2022-08-23 15:46:12 +02:00
|
|
|
res, err := http.Get(oidcEndpoint)
|
|
|
|
if err != nil {
|
2023-05-11 15:14:00 +02:00
|
|
|
return OIDCConfigResponse{}, fmt.Errorf("failed fetching OIDC configuration from endpoint %s %v", oidcEndpoint, err)
|
2022-08-23 15:46:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
err := res.Body.Close()
|
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Debugf("failed closing response body %v", err)
|
2022-08-23 15:46:12 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
body, err := io.ReadAll(res.Body)
|
|
|
|
if err != nil {
|
|
|
|
return OIDCConfigResponse{}, fmt.Errorf("failed reading OIDC configuration response body: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if res.StatusCode != 200 {
|
|
|
|
return OIDCConfigResponse{}, fmt.Errorf("OIDC configuration request returned status %d with response: %s",
|
|
|
|
res.StatusCode, string(body))
|
|
|
|
}
|
|
|
|
|
|
|
|
config := OIDCConfigResponse{}
|
|
|
|
err = json.Unmarshal(body, &config)
|
|
|
|
if err != nil {
|
|
|
|
return OIDCConfigResponse{}, fmt.Errorf("failed unmarshaling OIDC configuration response: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
2021-09-25 19:22:49 +02:00
|
|
|
func loadTLSConfig(certFile string, certKey string) (*tls.Config, error) {
|
|
|
|
// Load server's certificate and private key
|
|
|
|
serverCert, err := tls.LoadX509KeyPair(certFile, certKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-10-22 11:50:21 +02:00
|
|
|
// NewDefaultAppMetrics the credentials and return it
|
2021-09-25 19:22:49 +02:00
|
|
|
config := &tls.Config{
|
|
|
|
Certificates: []tls.Certificate{serverCert},
|
|
|
|
ClientAuth: tls.NoClientCert,
|
2022-08-15 19:36:00 +02:00
|
|
|
NextProtos: []string{
|
|
|
|
"h2", "http/1.1", // enable HTTP/2
|
|
|
|
},
|
2021-09-25 19:22:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
2022-05-26 12:55:39 +02:00
|
|
|
func handleRebrand(cmd *cobra.Command) error {
|
|
|
|
var err error
|
|
|
|
if logFile == defaultLogFile {
|
|
|
|
if migrateToNetbird(oldDefaultLogFile, defaultLogFile) {
|
|
|
|
cmd.Printf("will copy Log dir %s and its content to %s\n", oldDefaultLogDir, defaultLogDir)
|
|
|
|
err = cpDir(oldDefaultLogDir, defaultLogDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if mgmtConfig == defaultMgmtConfig {
|
|
|
|
if migrateToNetbird(oldDefaultMgmtConfig, defaultMgmtConfig) {
|
|
|
|
cmd.Printf("will copy Config dir %s and its content to %s\n", oldDefaultMgmtConfigDir, defaultMgmtConfigDir)
|
|
|
|
err = cpDir(oldDefaultMgmtConfigDir, defaultMgmtConfigDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if mgmtDataDir == defaultMgmtDataDir {
|
|
|
|
if migrateToNetbird(oldDefaultMgmtDataDir, defaultMgmtDataDir) {
|
|
|
|
cmd.Printf("will copy Config dir %s and its content to %s\n", oldDefaultMgmtDataDir, defaultMgmtDataDir)
|
|
|
|
err = cpDir(oldDefaultMgmtDataDir, defaultMgmtDataDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-13 14:11:21 +02:00
|
|
|
func cpFile(src, dst string) error {
|
|
|
|
var err error
|
|
|
|
var srcfd *os.File
|
|
|
|
var dstfd *os.File
|
|
|
|
var srcinfo os.FileInfo
|
|
|
|
|
|
|
|
if srcfd, err = os.Open(src); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer srcfd.Close()
|
|
|
|
|
|
|
|
if dstfd, err = os.Create(dst); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer dstfd.Close()
|
|
|
|
|
|
|
|
if _, err = io.Copy(dstfd, srcfd); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if srcinfo, err = os.Stat(src); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return os.Chmod(dst, srcinfo.Mode())
|
|
|
|
}
|
|
|
|
|
|
|
|
func copySymLink(source, dest string) error {
|
|
|
|
link, err := os.Readlink(source)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return os.Symlink(link, dest)
|
|
|
|
}
|
|
|
|
|
|
|
|
func cpDir(src string, dst string) error {
|
|
|
|
var err error
|
2022-08-18 18:22:15 +02:00
|
|
|
var fds []os.DirEntry
|
2022-05-13 14:11:21 +02:00
|
|
|
var srcinfo os.FileInfo
|
|
|
|
|
|
|
|
if srcinfo, err = os.Stat(src); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = os.MkdirAll(dst, srcinfo.Mode()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-18 18:22:15 +02:00
|
|
|
if fds, err = os.ReadDir(src); err != nil {
|
2022-05-13 14:11:21 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, fd := range fds {
|
|
|
|
srcfp := path.Join(src, fd.Name())
|
|
|
|
dstfp := path.Join(dst, fd.Name())
|
|
|
|
|
|
|
|
fileInfo, err := os.Stat(srcfp)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Couldn't get fileInfo; %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch fileInfo.Mode() & os.ModeType {
|
|
|
|
case os.ModeSymlink:
|
|
|
|
if err = copySymLink(srcfp, dstfp); err != nil {
|
|
|
|
log.Fatalf("Failed to copy from %s to %s; %v", srcfp, dstfp, err)
|
|
|
|
}
|
|
|
|
case os.ModeDir:
|
|
|
|
if err = cpDir(srcfp, dstfp); err != nil {
|
|
|
|
log.Fatalf("Failed to copy from %s to %s; %v", srcfp, dstfp, err)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
if err = cpFile(srcfp, dstfp); err != nil {
|
|
|
|
log.Fatalf("Failed to copy from %s to %s; %v", srcfp, dstfp, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func migrateToNetbird(oldPath, newPath string) bool {
|
2022-05-22 18:53:47 +02:00
|
|
|
_, errOld := os.Stat(oldPath)
|
|
|
|
_, errNew := os.Stat(newPath)
|
2022-05-13 14:11:21 +02:00
|
|
|
|
2022-05-22 18:53:47 +02:00
|
|
|
if errors.Is(errOld, fs.ErrNotExist) || errNew == nil {
|
2022-05-13 14:11:21 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|