Skip to content

Commit

Permalink
Fix viper concurrency issue
Browse files Browse the repository at this point in the history
  • Loading branch information
plorenz committed Feb 7, 2025
1 parent b5486cb commit 6d0737b
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 80 deletions.
11 changes: 0 additions & 11 deletions ziti/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,6 @@ func NewCmdRoot(in io.Reader, out, err io.Writer, cmd *cobra.Command) *cobra.Com
replacer := strings.NewReplacer("-", "_") // We use underscores in env var names, but use dashes in flag names
viper.SetEnvKeyReplacer(replacer)

// cmd.PersistentFlags().StringVar(&rootCommand.configFile, "config", "", "yaml config file (default is $HOME/.ziti.yaml)")
// viper.BindPFlag("config", cmd.PersistentFlags().Lookup("config"))
// viper.SetDefault("config", "$HOME/.ziti.yaml")

// cmd.PersistentFlags().StringVar(&rootCommand.RegistryPath, "state", "", "Location of state storage (ziti 'config' file). Overrides ZITI_STATE_STORE environment variable")
// viper.BindPFlag("ZITI_STATE_STORE", cmd.PersistentFlags().Lookup("state"))
// viper.BindEnv("ZITI_STATE_STORE")

// defaultClusterName := os.Getenv("ZITI_CLUSTER_NAME")
// cmd.PersistentFlags().StringVarP(&rootCommand.clusterName, "name", "", defaultClusterName, "Name of cluster. Overrides ZITI_CLUSTER_NAME environment variable")

p := common.NewOptionsProvider(out, err)

createCommands := create.NewCmdCreate(out, err)
Expand Down
32 changes: 32 additions & 0 deletions ziti/cmd/common/viper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
Copyright NetFoundry Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package common

import (
c "github.com/openziti/ziti/ziti/constants"
"github.com/spf13/viper"
"strings"
)

func NewViper() *viper.Viper {
result := viper.New()
result.SetEnvPrefix(c.ZITI) // All env vars we seek will be prefixed with "ZITI_"
result.AutomaticEnv()
replacer := strings.NewReplacer("-", "_") // We use underscores in env var names, but use dashes in flag names
result.SetEnvKeyReplacer(replacer)
return result
}
6 changes: 1 addition & 5 deletions ziti/cmd/helpers/env_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
package helpers

import (
"github.com/openziti/ziti/router/xgress_edge_tunnel"
edge "github.com/openziti/ziti/controller/config"
"github.com/openziti/ziti/router/xgress_edge_tunnel"
"github.com/openziti/ziti/ziti/constants"
"github.com/pkg/errors"
"os"
Expand Down Expand Up @@ -98,10 +98,6 @@ func GetCtrlAdvertisedAddress() string {
return getFromEnv(constants.CtrlAdvertisedAddressVarName, HostnameOrNetworkName)
}

func GetEdgeRouterIpOvderride() string {
return getFromEnv(constants.ZitiEdgeRouterIPOverrideVarName, defaultValue(""))
}

func GetCtrlAdvertisedPort() string {
return getFromEnv(constants.CtrlAdvertisedPortVarName, defaultValue(constants.DefaultCtrlAdvertisedPort))
}
Expand Down
3 changes: 2 additions & 1 deletion ziti/cmd/pki/pki.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ import (
"github.com/openziti/ziti/ziti/cmd/templates"
"github.com/openziti/ziti/ziti/pki/pki"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"io"
)

// PKIOptions contains the command line options
type PKIOptions struct {
common.CommonOptions

Flags PKIFlags
viper *viper.Viper
}

type PKIFlags struct {
Expand Down
78 changes: 29 additions & 49 deletions ziti/cmd/pki/pki_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,47 +23,30 @@ import (
"encoding/asn1"
"errors"
"fmt"
cmd2 "github.com/openziti/ziti/ziti/cmd/common"
cmdhelper "github.com/openziti/ziti/ziti/cmd/helpers"
"github.com/openziti/ziti/ziti/pki/pki"
"github.com/openziti/ziti/ziti/util"
"io"
"net"
"os"
"strings"
"sync"
"time"

"github.com/spf13/cobra"

"github.com/spf13/viper"
)

var viperLock sync.Mutex

// PKICreateOptions the options for the create spring command
type PKICreateOptions struct {
PKIOptions
}

// NewCmdPKICreate creates a command object for the "create" command
func NewCmdPKICreate(out io.Writer, errOut io.Writer) *cobra.Command {
options := &PKICreateOptions{
PKIOptions: PKIOptions{
CommonOptions: cmd2.CommonOptions{
Out: out,
Err: errOut,
},
},
}

cmd := &cobra.Command{
Use: "create",
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
cmdhelper.CheckErr(err)
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Help()
},
}

Expand All @@ -79,40 +62,37 @@ func NewCmdPKICreate(out io.Writer, errOut io.Writer) *cobra.Command {
}

func (options *PKICreateOptions) addPKICreateFlags(cmd *cobra.Command) {
viperLock.Lock()
defer viperLock.Unlock()

cmd.PersistentFlags().StringVarP(&options.Flags.PKIRoot, "pki-root", "", "", "Directory in which to store CA")
err := viper.BindPFlag("pki_root", cmd.PersistentFlags().Lookup("pki-root"))
err := options.viper.BindPFlag("pki_root", cmd.PersistentFlags().Lookup("pki-root"))
options.panicOnErr(err)

cmd.PersistentFlags().StringVarP(&options.Flags.PKIOrganization, "pki-organization", "", "NetFoundry", "Organization")
err = viper.BindPFlag("pki-organization", cmd.PersistentFlags().Lookup("pki-organization"))
err = options.viper.BindPFlag("pki-organization", cmd.PersistentFlags().Lookup("pki-organization"))
options.panicOnErr(err)

cmd.PersistentFlags().StringVarP(&options.Flags.PKIOrganizationalUnit, "pki-organizational-unit", "", "ADV-DEV", "Organization unit")
err = viper.BindPFlag("pki-organizational-unit", cmd.PersistentFlags().Lookup("pki-organizational-unit"))
err = options.viper.BindPFlag("pki-organizational-unit", cmd.PersistentFlags().Lookup("pki-organizational-unit"))
options.panicOnErr(err)

cmd.PersistentFlags().StringVarP(&options.Flags.PKICountry, "pki-country", "", "US", "Country")
err = viper.BindPFlag("pki-country", cmd.PersistentFlags().Lookup("pki-country"))
err = options.viper.BindPFlag("pki-country", cmd.PersistentFlags().Lookup("pki-country"))
options.panicOnErr(err)

cmd.PersistentFlags().StringVarP(&options.Flags.PKILocality, "pki-locality", "", "Charlotte", "Locality/Location")
err = viper.BindPFlag("pki-locality", cmd.PersistentFlags().Lookup("pki-locality"))
err = options.viper.BindPFlag("pki-locality", cmd.PersistentFlags().Lookup("pki-locality"))
options.panicOnErr(err)

// cmd.PersistentFlags().StringVarP(&options.Flags.PKILocality, "pki-location", "", "Charlotte", "Location/Locality")
// cmd.MarkFlagRequired("pki-location")
// viper.BindPFlag("pki-location", cmd.PersistentFlags().Lookup("pki-location"))
// options.viper.BindPFlag("pki-location", cmd.PersistentFlags().Lookup("pki-location"))

cmd.PersistentFlags().StringVarP(&options.Flags.PKIProvince, "pki-province", "", "NC", "Province/State")
err = viper.BindPFlag("pki-province", cmd.PersistentFlags().Lookup("pki-province"))
err = options.viper.BindPFlag("pki-province", cmd.PersistentFlags().Lookup("pki-province"))
options.panicOnErr(err)

// cmd.PersistentFlags().StringVarP(&options.Flags.PKIProvince, "pki-state", "", "NC", "State/Province")
// cmd.MarkFlagRequired("pki-state")
// viper.BindPFlag("pki-state", cmd.PersistentFlags().Lookup("pki-state"))
// options.viper.BindPFlag("pki-state", cmd.PersistentFlags().Lookup("pki-state"))
}

// Run implements this command
Expand All @@ -124,7 +104,7 @@ func (o *PKICreateOptions) Run() error {
func (o *PKICreateOptions) ObtainPKIRoot() (string, error) {
pkiRoot := o.Flags.PKIRoot
if pkiRoot == "" {
pkiRoot = viper.GetString("pki_root")
pkiRoot = o.viper.GetString("pki_root")
if pkiRoot == "" {
pkiRootDir, err := util.PKIRootDir()
if err != nil {
Expand All @@ -143,7 +123,7 @@ func (o *PKICreateOptions) ObtainPKIRoot() (string, error) {
func (o *PKICreateOptions) ObtainCAFile() (string, error) {
caFile := o.Flags.CAFile
if caFile == "" {
caFile = viper.GetString("ca-file")
caFile = o.viper.GetString("ca-file")
if caFile == "" {
var err error
caFile, err = util.PickValue("Required flag 'ca-file' not specified; Enter CA name now:", "ca", true)
Expand All @@ -159,7 +139,7 @@ func (o *PKICreateOptions) ObtainCAFile() (string, error) {
func (o *PKICreateOptions) ObtainIntermediateCAFile() (string, error) {
intermediateFile := o.Flags.IntermediateFile
if intermediateFile == "" {
intermediateFile = viper.GetString("intermediate-file")
intermediateFile = o.viper.GetString("intermediate-file")
if intermediateFile == "" {
var err error
intermediateFile, err = util.PickValue("Required flag 'intermediate-file' not specified; Enter Intermediate CA name now:", "intermediate", true)
Expand All @@ -173,7 +153,7 @@ func (o *PKICreateOptions) ObtainIntermediateCAFile() (string, error) {

// ObtainIntermediateCSRFile returns the value for intermediate-file
func (o *PKICreateOptions) ObtainIntermediateCSRFile() (string, error) {
intermediateCsrFile := viper.GetString("intermediate-csr-file")
intermediateCsrFile := o.viper.GetString("intermediate-csr-file")
if intermediateCsrFile == "" {
var err error
intermediateCsrFile, err = util.PickValue("Required flag 'intermediate--csr-file' not specified; Enter Intermediate CSR file name now:", "intermediate-csr", true)
Expand All @@ -186,7 +166,7 @@ func (o *PKICreateOptions) ObtainIntermediateCSRFile() (string, error) {

// ObtainCSRFile returns the value for csr-file
func (o *PKICreateOptions) ObtainCSRFile() (string, error) {
csrFile := viper.GetString("csr_file")
csrFile := o.viper.GetString("csr_file")
if csrFile == "" {
var err error
csrFile, err = util.PickValue("Required flag 'csr-file' not specified; Enter CSR name now:", "csr", true)
Expand All @@ -201,7 +181,7 @@ func (o *PKICreateOptions) ObtainCSRFile() (string, error) {
func (o *PKICreateOptions) ObtainServerCertFile() (string, error) {
serverFile := o.Flags.ServerFile
if serverFile == "" {
serverFile = viper.GetString("server-file")
serverFile = o.viper.GetString("server-file")
if serverFile == "" {
var err error
serverFile, err = util.PickValue("Required flag 'server-file' not specified; Enter name now:", "server", true)
Expand All @@ -217,7 +197,7 @@ func (o *PKICreateOptions) ObtainServerCertFile() (string, error) {
func (o *PKICreateOptions) ObtainClientCertFile() (string, error) {
clientFile := o.Flags.ClientFile
if clientFile == "" {
clientFile = viper.GetString("client-file")
clientFile = o.viper.GetString("client-file")
if clientFile == "" {
var err error
clientFile, err = util.PickValue("Required flag 'client-file' not specified; Enter name now:", "client", true)
Expand All @@ -233,7 +213,7 @@ func (o *PKICreateOptions) ObtainClientCertFile() (string, error) {
func (o *PKICreateOptions) ObtainKeyFile(required bool) (string, error) {
keyfile := o.Flags.KeyFile
if keyfile == "" {
keyfile = viper.GetString("key-file")
keyfile = o.viper.GetString("key-file")
if keyfile == "" {
if required {
var err error
Expand All @@ -251,7 +231,7 @@ func (o *PKICreateOptions) ObtainKeyFile(required bool) (string, error) {
func (o *PKICreateOptions) ObtainCAName(pkiRoot string) (string, error) {
caName := o.Flags.CAName
if caName == "" {
caName = viper.GetString("ca-name")
caName = o.viper.GetString("ca-name")
if caName == "" {
var err error
files, err := os.ReadDir(pkiRoot)
Expand Down Expand Up @@ -306,19 +286,19 @@ func (o *PKICreateOptions) ObtainFileName(caFile string, commonName string) stri
func (o *PKICreateOptions) ObtainPKIRequestTemplate(commonName string) *x509.Certificate {

subject := pkix.Name{CommonName: commonName}
if str := viper.GetString("pki-organization"); str != "" {
if str := o.viper.GetString("pki-organization"); str != "" {
subject.Organization = []string{str}
}
if str := viper.GetString("pki-locality"); str != "" {
if str := o.viper.GetString("pki-locality"); str != "" {
subject.Locality = []string{str}
}
if str := viper.GetString("pki-country"); str != "" {
if str := o.viper.GetString("pki-country"); str != "" {
subject.Country = []string{str}
}
if str := viper.GetString("pki-state"); str != "" {
if str := o.viper.GetString("pki-state"); str != "" {
subject.Province = []string{str}
}
if str := viper.GetString("pki-organizational-unit"); str != "" {
if str := o.viper.GetString("pki-organizational-unit"); str != "" {
subject.OrganizationalUnit = []string{str}
}

Expand Down Expand Up @@ -359,19 +339,19 @@ func (o *PKICreateOptions) ObtainKeyName(pkiRoot string) (string, error) {
func (o *PKICreateOptions) ObtainPKICSRRequestTemplate(commonName string) *x509.CertificateRequest {

subject := pkix.Name{CommonName: commonName}
if str := viper.GetString("pki-organization"); str != "" {
if str := o.viper.GetString("pki-organization"); str != "" {
subject.Organization = []string{str}
}
if str := viper.GetString("pki-locality"); str != "" {
if str := o.viper.GetString("pki-locality"); str != "" {
subject.Locality = []string{str}
}
if str := viper.GetString("pki-country"); str != "" {
if str := o.viper.GetString("pki-country"); str != "" {
subject.Country = []string{str}
}
if str := viper.GetString("pki-state"); str != "" {
if str := o.viper.GetString("pki-state"); str != "" {
subject.Province = []string{str}
}
if str := viper.GetString("pki-organizational-unit"); str != "" {
if str := o.viper.GetString("pki-organizational-unit"); str != "" {
subject.OrganizationalUnit = []string{str}
}

Expand Down
7 changes: 5 additions & 2 deletions ziti/cmd/pki/pki_create_ca.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package pki
import (
"fmt"
"github.com/openziti/ziti/controller/idgen"
cmd2 "github.com/openziti/ziti/ziti/cmd/common"
"github.com/openziti/ziti/ziti/cmd/common"
cmdhelper "github.com/openziti/ziti/ziti/cmd/helpers"
"github.com/openziti/ziti/ziti/internal/log"
"github.com/openziti/ziti/ziti/pki/certificate"
Expand All @@ -42,10 +42,11 @@ func NewCmdPKICreateCA(out io.Writer, errOut io.Writer) *cobra.Command {
options := &PKICreateCAOptions{
PKICreateOptions: PKICreateOptions{
PKIOptions: PKIOptions{
CommonOptions: cmd2.CommonOptions{
CommonOptions: common.CommonOptions{
Out: out,
Err: errOut,
},
viper: common.NewViper(),
},
},
}
Expand All @@ -68,6 +69,8 @@ func NewCmdPKICreateCA(out io.Writer, errOut io.Writer) *cobra.Command {
const FlagCaName = "ca-name"

func (o *PKICreateCAOptions) addPKICreateCAFlags(cmd *cobra.Command) {
o.addPKICreateFlags(cmd)

cmd.Flags().StringVarP(&o.Flags.PKIRoot, "pki-root", "", "", "Directory in which PKI resides")
cmd.Flags().StringVarP(&o.Flags.CAFile, "ca-file", "", "", "Dir/File name (within PKI_ROOT) in which to store new CA")
cmd.Flags().StringVarP(&o.Flags.CAName, FlagCaName, "", "NetFoundry Inc. Certificate Authority", "Name of CA")
Expand Down
7 changes: 5 additions & 2 deletions ziti/cmd/pki/pki_create_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package pki
import (
"fmt"
"github.com/openziti/ziti/controller/idgen"
cmd2 "github.com/openziti/ziti/ziti/cmd/common"
"github.com/openziti/ziti/ziti/cmd/common"
cmdhelper "github.com/openziti/ziti/ziti/cmd/helpers"
"github.com/openziti/ziti/ziti/internal/log"
"github.com/openziti/ziti/ziti/pki/certificate"
Expand All @@ -42,10 +42,11 @@ func NewCmdPKICreateClient(out io.Writer, errOut io.Writer) *cobra.Command {
options := &PKICreateClientOptions{
PKICreateOptions: PKICreateOptions{
PKIOptions: PKIOptions{
CommonOptions: cmd2.CommonOptions{
CommonOptions: common.CommonOptions{
Out: out,
Err: errOut,
},
viper: common.NewViper(),
},
},
}
Expand All @@ -69,6 +70,8 @@ func NewCmdPKICreateClient(out io.Writer, errOut io.Writer) *cobra.Command {
const FlagCaClientName = "client-name"

func (o *PKICreateClientOptions) addPKICreateClientFlags(cmd *cobra.Command) {
o.addPKICreateFlags(cmd)

cmd.Flags().StringVarP(&o.Flags.PKIRoot, "pki-root", "", "", "Directory in which PKI resides")
cmd.Flags().StringVarP(&o.Flags.CAName, "ca-name", "", "intermediate", "Name of Intermediate CA (within PKI_ROOT) to use to sign the new Client certificate")
cmd.Flags().StringVarP(&o.Flags.ClientFile, "client-file", "", "client", "Name of file (under chosen CA) in which to store new Client certificate and private key")
Expand Down
Loading

0 comments on commit 6d0737b

Please sign in to comment.