|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + |
| 8 | + "k8s.io/client-go/util/homedir" |
| 9 | + |
| 10 | + flag "github.com/spf13/pflag" |
| 11 | + "k8s.io/helm/pkg/helm" |
| 12 | + helm_env "k8s.io/helm/pkg/helm/environment" |
| 13 | + "k8s.io/helm/pkg/tlsutil" |
| 14 | +) |
| 15 | + |
| 16 | +const ( |
| 17 | + tlsCaCertDefault = "$HELM_HOME/ca.pem" |
| 18 | + tlsCertDefault = "$HELM_HOME/cert.pem" |
| 19 | + tlsKeyDefault = "$HELM_HOME/key.pem" |
| 20 | +) |
| 21 | + |
| 22 | +var ( |
| 23 | + settings helm_env.EnvSettings |
| 24 | + DefaultHelmHome = filepath.Join(homedir.HomeDir(), ".helm") |
| 25 | + |
| 26 | + tlsCaCertFile string // path to TLS CA certificate file |
| 27 | + tlsCertFile string // path to TLS certificate file |
| 28 | + tlsKeyFile string // path to TLS key file |
| 29 | + tlsVerify bool // enable TLS and verify remote certificates |
| 30 | + tlsEnable bool // enable TLS |
| 31 | +) |
| 32 | + |
| 33 | +func addCommonCmdOptions(f *flag.FlagSet) { |
| 34 | + f.StringVar(&tlsCaCertFile, "tls-ca-cert", tlsCaCertDefault, "path to TLS CA certificate file") |
| 35 | + f.StringVar(&tlsCertFile, "tls-cert", tlsCertDefault, "path to TLS certificate file") |
| 36 | + f.StringVar(&tlsKeyFile, "tls-key", tlsKeyDefault, "path to TLS key file") |
| 37 | + f.BoolVar(&tlsVerify, "tls-verify", false, "enable TLS for request and verify remote") |
| 38 | + f.BoolVar(&tlsEnable, "tls", false, "enable TLS for request") |
| 39 | + |
| 40 | + f.StringVar((*string)(&settings.Home), "home", DefaultHelmHome, "location of your Helm config. Overrides $HELM_HOME") |
| 41 | +} |
| 42 | + |
| 43 | +func createHelmClient() helm.Interface { |
| 44 | + options := []helm.Option{helm.Host(os.Getenv("TILLER_HOST")), helm.ConnectTimeout(int64(30))} |
| 45 | + |
| 46 | + if tlsVerify || tlsEnable { |
| 47 | + if tlsCaCertFile == "" { |
| 48 | + tlsCaCertFile = settings.Home.TLSCaCert() |
| 49 | + } |
| 50 | + if tlsCertFile == "" { |
| 51 | + tlsCertFile = settings.Home.TLSCert() |
| 52 | + } |
| 53 | + if tlsKeyFile == "" { |
| 54 | + tlsKeyFile = settings.Home.TLSKey() |
| 55 | + } |
| 56 | + |
| 57 | + tlsopts := tlsutil.Options{KeyFile: tlsKeyFile, CertFile: tlsCertFile, InsecureSkipVerify: true} |
| 58 | + if tlsVerify { |
| 59 | + tlsopts.CaCertFile = tlsCaCertFile |
| 60 | + tlsopts.InsecureSkipVerify = false |
| 61 | + } |
| 62 | + |
| 63 | + tlscfg, err := tlsutil.ClientConfig(tlsopts) |
| 64 | + if err != nil { |
| 65 | + fmt.Fprintln(os.Stderr, err) |
| 66 | + os.Exit(2) |
| 67 | + } |
| 68 | + |
| 69 | + options = append(options, helm.WithTLS(tlscfg)) |
| 70 | + } |
| 71 | + |
| 72 | + return helm.NewClient(options...) |
| 73 | +} |
| 74 | + |
| 75 | +func expandTLSPaths() { |
| 76 | + tlsCaCertFile = os.ExpandEnv(tlsCaCertFile) |
| 77 | + tlsCertFile = os.ExpandEnv(tlsCertFile) |
| 78 | + tlsKeyFile = os.ExpandEnv(tlsKeyFile) |
| 79 | +} |
0 commit comments