1

I have been trying to connect to cosmos cassandra db using gocql.

func GetSession(cosmosCassandraContactPoint, cosmosCassandraPort, cosmosCassandraUser, cosmosCassandraPassword string) *gocql.Session {
    clusterConfig := gocql.NewCluster(cosmosCassandraContactPoint)
    port, err := strconv.Atoi(cosmosCassandraPort)
    if err != nil {
        log.Fatal(err)
    }
    clusterConfig.Port = port
    clusterConfig.ProtoVersion = 4
    clusterConfig.Authenticator = gocql.PasswordAuthenticator{Username: cosmosCassandraUser, Password: cosmosCassandraPassword}
    clusterConfig.SslOpts = &gocql.SslOptions{Config: &tls.Config{MinVersion: tls.VersionTLS12}}

    clusterConfig.ConnectTimeout = 10 * time.Second
    clusterConfig.Timeout = 10 * time.Second
    clusterConfig.DisableInitialHostLookup = true

    // uncomment if you want to track time taken for individual queries
    //clusterConfig.QueryObserver = timer{}

    // uncomment if you want to track time taken for each connection to Cassandra
    //clusterConfig.ConnectObserver = timer{}

    session, err := clusterConfig.CreateSession()
    if err != nil {
        log.Fatal("Failed to connect to Azure Cosmos DB", err)
    }

    return session
}

I have been getting the following error :

unable to create session: control: unable to connect to initial hosts: Invalid Cosmos DB account or key

Not sure what the issue here is.

2 Answers2

1

It doesn't look like you've configured the necessary options for the SSL/TLS configuration, particularly the certificates.

I haven't connected to a Cosmos DB before so I'm not sure of the certs/keys required but I previously helped someone configure the gocql driver with the right TLS settings in this post -- https://community.datastax.com/questions/3753/.

In their environment, they needed to provide the certs and keys to connect as follows:

certPath, _ := filepath.Abs("/home/erick/astra-bundle/cert")
keyPath, _ := filepath.Abs("/home/erick/astra-bundle/key")
caPath, _ := filepath.Abs("/home/erick/astra-bundle/ca.crt")
cert, _ := tls.LoadX509KeyPair(certPath, keyPath)
caCert, _  := ioutil.ReadFile(caPath)
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
tlsConfig := &tls.Config{
    Certificates: []tls.Certificate{cert},
    RootCAs:      caCertPool,
}
cluster.SslOpts = &gocql.SslOptions{
    Config: tlsConfig,
    EnableHostVerification: false,
}

Details are in the post above. I hope this helps. Cheers!

Erick Ramirez
  • 13,964
  • 1
  • 18
  • 23
0

It seems your account or key is wrong.

First,please make sure your API is CASSANDRA API.You can check at here. enter image description here

Second,please make sure your account or key is right.

COSMOSDB_CASSANDRA_CONTACT_POINT=<value for "CONTACT POINT">
COSMOSDB_CASSANDRA_PORT=<value for "PORT">
COSMOSDB_CASSANDRA_USER=<value for "USERNAME">
COSMOSDB_CASSANDRA_PASSWORD=<value for "PRIMARY PASSWORD">

You can find them here: enter image description here

More details,you can refer to this documentation. Hope this can help you.

Steve Johnson
  • 8,057
  • 1
  • 6
  • 17