From 3cb05c5cdc9394e0a3791698796a4bb2b8fdf637 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Fri, 12 Jul 2024 11:02:02 +0200 Subject: [PATCH] fix tests and alias Signed-off-by: Kristoffer Dalby --- hscontrol/app.go | 4 +++- hscontrol/types/config.go | 26 ++++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/hscontrol/app.go b/hscontrol/app.go index a4995d09cf0..726b9d0bd2f 100644 --- a/hscontrol/app.go +++ b/hscontrol/app.go @@ -1021,8 +1021,10 @@ func (h *Headscale) loadACLPolicy() error { switch h.cfg.Policy.Mode { case types.PolicyModeFile: path := h.cfg.Policy.Path + + // It is fine to start headscale without a policy file. if len(path) == 0 { - return fmt.Errorf("policy path is empty") + return nil } absPath := util.AbsolutePathFromConfigPath(path) diff --git a/hscontrol/types/config.go b/hscontrol/types/config.go index 46758d91a9c..6eae9a32f89 100644 --- a/hscontrol/types/config.go +++ b/hscontrol/types/config.go @@ -207,8 +207,6 @@ func LoadConfig(path string, isFile bool) error { viper.AutomaticEnv() viper.SetDefault("policy.mode", "file") - // This maintains backward-compatibility with the old config file. - viper.RegisterAlias("acl_policy_path", "policy.path") viper.SetDefault("tls_letsencrypt_cache_dir", "/var/www/.cache") viper.SetDefault("tls_letsencrypt_challenge_type", HTTP01ChallengeType) @@ -267,6 +265,13 @@ func LoadConfig(path string, isFile bool) error { return fmt.Errorf("fatal error reading config file: %w", err) } + // Register aliases for backward compatibility + // Has to be called _after_ viper.ReadInConfig() + // https://github.com/spf13/viper/issues/560 + + // Alias the old ACL Policy path with the new configuration option. + registerAliasAndDeprecate("policy.path", "acl_policy_path") + // Collect any validation errors and return them all at once var errorText string if (viper.GetString("tls_letsencrypt_hostname") != "") && @@ -802,3 +807,20 @@ func GetHeadscaleConfig() (*Config, error) { func IsCLIConfigured() bool { return viper.GetString("cli.address") != "" && viper.GetString("cli.api_key") != "" } + +// registerAliasAndDeprecate will register an alias between the newKey and the oldKey, +// and log a deprecation warning if the oldKey is set. +func registerAliasAndDeprecate(newKey, oldKey string) { + // NOTE: RegisterAlias is called with NEW KEY -> OLD KEY + viper.RegisterAlias(newKey, oldKey) + if viper.IsSet(oldKey) { + log.Warn().Msgf("The %q configuration key is deprecated. Please use %q instead. %q will be removed in the future.", oldKey, newKey, oldKey) + } +} + +// deprecateAndFatal will log a fatal deprecation warning if the oldKey is set. +func deprecateAndFatal(newKey, oldKey string) { + if viper.IsSet(oldKey) { + log.Fatal().Msgf("The %q configuration key is deprecated. Please use %q instead. %q has been removed.", oldKey, newKey, oldKey) + } +}