From 633001c8ba9d7a59ad4f2fd1feeae0a94069f9a9 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Thu, 17 Jan 2019 01:34:32 +0530 Subject: [PATCH] Inherit certsDir from configDir if latter is set (#7098) This is to ensure backward compatibility for all existing deployments which use custom config dir to point to their certs directory. --- cmd/common-main.go | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/cmd/common-main.go b/cmd/common-main.go index a17c73616..ca4de0d39 100644 --- a/cmd/common-main.go +++ b/cmd/common-main.go @@ -78,18 +78,23 @@ func loadLoggers() { } -func newConfigDirFromCtx(ctx *cli.Context, option string, getDefaultDir func() string) *ConfigDir { +func newConfigDirFromCtx(ctx *cli.Context, option string, getDefaultDir func() string) (*ConfigDir, bool) { var dir string + var dirSet bool switch { case ctx.IsSet(option): dir = ctx.String(option) + dirSet = true case ctx.GlobalIsSet(option): dir = ctx.GlobalString(option) + dirSet = true // cli package does not expose parent's option option. Below code is workaround. if dir == "" || dir == getDefaultDir() { + dirSet = false // Unset to false since GlobalIsSet() true is a false positive. if ctx.Parent().GlobalIsSet(option) { dir = ctx.Parent().GlobalString(option) + dirSet = true } } default: @@ -111,7 +116,7 @@ func newConfigDirFromCtx(ctx *cli.Context, option string, getDefaultDir func() s logger.FatalIf(mkdirAllIgnorePerm(dirAbs), "Unable to create directory specified %s=%s", option, dir) - return &ConfigDir{path: dirAbs} + return &ConfigDir{path: dirAbs}, dirSet } func handleCommonCmdArgs(ctx *cli.Context) { @@ -142,8 +147,17 @@ func handleCommonCmdArgs(ctx *cli.Context) { } // Set all config, certs and CAs directories. - globalConfigDir = newConfigDirFromCtx(ctx, "config-dir", defaultConfigDir.Get) - globalCertsDir = newConfigDirFromCtx(ctx, "certs-dir", defaultCertsDir.Get) + var configSet, certsSet bool + globalConfigDir, configSet = newConfigDirFromCtx(ctx, "config-dir", defaultConfigDir.Get) + globalCertsDir, certsSet = newConfigDirFromCtx(ctx, "certs-dir", defaultCertsDir.Get) + + // Remove this code when we deprecate and remove config-dir. + // This code is to make sure we inherit from the config-dir + // option if certs-dir is not provided. + if !certsSet && configSet { + globalCertsDir = &ConfigDir{path: filepath.Join(globalConfigDir.Get(), certsDir)} + } + globalCertsCADir = &ConfigDir{path: filepath.Join(globalCertsDir.Get(), certsCADir)} logger.FatalIf(mkdirAllIgnorePerm(globalCertsCADir.Get()), "Unable to create certs CA directory at %s", globalCertsCADir.Get())