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

Cognito Unable to cache Credentials #3450

Open
vikrantuk opened this issue Nov 2, 2023 · 8 comments
Open

Cognito Unable to cache Credentials #3450

vikrantuk opened this issue Nov 2, 2023 · 8 comments
Labels
pending-response Issue is pending response from the issue requestor

Comments

@vikrantuk
Copy link

vikrantuk commented Nov 2, 2023

How to enable caching for CognitoCachingCredentialsProvider?

I'm using com.amazonaws:aws-android-sdk-iot

Code snippets:
CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider( appContext, COGNITO_POOL_ID, MY_REGION );
AWSIotMqttManager mqttManager = new AWSIotMqttManager(clientId, CUSTOMER_SPECIFIC_ENDPOINT);
mqttManager.connect(credentialsProvider, callback);

Logs:
2023-11-03 01:30:38.332 24968-27053 AWSKeyValueStore I Detected Android API Level = 29
2023-11-03 01:30:38.332 24968-27053 AWSKeyValueStore I Creating the AWSKeyValueStore with key for sharedPreferencesForData = com.amazonaws.android.auth
2023-11-03 01:30:38.332 24968-27053 CognitoCachingCredentia D Loading credentials from SharedPreferences
2023-11-03 01:30:38.333 24968-27053 CognitoCachingCredentia D No valid credentials found in SharedPreferences
2023-11-03 01:30:38.336 24968-27054 AWSIotMqttManager D MQTT broker: a1sfow6qlhnw5d-ats.iot.ap-south-1.amazonaws.com:443
2023-11-03 01:30:38.357 24968-27054 AWSIotMqttManager D ready to do mqtt connect
2023-11-03 01:30:38.357 24968-27054 AWSIotMqttManager I metrics collection is enabled, username: ?SDK=Android&Version=2.22.6
2023-11-03 01:30:38.357 24968-27054 AWSIotMqttManager I resetting reconnect attempt and retry time
2023-11-03 01:30:38.357 24968-27054 AWSIotMqttManager D Setting up Callback for MqttClient
2023-11-03 01:30:38.357 24968-27054 MQTT D Status = Connecting
2023-11-03 01:30:38.408 24968-24968 System.out I Setting reference for amount text
2023-11-03 01:30:38.408 24968-24968 System.out I Setting reference for ui messages.
2023-11-03 01:30:38.869 24968-27061 AWSIotMqttManager I onSuccess: mqtt connection is successful.
2023-11-03 01:30:38.869 24968-27061 MQTT D Status = Connected

Environment:

  • SDK Version: 2.73.0

Device Information:

  • Device: Android
  • Android Version: 10
@ankpshah ankpshah assigned ankpshah and unassigned ankpshah Nov 3, 2023
@ankpshah ankpshah added the pending-triage Issue is pending triage label Nov 3, 2023
@tylerjroach
Copy link
Contributor

Are you only using aws-sdk-android, or also using Amplify?

If you are using MobileClient for Auth, you would should pass the MobileClient instance directly into the mqtt manager.
Ex: mqttManager.connect(AWSMobileClient.getInstance()....

@vikrantuk
Copy link
Author

bileClient instance directly into the mqtt m

I'm using aws-sdk-android only

@ankpshah
Copy link
Contributor

ankpshah commented Nov 5, 2023

As mentioned by Tyler you need to use: mqttManager.connect(AWSMobileClient.getInstance()....
More Details here
Let us know if you are still facing the issue

@ankpshah ankpshah added the pending-response Issue is pending response from the issue requestor label Nov 5, 2023
@vikrantuk
Copy link
Author

As mentioned by Tyler you need to use: mqttManager.connect(AWSMobileClient.getInstance().... More Details here Let us know if you are still facing the issue

Why is it necessary to use AWSMobileClient? Why to add one more dependency just for the sake of getting caching working?

@tylerjroach
Copy link
Contributor

If you already have a method of fetching AWS Credentials, you may want to just implement your own Credentials Provider with a cache (aws-amplify/amplify-android#2400 (comment)).

As far as the CognitoCachingCredentialsProvider not working as expecting for you, please enable logging on that file to help debug what may be working incorrectly. You can post the log here, but be careful to omit any credentials.

LogFactory.getLog(CognitoCachingCredentialsProvider.class).setLevel(LogFactory.Level.ALL);

@vikrantuk
Copy link
Author

If you already have a method of fetching AWS Credentials, you may want to just implement your own Credentials Provider with a cache (aws-amplify/amplify-android#2400 (comment)).

As far as the CognitoCachingCredentialsProvider not working as expecting for you, please enable logging on that file to help debug what may be working incorrectly. You can post the log here, but be careful to omit any credentials.

LogFactory.getLog(CognitoCachingCredentialsProvider.class).setLevel(LogFactory.Level.ALL);

Hi @tylerjroach thank you for understanding my concern. Pasting logs below:

2023-11-08 12:44:33.835 9994-10981 LogFactory W Truncating log tag length as it exceed 23, the limit imposed by Android on certain API Levels
2023-11-08 12:44:33.857 9994-10981 AWSKeyValueStore I Detected Android API Level = 29
2023-11-08 12:44:33.857 9994-10981 AWSKeyValueStore I Creating the AWSKeyValueStore with key for sharedPreferencesForData = com.amazonaws.android.auth
2023-11-08 12:44:33.858 9994-10981 CognitoCachingCredentia D Loading credentials from SharedPreferences
2023-11-08 12:44:33.858 9994-10981 CognitoCachingCredentia D No valid credentials found in SharedPreferences
2023-11-08 12:44:33.862 9994-10982 AWSIotMqttManager D MQTT broker: **************-***.iot.ap-***.amazonaws.com:443
2023-11-08 12:44:33.900 9994-10982 AWSIotMqttManager D ready to do mqtt connect
2023-11-08 12:44:33.901 9994-10982 AWSIotMqttManager I metrics collection is enabled, username: ?SDK=Android&Version=2.22.6
2023-11-08 12:44:33.901 9994-10982 AWSIotMqttManager I resetting reconnect attempt and retry time
2023-11-08 12:44:33.901 9994-10982 AWSIotMqttManager D Setting up Callback for MqttClient
2023-11-08 12:44:33.901 9994-10982 MQTT D Status = Connecting
2023-11-08 12:44:34.456 9994-10989 AWSIotMqttManager I onSuccess: mqtt connection is successful.
2023-11-08 12:44:34.456 9994-10989 MQTT D Status = Connected
2023-11-08 12:44:34.456 9994-10989 System.out I MQTT Connected

@tylerjroach
Copy link
Contributor

tylerjroach commented Nov 8, 2023

What I am not seeing in those logs is :

We should see this log when getCredentials is called to fetch the credentials.

I'm also not seeing:

This is what would save the fetched credentials.

Can you try creating your CognitoCachingCredentialsProvider and then manually calling getCredentials to see if either of these show in the logs for you? If you know how to set breakpoints inside CognitoCachingCredentialsProvider, it would also be helpful to follow the path being taken when getCredentials is called.

@vikrantuk
Copy link
Author

Hi @tylerjroach , I tried running this code :
LogFactory.getLog(CognitoCachingCredentialsProvider.class).setLevel(LogFactory.Level.ALL);
credentialsProvider = new CognitoCachingCredentialsProvider( appContext, COGNITO_POOL_ID, MY_REGION );
this.context = appContext;
Log.d("MQTT", "Connection: before calling getCredentials");
credentialsProvider.getCredentials();
Log.d("MQTT", "Connection: after calling getCredentials");

But I'm not able to see logs you are requesting.
Pasting the logs I'm able to access.

2023-11-16 21:56:31.614 30024-30253 LogFactory W Truncating log tag length as it exceed 23, the limit imposed by Android on certain API Levels
2023-11-16 21:56:31.628 30024-30253 chatty I uid=10114(packagename) Thread-5 identical 1 line
2023-11-16 21:56:31.630 30024-30253 LogFactory W Truncating log tag length as it exceed 23, the limit imposed by Android on certain API Levels
2023-11-16 21:56:31.755 30024-30253 AWSKeyValueStore I Detected Android API Level = 29
2023-11-16 21:56:31.756 30024-30253 AWSKeyValueStore I Creating the AWSKeyValueStore with key for sharedPreferencesForData = com.amazonaws.android.auth
2023-11-16 21:56:31.763 30024-30253 KeyProvider23 D AndroidKeyStore contains keyAlias com.amazonaws.android.auth.aesKeyStoreAlias
2023-11-16 21:56:31.763 30024-30253 KeyProvider23 D Loading the encryption key from Android KeyStore.
2023-11-16 21:56:31.801 30024-30253 CognitoCachingCredentia D Loading credentials from SharedPreferences
2023-11-16 21:56:31.803 30024-30253 KeyProvider23 D AndroidKeyStore contains keyAlias com.amazonaws.android.auth.aesKeyStoreAlias
2023-11-16 21:56:31.804 30024-30253 KeyProvider23 D Loading the encryption key from Android KeyStore.
2023-11-16 21:56:31.826 30024-30253 CognitoCachingCredentia D No valid credentials found in SharedPreferences
2023-11-16 21:56:31.828 30024-30253 KeyProvider23 D AndroidKeyStore contains keyAlias com.amazonaws.android.auth.aesKeyStoreAlias
2023-11-16 21:56:31.828 30024-30253 KeyProvider23 D Loading the encryption key from Android KeyStore.
2023-11-16 21:56:31.847 30024-30253 KeyProvider23 D AndroidKeyStore contains keyAlias com.amazonaws.android.auth.aesKeyStoreAlias
2023-11-16 21:56:31.847 30024-30253 KeyProvider23 D Loading the encryption key from Android KeyStore.
2023-11-16 21:56:31.866 30024-30253 KeyProvider23 D AndroidKeyStore contains keyAlias com.amazonaws.android.auth.aesKeyStoreAlias
2023-11-16 21:56:31.866 30024-30253 KeyProvider23 D Loading the encryption key from Android KeyStore.
2023-11-16 21:56:31.890 30024-30253 MQTT D Connection: before calling getCredentials
2023-11-16 21:56:31.890 30024-30253 MQTT D Connection: after calling getCredentials

@joon-won joon-won removed the pending-triage Issue is pending triage label Mar 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pending-response Issue is pending response from the issue requestor
Projects
None yet
Development

No branches or pull requests

4 participants