2021-08-15 16:56:26 +02:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
2022-05-12 11:17:24 +02:00
|
|
|
"context"
|
2021-08-18 13:35:42 +02:00
|
|
|
"fmt"
|
2022-05-12 11:17:24 +02:00
|
|
|
mgm "github.com/netbirdio/netbird/management/client"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2022-04-15 17:30:12 +02:00
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
|
2022-03-26 12:08:54 +01:00
|
|
|
"github.com/netbirdio/netbird/iface"
|
|
|
|
"github.com/netbirdio/netbird/util"
|
2021-08-15 16:56:26 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
|
|
|
)
|
|
|
|
|
2021-08-18 13:35:42 +02:00
|
|
|
var managementURLDefault *url.URL
|
|
|
|
|
|
|
|
func ManagementURLDefault() *url.URL {
|
|
|
|
return managementURLDefault
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2022-04-15 17:30:12 +02:00
|
|
|
managementURL, err := parseURL("Management URL", "https://api.wiretrustee.com:33073")
|
2021-08-18 13:35:42 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
managementURLDefault = managementURL
|
|
|
|
}
|
2021-08-15 16:56:26 +02:00
|
|
|
|
|
|
|
// Config Configuration type
|
|
|
|
type Config struct {
|
|
|
|
// Wireguard private key of local peer
|
|
|
|
PrivateKey string
|
2021-11-21 17:47:19 +01:00
|
|
|
PreSharedKey string
|
2021-08-18 13:35:42 +02:00
|
|
|
ManagementURL *url.URL
|
2022-04-15 17:30:12 +02:00
|
|
|
AdminURL *url.URL
|
2021-08-15 16:56:26 +02:00
|
|
|
WgIface string
|
|
|
|
IFaceBlackList []string
|
|
|
|
}
|
|
|
|
|
2022-04-15 17:30:12 +02:00
|
|
|
// createNewConfig creates a new config generating a new Wireguard key and saving to file
|
|
|
|
func createNewConfig(managementURL, adminURL, configPath, preSharedKey string) (*Config, error) {
|
2021-08-15 16:56:26 +02:00
|
|
|
wgKey := generateKey()
|
|
|
|
config := &Config{PrivateKey: wgKey, WgIface: iface.WgInterfaceDefault, IFaceBlackList: []string{}}
|
|
|
|
if managementURL != "" {
|
2022-04-15 17:30:12 +02:00
|
|
|
URL, err := parseURL("Management URL", managementURL)
|
2021-08-18 13:35:42 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
config.ManagementURL = URL
|
2021-08-15 16:56:26 +02:00
|
|
|
} else {
|
2021-08-18 13:35:42 +02:00
|
|
|
config.ManagementURL = managementURLDefault
|
2021-08-15 16:56:26 +02:00
|
|
|
}
|
|
|
|
|
2021-11-21 17:47:19 +01:00
|
|
|
if preSharedKey != "" {
|
|
|
|
config.PreSharedKey = preSharedKey
|
|
|
|
}
|
|
|
|
|
2021-08-18 13:35:42 +02:00
|
|
|
config.IFaceBlackList = []string{iface.WgInterfaceDefault, "tun0"}
|
|
|
|
|
2021-08-15 16:56:26 +02:00
|
|
|
err := util.WriteJson(configPath, config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
2022-04-15 17:30:12 +02:00
|
|
|
func parseURL(serviceName, managementURL string) (*url.URL, error) {
|
2021-08-18 13:35:42 +02:00
|
|
|
parsedMgmtURL, err := url.ParseRequestURI(managementURL)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed parsing management URL %s: [%s]", managementURL, err.Error())
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-04-15 17:30:12 +02:00
|
|
|
if parsedMgmtURL.Scheme != "https" && parsedMgmtURL.Scheme != "http" {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"invalid %s URL provided %s. Supported format [http|https]://[host]:[port]",
|
|
|
|
serviceName, managementURL)
|
2021-08-18 13:35:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return parsedMgmtURL, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadConfig reads existing config. In case provided managementURL is not empty overrides the read property
|
2022-04-15 17:30:12 +02:00
|
|
|
func ReadConfig(managementURL, adminURL, configPath string, preSharedKey *string) (*Config, error) {
|
2021-08-18 13:35:42 +02:00
|
|
|
config := &Config{}
|
2022-05-12 11:17:24 +02:00
|
|
|
if _, err := os.Stat(configPath); os.IsNotExist(err) {
|
|
|
|
return nil, status.Errorf(codes.NotFound, "config file doesn't exist")
|
|
|
|
}
|
|
|
|
|
2022-04-15 17:30:12 +02:00
|
|
|
if _, err := util.ReadJson(configPath, config); err != nil {
|
2021-08-18 13:35:42 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-04-15 17:30:12 +02:00
|
|
|
refresh := false
|
|
|
|
|
2022-03-13 15:17:18 +01:00
|
|
|
if managementURL != "" && config.ManagementURL.String() != managementURL {
|
2022-04-15 17:30:12 +02:00
|
|
|
log.Infof("new Management URL provided, updated to %s (old value %s)",
|
|
|
|
managementURL, config.ManagementURL)
|
|
|
|
newURL, err := parseURL("Management URL", managementURL)
|
2021-08-15 16:56:26 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-04-15 17:30:12 +02:00
|
|
|
config.ManagementURL = newURL
|
|
|
|
refresh = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if adminURL != "" && (config.AdminURL == nil || config.AdminURL.String() != adminURL) {
|
|
|
|
log.Infof("new Admin Panel URL provided, updated to %s (old value %s)",
|
|
|
|
adminURL, config.AdminURL)
|
|
|
|
newURL, err := parseURL("Admin Panel URL", adminURL)
|
2022-03-13 15:17:18 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-04-15 17:30:12 +02:00
|
|
|
config.AdminURL = newURL
|
|
|
|
refresh = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if preSharedKey != nil && config.PreSharedKey != *preSharedKey {
|
|
|
|
log.Infof("new pre-shared key provided, updated to %s (old value %s)",
|
|
|
|
*preSharedKey, config.PreSharedKey)
|
|
|
|
config.PreSharedKey = *preSharedKey
|
|
|
|
refresh = true
|
2021-08-15 16:56:26 +02:00
|
|
|
}
|
|
|
|
|
2022-04-15 17:30:12 +02:00
|
|
|
if refresh {
|
|
|
|
// since we have new management URL, we need to update config file
|
|
|
|
if err := util.WriteJson(configPath, config); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return config, nil
|
2021-08-18 13:35:42 +02:00
|
|
|
}
|
2021-08-15 16:56:26 +02:00
|
|
|
|
2021-08-18 13:35:42 +02:00
|
|
|
// GetConfig reads existing config or generates a new one
|
2022-04-15 17:30:12 +02:00
|
|
|
func GetConfig(managementURL, adminURL, configPath, preSharedKey string) (*Config, error) {
|
2021-08-18 13:35:42 +02:00
|
|
|
if _, err := os.Stat(configPath); os.IsNotExist(err) {
|
|
|
|
log.Infof("generating new config %s", configPath)
|
2022-04-15 17:30:12 +02:00
|
|
|
return createNewConfig(managementURL, adminURL, configPath, preSharedKey)
|
2021-08-18 13:35:42 +02:00
|
|
|
} else {
|
2022-04-15 17:30:12 +02:00
|
|
|
// don't overwrite pre-shared key if we receive asterisks from UI
|
|
|
|
pk := &preSharedKey
|
|
|
|
if preSharedKey == "**********" {
|
|
|
|
pk = nil
|
|
|
|
}
|
|
|
|
return ReadConfig(managementURL, adminURL, configPath, pk)
|
2021-08-18 13:35:42 +02:00
|
|
|
}
|
2021-08-15 16:56:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// generateKey generates a new Wireguard private key
|
|
|
|
func generateKey() string {
|
2022-01-16 17:10:36 +01:00
|
|
|
key, err := wgtypes.GeneratePrivateKey()
|
2021-08-15 16:56:26 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return key.String()
|
|
|
|
}
|
2022-05-12 11:17:24 +02:00
|
|
|
|
|
|
|
// DeviceAuthorizationFlow represents Device Authorization Flow information
|
|
|
|
type DeviceAuthorizationFlow struct {
|
|
|
|
Provider string
|
|
|
|
ProviderConfig ProviderConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProviderConfig has all attributes needed to initiate a device authorization flow
|
|
|
|
type ProviderConfig struct {
|
|
|
|
// ClientID An IDP application client id
|
|
|
|
ClientID string
|
|
|
|
// ClientSecret An IDP application client secret
|
|
|
|
ClientSecret string
|
|
|
|
// Domain An IDP API domain
|
|
|
|
Domain string
|
|
|
|
// Audience An Audience for to authorization validation
|
|
|
|
Audience string
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetDeviceAuthorizationFlowInfo(ctx context.Context, config *Config) (DeviceAuthorizationFlow, error) {
|
|
|
|
// validate our peer's Wireguard PRIVATE key
|
|
|
|
myPrivateKey, err := wgtypes.ParseKey(config.PrivateKey)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed parsing Wireguard key %s: [%s]", config.PrivateKey, err.Error())
|
|
|
|
return DeviceAuthorizationFlow{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var mgmTlsEnabled bool
|
|
|
|
if config.ManagementURL.Scheme == "https" {
|
|
|
|
mgmTlsEnabled = true
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugf("connecting to Management Service %s", config.ManagementURL.String())
|
|
|
|
mgmClient, err := mgm.NewClient(ctx, config.ManagementURL.Host, myPrivateKey, mgmTlsEnabled)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed connecting to Management Service %s %v", config.ManagementURL.String(), err)
|
|
|
|
return DeviceAuthorizationFlow{}, err
|
|
|
|
}
|
|
|
|
log.Debugf("connected to management Service %s", config.ManagementURL.String())
|
|
|
|
|
|
|
|
serverKey, err := mgmClient.GetServerPublicKey()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed while getting Management Service public key: %v", err)
|
|
|
|
return DeviceAuthorizationFlow{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
protoDeviceAuthorizationFlow, err := mgmClient.GetDeviceAuthorizationFlow(*serverKey)
|
|
|
|
if err != nil {
|
|
|
|
if s, ok := status.FromError(err); ok && s.Code() == codes.NotFound {
|
|
|
|
log.Warnf("server couldn't find device flow, contact admin: %v", err)
|
|
|
|
return DeviceAuthorizationFlow{}, err
|
|
|
|
} else {
|
|
|
|
log.Errorf("failed to retrieve device flow: %v", err)
|
|
|
|
return DeviceAuthorizationFlow{}, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = mgmClient.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed closing Management Service client: %v", err)
|
|
|
|
return DeviceAuthorizationFlow{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return DeviceAuthorizationFlow{
|
|
|
|
Provider: protoDeviceAuthorizationFlow.Provider.String(),
|
|
|
|
|
|
|
|
ProviderConfig: ProviderConfig{
|
|
|
|
Audience: protoDeviceAuthorizationFlow.ProviderConfig.Audience,
|
|
|
|
ClientID: protoDeviceAuthorizationFlow.ProviderConfig.ClientID,
|
|
|
|
ClientSecret: protoDeviceAuthorizationFlow.ProviderConfig.ClientSecret,
|
|
|
|
Domain: protoDeviceAuthorizationFlow.ProviderConfig.Domain,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|