Skip to content

Commit 5abc7b2

Browse files
vdesjardinsmumoshu
authored andcommitted
feat: TLS-based client auth support (#71)
Add support for connecting to a Tiller server secured with TLS. Resolves #35
1 parent 01eba5c commit 5abc7b2

File tree

4 files changed

+104
-8
lines changed

4 files changed

+104
-8
lines changed

cmd/helpers.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
}

cmd/revision.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ type revision struct {
2323
const revisionCmdLongUsage = `
2424
This command compares the manifests details of a named release.
2525
26-
It can be used to compare the manifests of
27-
26+
It can be used to compare the manifests of
27+
2828
- lastest REVISION with specified REVISION
2929
$ helm diff revision [flags] RELEASE REVISION1
30-
Example:
30+
Example:
3131
$ helm diff revision my-release 2
3232
3333
- REVISION1 with REVISION2
3434
$ helm diff revision [flags] RELEASE REVISION1 REVISION2
35-
Example:
35+
Example:
3636
$ helm diff revision my-release 2 3
3737
`
3838

@@ -42,6 +42,9 @@ func revisionCmd() *cobra.Command {
4242
Use: "revision [flags] RELEASE REVISION1 [REVISION2]",
4343
Short: "Shows diff between revision's manifests",
4444
Long: revisionCmdLongUsage,
45+
PersistentPreRun: func(*cobra.Command, []string) {
46+
expandTLSPaths()
47+
},
4548
RunE: func(cmd *cobra.Command, args []string) error {
4649
if v, _ := cmd.Flags().GetBool("version"); v {
4750
fmt.Println(Version)
@@ -62,7 +65,7 @@ func revisionCmd() *cobra.Command {
6265
diff.release = args[0]
6366
diff.revisions = args[1:]
6467
if diff.client == nil {
65-
diff.client = helm.NewClient(helm.Host(os.Getenv("TILLER_HOST")), helm.ConnectTimeout(int64(30)))
68+
diff.client = createHelmClient()
6669
}
6770
return diff.differentiate()
6871
},
@@ -72,6 +75,9 @@ func revisionCmd() *cobra.Command {
7275
revisionCmd.Flags().StringArrayVar(&diff.suppressedKinds, "suppress", []string{}, "allows suppression of the values listed in the diff output")
7376
revisionCmd.Flags().IntVarP(&diff.outputContext, "context", "C", -1, "output NUM lines of context around changes")
7477
revisionCmd.SuggestionsMinimumDistance = 1
78+
79+
addCommonCmdOptions(revisionCmd.Flags())
80+
7581
return revisionCmd
7682
}
7783

cmd/rollback.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type rollback struct {
2020
}
2121

2222
const rollbackCmdLongUsage = `
23-
This command compares the laset manifests details of a named release
23+
This command compares the laset manifests details of a named release
2424
with specific revision values to rollback.
2525
2626
It forecasts/visualizes changes, that a helm rollback could perform.
@@ -33,6 +33,9 @@ func rollbackCmd() *cobra.Command {
3333
Short: "Show a diff explaining what a helm rollback could perform",
3434
Long: rollbackCmdLongUsage,
3535
Example: " helm diff rollback my-release 2",
36+
PersistentPreRun: func(*cobra.Command, []string) {
37+
expandTLSPaths()
38+
},
3639
RunE: func(cmd *cobra.Command, args []string) error {
3740
if v, _ := cmd.Flags().GetBool("version"); v {
3841
fmt.Println(Version)
@@ -51,7 +54,7 @@ func rollbackCmd() *cobra.Command {
5154
diff.revisions = args[1:]
5255

5356
if diff.client == nil {
54-
diff.client = helm.NewClient(helm.Host(os.Getenv("TILLER_HOST")), helm.ConnectTimeout(int64(30)))
57+
diff.client = createHelmClient()
5558
}
5659

5760
return diff.backcast()
@@ -62,6 +65,9 @@ func rollbackCmd() *cobra.Command {
6265
rollbackCmd.Flags().StringArrayVar(&diff.suppressedKinds, "suppress", []string{}, "allows suppression of the values listed in the diff output")
6366
rollbackCmd.Flags().IntVarP(&diff.outputContext, "context", "C", -1, "output NUM lines of context around changes")
6467
rollbackCmd.SuggestionsMinimumDistance = 1
68+
69+
addCommonCmdOptions(rollbackCmd.Flags())
70+
6571
return rollbackCmd
6672
}
6773

cmd/upgrade.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ func newChartCommand() *cobra.Command {
4646
Args: func(cmd *cobra.Command, args []string) error {
4747
return checkArgsLength(len(args), "release name", "chart path")
4848
},
49+
PersistentPreRun: func(*cobra.Command, []string) {
50+
expandTLSPaths()
51+
},
4952
RunE: func(cmd *cobra.Command, args []string) error {
5053

5154
if q, _ := cmd.Flags().GetBool("suppress-secrets"); q {
@@ -55,7 +58,7 @@ func newChartCommand() *cobra.Command {
5558
diff.release = args[0]
5659
diff.chart = args[1]
5760
if diff.client == nil {
58-
diff.client = helm.NewClient(helm.Host(os.Getenv("TILLER_HOST")), helm.ConnectTimeout(int64(30)))
61+
diff.client = createHelmClient()
5962
}
6063
return diff.run()
6164
},
@@ -73,6 +76,8 @@ func newChartCommand() *cobra.Command {
7376
f.StringArrayVar(&diff.suppressedKinds, "suppress", []string{}, "allows suppression of the values listed in the diff output")
7477
f.IntVarP(&diff.outputContext, "context", "C", -1, "output NUM lines of context around changes")
7578

79+
addCommonCmdOptions(f)
80+
7681
return cmd
7782

7883
}

0 commit comments

Comments
 (0)