-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generic Storage interface for use on all client and on the server.
Signed-off-by: Peter Sorotokin <[email protected]>
- Loading branch information
Showing
26 changed files
with
2,501 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
...rc/androidInstrumentedTest/kotlin/com/android/identity/storage/testStorageList.android.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.android.identity.storage | ||
|
||
import android.app.Instrumentation | ||
import androidx.test.platform.app.InstrumentationRegistry | ||
import com.android.identity.storage.android.AndroidStorage | ||
import com.android.identity.storage.ephemeral.EphemeralStorage | ||
import kotlinx.datetime.Clock | ||
import java.io.File | ||
|
||
/** | ||
* Creates a list of empty [Storage] objects for testing. | ||
*/ | ||
actual fun createTransientStorageList(testClock: Clock): List<Storage> { | ||
return listOf<Storage>( | ||
EphemeralStorage(testClock), | ||
/* | ||
TODO: this can be enabled once SqliteStorage is moved into commonMain | ||
com.android.identity.storage.sqlite.SqliteStorage( | ||
connection = AndroidSQLiteDriver().open(":memory:"), | ||
clock = testClock | ||
), | ||
com.android.identity.storage.sqlite.SqliteStorage( | ||
connection = BundledSQLiteDriver().open(":memory:"), | ||
clock = testClock, | ||
// bundled sqlite crashes when used with Dispatchers.IO | ||
coroutineContext = newSingleThreadContext("DB") | ||
), | ||
*/ | ||
AndroidStorage( | ||
databasePath = null, | ||
clock = testClock, | ||
keySize = 3 | ||
) | ||
) | ||
} | ||
|
||
val knownNames = mutableSetOf<String>() | ||
|
||
actual fun createPersistentStorage(name: String, testClock: Clock): Storage? { | ||
val context = InstrumentationRegistry.getInstrumentation().context | ||
val dbFile = context.getDatabasePath("$name.db") | ||
if (knownNames.add(name)) { | ||
dbFile.delete() | ||
} | ||
return AndroidStorage( | ||
databasePath = dbFile.absolutePath, | ||
clock = testClock, | ||
keySize = 3 | ||
) | ||
} |
64 changes: 64 additions & 0 deletions
64
identity/src/androidMain/kotlin/com/android/identity/storage/android/AndroidStorage.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.android.identity.storage.android | ||
|
||
import android.database.sqlite.SQLiteDatabase | ||
import com.android.identity.storage.Storage | ||
import com.android.identity.storage.base.BaseStorage | ||
import com.android.identity.storage.base.BaseStorageTable | ||
import com.android.identity.storage.StorageTableSpec | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.async | ||
import kotlinx.datetime.Clock | ||
import kotlin.coroutines.CoroutineContext | ||
|
||
/** | ||
* [Storage] implementation based on Android [SQLiteDatabase] API. | ||
*/ | ||
class AndroidStorage: BaseStorage { | ||
private val coroutineContext: CoroutineContext | ||
private val databaseFactory: () -> SQLiteDatabase | ||
internal val keySize: Int | ||
private var database: SQLiteDatabase? = null | ||
|
||
constructor( | ||
database: SQLiteDatabase, | ||
clock: Clock, | ||
coroutineContext: CoroutineContext = Dispatchers.IO, | ||
keySize: Int = 9 | ||
): super(clock) { | ||
this.database = database | ||
databaseFactory = { throw IllegalStateException("unexpected call") } | ||
this.coroutineContext = coroutineContext | ||
this.keySize = keySize | ||
} | ||
|
||
constructor( | ||
databasePath: String?, | ||
clock: Clock, | ||
coroutineContext: CoroutineContext = Dispatchers.IO, | ||
keySize: Int = 9 | ||
): super(clock) { | ||
databaseFactory = { | ||
SQLiteDatabase.openOrCreateDatabase(databasePath ?: ":memory:", null) | ||
} | ||
this.coroutineContext = coroutineContext | ||
this.keySize = keySize | ||
} | ||
|
||
override suspend fun createTable(tableSpec: StorageTableSpec): BaseStorageTable { | ||
if (database == null) { | ||
database = databaseFactory() | ||
} | ||
val table = AndroidStorageTable(this, tableSpec) | ||
table.init() | ||
return table | ||
} | ||
|
||
internal suspend fun<T> withDatabase( | ||
block: suspend CoroutineScope.(database: SQLiteDatabase) -> T | ||
): T { | ||
return CoroutineScope(coroutineContext).async { | ||
block(database!!) | ||
}.await() | ||
} | ||
} |
205 changes: 205 additions & 0 deletions
205
identity/src/androidMain/kotlin/com/android/identity/storage/android/AndroidStorageTable.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,205 @@ | ||
package com.android.identity.storage.android | ||
|
||
import android.content.ContentValues | ||
import android.database.AbstractWindowedCursor | ||
import android.database.CursorWindow | ||
import android.os.Build | ||
import com.android.identity.storage.KeyExistsStorageException | ||
import com.android.identity.storage.NoRecordStorageException | ||
import com.android.identity.storage.base.BaseStorageTable | ||
import com.android.identity.storage.StorageTableSpec | ||
import com.android.identity.storage.base.SqlStatementMaker | ||
import com.android.identity.util.toBase64Url | ||
import kotlinx.datetime.Instant | ||
import kotlinx.io.bytestring.ByteString | ||
import kotlin.random.Random | ||
|
||
internal class AndroidStorageTable( | ||
private val owner: AndroidStorage, | ||
spec: StorageTableSpec | ||
): BaseStorageTable(spec) { | ||
private val sql = SqlStatementMaker( | ||
spec, | ||
textType = "TEXT", | ||
blobType = "BLOB", | ||
longType = "INTEGER", | ||
useReturningClause = false, | ||
collationCharset = null | ||
) | ||
|
||
suspend fun init() { | ||
owner.withDatabase { database -> | ||
database.execSQL(sql.createTableStatement) | ||
} | ||
} | ||
|
||
override suspend fun get(key: String, partitionId: String?): ByteString? { | ||
checkPartition(partitionId) | ||
return owner.withDatabase { database -> | ||
val cursor = database.query( | ||
sql.tableName, | ||
arrayOf("data"), | ||
sql.conditionWithExpiration(owner.clock.now().epochSeconds), | ||
whereArgs(key, partitionId), | ||
null, | ||
null, | ||
null | ||
) | ||
// TODO: Older OS versions don't support setting the cursor window size. | ||
// What should we do with older OS versions? | ||
// Also note that a large window size may lead to longer delays when loading from the | ||
// database. And if we keep this, replace the magic number with a constant. | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { | ||
// The default window size of 2MB is too small for video files. | ||
(cursor as? AbstractWindowedCursor)?.window = CursorWindow( | ||
"Larger Window", 256 * 1024 * 1024) | ||
} | ||
if (cursor.moveToFirst()) { | ||
val bytes = cursor.getBlob(0) | ||
cursor.close() | ||
ByteString(bytes) | ||
} else { | ||
cursor.close() | ||
null | ||
} | ||
} | ||
} | ||
|
||
override suspend fun insert( | ||
key: String?, | ||
data: ByteString, | ||
partitionId: String?, | ||
expiration: Instant | ||
): String { | ||
if (key != null) { | ||
checkKey(key) | ||
} | ||
checkPartition(partitionId) | ||
checkExpiration(expiration) | ||
return owner.withDatabase { database -> | ||
if (key != null && spec.supportExpiration) { | ||
// if there is an entry with this key, but it is expired, it needs to be purged. | ||
// Purging expired keys does not interfere with operation atomicity | ||
database.delete( | ||
sql.tableName, | ||
sql.purgeExpiredWithIdCondition(owner.clock.now().epochSeconds), | ||
whereArgs(key, partitionId) | ||
) | ||
} | ||
var newKey: String | ||
var done = false | ||
do { | ||
newKey = key ?: Random.nextBytes(owner.keySize).toBase64Url() | ||
val values = ContentValues().apply { | ||
put("id", newKey) | ||
if (spec.supportPartitions) { | ||
put("partitionId", partitionId) | ||
} | ||
if (spec.supportExpiration) { | ||
put("expiration", expiration.epochSeconds) | ||
} | ||
put("data", data.toByteArray()) | ||
} | ||
val rowId = database.insert(sql.tableName, null, values) | ||
if (rowId >= 0) { | ||
done = true | ||
} else if (key != null) { | ||
throw KeyExistsStorageException( | ||
"Record with ${recordDescription(key, partitionId)} already exists") | ||
} | ||
} while (!done) | ||
newKey | ||
} | ||
} | ||
|
||
override suspend fun update( | ||
key: String, | ||
data: ByteString, | ||
partitionId: String?, | ||
expiration: Instant? | ||
) { | ||
checkPartition(partitionId) | ||
if (expiration != null) { | ||
checkExpiration(expiration) | ||
} | ||
owner.withDatabase { database -> | ||
val nowSeconds = owner.clock.now().epochSeconds | ||
val values = ContentValues().apply { | ||
if (expiration != null) { | ||
put("expiration", expiration.epochSeconds) | ||
} | ||
put("data", data.toByteArray()) | ||
} | ||
val count = database.update( | ||
sql.tableName, | ||
values, | ||
sql.conditionWithExpiration(nowSeconds), | ||
whereArgs(key, partitionId) | ||
) | ||
if (count != 1) { | ||
throw NoRecordStorageException( | ||
"No record with ${recordDescription(key, partitionId)}") | ||
} | ||
} | ||
} | ||
|
||
override suspend fun delete(key: String, partitionId: String?): Boolean { | ||
checkPartition(partitionId) | ||
return owner.withDatabase { database -> | ||
val nowSeconds = owner.clock.now().epochSeconds | ||
val count = database.delete( | ||
sql.tableName, | ||
sql.conditionWithExpiration(nowSeconds), | ||
whereArgs(key, partitionId) | ||
) | ||
count > 0 | ||
} | ||
} | ||
|
||
override suspend fun deleteAll() { | ||
owner.withDatabase { database -> | ||
database.execSQL(sql.deleteAllStatement) | ||
} | ||
} | ||
|
||
override suspend fun enumerate( | ||
partitionId: String?, | ||
afterKey: String?, | ||
limit: Int | ||
): List<String> { | ||
checkPartition(partitionId) | ||
return owner.withDatabase { database -> | ||
val cursor = database.query( | ||
sql.tableName, | ||
arrayOf("id"), | ||
sql.enumerateConditionWithExpiration(owner.clock.now().epochSeconds), | ||
whereArgs(afterKey ?: "", partitionId), | ||
null, | ||
null, | ||
"id", | ||
if (limit < Int.MAX_VALUE) "0, $limit" else null | ||
) | ||
val list = mutableListOf<String>() | ||
while (cursor.moveToNext()) { | ||
list.add(cursor.getString(0)) | ||
} | ||
cursor.close() | ||
list | ||
} | ||
} | ||
|
||
override suspend fun purgeExpired() { | ||
owner.withDatabase { database -> | ||
database.execSQL(sql.purgeExpiredStatement | ||
.replace("?", owner.clock.now().epochSeconds.toString())) | ||
} | ||
} | ||
|
||
private fun whereArgs(key: String, partitionId: String?): Array<String> { | ||
return if (spec.supportPartitions) { | ||
arrayOf(key, partitionId!!) | ||
} else { | ||
arrayOf(key) | ||
} | ||
} | ||
} |
Oops, something went wrong.