Skip to content

Commit

Permalink
Merge pull request #1187 from atsign-foundation/delete_expired_keys
Browse files Browse the repository at this point in the history
feat: schedule job that removes expired keys
  • Loading branch information
murali-shris authored Mar 6, 2024
2 parents 2b674a7 + e226cb0 commit 566ca92
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 3 deletions.
2 changes: 2 additions & 0 deletions packages/at_client/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
## 3.0.76
- feat: Introduce mechanism to identify and delete expired keys
## 3.0.75
- feat: Introduce feature to fetch enrollment requests from the server
## 3.0.74
Expand Down
2 changes: 2 additions & 0 deletions packages/at_client/lib/src/manager/storage_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class StorageManager {
.getSecondaryKeyStoreManager()!;
await hiveKeyStore.initialize();
keyStoreManager.keyStore = hiveKeyStore;
manager.scheduleKeyExpireTask(null,
runTimeInterval: preferences!.expiryCheckTimeInterval);
isStorageInitialized = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class AtClientConfig {

/// Represents the at_client version.
/// Must always be the same as the actual version in pubspec.yaml
final String atClientVersion = '3.0.75';
final String atClientVersion = '3.0.76';

/// Represents the client commit log compaction time interval
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ class AtClientPreference {

Duration monitorHeartbeatInterval = Duration(seconds: 10);

/// Time interval for the scheduled task that removes expired keys from local keyStore
Duration expiryCheckTimeInterval = Duration(minutes: 10);

///[OptionalParameter] when set to true logs TLS Keys to file.
bool decryptPackets = false;

Expand Down
4 changes: 2 additions & 2 deletions packages/at_client/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description: The at_client library is the non-platform specific Client SDK which
##
##
## NB: When incrementing the version, please also increment the version in AtClientConfig file
version: 3.0.75
version: 3.0.76
## NB: When incrementing the version, please also increment the version in AtClientConfig file
##

Expand Down Expand Up @@ -36,7 +36,7 @@ dependencies:
at_chops: ^2.0.0
at_lookup: ^3.0.46
at_persistence_spec: ^2.0.14
at_persistence_secondary_server: ^3.0.60
at_persistence_secondary_server: ^3.0.61
meta: ^1.8.0
version: ^3.0.2

Expand Down
103 changes: 103 additions & 0 deletions packages/at_client/test/delete_expired_keys_task_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import 'dart:io';

import 'package:at_client/at_client.dart';
import 'package:at_persistence_secondary_server/at_persistence_secondary_server.dart';
import 'package:at_persistence_secondary_server/src/keystore/hive_keystore.dart';
import 'package:mocktail/mocktail.dart';
import 'package:test/test.dart';

class MockCommitLog extends Mock implements ClientAtCommitLog {}

void main() {
var storagePath = '${Directory.current.path}/test/hive';
String atsign = '@expiry_test';
var commitLog = MockCommitLog();

group('validate behaviour of scheduled deleteExpiredKeys task', () {
when(() => commitLog.commit(any(), CommitOp.UPDATE_ALL))
.thenAnswer((_) => Future.value(1));

when(() => commitLog.commit(any(), CommitOp.DELETE))
.thenAnswer((_) => Future.value(2));

setUp(() async {
AtClientImpl.atClientInstanceMap.remove(atsign);
await setUpStorage(atsign, storagePath, commitLog);
});

test('verify that delete expired keys task removes expired keys', () async {
String key1 = 'public:expired_key_1.test$atsign';
AtMetaData metadata = AtMetaData()..ttl = 1;
AtData data = AtData()
..data = 'data_key_1'
..metaData = metadata;
await getKeyStore(atsign)?.put(key1, data);

String key2 = 'public:expired_key_2.test$atsign';
data = AtData()
..data = 'data_key_2'
..metaData = metadata;
await getKeyStore(atsign)?.put(key2, data);

String key3 = 'public:unexpired_key_3.test$atsign';
metadata.ttl = 1000000;
data = AtData()
..data = 'data_key_3'
..metaData = metadata;
await getKeyStore(atsign)?.put(key3, data);

await SecondaryPersistenceStoreFactory.getInstance()
.getSecondaryPersistenceStore(atsign)
?.getSecondaryKeyStore()
?.deleteExpiredKeys();

int exceptionCatchCount = 0;
try {
await getKeyStore(atsign)?.get(key1);
} on Exception catch (e) {
expect(e.toString().contains('$key1 does not exist in keystore'), true);
exceptionCatchCount++;
}

try {
await getKeyStore(atsign)?.get(key2);
} on Exception catch (e) {
expect(e.toString().contains('$key2 does not exist in keystore'), true);
exceptionCatchCount++;
}

expect((await getKeyStore(atsign)?.get(key3))?.data, 'data_key_3');
expect(exceptionCatchCount,
2); // this counter maintains the count of how many exceptions have been caught
});

tearDown(() async {
await tearDownLocalStorage(storagePath);
});
});
}

HiveKeystore? getKeyStore(String atsign) {
return SecondaryPersistenceStoreFactory.getInstance()
.getSecondaryPersistenceStore(atsign)
?.getSecondaryKeyStore();
}

Future<void> setUpStorage(
String atsign, String storagePath, ClientAtCommitLog commitLog) async {
var manager = SecondaryPersistenceStoreFactory.getInstance()
.getSecondaryPersistenceStore(atsign);
await manager?.getHivePersistenceManager()?.init(storagePath);
manager?.getSecondaryKeyStore()?.commitLog = commitLog;
}

Future<void> tearDownLocalStorage(storageDir) async {
try {
var isExists = await Directory(storageDir).exists();
if (isExists) {
Directory(storageDir).deleteSync(recursive: true);
}
} catch (e, st) {
print('local_secondary_test.dart: exception / error in tearDown: $e, $st');
}
}

0 comments on commit 566ca92

Please sign in to comment.