Skip to content

Commit 21016f0

Browse files
Karthikeyan Vasuki BalasubramaniamAWS
authored andcommitted
[Core] Fix AWSKeyValueStore for stringSet operations and propagate exceptions when encryption key cannot be loaded/created
1 parent 56e4bc0 commit 21016f0

File tree

6 files changed

+42
-9
lines changed

6 files changed

+42
-9
lines changed

aws-android-sdk-core-test/src/androidTest/java/com/amazonaws/internal/keyvaluestore/AWSKeyValueStoreMigrationIntegrationTest.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,29 @@
3636
import com.amazonaws.regions.Regions;
3737

3838
import java.util.ArrayList;
39+
import java.util.Arrays;
40+
import java.util.HashSet;
41+
import java.util.Set;
3942

4043

4144
@RunWith(AndroidJUnit4.class)
4245
public class AWSKeyValueStoreMigrationIntegrationTest extends CoreIntegrationTestBase {
4346

4447
private static String TAG = CognitoCachingCredentialsProviderIntegrationTest.class.getSimpleName();
48+
private static SharedPreferences sharedPreferencesForAuth;
49+
private static final String SHARED_PREFERENCES_NAME = "com.amazonaws.android.auth";
4550

4651
private ArrayList<CognitoCachingCredentialsProvider> credentialsProviders =new ArrayList<CognitoCachingCredentialsProvider>();
4752
private CognitoCachingCredentialsProvider credentialsProvider;
4853

49-
private static SharedPreferences sharedPreferencesForAuth;
5054
private String identityPoolId;
5155
private long time;
56+
private Set<String> stringSet;
5257

5358
@BeforeClass
5459
public static void setupBeforeClass() {
5560
sharedPreferencesForAuth = InstrumentationRegistry.getTargetContext()
56-
.getSharedPreferences("com.amazonaws.android.auth", Context.MODE_PRIVATE);
61+
.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
5762
}
5863

5964
@AfterClass
@@ -68,12 +73,14 @@ public static void tearDownAfterClass() {
6873
public void setUp() throws Exception {
6974
time = System.currentTimeMillis();
7075
identityPoolId = getPackageConfigure().getString("identity_pool_id");
76+
stringSet = new HashSet<>(Arrays.asList("openid", "profile", "email"));
7177
sharedPreferencesForAuth.edit()
7278
.putString(identityPoolId + ".accessKey" , "accessKey")
7379
.putString(identityPoolId + ".secretKey" , "secretKey")
7480
.putString(identityPoolId + ".sessionToken" , "sessionToken")
7581
.putString(identityPoolId + ".identityId" , "identityId")
7682
.putLong(identityPoolId + ".expirationDate" , time)
83+
.putStringSet("stringSet", stringSet)
7784
.commit();
7885
}
7986

@@ -89,14 +96,15 @@ public void tearDown() {
8996

9097
@Test
9198
public void testCachedAWSCredentialsMigration() throws Exception {
92-
Log.d(TAG, "SharedPreferences contents before migration for com.amazonaws.android.auth => " +
99+
Log.d(TAG, "SharedPreferences contents before migration for " + SHARED_PREFERENCES_NAME + "=> " +
93100
sharedPreferencesForAuth.getAll().toString());
94101

95102
assertEquals("accessKey", sharedPreferencesForAuth.getString(identityPoolId + ".accessKey", null));
96103
assertEquals("secretKey", sharedPreferencesForAuth.getString(identityPoolId + ".secretKey", null));
97104
assertEquals("sessionToken", sharedPreferencesForAuth.getString(identityPoolId + ".sessionToken", null));
98105
assertEquals("identityId", sharedPreferencesForAuth.getString(identityPoolId + ".identityId", null));
99106
assertEquals(time, sharedPreferencesForAuth.getLong(identityPoolId + ".expirationDate", 0));
107+
assertEquals(stringSet, sharedPreferencesForAuth.getStringSet("stringSet", null));
100108

101109
credentialsProvider = new CognitoCachingCredentialsProvider(
102110
InstrumentationRegistry.getTargetContext(),
@@ -105,17 +113,20 @@ public void testCachedAWSCredentialsMigration() throws Exception {
105113
credentialsProviders.add(credentialsProvider);
106114

107115
AWSKeyValueStore awsKeyValueStore = new AWSKeyValueStore(InstrumentationRegistry.getTargetContext(),
108-
"com.amazonaws.android.auth",
116+
SHARED_PREFERENCES_NAME,
109117
true);
118+
assertNotNull(awsKeyValueStore);
110119

111-
Log.d(TAG, "SharedPreferences contents after migration for com.amazonaws.android.auth => " +
120+
Log.d(TAG, "SharedPreferences contents after migration for " + SHARED_PREFERENCES_NAME + " => " +
112121
sharedPreferencesForAuth.getAll().toString());
113122

114123
assertEquals("accessKey", awsKeyValueStore.get(identityPoolId + ".accessKey"));
115124
assertEquals("secretKey", awsKeyValueStore.get(identityPoolId + ".secretKey"));
116125
assertEquals("sessionToken", awsKeyValueStore.get(identityPoolId + ".sessionToken"));
117126
assertEquals("identityId", awsKeyValueStore.get(identityPoolId + ".identityId"));
118127
assertEquals(String.valueOf(time), awsKeyValueStore.get(identityPoolId + ".expirationDate"));
128+
assertNotNull(awsKeyValueStore.get("stringSet"));
129+
verifyStringSet();
119130

120131
credentialsProvider.clearCredentials();
121132
credentialsProvider.clear();
@@ -131,6 +142,13 @@ public void testCachedAWSCredentialsMigration() throws Exception {
131142
verifyCredentialsProviderClear();
132143
}
133144

145+
private void verifyStringSet() {
146+
assertNotNull(sharedPreferencesForAuth.getString("stringSet.encrypted", null));
147+
assertNotNull(sharedPreferencesForAuth.getString("stringSet.encrypted.iv", null));
148+
assertNotNull(sharedPreferencesForAuth.getString("stringSet.encrypted.keyvaluestoreversion", null));
149+
assertNull(sharedPreferencesForAuth.getString("stringSet", null));
150+
}
151+
134152
private void verifySharedPreferencesContents() {
135153
assert sharedPreferencesForAuth.getAll().keySet().size() == credentialsProviders.size() * 5;
136154

aws-android-sdk-core/src/main/java/com/amazonaws/auth/CognitoCachingCredentialsProvider.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,7 @@ private void initialize(Context context) {
431431
registerIdentityChangedListener(listener);
432432
} catch (Exception ex) {
433433
Log.e(TAG, "Error in initializing the CognitoCachingCredentialsProvider. " + ex);
434+
throw new IllegalStateException("Error in initializing the CognitoCachingCredentialsProvider. ", ex);
434435
}
435436
}
436437

aws-android-sdk-core/src/main/java/com/amazonaws/internal/keyvaluestore/AWSKeyValueStore.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import android.content.SharedPreferences;
2020
import android.os.Build;
2121

22+
import com.amazonaws.auth.policy.actions.SecurityTokenServiceActions;
2223
import com.amazonaws.logging.Log;
2324
import com.amazonaws.logging.LogFactory;
2425
import com.amazonaws.util.Base64;
@@ -27,7 +28,9 @@
2728
import java.security.SecureRandom;
2829
import java.security.spec.AlgorithmParameterSpec;
2930
import java.util.HashMap;
31+
import java.util.Iterator;
3032
import java.util.Map;
33+
import java.util.Set;
3134

3235
import javax.crypto.Cipher;
3336
import javax.crypto.spec.GCMParameterSpec;
@@ -315,6 +318,17 @@ private void onMigrateFromNoEncryption() {
315318
} else if (map.get(spKey) instanceof Integer) {
316319
Integer intValue = sharedPreferences.getInt(spKey, 0);
317320
put(spKey, String.valueOf(intValue));
321+
} else if (map.get(spKey) instanceof Set) {
322+
Set<String> stringSet = (Set<String>) map.get(spKey);
323+
StringBuilder stringBuilder = new StringBuilder();
324+
Iterator<String> setIterator = stringSet.iterator();
325+
while (setIterator.hasNext()) {
326+
stringBuilder.append(setIterator.next());
327+
if (setIterator.hasNext()) {
328+
stringBuilder.append(",");
329+
}
330+
}
331+
put(spKey, stringBuilder.toString());
318332
}
319333

320334
// Remove the key since key.encrypted is written.

aws-android-sdk-core/src/main/java/com/amazonaws/internal/keyvaluestore/KeyProvider10.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ public Key getKey(SharedPreferences sharedPreferences, String keyAlias, Context
5858
return secretKey;
5959
}
6060
} catch (Exception ex) {
61-
logger.error("Error occurred while getting the key." + ex);
62-
return null;
61+
logger.error("Error in loading the key from keystore.");
62+
throw new IllegalStateException(ex);
6363
}
6464
}
6565
}

aws-android-sdk-core/src/main/java/com/amazonaws/internal/keyvaluestore/KeyProvider18.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public Key getKey(SharedPreferences sharedPreferences,
9292
}
9393
} catch (Exception ex) {
9494
logger.error("Error in getting the key.", ex);
95-
return null;
95+
throw new IllegalStateException(ex);
9696
}
9797
}
9898
}

aws-android-sdk-core/src/main/java/com/amazonaws/internal/keyvaluestore/KeyProvider23.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public Key getKey(SharedPreferences sharedPreferences,
6262
}
6363
} catch (Exception ex) {
6464
logger.error("Error in accessing the Android KeyStore.", ex);
65-
return null;
65+
throw new IllegalStateException(ex);
6666
}
6767
}
6868
}

0 commit comments

Comments
 (0)