Cant connect to Cassandra from Spring Boot Application

53 Views Asked by At

Currently cracking my head over this error with Cassandra:

Error creating bean with name 'cassandraSession' defined in class path resource [com/project/config/CassandraConfiguration.class]: Invocation of init method failed; nested exception is com.datastax.oss.driver.api.core.AllNodesFailedException: Could not reach any contact point, make sure you've provided valid addresses

I'm able to see this error after deploying and viewing a single pods logs. This issue surfaced after switching from using IAM User static credentials to using IAM User credentials so aside from how I'm handling auth, the configuration is pretty much the same.

@EnableCassandraRepositories
@Configuration
class CassandraConfiguration : AbstractCassandraConfiguration() {

    @Value("\${aws.region}")
    private lateinit var regionText: String

    @Value("aws.iam.user.arn")
    private lateinit var assumeRoleArn: String

    @Value("\${aws.cassandra.host}")
    private lateinit var cassandraHost: String

    @Value("\${AWS_KEYSPACE_NAME:keyspace}")
    private lateinit var keySpaceName: String

    @Value("\${AWS_KEYSPACE_NAME_SUFFIX:}")
    private lateinit var keySpaceNameSuffix: String

    override fun cassandraSession(): CqlSessionFactoryBean {

        val awsCqlSessionFactoryBean = AWSCqlSessionFactoryBean(
            regionText,
            assumeRoleArn,
            cassandraHost,
            keySpaceName + keySpaceNameSuffix
        )
        awsCqlSessionFactoryBean.setKeyspaceName(keySpaceName + keySpaceNameSuffix)
        return awsCqlSessionFactoryBean
    }

    override fun getContactPoints(): String {
        return cassandraHost
    }

    override fun getKeyspaceName(): String {
        return keySpaceName
    }

    override fun getPort(): Int {
        return 9042
    }
}

The Cassandra config is being used by the CQL Bean

class AWSCqlSessionFactoryBean(
    private val regionName: String,
    private val assumeRoleArn: String,
    private val cassandraHost: String,
    private val keySpaceName: String
) : CqlSessionFactoryBean() {
    override fun buildSession(sessionBuilder: CqlSessionBuilder): CqlSession {
        val contactPoints = listOf(
            InetSocketAddress.createUnresolved(cassandraHost, 9042)
        )

        return CqlSession.builder()
            .addContactPoints(contactPoints)
            .withSslContext(SSLContext.getDefault())
            .withLocalDatacenter(regionName)
            .withKeyspace(keySpaceName)
            .withConfigLoader(DriverConfigLoader.fromClasspath("cassandra.conf"))
            .withAuthProvider(
                SigV4AuthProvider(
                    getAWSCredientials(),
                    "region"
                )
            )
            .build()
    }

    override fun buildSystemSession(sessionBuilder: CqlSessionBuilder): CqlSession {
        return buildSession(sessionBuilder)
    }

    fun getAWSCredientials(): AwsCredentialsProvider {
        val region = Region.of(regionName)

        val stsClient: StsClient = StsClient.builder()
            .credentialsProvider(WebIdentityTokenFileCredentialsProvider.create())
            .region(region)
            .build()

        val assumeRoleRequest: AssumeRoleRequest = AssumeRoleRequest.builder()
            .roleArn(assumeRoleArn)
            .roleSessionName("my-service")
            .build()

        return StsAssumeRoleCredentialsProvider.builder()
            .stsClient(stsClient)
            .refreshRequest(Supplier<AssumeRoleRequest> { assumeRoleRequest })
   

Deps

"org.springframework.data:spring-data-cassandra:3.0.1.RELEASE"
"org.springframework.data:spring-data-cassandra:5.2.6.RELEASE"
"software.aws.mcs:aws-sigv4-auth-cassandra-java-driver-plugin:4.0.9"
"io.github.acm19:aws-request-signing-apache-interceptor:2.3.1"
id("org.springframework.boot") version "2.3.0.RELEASE"

I'm at a loss now because it appears that my configurations are all correct but am unsure of how to debug further.

1

There are 1 best solutions below

0
Andrew On

The AWS port for keyspaces is on the SSL 9142 port, but you are referencing 9042.