Skip to content

Commit

Permalink
prune redundant flag 'create config controller --databaseFile'; norma…
Browse files Browse the repository at this point in the history
…lize ZITI_CTRL_DATABASE_FILE for 'create config controller' and 'create config environment'; test defaults
  • Loading branch information
qrkourier committed Mar 8, 2024
1 parent acb38db commit a1ea703
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 47 deletions.
1 change: 1 addition & 0 deletions ziti/cmd/create/create_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ func (data *ConfigTemplateValues) PopulateConfigValues() {
data.Controller.Ctrl.AltAdvertisedAddress = cmdHelper.GetCtrlEdgeAltAdvertisedAddress()
data.Controller.Ctrl.BindAddress = cmdHelper.GetCtrlBindAddress()
data.Controller.Ctrl.AdvertisedPort = cmdHelper.GetCtrlAdvertisedPort()
data.Controller.Database.DatabaseFile = cmdHelper.GetCtrlDatabaseFile()
// healthChecks:
data.Controller.HealthChecks.Interval = fabCtrl.DefaultHealthChecksBoltCheckInterval
data.Controller.HealthChecks.Timeout = fabCtrl.DefaultHealthChecksBoltCheckTimeout
Expand Down
19 changes: 0 additions & 19 deletions ziti/cmd/create/create_config_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (

const (
optionCtrlPort = "ctrlPort"
optionDatabaseFile = "databaseFile"
optionEdgeIdentityEnrollmentDuration = "identityEnrollmentDuration"
optionEdgeRouterEnrollmentDuration = "routerEnrollmentDuration"
)
Expand Down Expand Up @@ -121,10 +120,6 @@ func NewCmdCreateConfigController() *CreateControllerConfigCmd {
data.Controller.EdgeEnrollment.EdgeRouterDuration = controllerOptions.EdgeRouterEnrollmentDuration
}

if controllerOptions.DatabaseFile != "" && controllerOptions.DatabaseFile != constants.DefaultCtrlDatabaseFile {
data.Controller.Database.DatabaseFile = controllerOptions.DatabaseFile
}

// process identity information
SetControllerIdentity(&data.Controller)
SetEdgeConfig(&data.Controller)
Expand All @@ -151,7 +146,6 @@ func NewCmdCreateConfigController() *CreateControllerConfigCmd {

func (options *CreateConfigControllerOptions) addFlags(cmd *cobra.Command) {
cmd.Flags().StringVar(&options.CtrlPort, optionCtrlPort, constants.DefaultCtrlAdvertisedPort, "port used for the router to controller communication")
cmd.Flags().StringVar(&options.DatabaseFile, optionDatabaseFile, constants.DefaultCtrlDatabaseFile, "location of the database file")
cmd.Flags().DurationVar(&options.EdgeIdentityEnrollmentDuration, optionEdgeIdentityEnrollmentDuration, edge.DefaultEdgeEnrollmentDuration, "the edge identity enrollment duration, use 0h0m0s format")
cmd.Flags().DurationVar(&options.EdgeRouterEnrollmentDuration, optionEdgeRouterEnrollmentDuration, edge.DefaultEdgeEnrollmentDuration, "the edge router enrollment duration, use 0h0m0s format")
}
Expand Down Expand Up @@ -198,7 +192,6 @@ func SetControllerIdentity(data *ControllerTemplateValues) {
SetControllerIdentityServerCert(data)
SetControllerIdentityKey(data)
SetControllerIdentityCA(data)
setDatabaseFile(data)
}
func SetControllerIdentityCert(c *ControllerTemplateValues) {
val := os.Getenv(constants.PkiCtrlCertVarName)
Expand Down Expand Up @@ -229,18 +222,6 @@ func SetControllerIdentityCA(c *ControllerTemplateValues) {
c.Identity.Ca = helpers.NormalizePath(val)
}

func setDatabaseFile(c *ControllerTemplateValues) {
if c.Database.DatabaseFile != "" {
c.Database.DatabaseFile = helpers.NormalizePath(c.Database.DatabaseFile)
} else {
val := os.Getenv(constants.CtrlDatabaseFileVarName)
if val == "" {
val = constants.DefaultCtrlDatabaseFile // default
}
c.Database.DatabaseFile = helpers.NormalizePath(val)
}
}

func SetEdgeConfig(data *ControllerTemplateValues) {
SetEdgeSigningCert(data)
SetEdgeSigningKey(data)
Expand Down
13 changes: 13 additions & 0 deletions ziti/cmd/create/create_config_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package create
import (
"fmt"
cmdhelper "github.com/openziti/ziti/ziti/cmd/helpers"
"github.com/openziti/ziti/ziti/constants"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
Expand Down Expand Up @@ -276,6 +277,18 @@ func TestCtrlConfigDefaultsWhenUnset(t *testing.T) {
assert.Equal(t, expectedValue, ctrlConfig.Identity.Ca)
})

t.Run("TestDatabaseFileEnv", func(t *testing.T) {
expectedValue := constants.DefaultCtrlDatabaseFile

assert.Equal(t, expectedValue, data.Controller.Database.DatabaseFile)
})

t.Run("TestDatabaseFileConfig", func(t *testing.T) {
expectedValue := cmdhelper.GetZitiHome() + "/" + constants.DefaultCtrlDatabaseFile

assert.Equal(t, expectedValue, ctrlConfig.Db)
})

// ctrl:
t.Run("TestBindAddress", func(t *testing.T) {
expectedValue := testDefaultCtrlBindAddress
Expand Down
2 changes: 2 additions & 0 deletions ziti/cmd/create/create_config_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ func NewCmdCreateConfigEnvironment() *cobra.Command {
{constants.PkiCtrlServerCertVarName, constants.PkiCtrlServerCertVarDescription, data.Controller.Identity.ServerCert},
{constants.PkiCtrlKeyVarName, constants.PkiCtrlKeyVarDescription, data.Controller.Identity.Key},
{constants.PkiCtrlCAVarName, constants.PkiCtrlCAVarDescription, data.Controller.Identity.Ca},
{constants.CtrlDatabaseFileVarName, constants.CtrlDatabaseFileVarDescription, data.Controller.Database.DatabaseFile},
{constants.CtrlBindAddressVarName, constants.CtrlBindAddressVarDescription, data.Controller.Ctrl.BindAddress},
{constants.CtrlAdvertisedAddressVarName, constants.CtrlAdvertisedAddressVarDescription, data.Controller.Ctrl.AdvertisedAddress},
{constants.CtrlAdvertisedPortVarName, constants.CtrlAdvertisedPortVarDescription, data.Controller.Ctrl.AdvertisedPort},
Expand Down Expand Up @@ -177,6 +178,7 @@ func NewCmdCreateConfigEnvironment() *cobra.Command {
sb.WriteString(fmt.Sprintf("%-40s %-50s %s\n", constants.PkiCtrlServerCertVarName, constants.PkiCtrlServerCertVarDescription, data.Controller.Identity.ServerCert))
sb.WriteString(fmt.Sprintf("%-40s %-50s %s\n", constants.PkiCtrlKeyVarName, constants.PkiCtrlKeyVarDescription, data.Controller.Identity.Key))
sb.WriteString(fmt.Sprintf("%-40s %-50s %s\n", constants.PkiCtrlCAVarName, constants.PkiCtrlCAVarDescription, data.Controller.Identity.Ca))
sb.WriteString(fmt.Sprintf("%-40s %-50s %s\n", constants.CtrlDatabaseFileVarName, constants.CtrlDatabaseFileVarDescription, data.Controller.Database.DatabaseFile))
sb.WriteString(fmt.Sprintf("%-40s %-50s %s\n", constants.CtrlBindAddressVarName, constants.CtrlBindAddressVarDescription, data.Controller.Ctrl.BindAddress))
sb.WriteString(fmt.Sprintf("%-40s %-50s %s\n", constants.CtrlAdvertisedAddressVarName, constants.CtrlAdvertisedAddressVarDescription, data.Controller.Ctrl.AdvertisedAddress))
sb.WriteString(fmt.Sprintf("%-40s %-50s %s\n", constants.CtrlEdgeAltAdvertisedAddressVarName, constants.CtrlEdgeAltAdvertisedAddressVarDescription, data.Controller.Ctrl.AdvertisedAddress))
Expand Down
49 changes: 25 additions & 24 deletions ziti/cmd/create/create_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,43 +33,44 @@ const (

func getZitiEnvironmentVariables() []string {
return []string{
"ZITI_HOME",
"ZITI_ROUTER_NAME",
"ZITI_ROUTER_PORT",
"ZITI_PKI_CTRL_CERT",
"ZITI_PKI_CTRL_SERVER_CERT",
"ZITI_PKI_CTRL_KEY",
"ZITI_PKI_CTRL_CA",
"ZITI_CTRL_BIND_ADDRESS",
"ZITI_CTRL_ADVERTISED_ADDRESS",
"ZITI_CTRL_EDGE_ALT_ADVERTISED_ADDRESS",
"ZITI_CTRL_ADVERTISED_PORT",
"ZITI_PKI_SIGNER_CERT",
"ZITI_PKI_SIGNER_KEY",
"ZITI_CTRL_BIND_ADDRESS",
"ZITI_CTRL_DATABASE_FILE",
"ZITI_CTRL_EDGE_ADVERTISED_ADDRESS",
"ZITI_CTRL_EDGE_ADVERTISED_PORT",
"ZITI_CTRL_EDGE_ALT_ADVERTISED_ADDRESS",
"ZITI_CTRL_EDGE_BIND_ADDRESS",
"ZITI_EDGE_IDENTITY_ENROLLMENT_DURATION",
"ZITI_HOME",
"ZITI_PKI_ALT_SERVER_CERT",
"ZITI_PKI_ALT_SERVER_KEY",
"ZITI_PKI_CTRL_CA",
"ZITI_PKI_CTRL_CERT",
"ZITI_PKI_CTRL_KEY",
"ZITI_PKI_CTRL_SERVER_CERT",
"ZITI_PKI_EDGE_CA",
"ZITI_PKI_EDGE_CERT",
"ZITI_PKI_EDGE_KEY",
"ZITI_PKI_EDGE_SERVER_CERT",
"ZITI_PKI_EDGE_CERT",
"ZITI_ROUTER_IDENTITY_CERT",
"ZITI_ROUTER_IDENTITY_SERVER_CERT",
"ZITI_ROUTER_IDENTITY_KEY",
"ZITI_ROUTER_IDENTITY_CA",
"ZITI_ROUTER_IP_OVERRIDE",
"ZITI_EDGE_IDENTITY_ENROLLMENT_DURATION",
"ZITI_ROUTER_ENROLLMENT_DURATION",
"ZITI_PKI_SIGNER_CERT",
"ZITI_PKI_SIGNER_KEY",
"ZITI_ROUTER_ADVERTISED_ADDRESS",
"ZITI_ROUTER_LISTENER_BIND_PORT",
"ZITI_PKI_ALT_SERVER_CERT",
"ZITI_PKI_ALT_SERVER_KEY",
"ZITI_CTRL_EDGE_BIND_ADDRESS",
"ZITI_ROUTER_CSR_C",
"ZITI_ROUTER_CSR_ST",
"ZITI_ROUTER_CSR_L",
"ZITI_ROUTER_CSR_O",
"ZITI_ROUTER_CSR_OU",
"ZITI_ROUTER_CSR_SANS_DNS",
"ZITI_ROUTER_CSR_ST",
"ZITI_ROUTER_ENROLLMENT_DURATION",
"ZITI_ROUTER_IDENTITY_CA",
"ZITI_ROUTER_IDENTITY_CERT",
"ZITI_ROUTER_IDENTITY_KEY",
"ZITI_ROUTER_IDENTITY_SERVER_CERT",
"ZITI_ROUTER_IP_OVERRIDE",
"ZITI_ROUTER_LISTENER_BIND_PORT",
"ZITI_ROUTER_NAME",
"ZITI_ROUTER_PORT",
}
}

Expand Down
8 changes: 4 additions & 4 deletions ziti/cmd/helpers/env_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ func GetCtrlAdvertisedPort() string {
return getFromEnv(constants.CtrlAdvertisedPortVarName, defaultValue(constants.DefaultCtrlAdvertisedPort))
}

func GetCtrlDatabaseFile() string {
return NormalizePath(getFromEnv(constants.CtrlDatabaseFileVarName, defaultValue(constants.DefaultCtrlDatabaseFile)))
}

func GetCtrlEdgeBindAddress() string {
return getFromEnv(constants.CtrlEdgeBindAddressVarName, defaultValue(constants.DefaultCtrlEdgeBindAddress))
}
Expand All @@ -120,10 +124,6 @@ func GetCtrlEdgeAdvertisedPort() string {
return getFromEnv(constants.CtrlEdgeAdvertisedPortVarName, defaultValue(constants.DefaultCtrlEdgeAdvertisedPort))
}

func GetCtrlDatabaseFile() string {
return getFromEnv(constants.CtrlDatabaseFileVarName, defaultValue(constants.DefaultCtrlDatabaseFile))
}

func GetZitiEdgeRouterPort() string {
return getFromEnv(constants.ZitiEdgeRouterPortVarName, defaultValue(constants.DefaultZitiEdgeRouterPort))
}
Expand Down

0 comments on commit a1ea703

Please sign in to comment.