Skip to content

Commit

Permalink
default to tls enabled with a warning error if tls verification fails (
Browse files Browse the repository at this point in the history
  • Loading branch information
jmorganca committed Aug 10, 2021
1 parent ff6dbea commit 2451248
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
6 changes: 1 addition & 5 deletions internal/cmd/cmd.go
Expand Up @@ -785,11 +785,7 @@ func newEngineCmd() *cobra.Command {
},
}

skipTLSVerify := true
// TODO (https://github.com/infrahq/infra/issues/58): warn users instead of skipping TLS verification
// OR find a way to include the server certificate in the api key
// skipTLSVerify := len(os.Getenv("INFRA_ENGINE_SKIP_TLS_VERIFY")) > 0
cmd.PersistentFlags().BoolVarP(&options.SkipTLSVerify, "skip-tls-verify", "k", skipTLSVerify, "skip TLS verification")
cmd.PersistentFlags().BoolVar(&options.ForceTLSVerify, "force-tls-verify", false, "force TLS verification")
cmd.Flags().StringVarP(&options.Registry, "registry", "r", os.Getenv("INFRA_ENGINE_REGISTRY"), "registry hostname")
cmd.Flags().StringVarP(&options.Name, "name", "n", os.Getenv("INFRA_ENGINE_NAME"), "cluster name")
cmd.Flags().StringVarP(&options.Endpoint, "endpoint", "e", os.Getenv("INFRA_ENGINE_ENDPOINT"), "cluster endpoint")
Expand Down
41 changes: 32 additions & 9 deletions internal/engine/engine.go
Expand Up @@ -29,11 +29,11 @@ import (
)

type Options struct {
Registry string
Name string
Endpoint string
SkipTLSVerify bool
APIKey string
Registry string
Name string
Endpoint string
ForceTLSVerify bool
APIKey string
}

type RoleBinding struct {
Expand Down Expand Up @@ -220,7 +220,32 @@ func Run(options Options) error {
registry += ":443"
}

creds := credentials.NewTLS(&tls.Config{InsecureSkipVerify: options.SkipTLSVerify})
tlsConfig := &tls.Config{}
if !options.ForceTLSVerify {
// TODO (https://github.com/infrahq/infra/issues/174)
// Find a way to re-use the built-in TLS verification code vs
// this custom code based on the official go TLS example code
// which states this is approximately the same.
tlsConfig.InsecureSkipVerify = true
tlsConfig.VerifyConnection = func(cs tls.ConnectionState) error {
opts := x509.VerifyOptions{
DNSName: cs.ServerName,
Intermediates: x509.NewCertPool(),
}
for _, cert := range cs.PeerCertificates[1:] {
opts.Intermediates.AddCert(cert)
}
_, err := cs.PeerCertificates[0].Verify(opts)

if err != nil {
fmt.Println("Warning: could not verify registry TLS certificates: " + err.Error())
}

return nil
}
}

creds := credentials.NewTLS(tlsConfig)
conn, err := grpc.Dial(registry, grpc.WithTransportCredentials(creds), withClientAuthUnaryInterceptor(options.APIKey))
if err != nil {
return err
Expand Down Expand Up @@ -347,9 +372,7 @@ func Run(options Options) error {
Transport: &BearerTransport{
Token: options.APIKey,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: options.SkipTLSVerify,
},
TLSClientConfig: tlsConfig,
},
},
},
Expand Down

0 comments on commit 2451248

Please sign in to comment.