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

fix: introduce a new config key to store an atsign's blocklist #1622

Merged
merged 15 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import 'package:hive/hive.dart';
class AtConfig {
var logger = AtSignLogger('AtConfig');

///stores 'Configuration' type under [configkey] in secondary.
String olConfigKey = 'configKey';
String configKey = 'private:blocklist';
var keyStoreHelper = HiveKeyStoreHelper.getInstance();
///stores 'Configuration' type under [configKey] in secondary.
final oldConfigKey = HiveKeyStoreHelper.getInstance().prepareKey('configKey');
final configKey =
HiveKeyStoreHelper.getInstance().prepareKey('private:blocklist');
final String? _atSign;
AtCommitLog? _commitLog;
late HivePersistenceManager persistenceManager;
Expand Down Expand Up @@ -58,8 +58,8 @@ class AtConfig {
Set<String> blockedAtsignsSet = await getBlockList();
// remove the atsign in unblockAtsignList from the existing blocklist
if (blockedAtsignsSet.isNotEmpty) {
var config = Configuration(
List.from(blockedAtsignsSet.difference(Set.from(unblockAtsignsList))));
var config = Configuration(List.from(
blockedAtsignsSet.difference(Set.from(unblockAtsignsList))));
result = await prepareAndStoreData(config, existingData);
}
} on Exception catch (e) {
Expand Down Expand Up @@ -95,8 +95,7 @@ class AtConfig {
Future<AtData?> get(String key) async {
AtData? value;
try {
var hiveKey = keyStoreHelper.prepareKey(key);
value = await (persistenceManager.getBox() as LazyBox).get(hiveKey);
value = await (persistenceManager.getBox() as LazyBox).get(key);
} on Exception catch (exception) {
logger.severe('HiveKeystore get exception: $exception');
throw DataStoreException('exception in get: ${exception.toString()}');
Expand Down Expand Up @@ -127,12 +126,11 @@ class AtConfig {
///Returns 'success' after successfully persisting data into secondary.
Future<String> prepareAndStoreData(config, [existingData]) async {
String result;
configKey = keyStoreHelper.prepareKey(configKey);
var newData = AtData();
newData.data = jsonEncode(config);

newData = keyStoreHelper.prepareDataForKeystoreOperation(newData,
existingAtData: existingData);
newData = HiveKeyStoreHelper.getInstance()
.prepareDataForKeystoreOperation(newData, existingAtData: existingData);

logger.finest('Storing the config key:$configKey | Value: $newData');
await persistenceManager.getBox().put(configKey, newData);
Expand All @@ -154,13 +152,27 @@ class AtConfig {
existingData = await get(configKey);
} on KeyNotFoundException catch (e) {
logger.finer('Could not fetch data with NEW config-key | ${e.message}');
} on Exception catch (e) {
logger.finer('Could not fetch data with NEW config-key | $e');
rethrow;
}
if (existingData == null) {
// If data could not be fetched with the new config-key, try fetching the data
// using the old config-key and delete the old key from keystore
try {
existingData = await get(olConfigKey);
await (persistenceManager.getBox() as LazyBox).delete(olConfigKey);
existingData = await get(oldConfigKey);
AtData newAtData = AtData()..data = existingData?.data;
HiveKeyStoreHelper.getInstance().prepareDataForKeystoreOperation(
newAtData,
existingAtData: existingData);
// store the existing data with the new key
await (persistenceManager.getBox() as LazyBox).put(configKey, newAtData);
srieteja marked this conversation as resolved.
Show resolved Hide resolved
await (persistenceManager.getBox() as LazyBox).delete(oldConfigKey);
} on KeyNotFoundException catch (e) {
logger.finer('Could not fetch data with OLD config-key | ${e.message}');
} on Exception catch (e) {
logger.finer('Could not fetch data with OLD config-key | $e');
rethrow;
}
}
return existingData;
Expand Down
27 changes: 27 additions & 0 deletions packages/at_persistence_secondary_server/test/at_config_test.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import 'dart:convert';
import 'dart:io';

import 'package:at_persistence_secondary_server/at_persistence_secondary_server.dart';
import 'package:at_persistence_secondary_server/src/config/configuration.dart';
import 'package:at_persistence_secondary_server/src/keystore/hive_keystore_helper.dart';
import 'package:hive/hive.dart';
import 'package:test/test.dart';

void main() async {
Expand Down Expand Up @@ -64,6 +68,29 @@ void main() async {
throwsA(predicate((dynamic e) => e is AssertionError)));
});

test('verify backwards compatibility of blocklist with config key', () async {
AtConfig atConfig = AtConfig(
await AtCommitLogManagerImpl.getInstance().getCommitLog('@test_user_1'),
'@test_user_1');
LazyBox box = atConfig.persistenceManager.getBox() as LazyBox;
List<String> blockedAtsigns = [
'@blocked_user_1',
'@blocked_user_2',
'@blocked_user_3'
];
var blockedConfig = Configuration(blockedAtsigns);
AtData atData = AtData()..data = jsonEncode(blockedConfig);
atData = HiveKeyStoreHelper.getInstance().prepareDataForKeystoreOperation(atData);
await box.put(atConfig.oldConfigKey, atData);
// fetch the data that has been put into the keystore using the new config key
var blockList = await atConfig.getBlockList();
expect(blockList.toList(), blockedAtsigns);
// verify that the new config key has been put into the keystore
assert(box.containsKey(atConfig.configKey));
// verify that the oldConfigKey has been deleted
box.containsKey(atConfig.oldConfigKey);
});

try {
tearDown(() async => await tearDownFunc());
} on Exception catch (e) {
Expand Down
Loading