github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/cmd/get/vault/config/get_vault_config.go (about) 1 package config 2 3 import ( 4 "fmt" 5 "runtime" 6 7 "github.com/olli-ai/jx/v2/pkg/vault" 8 "github.com/pkg/errors" 9 10 "github.com/olli-ai/jx/v2/pkg/cmd/helper" 11 12 "github.com/olli-ai/jx/v2/pkg/cmd/opts" 13 "github.com/olli-ai/jx/v2/pkg/cmd/templates" 14 "github.com/spf13/cobra" 15 ) 16 17 type GetVaultConfigOptions struct { 18 *opts.CommonOptions 19 20 Namespace string 21 Name string 22 terminal string 23 } 24 25 var ( 26 getVaultConfigLong = templates.LongDesc(` 27 Used to echo the Vault connection configuration for the Jenkins X system Vault. 28 To have the settings apply to the current terminal session the output must be evaluated, for example: 29 30 $ eval $(jx get vault-config) 31 32 Together with the name and namespace option, this command can be used to echo the connection configuration 33 for any vault installed via 'jx add vault'. 34 `) 35 36 getVaultConfigExample = templates.Examples(` 37 # Gets vault config 38 jx get vault-config 39 `) 40 ) 41 42 // NewCmdGetVaultConfig creates a new command for 'jx get secrets' 43 func NewCmdGetVaultConfig(commonOpts *opts.CommonOptions) *cobra.Command { 44 options := &GetVaultConfigOptions{ 45 CommonOptions: commonOpts, 46 } 47 48 cmd := &cobra.Command{ 49 Use: "vault-config", 50 Short: "Gets the configuration for using the Vault CLI", 51 Long: getVaultConfigLong, 52 Example: getVaultConfigExample, 53 Run: func(c *cobra.Command, args []string) { 54 options.Cmd = c 55 options.Args = args 56 err := options.Run() 57 helper.CheckErr(err) 58 }, 59 } 60 61 cmd.Flags().StringVarP(&options.Namespace, "namespace", "n", "", "Namespace from where to get the Vault config") 62 cmd.Flags().StringVarP(&options.Name, "name", "m", "", "Name of the Vault to get the config for") 63 cmd.Flags().StringVarP(&options.terminal, "terminal", "t", "", "terminal type output override. Values: ['sh', 'cmd'].") 64 return cmd 65 } 66 67 // Run implements the command 68 func (o *GetVaultConfigOptions) Run() error { 69 var vaultClient vault.Client 70 var err error 71 72 if o.Name != "" || o.Namespace != "" { 73 vaultClient, err = o.vaultClient(o.Name, o.Namespace) 74 if err != nil { 75 return err 76 } 77 } else { 78 vaultClient, err = o.systemVaultClient() 79 if err != nil { 80 return err 81 } 82 } 83 84 url, token, err := vaultClient.Config() 85 // Echo the client config out to the command line to be piped into bash 86 if o.terminal == "" { 87 if runtime.GOOS == "windows" { 88 o.terminal = "cmd" 89 } else { 90 o.terminal = "sh" 91 } 92 } 93 if o.terminal == "cmd" { 94 _, _ = fmt.Fprintf(o.Out, "set VAULT_ADDR=%s\nset VAULT_TOKEN=%s\n", url.String(), token) 95 } else { 96 _, _ = fmt.Fprintf(o.Out, "export VAULT_ADDR=%s\nexport VAULT_TOKEN=%s\n", url.String(), token) 97 } 98 99 return err 100 } 101 102 func (o *GetVaultConfigOptions) systemVaultClient() (vault.Client, error) { 103 _, devNamespace, err := o.KubeClientAndDevNamespace() 104 if err != nil { 105 return nil, errors.Wrap(err, "unable to create Kube client") 106 } 107 108 return o.SystemVaultClient(devNamespace) 109 } 110 111 func (o *GetVaultConfigOptions) vaultClient(name string, namespace string) (vault.Client, error) { 112 factory := o.GetFactory() 113 client, err := factory.CreateInternalVaultClient(name, namespace) 114 if err != nil { 115 return nil, errors.Wrap(err, "unable to create Vault client for Jenkins X managed Vault instance") 116 } 117 118 return client, nil 119 }