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

Location Credentials Provider is failing #1411

Open
1 task done
drayan85 opened this issue Sep 19, 2024 · 7 comments
Open
1 task done

Location Credentials Provider is failing #1411

drayan85 opened this issue Sep 19, 2024 · 7 comments
Labels
bug This issue is a bug. potential-regression Marking this issue as a potential regression to be checked by team member

Comments

@drayan85
Copy link

drayan85 commented Sep 19, 2024

Describe the bug

We are using Cognito pool ID in our Android Project to use the AWS location service.

When we try to create the LocationCredentialProvider (AuthHelper(context).authenticateWithCognitoIdentityPool(xxxx)) it is failing after 1.2.39 -> 1.3.34 with the following error:
java.lang.NoClassDefFoundError: aws.sdk.kotlin.services.cognitoidentity.endpoints.internal.PartitionsKt

Regression Issue

  • Select this option if this issue appears to be a regression.

Expected behavior

It should successfully return the LocationCredentialsProvider object

Current behavior

Throwing Exception:

java.lang.NoSuchMethodError: No direct method <init>(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Boolean;Ljava/lang/Boolean;ILkotlin/jvm/internal/DefaultConstructorMarker;)
V in class Laws/sdk/kotlin/runtime/endpoint/functions/PartitionConfig; or its super classes (declaration of 'aws.sdk.kotlin.runtime.endpoint.functions.PartitionConfig' 
appears in /data/app/~~S8m3JjDNTe7asB6VD3qQwA==/com.android-aws-location-A2tHD-m82ib5570DnPQ5Eg==/base.apk!classes20.dex)

Steps to Reproduce

Location demo app was working as expected when aws.sdk.kotlin:location => 1.2.38 and just updating this to 1.2.39 in the gradle configuration started get this exception.
FYI, We are not forcing the OKHTTP3 library to downgrade (Some other issue mentioning that this exception is occurred when they force downgrade to OkHttp3 to 4.x verion )

Possible Solution

Currently we are downgraded the aws.sdk.kotlin:location to 1.2.38 and working as expected.

Exception started inside the LocationCredentialsProvider -> val getIdResponse = cognitoIdentityClient?.getId(GetIdRequest { this.identityPoolId = identityPoolId })

    private suspend fun generateCredentials(region: String, identityPoolId: String) {
        if (cognitoIdentityClient == null) {
            cognitoIdentityClient = generateCognitoIdentityClient(region)
        }
        try {
            val getIdResponse = cognitoIdentityClient?.getId(GetIdRequest { this.identityPoolId = identityPoolId })
            val identityId =
                getIdResponse?.identityId ?: throw Exception("Failed to get identity ID")
            if (identityId.isNotEmpty()) {
                val getCredentialsResponse =
                    cognitoIdentityClient?.getCredentialsForIdentity(GetCredentialsForIdentityRequest {
                        this.identityId = identityId
                    })

                val credentials = getCredentialsResponse?.credentials
                    ?: throw Exception("Failed to get credentials")
                if (credentials.accessKeyId == null || credentials.secretKey == null || credentials.sessionToken == null) throw Exception(
                    "Credentials generation failed"
                )
                cognitoCredentialsProvider = CognitoCredentialsProvider(
                    context,
                    identityId,
                    credentials
                )
                locationClient = null
            }
        } catch (e: Exception) {
            throw Exception("Credentials generation failed")
        }
    }

Context

We are far away from the latest of the library in our production app and also concern about using not stable OkHttp3 (5.0.0-alpha.14) library which will override by the AWS library (transient dependency) in our production app

AWS SDK for Kotlin version

1.2.38 - 1.3.34

Platform (JVM/JS/Native)

JVM (Kotlin)

Operating system and version

Android 13

@drayan85 drayan85 added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Sep 19, 2024
@github-actions github-actions bot added the potential-regression Marking this issue as a potential regression to be checked by team member label Sep 19, 2024
@0marperez
Copy link
Contributor

0marperez commented Sep 23, 2024

Hi, thanks for the report. It looks like your issue might be caused by a versioning conflict in these two versions of the SDK that are not compatible (1.2.39 & 1.2.21). Versions labelled 1.2.x should be compatible with each other but it seems like there might be an issue here. We'll be working on a fix.

In regards to your concern with using an alpha version of OkHttp 3 we have created an http engine for this that uses OkHttp 4.x. You can look at the readme here

@drayan85
Copy link
Author

drayan85 commented Sep 24, 2024

But We could not find a way to add the okHttp4Engine in the android location library to enforce the OkHttp Engine.

Could you please guide us where we can find documentation or sample code snippet that we can use ?

@0marperez
Copy link
Contributor

Sure, here's an example of how you could use it

Your build.gradle file:

dependencies {
    implementation("aws.sdk.kotlin:location:$SDK_VERSION") // and any other AWS SDK clients... 
    implementation("aws.smithy.kotlin:http-client-engine-okhttp4:$SMITHY_KOTLIN_VERSION") // depend on OkHttp4Engine
}

configurations.all {
    resolutionStrategy {
        // Force resolve to OkHttp 4.x
        force("com.squareup.okhttp3:okhttp:4.12.0") // or whichever version you are using... 
    }
    exclude(group = "com.squareup.okhttp3", module = "okhttp-coroutines") // Exclude dependency on okhttp-coroutines, which is introduced in 5.0.0-alpha.X 
}

Your code:

import aws.sdk.kotlin.services.location.LocationClient
import aws.smithy.kotlin.runtime.http.engine.okhttp4.OkHttp4Engine
OkHttp4Engine().use { okHttp4Engine ->
    LocationClient {
        httpClient = okHttp4Engine
    }.use {
        // Your operation `it.listKeys {}` for example
    }
}

@0marperez 0marperez removed the needs-triage This issue or PR still needs to be triaged. label Sep 24, 2024
@drayan85
Copy link
Author

In our case we are using AWS Location auth library as well.

dependencies {
    implementation ("software.amazon.location:auth:$AUTH_VERSION")
    implementation("aws.sdk.kotlin:location:$SDK_VERSION") // and any other AWS SDK clients... 
    implementation("aws.smithy.kotlin:http-client-engine-okhttp4:$SMITHY_KOTLIN_VERSION") // depend on OkHttp4Engine
}

configurations.all {
    resolutionStrategy {
        // Force resolve to OkHttp 4.x
        force("com.squareup.okhttp3:okhttp:4.12.0") // or whichever version you are using... 
    }
    exclude(group = "com.squareup.okhttp3", module = "okhttp-coroutines") // Exclude dependency on okhttp-coroutines, which is introduced in 5.0.0-alpha.X 
}

In our Code:

val locationCredentialsProvider: LocationCredentialsProvider = 
                       AuthHelper(context).authenticateWithCognitoIdentityPool("xxxxxxxxxx")
val locationClient: LocationClient = locationCredentialsProvider.getLocationClient()
val request = SearchPlaceIndexForSuggestionsRequest {
      text = "sydney"
      indexName = "xxxxx"
}
val response = locationClient.searchPlaceIndexForSuggestions(request)

Since we are get the authorised client via LocationCredentialProvider, How do I initiate the authorisation using our dedicated Cognito Pool ID and Places Index if I received the Location client as you suggested follow:

OkHttp4Engine().use { okHttp4Engine ->
    LocationClient {
        httpClient = okHttp4Engine
    }.use { locationClient ->
       val request = SearchPlaceIndexForSuggestionsRequest {
          text = "sydney"
          indexName = "xxxxx"
        }
        val response = locationClient.searchPlaceIndexForSuggestions(request)
    }
}

@drayan85
Copy link
Author

drayan85 commented Oct 3, 2024

Any update on forcing the OkHttp4Engine, LocationClient with Cognito Pool ID authorisation ?

@lauzadis
Copy link
Member

lauzadis commented Oct 3, 2024

You will want to override the client configuration of the LocationClient returned from locationCredentialsProvider.getLocationClient() using withConfig { ... }. See our developer guide for details: https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/override-client-config.html

It will look something like this:

val locationCredentialsProvider: LocationCredentialsProvider = AuthHelper(context).authenticateWithCognitoIdentityPool("xxxxxxxxxx")

val locationClient: LocationClient = locationCredentialsProvider.getLocationClient()

val okHttp4Engine = OkHttp4Engine()

val overriddenLocationClient = locationClient.withConfig {
    httpClient = okHttp4Engine
}

val request = SearchPlaceIndexForSuggestionsRequest {
      text = "sydney"
      indexName = "xxxxx"
}

val response = locationClient.searchPlaceIndexForSuggestions(request)

// Make sure to close all clients and engines when you are done using them
overriddenLocationClient.close()
okHttp4Engine.close()
locationClient.close()

@drayan85
Copy link
Author

drayan85 commented Oct 4, 2024

Thanks @lauzadis for the info.

But still we are stuck with the following error:
at aws.sdk.kotlin.services.cognitoidentity.endpoints.internal.PartitionsKt.<clinit>(Partitions.kt:18)

Hopefully when #1415 get merge we should be able to upgrade aws.sdk.kotlin:location to the latest version

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. potential-regression Marking this issue as a potential regression to be checked by team member
Projects
None yet
Development

No branches or pull requests

3 participants