Skip to content
This repository has been archived by the owner on Jun 9, 2019. It is now read-only.

Commit

Permalink
[FIXED] assignment copies lock value for crypto/tls.Config
Browse files Browse the repository at this point in the history
Running `go vet ./...` with `go 1.7.3` would report the following:

```
nats.go:769: assignment copies lock value to tlsCopy: crypto/tls.Config contains sync.Once contains sync.Mutex
```

Add a “clone” function while waiting for this to be addressed
by the language itself (https://go-review.googlesource.com/#/c/28075/)
  • Loading branch information
kozlovic committed Oct 21, 2016
1 parent 6fb7c9f commit 93dfbd6
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 4 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
@@ -1,8 +1,8 @@
language: go
sudo: false
go:
- 1.5
- 1.6
- 1.6.3
- 1.7.3
install:
- go get -t ./...
- go get github.com/nats-io/gnatsd
Expand Down
5 changes: 3 additions & 2 deletions nats.go
Expand Up @@ -23,6 +23,7 @@ import (
"sync/atomic"
"time"

"github.com/nats-io/nats/util"
"github.com/nats-io/nuid"
)

Expand Down Expand Up @@ -766,13 +767,13 @@ func (nc *Conn) makeTLSConn() {
// default to InsecureSkipVerify.
// TODO(dlc) - We should make the more secure version the default.
if nc.Opts.TLSConfig != nil {
tlsCopy := *nc.Opts.TLSConfig
tlsCopy := util.CloneTLSConfig(nc.Opts.TLSConfig)
// If its blank we will override it with the current host
if tlsCopy.ServerName == _EMPTY_ {
h, _, _ := net.SplitHostPort(nc.url.Host)
tlsCopy.ServerName = h
}
nc.conn = tls.Client(nc.conn, &tlsCopy)
nc.conn = tls.Client(nc.conn, tlsCopy)
} else {
nc.conn = tls.Client(nc.conn, &tls.Config{InsecureSkipVerify: true})
}
Expand Down
37 changes: 37 additions & 0 deletions util/tls.go
@@ -0,0 +1,37 @@
// Copyright 2016 Apcera Inc. All rights reserved.
// +build go1.7

package util

import (
"crypto/tls"
)

// CloneTLSConfig returns a copy of c. Only the exported fields are copied.
// This is temporary, until this is provided by the language.
// https://go-review.googlesource.com/#/c/28075/
func CloneTLSConfig(c *tls.Config) *tls.Config {
return &tls.Config{
Rand: c.Rand,
Time: c.Time,
Certificates: c.Certificates,
NameToCertificate: c.NameToCertificate,
GetCertificate: c.GetCertificate,
RootCAs: c.RootCAs,
NextProtos: c.NextProtos,
ServerName: c.ServerName,
ClientAuth: c.ClientAuth,
ClientCAs: c.ClientCAs,
InsecureSkipVerify: c.InsecureSkipVerify,
CipherSuites: c.CipherSuites,
PreferServerCipherSuites: c.PreferServerCipherSuites,
SessionTicketsDisabled: c.SessionTicketsDisabled,
SessionTicketKey: c.SessionTicketKey,
ClientSessionCache: c.ClientSessionCache,
MinVersion: c.MinVersion,
MaxVersion: c.MaxVersion,
CurvePreferences: c.CurvePreferences,
DynamicRecordSizingDisabled: c.DynamicRecordSizingDisabled,
Renegotiation: c.Renegotiation,
}
}
35 changes: 35 additions & 0 deletions util/tls_pre17.go
@@ -0,0 +1,35 @@
// Copyright 2016 Apcera Inc. All rights reserved.
// +build go1.5,!go1.7

package util

import (
"crypto/tls"
)

// CloneTLSConfig returns a copy of c. Only the exported fields are copied.
// This is temporary, until this is provided by the language.
// https://go-review.googlesource.com/#/c/28075/
func CloneTLSConfig(c *tls.Config) *tls.Config {
return &tls.Config{
Rand: c.Rand,
Time: c.Time,
Certificates: c.Certificates,
NameToCertificate: c.NameToCertificate,
GetCertificate: c.GetCertificate,
RootCAs: c.RootCAs,
NextProtos: c.NextProtos,
ServerName: c.ServerName,
ClientAuth: c.ClientAuth,
ClientCAs: c.ClientCAs,
InsecureSkipVerify: c.InsecureSkipVerify,
CipherSuites: c.CipherSuites,
PreferServerCipherSuites: c.PreferServerCipherSuites,
SessionTicketsDisabled: c.SessionTicketsDisabled,
SessionTicketKey: c.SessionTicketKey,
ClientSessionCache: c.ClientSessionCache,
MinVersion: c.MinVersion,
MaxVersion: c.MaxVersion,
CurvePreferences: c.CurvePreferences,
}
}

0 comments on commit 93dfbd6

Please sign in to comment.