github.com/koderover/helm@v2.17.0+incompatible/pkg/tlsutil/cfg.go (about) 1 /* 2 Copyright The Helm Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package tlsutil 18 19 import ( 20 "crypto/tls" 21 "crypto/x509" 22 "fmt" 23 "os" 24 ) 25 26 // Options represents configurable options used to create client and server TLS configurations. 27 type Options struct { 28 CaCertFile string 29 // If either the KeyFile or CertFile is empty, ClientConfig() will not load them, 30 // preventing Helm from authenticating to Tiller. They are required to be non-empty 31 // when calling ServerConfig, otherwise an error is returned. 32 KeyFile string 33 CertFile string 34 // Client-only options 35 InsecureSkipVerify bool 36 // Overrides the server name used to verify the hostname on the returned 37 // certificates from the server. 38 ServerName string 39 // Server-only options 40 ClientAuth tls.ClientAuthType 41 } 42 43 // ClientConfig returns a TLS configuration for use by a Helm client. 44 func ClientConfig(opts Options) (cfg *tls.Config, err error) { 45 var cert *tls.Certificate 46 var pool *x509.CertPool 47 48 if opts.CertFile != "" || opts.KeyFile != "" { 49 if cert, err = CertFromFilePair(opts.CertFile, opts.KeyFile); err != nil { 50 if os.IsNotExist(err) { 51 return nil, fmt.Errorf("could not load x509 key pair (cert: %q, key: %q): %v", opts.CertFile, opts.KeyFile, err) 52 } 53 return nil, fmt.Errorf("could not read x509 key pair (cert: %q, key: %q): %v", opts.CertFile, opts.KeyFile, err) 54 } 55 } 56 if !opts.InsecureSkipVerify && opts.CaCertFile != "" { 57 if pool, err = CertPoolFromFile(opts.CaCertFile); err != nil { 58 return nil, err 59 } 60 } 61 cfg = &tls.Config{ 62 InsecureSkipVerify: opts.InsecureSkipVerify, 63 Certificates: []tls.Certificate{*cert}, 64 ServerName: opts.ServerName, 65 RootCAs: pool, 66 } 67 return cfg, nil 68 } 69 70 // ServerConfig returns a TLS configuration for use by the Tiller server. 71 func ServerConfig(opts Options) (cfg *tls.Config, err error) { 72 var cert *tls.Certificate 73 var pool *x509.CertPool 74 75 if cert, err = CertFromFilePair(opts.CertFile, opts.KeyFile); err != nil { 76 if os.IsNotExist(err) { 77 return nil, fmt.Errorf("could not load x509 key pair (cert: %q, key: %q): %v", opts.CertFile, opts.KeyFile, err) 78 } 79 return nil, fmt.Errorf("could not read x509 key pair (cert: %q, key: %q): %v", opts.CertFile, opts.KeyFile, err) 80 } 81 if opts.ClientAuth >= tls.VerifyClientCertIfGiven && opts.CaCertFile != "" { 82 if pool, err = CertPoolFromFile(opts.CaCertFile); err != nil { 83 return nil, err 84 } 85 } 86 87 cfg = &tls.Config{MinVersion: tls.VersionTLS12, ClientAuth: opts.ClientAuth, Certificates: []tls.Certificate{*cert}, ClientCAs: pool} 88 return cfg, nil 89 }