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

Use ConnectionConfig values when establishing session to initial contact point #106

Merged
merged 1 commit into from
Nov 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
}
58 changes: 55 additions & 3 deletions scyllacloud/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,17 +170,69 @@ func (cc *ConnectionConfig) getDataOrReadFile(data []byte, path string) ([]byte,
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
244 changes: 243 additions & 1 deletion scyllacloud/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func TestCloudCluster(t *testing.T) {

cloudConfig, err := NewCloudCluster(path)
if !reflect.DeepEqual(test.expectedError, err) {
t.Errorf("expected error %q, got %q", test.expectedError, err)
t.Errorf("expected error %#v, got %#v", test.expectedError, err)
}
if test.verifyClusterConfig != nil {
test.verifyClusterConfig(t, cc, cloudConfig)
Expand All @@ -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 %#v, got %#v", 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 %#v, got %#v", 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 %#v, got %#v", 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