Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[govc] Introduce TLSHandshakeTimeout parameter #890

Merged
merged 2 commits into from
Oct 23, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions govc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ to set defaults:
> govc about.cert -u host -k -thumbprint | tee -a $GOVC_TLS_KNOWN_HOSTS
> govc about -u user:pass@host

* `GOVC_TLS_HANDSHAKE_TIMEOUT`: Limits the time spent performing the TLS handshake.

* `GOVC_INSECURE`: Disable certificate verification.

> This option sets Go's tls.Config.InsecureSkipVerify flag and is false by default.
Expand Down
71 changes: 44 additions & 27 deletions govc/flags/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ import (
"flag"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"time"

"github.com/vmware/govmomi/session"
"github.com/vmware/govmomi/vim25"
Expand All @@ -37,18 +39,19 @@ import (
)

const (
envURL = "GOVC_URL"
envUsername = "GOVC_USERNAME"
envPassword = "GOVC_PASSWORD"
envCertificate = "GOVC_CERTIFICATE"
envPrivateKey = "GOVC_PRIVATE_KEY"
envInsecure = "GOVC_INSECURE"
envPersist = "GOVC_PERSIST_SESSION"
envMinAPIVersion = "GOVC_MIN_API_VERSION"
envVimNamespace = "GOVC_VIM_NAMESPACE"
envVimVersion = "GOVC_VIM_VERSION"
envTLSCaCerts = "GOVC_TLS_CA_CERTS"
envTLSKnownHosts = "GOVC_TLS_KNOWN_HOSTS"
envURL = "GOVC_URL"
envUsername = "GOVC_USERNAME"
envPassword = "GOVC_PASSWORD"
envCertificate = "GOVC_CERTIFICATE"
envPrivateKey = "GOVC_PRIVATE_KEY"
envInsecure = "GOVC_INSECURE"
envPersist = "GOVC_PERSIST_SESSION"
envMinAPIVersion = "GOVC_MIN_API_VERSION"
envVimNamespace = "GOVC_VIM_NAMESPACE"
envVimVersion = "GOVC_VIM_VERSION"
envTLSCaCerts = "GOVC_TLS_CA_CERTS"
envTLSKnownHosts = "GOVC_TLS_KNOWN_HOSTS"
envTLSHandshakeTimeout = "GOVC_TLS_HANDSHAKE_TIMEOUT"
)

const cDescr = "ESX or vCenter URL"
Expand All @@ -58,21 +61,21 @@ type ClientFlag struct {

*DebugFlag

url *url.URL
username string
password string
cert string
key string
insecure bool
persist bool
minAPIVersion string
vimNamespace string
vimVersion string
tlsCaCerts string
tlsKnownHosts string
tlsHostHash string

client *vim25.Client
url *url.URL
username string
password string
cert string
key string
insecure bool
persist bool
minAPIVersion string
vimNamespace string
vimVersion string
tlsCaCerts string
tlsKnownHosts string
tlsHostHash string
tlsHandshakeTimeout time.Duration
client *vim25.Client
}

var (
Expand Down Expand Up @@ -215,6 +218,16 @@ func (flag *ClientFlag) Register(ctx context.Context, f *flag.FlagSet) {
usage := fmt.Sprintf("TLS known hosts file [%s]", envTLSKnownHosts)
f.StringVar(&flag.tlsKnownHosts, "tls-known-hosts", value, usage)
}

{
value, err := time.ParseDuration(os.Getenv(envTLSHandshakeTimeout))
if err != nil {
value = 10 * time.Second
}
usage := fmt.Sprintf("TLS handshake timeout [%s]", envTLSHandshakeTimeout)
f.DurationVar(&flag.tlsHandshakeTimeout, "tls-handshake-timeout", value, usage)
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extra blank line :)

})
}

Expand Down Expand Up @@ -275,6 +288,10 @@ func (flag *ClientFlag) configure(sc *soap.Client) (soap.RoundTripper, error) {
return nil, err
}

if t, ok := sc.Transport.(*http.Transport); ok {
t.TLSHandshakeTimeout = flag.tlsHandshakeTimeout
}

// Retry twice when a temporary I/O error occurs.
// This means a maximum of 3 attempts.
return vim25.Retry(sc, vim25.TemporaryNetworkError(3)), nil
Expand Down
18 changes: 18 additions & 0 deletions govc/test/cli.bats
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,24 @@ load test_helper
assert_success
assert_line "Vendor: VMware, Inc."

run govc about --tls-handshake-timeout=10s
assert_success
assert_line "Vendor: VMware, Inc."

run env GOVC_TLS_HANDSHAKE_TIMEOUT=10s govc about
assert_success
assert_line "Vendor: VMware, Inc."

run env GOVC_TLS_HANDSHAKE_TIMEOUT=NOT_A_DURATION govc about
assert_success
assert_line "Vendor: VMware, Inc."

run govc about --tls-handshake-timeout=1ns
assert_failure

run env GOVC_TLS_HANDSHAKE_TIMEOUT=1ns govc about
assert_failure

run govc about -json
assert_success

Expand Down
1 change: 1 addition & 0 deletions govc/test/test_helper.bash
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export GOVC_PERSIST_SESSION=false
unset GOVC_URL
unset GOVC_DEBUG
unset GOVC_TLS_KNOWN_HOSTS
unset GOVC_TLS_HANDSHAKE_TIMEOUT
unset GOVC_DATACENTER
unset GOVC_HOST
unset GOVC_USERNAME
Expand Down