Use configuration input struct (#645)

As we will be passing more flags to configure
 local agents, we need a more flexible type
This commit is contained in:
Maycon Santos 2023-01-08 12:57:28 +01:00 committed by GitHub
parent 27f4993ce3
commit ca62f6787a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 91 additions and 41 deletions

View File

@ -38,7 +38,12 @@ var loginCmd = &cobra.Command{
return err return err
} }
config, err := internal.GetConfig(managementURL, adminURL, configPath, preSharedKey) config, err := internal.GetConfig(internal.ConfigInput{
ManagementURL: managementURL,
AdminURL: adminURL,
ConfigPath: configPath,
PreSharedKey: &preSharedKey,
})
if err != nil { if err != nil {
return fmt.Errorf("get config file: %v", err) return fmt.Errorf("get config file: %v", err)
} }

View File

@ -56,7 +56,9 @@ var sshCmd = &cobra.Command{
ctx := internal.CtxInitState(cmd.Context()) ctx := internal.CtxInitState(cmd.Context())
config, err := internal.ReadConfig("", "", configPath, nil) config, err := internal.ReadConfig(internal.ConfigInput{
ConfigPath: configPath,
})
if err != nil { if err != nil {
return err return err
} }

View File

@ -35,7 +35,12 @@ var upCmd = &cobra.Command{
return err return err
} }
config, err := internal.GetConfig(managementURL, adminURL, configPath, preSharedKey) config, err := internal.GetConfig(internal.ConfigInput{
ManagementURL: managementURL,
AdminURL: adminURL,
ConfigPath: configPath,
PreSharedKey: &preSharedKey,
})
if err != nil { if err != nil {
return fmt.Errorf("get config file: %v", err) return fmt.Errorf("get config file: %v", err)
} }

View File

@ -30,6 +30,14 @@ func init() {
managementURLDefault = managementURL managementURLDefault = managementURL
} }
// ConfigInput carries configuration changes to the client
type ConfigInput struct {
ManagementURL string
AdminURL string
ConfigPath string
PreSharedKey *string
}
// Config Configuration type // Config Configuration type
type Config struct { type Config struct {
// Wireguard private key of local peer // Wireguard private key of local peer
@ -63,7 +71,7 @@ type Config struct {
} }
// createNewConfig creates a new config generating a new Wireguard key and saving to file // createNewConfig creates a new config generating a new Wireguard key and saving to file
func createNewConfig(managementURL, adminURL, configPath, preSharedKey string) (*Config, error) { func createNewConfig(input ConfigInput) (*Config, error) {
wgKey := generateKey() wgKey := generateKey()
pem, err := ssh.GeneratePrivateKey(ssh.ED25519) pem, err := ssh.GeneratePrivateKey(ssh.ED25519)
if err != nil { if err != nil {
@ -77,8 +85,8 @@ func createNewConfig(managementURL, adminURL, configPath, preSharedKey string) (
IFaceBlackList: []string{}, IFaceBlackList: []string{},
DisableIPv6Discovery: false, DisableIPv6Discovery: false,
} }
if managementURL != "" { if input.ManagementURL != "" {
URL, err := ParseURL("Management URL", managementURL) URL, err := ParseURL("Management URL", input.ManagementURL)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -87,22 +95,22 @@ func createNewConfig(managementURL, adminURL, configPath, preSharedKey string) (
config.ManagementURL = managementURLDefault config.ManagementURL = managementURLDefault
} }
if preSharedKey != "" { if input.PreSharedKey != nil {
config.PreSharedKey = preSharedKey config.PreSharedKey = *input.PreSharedKey
} }
if adminURL != "" { if input.AdminURL != "" {
newURL, err := ParseURL("Admin Panel URL", adminURL) newURL, err := ParseURL("Admin Panel URL", input.AdminURL)
if err != nil { if err != nil {
return nil, err return nil, err
} }
config.AdminURL = newURL config.AdminURL = newURL
} }
config.IFaceBlackList = []string{iface.WgInterfaceDefault, "wt", "utun", "tun0", "zt", "ZeroTier", "utun", "wg", "ts", config.IFaceBlackList = []string{iface.WgInterfaceDefault, "wt", "utun", "tun0", "zt", "ZeroTier", "wg", "ts",
"Tailscale", "tailscale", "docker", "veth", "br-"} "Tailscale", "tailscale", "docker", "veth", "br-"}
err = util.WriteJson(configPath, config) err = util.WriteJson(input.ConfigPath, config)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -128,22 +136,22 @@ func ParseURL(serviceName, managementURL string) (*url.URL, error) {
} }
// ReadConfig reads existing config. In case provided managementURL is not empty overrides the read property // ReadConfig reads existing config. In case provided managementURL is not empty overrides the read property
func ReadConfig(managementURL, adminURL, configPath string, preSharedKey *string) (*Config, error) { func ReadConfig(input ConfigInput) (*Config, error) {
config := &Config{} config := &Config{}
if _, err := os.Stat(configPath); os.IsNotExist(err) { if _, err := os.Stat(input.ConfigPath); os.IsNotExist(err) {
return nil, status.Errorf(codes.NotFound, "config file doesn't exist") return nil, status.Errorf(codes.NotFound, "config file doesn't exist")
} }
if _, err := util.ReadJson(configPath, config); err != nil { if _, err := util.ReadJson(input.ConfigPath, config); err != nil {
return nil, err return nil, err
} }
refresh := false refresh := false
if managementURL != "" && config.ManagementURL.String() != managementURL { if input.ManagementURL != "" && config.ManagementURL.String() != input.ManagementURL {
log.Infof("new Management URL provided, updated to %s (old value %s)", log.Infof("new Management URL provided, updated to %s (old value %s)",
managementURL, config.ManagementURL) input.ManagementURL, config.ManagementURL)
newURL, err := ParseURL("Management URL", managementURL) newURL, err := ParseURL("Management URL", input.ManagementURL)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -151,10 +159,10 @@ func ReadConfig(managementURL, adminURL, configPath string, preSharedKey *string
refresh = true refresh = true
} }
if adminURL != "" && (config.AdminURL == nil || config.AdminURL.String() != adminURL) { if input.AdminURL != "" && (config.AdminURL == nil || config.AdminURL.String() != input.AdminURL) {
log.Infof("new Admin Panel URL provided, updated to %s (old value %s)", log.Infof("new Admin Panel URL provided, updated to %s (old value %s)",
adminURL, config.AdminURL) input.AdminURL, config.AdminURL)
newURL, err := ParseURL("Admin Panel URL", adminURL) newURL, err := ParseURL("Admin Panel URL", input.AdminURL)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -162,10 +170,10 @@ func ReadConfig(managementURL, adminURL, configPath string, preSharedKey *string
refresh = true refresh = true
} }
if preSharedKey != nil && config.PreSharedKey != *preSharedKey { if input.PreSharedKey != nil && config.PreSharedKey != *input.PreSharedKey {
log.Infof("new pre-shared key provided, updated to %s (old value %s)", log.Infof("new pre-shared key provided, updated to %s (old value %s)",
*preSharedKey, config.PreSharedKey) *input.PreSharedKey, config.PreSharedKey)
config.PreSharedKey = *preSharedKey config.PreSharedKey = *input.PreSharedKey
refresh = true refresh = true
} }
if config.SSHKey == "" { if config.SSHKey == "" {
@ -184,7 +192,7 @@ func ReadConfig(managementURL, adminURL, configPath string, preSharedKey *string
if refresh { if refresh {
// since we have new management URL, we need to update config file // since we have new management URL, we need to update config file
if err := util.WriteJson(configPath, config); err != nil { if err := util.WriteJson(input.ConfigPath, config); err != nil {
return nil, err return nil, err
} }
} }
@ -193,17 +201,16 @@ func ReadConfig(managementURL, adminURL, configPath string, preSharedKey *string
} }
// GetConfig reads existing config or generates a new one // GetConfig reads existing config or generates a new one
func GetConfig(managementURL, adminURL, configPath, preSharedKey string) (*Config, error) { func GetConfig(input ConfigInput) (*Config, error) {
if _, err := os.Stat(configPath); os.IsNotExist(err) { if _, err := os.Stat(input.ConfigPath); os.IsNotExist(err) {
log.Infof("generating new config %s", configPath) log.Infof("generating new config %s", input.ConfigPath)
return createNewConfig(managementURL, adminURL, configPath, preSharedKey) return createNewConfig(input)
} else { } else {
// don't overwrite pre-shared key if we receive asterisks from UI // don't overwrite pre-shared key if we receive asterisks from UI
pk := &preSharedKey if *input.PreSharedKey == "**********" {
if preSharedKey == "**********" { input.PreSharedKey = nil
pk = nil
} }
return ReadConfig(managementURL, adminURL, configPath, pk) return ReadConfig(input)
} }
} }

View File

@ -20,7 +20,12 @@ func TestGetConfig(t *testing.T) {
preSharedKey := "preSharedKey" preSharedKey := "preSharedKey"
// case 1: new config has to be generated // case 1: new config has to be generated
config, err := GetConfig(managementURL, adminURL, path, preSharedKey) config, err := GetConfig(ConfigInput{
ManagementURL: managementURL,
AdminURL: adminURL,
ConfigPath: path,
PreSharedKey: &preSharedKey,
})
if err != nil { if err != nil {
return return
} }
@ -33,7 +38,12 @@ func TestGetConfig(t *testing.T) {
} }
// case 2: existing config -> fetch it // case 2: existing config -> fetch it
config, err = GetConfig(managementURL, adminURL, path, preSharedKey) config, err = GetConfig(ConfigInput{
ManagementURL: managementURL,
AdminURL: adminURL,
ConfigPath: path,
PreSharedKey: &preSharedKey,
})
if err != nil { if err != nil {
return return
} }
@ -43,7 +53,12 @@ func TestGetConfig(t *testing.T) {
// case 3: existing config, but new managementURL has been provided -> update config // case 3: existing config, but new managementURL has been provided -> update config
newManagementURL := "https://test.newManagement.url:33071" newManagementURL := "https://test.newManagement.url:33071"
config, err = GetConfig(newManagementURL, adminURL, path, preSharedKey) config, err = GetConfig(ConfigInput{
ManagementURL: newManagementURL,
AdminURL: adminURL,
ConfigPath: path,
PreSharedKey: &preSharedKey,
})
if err != nil { if err != nil {
return return
} }

View File

@ -306,7 +306,10 @@ func UpdateOldManagementPort(ctx context.Context, config *Config, configPath str
} }
// everything is alright => update the config // everything is alright => update the config
newConfig, err := ReadConfig(newURL.String(), "", configPath, nil) newConfig, err := ReadConfig(ConfigInput{
ManagementURL: newURL.String(),
ConfigPath: configPath,
})
if err != nil { if err != nil {
log.Infof("couldn't switch to the new Management %s", newURL.String()) log.Infof("couldn't switch to the new Management %s", newURL.String())
return config, fmt.Errorf("failed updating config file: %v", err) return config, fmt.Errorf("failed updating config file: %v", err)

View File

@ -78,9 +78,17 @@ func (s *Server) Start() error {
// if configuration exists, we just start connections. if is new config we skip and set status NeedsLogin // if configuration exists, we just start connections. if is new config we skip and set status NeedsLogin
// on failure we return error to retry // on failure we return error to retry
config, err := internal.ReadConfig(s.managementURL, s.adminURL, s.configPath, nil) config, err := internal.ReadConfig(internal.ConfigInput{
ManagementURL: s.managementURL,
AdminURL: s.adminURL,
ConfigPath: s.configPath,
})
if errorStatus, ok := gstatus.FromError(err); ok && errorStatus.Code() == codes.NotFound { if errorStatus, ok := gstatus.FromError(err); ok && errorStatus.Code() == codes.NotFound {
config, err = internal.GetConfig(s.managementURL, s.adminURL, s.configPath, "") config, err = internal.GetConfig(internal.ConfigInput{
ManagementURL: s.managementURL,
AdminURL: s.adminURL,
ConfigPath: s.configPath,
})
if err != nil { if err != nil {
log.Warnf("unable to create configuration file: %v", err) log.Warnf("unable to create configuration file: %v", err)
return err return err
@ -165,7 +173,12 @@ func (s *Server) Login(callerCtx context.Context, msg *proto.LoginRequest) (*pro
} }
s.mutex.Unlock() s.mutex.Unlock()
config, err := internal.GetConfig(managementURL, adminURL, s.configPath, msg.PreSharedKey) config, err := internal.GetConfig(internal.ConfigInput{
ManagementURL: managementURL,
AdminURL: adminURL,
ConfigPath: s.configPath,
PreSharedKey: &msg.PreSharedKey,
})
if err != nil { if err != nil {
return nil, err return nil, err
} }