Skip to content

Commit

Permalink
UPSTREAM: <carry>: Use ConnectionConfig fields when establishing sess…
Browse files Browse the repository at this point in the history
…ion to initial contact point

When host information was missing, driver used resolved IP address as TLS.ServerName
Instead it should connect to Server specified in ConnectionConfig and use NodeDomain
as SNI.
  • Loading branch information
zimnx committed Nov 8, 2022
1 parent e3e27db commit 64bad46
Show file tree
Hide file tree
Showing 5 changed files with 721 additions and 7 deletions.
7 changes: 7 additions & 0 deletions host_source_scylla.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package gocql

func (h *HostInfo) SetDatacenter(dc string) {
h.mu.Lock()
defer h.mu.Unlock()
h.dataCenter = dc
}
61 changes: 58 additions & 3 deletions scyllacloud/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,23 +164,78 @@ func (cc *ConnectionConfig) GetInitialContactPoints() []string {

func (cc *ConnectionConfig) getDataOrReadFile(data []byte, path string) ([]byte, error) {
if len(data) == 0 {
if _, err := os.Stat(path); os.IsNotExist(err) {
return nil, os.ErrNotExist
}
return os.ReadFile(path)
}

return data, nil
}

func (cc *ConnectionConfig) GetCurrentDatacenterConfig() (*Datacenter, error) {
contextConf, err := cc.GetCurrentContextConfig()
if err != nil {
return nil, fmt.Errorf("can't get current context config: %w", err)
}

if len(contextConf.DatacenterName) == 0 {
return nil, fmt.Errorf("datacenterName in current context can't be empty")
}

dcConf, ok := cc.Datacenters[contextConf.DatacenterName]
if !ok {
return nil, fmt.Errorf("datacenter %q does not exists", contextConf.DatacenterName)
}

return dcConf, nil
}

func (cc *ConnectionConfig) GetCurrentContextConfig() (*Context, error) {
if len(cc.CurrentContext) == 0 {
return nil, fmt.Errorf("current context can't be empty")
}

contextConf, ok := cc.Contexts[cc.CurrentContext]
if !ok {
return nil, fmt.Errorf("context %q does not exists", cc.CurrentContext)
}

return contextConf, nil
}

func (cc *ConnectionConfig) GetCurrentAuthInfo() (*AuthInfo, error) {
contextConf, err := cc.GetCurrentContextConfig()
if err != nil {
return nil, fmt.Errorf("can't get current context config: %w", err)
}

if len(contextConf.AuthInfoName) == 0 {
return nil, fmt.Errorf("authInfo in current context can't be empty")
}

authInfo, ok := cc.AuthInfos[contextConf.AuthInfoName]
if !ok {
return nil, fmt.Errorf("authInfo %q does not exists", contextConf.AuthInfoName)
}

return authInfo, nil
}

func (cc *ConnectionConfig) GetClientCertificate() (*tls.Certificate, error) {
confContext := cc.Contexts[cc.CurrentContext]
authInfo := cc.AuthInfos[confContext.AuthInfoName]
authInfo, err := cc.GetCurrentAuthInfo()
if err != nil {
return nil, fmt.Errorf("can't get current auth info: %w", err)
}

clientCert, err := cc.getDataOrReadFile(authInfo.ClientCertificateData, authInfo.ClientCertificatePath)
if err != nil {
return nil, fmt.Errorf("can't read client certificate: %w", err)
}

clientKey, err := cc.getDataOrReadFile(authInfo.ClientKeyData, authInfo.ClientKeyPath)
if err != nil {
return nil, fmt.Errorf("can't read client certificate: %w", err)
return nil, fmt.Errorf("can't read client key: %w", err)
}

cert, err := tls.X509KeyPair(clientCert, clientKey)
Expand Down
242 changes: 242 additions & 0 deletions scyllacloud/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,248 @@ func TestCloudCluster(t *testing.T) {
}
}

func TestConnectionConfig_GetCurrentContextConfig(t *testing.T) {
tt := []struct {
name string
connConfig *ConnectionConfig
expectedContext *Context
expectedError error
}{
{
name: "empty current context",
connConfig: &ConnectionConfig{
CurrentContext: "",
},
expectedContext: nil,
expectedError: fmt.Errorf("current context can't be empty"),
},
{
name: "not existing current context",
connConfig: &ConnectionConfig{
CurrentContext: "not-existing-context",
},
expectedContext: nil,
expectedError: fmt.Errorf(`context "not-existing-context" does not exists`),
},
{
name: "context from current context is returned",
connConfig: &ConnectionConfig{
Contexts: map[string]*Context{
"default": {
AuthInfoName: "admin",
DatacenterName: "us-east-1",
},
},
CurrentContext: "default",
},
expectedContext: &Context{
AuthInfoName: "admin",
DatacenterName: "us-east-1",
},
expectedError: nil,
},
}

for i := range tt {
tc := tt[i]
t.Run(tc.name, func(t *testing.T) {
contextConf, err := tc.connConfig.GetCurrentContextConfig()
if !reflect.DeepEqual(tc.expectedError, err) {
t.Errorf("expected error %q, got %q", tc.expectedError, err)
}
if !reflect.DeepEqual(tc.expectedContext, contextConf) {
t.Errorf("expected context %v, got %v", tc.expectedContext, contextConf)
}
})
}
}

func TestConnectionConfig_GetCurrentAuthInfo(t *testing.T) {
tt := []struct {
name string
connConfig *ConnectionConfig
expectedAuthInfo *AuthInfo
expectedError error
}{
{
name: "empty current context",
connConfig: &ConnectionConfig{
CurrentContext: "",
},
expectedAuthInfo: nil,
expectedError: fmt.Errorf("can't get current context config: %w", fmt.Errorf("current context can't be empty")),
},
{
name: "not existing current context",
connConfig: &ConnectionConfig{
CurrentContext: "not-existing-context",
},
expectedAuthInfo: nil,
expectedError: fmt.Errorf("can't get current context config: %w", fmt.Errorf(`context "not-existing-context" does not exists`)),
},
{
name: "empty auth info name in current context",
connConfig: &ConnectionConfig{
Contexts: map[string]*Context{
"default": {
AuthInfoName: "",
},
},
CurrentContext: "default",
},
expectedAuthInfo: nil,
expectedError: fmt.Errorf("authInfo in current context can't be empty"),
},
{
name: "not existing auth info name in current context",
connConfig: &ConnectionConfig{
Contexts: map[string]*Context{
"default": {
AuthInfoName: "not-existing-auth-info",
},
},
CurrentContext: "default",
},
expectedAuthInfo: nil,
expectedError: fmt.Errorf(`authInfo "not-existing-auth-info" does not exists`),
},
{
name: "auth info from current context is returned",
connConfig: &ConnectionConfig{
AuthInfos: map[string]*AuthInfo{
"admin": {
ClientCertificatePath: "client-cert-path",
ClientKeyPath: "client-key-path",
Username: "username",
Password: "password",
},
},
Contexts: map[string]*Context{
"default": {
AuthInfoName: "admin",
},
},
CurrentContext: "default",
},
expectedAuthInfo: &AuthInfo{
ClientCertificatePath: "client-cert-path",
ClientKeyPath: "client-key-path",
Username: "username",
Password: "password",
},
expectedError: nil,
},
}

for i := range tt {
tc := tt[i]
t.Run(tc.name, func(t *testing.T) {
ai, err := tc.connConfig.GetCurrentAuthInfo()
if !reflect.DeepEqual(tc.expectedError, err) {
t.Errorf("expected error %q, got %q", tc.expectedError, err)
}
if !reflect.DeepEqual(tc.expectedAuthInfo, ai) {
t.Errorf("expected authInfo %v, got %v", tc.expectedAuthInfo, ai)
}
})
}
}

func TestConnectionConfig_GetCurrentDatacenterConfig(t *testing.T) {
tt := []struct {
name string
connConfig *ConnectionConfig
expectedDatacenter *Datacenter
expectedError error
}{
{
name: "empty current context",
connConfig: &ConnectionConfig{
CurrentContext: "",
},
expectedDatacenter: nil,
expectedError: fmt.Errorf("can't get current context config: %w", fmt.Errorf("current context can't be empty")),
},
{
name: "not existing current context",
connConfig: &ConnectionConfig{
CurrentContext: "not-existing-context",
},
expectedDatacenter: nil,
expectedError: fmt.Errorf("can't get current context config: %w", fmt.Errorf(`context "not-existing-context" does not exists`)),
},
{
name: "empty datacenter name in current context",
connConfig: &ConnectionConfig{
Contexts: map[string]*Context{
"default": {
DatacenterName: "",
},
},
CurrentContext: "default",
},
expectedDatacenter: nil,
expectedError: fmt.Errorf("datacenterName in current context can't be empty"),
},
{
name: "not existing datacenter name in current context",
connConfig: &ConnectionConfig{
Contexts: map[string]*Context{
"default": {
DatacenterName: "not-existing-dc",
},
},
CurrentContext: "default",
},
expectedDatacenter: nil,
expectedError: fmt.Errorf(`datacenter "not-existing-dc" does not exists`),
},
{
name: "datacenter from current context is returned",
connConfig: &ConnectionConfig{
Datacenters: map[string]*Datacenter{
"us-east-1": {
CertificateAuthorityPath: "path-to-ca-cert",
Server: "server",
TLSServerName: "tls-server-name",
NodeDomain: "node-domain",
InsecureSkipTLSVerify: true,
ProxyURL: "proxy-url",
},
},
Contexts: map[string]*Context{
"default": {
DatacenterName: "us-east-1",
},
},
CurrentContext: "default",
},
expectedDatacenter: &Datacenter{
CertificateAuthorityPath: "path-to-ca-cert",
Server: "server",
TLSServerName: "tls-server-name",
NodeDomain: "node-domain",
InsecureSkipTLSVerify: true,
ProxyURL: "proxy-url",
},
expectedError: nil,
},
}

for i := range tt {
tc := tt[i]
t.Run(tc.name, func(t *testing.T) {
dc, err := tc.connConfig.GetCurrentDatacenterConfig()
if !reflect.DeepEqual(tc.expectedError, err) {
t.Errorf("expected error %q, got %q", tc.expectedError, err)
}
if !reflect.DeepEqual(tc.expectedDatacenter, dc) {
t.Errorf("expected datacenter %v, got %v", tc.expectedDatacenter, dc)
}
})
}
}

func writeCloudConnectionConfigToTemp(t *testing.T, cc *ConnectionConfig) (*ConnectionConfig, string) {
f, err := os.CreateTemp(os.TempDir(), "gocql-cloud-conn-config-")
if err != nil {
Expand Down
18 changes: 14 additions & 4 deletions scyllacloud/hostdialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func NewSniHostDialer(connConfig *ConnectionConfig, dialer gocql.Dialer) *SniHos
func (s *SniHostDialer) DialHost(ctx context.Context, host *gocql.HostInfo) (*gocql.DialedHost, error) {
hostID := host.HostID()
if len(hostID) == 0 {
return s.dialInitialContactPoint(ctx, host)
return s.dialInitialContactPoint(ctx)
}

dcName := host.DataCenter()
Expand Down Expand Up @@ -77,7 +77,7 @@ func (s *SniHostDialer) DialHost(ctx context.Context, host *gocql.HostInfo) (*go
})
}

func (s *SniHostDialer) dialInitialContactPoint(ctx context.Context, host *gocql.HostInfo) (*gocql.DialedHost, error) {
func (s *SniHostDialer) dialInitialContactPoint(ctx context.Context) (*gocql.DialedHost, error) {
insecureSkipVerify := false
for _, dc := range s.connConfig.Datacenters {
if dc.InsecureSkipTLSVerify {
Expand All @@ -96,8 +96,18 @@ func (s *SniHostDialer) dialInitialContactPoint(ctx context.Context, host *gocql
return nil, fmt.Errorf("can't get root CA from configuration: %w", err)
}

return s.connect(ctx, s.dialer, host.HostnameAndPort(), &tls.Config{
ServerName: host.Hostname(),
dcConf, err := s.connConfig.GetCurrentDatacenterConfig()
if err != nil {
return nil, fmt.Errorf("can't get current datacenter config: %w", err)
}

serverName := dcConf.NodeDomain
if len(serverName) == 0 {
serverName = dcConf.Server
}

return s.connect(ctx, s.dialer, dcConf.Server, &tls.Config{
ServerName: serverName,
RootCAs: ca,
InsecureSkipVerify: insecureSkipVerify,
Certificates: []tls.Certificate{*clientCertificate},
Expand Down
Loading

0 comments on commit 64bad46

Please sign in to comment.