Skip to content

Commit

Permalink
fix tests and alias
Browse files Browse the repository at this point in the history
Signed-off-by: Kristoffer Dalby <[email protected]>
  • Loading branch information
kradalby committed Jul 12, 2024
1 parent 71691ee commit 4bd34d9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
4 changes: 3 additions & 1 deletion hscontrol/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
26 changes: 24 additions & 2 deletions hscontrol/types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,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)
Expand Down Expand Up @@ -262,6 +260,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") != "") &&
Expand Down Expand Up @@ -795,3 +800,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)
}
}

0 comments on commit 4bd34d9

Please sign in to comment.