diff --git a/packages/at_persistence_secondary_server/CHANGELOG.md b/packages/at_persistence_secondary_server/CHANGELOG.md index 22c5b964e..0d11431a9 100644 --- a/packages/at_persistence_secondary_server/CHANGELOG.md +++ b/packages/at_persistence_secondary_server/CHANGELOG.md @@ -1,3 +1,5 @@ +## 3.0.58 +- fix: Modify "lastCommittedSequenceNumberWithRegex" to return highest commitId among enrolled namespaces ## 3.0.57 - fix: Refactor commit log keystore to optimize memory usage ## 3.0.56 diff --git a/packages/at_persistence_secondary_server/lib/src/compaction/at_compaction_stats_service_impl.dart b/packages/at_persistence_secondary_server/lib/src/compaction/at_compaction_stats_service_impl.dart index 239fc35fa..210627ce3 100644 --- a/packages/at_persistence_secondary_server/lib/src/compaction/at_compaction_stats_service_impl.dart +++ b/packages/at_persistence_secondary_server/lib/src/compaction/at_compaction_stats_service_impl.dart @@ -38,13 +38,13 @@ class AtCompactionStatsServiceImpl implements AtCompactionStatsService { ///changes the value of [compactionStatsKey] to match the AtLogType being processed void _getKey() { if (_atCompaction is AtCommitLog) { - compactionStatsKey = commitLogCompactionKey; + compactionStatsKey = AtConstants.commitLogCompactionKey; } if (_atCompaction is AtAccessLog) { - compactionStatsKey = accessLogCompactionKey; + compactionStatsKey = AtConstants.accessLogCompactionKey; } if (_atCompaction is AtNotificationKeystore) { - compactionStatsKey = notificationCompactionKey; + compactionStatsKey = AtConstants.notificationCompactionKey; } } } diff --git a/packages/at_persistence_secondary_server/lib/src/log/commitlog/at_commit_log.dart b/packages/at_persistence_secondary_server/lib/src/log/commitlog/at_commit_log.dart index fcc57a79d..de249cb53 100644 --- a/packages/at_persistence_secondary_server/lib/src/log/commitlog/at_commit_log.dart +++ b/packages/at_persistence_secondary_server/lib/src/log/commitlog/at_commit_log.dart @@ -87,8 +87,10 @@ class AtCommitLog extends BaseAtCommitLog { /// Returns the latest committed sequence number with regex @server - Future lastCommittedSequenceNumberWithRegex(String regex) async { - return await _commitLogKeyStore.lastCommittedSequenceNumberWithRegex(regex); + Future lastCommittedSequenceNumberWithRegex(String regex, + {List? enrolledNamespace}) async { + return await _commitLogKeyStore.lastCommittedSequenceNumberWithRegex(regex, + enrolledNamespace: enrolledNamespace); } /// Returns the first committed sequence number diff --git a/packages/at_persistence_secondary_server/lib/src/log/commitlog/commit_log_keystore.dart b/packages/at_persistence_secondary_server/lib/src/log/commitlog/commit_log_keystore.dart index 477dbb888..e32a530d0 100644 --- a/packages/at_persistence_secondary_server/lib/src/log/commitlog/commit_log_keystore.dart +++ b/packages/at_persistence_secondary_server/lib/src/log/commitlog/commit_log_keystore.dart @@ -58,9 +58,11 @@ class CommitLogKeyStore extends BaseCommitLogKeyStore { } /// Returns the latest committed sequence number with regex - Future lastCommittedSequenceNumberWithRegex(String regex) async { + Future lastCommittedSequenceNumberWithRegex(String regex, + {List? enrolledNamespace}) async { var lastCommittedEntry = (getBox() as Box).values.lastWhere( - (entry) => (_acceptKey(entry.atKey, regex)), + (entry) => (_acceptKey(entry.atKey, regex, + enrolledNamespace: enrolledNamespace)), orElse: () => NullCommitEntry()); var lastCommittedSequenceNum = (lastCommittedEntry != null) ? lastCommittedEntry.key : null; @@ -177,8 +179,30 @@ class CommitLogKeyStore extends BaseCommitLogKeyStore { } } - bool _acceptKey(String atKey, String regex) { - return _isRegexMatches(atKey, regex) || _isSpecialKey(atKey); + bool _acceptKey(String atKey, String regex, + {List? enrolledNamespace}) { + return _isNamespaceAuthorised(atKey, enrolledNamespace) && + (_isRegexMatches(atKey, regex) || _isSpecialKey(atKey)); + } + + bool _isNamespaceAuthorised(String atKey, List? enrolledNamespace) { + // This is work-around for : https://github.com/atsign-foundation/at_server/issues/1570 + if (atKey.toLowerCase() == 'configkey') { + return true; + } + String? keyNamespace = AtKey.fromString(atKey).namespace; + // If enrolledNamespace is null or keyNamespace is null, fallback to + // existing behaviour - the key is authorized for the client to receive. So return true. + if (enrolledNamespace == null || + enrolledNamespace.isEmpty || + (keyNamespace == null || keyNamespace.isEmpty)) { + return true; + } + if (enrolledNamespace.contains('*') || + enrolledNamespace.contains(keyNamespace)) { + return true; + } + return false; } bool _isRegexMatches(String atKey, String regex) { @@ -186,10 +210,10 @@ class CommitLogKeyStore extends BaseCommitLogKeyStore { } bool _isSpecialKey(String atKey) { - return atKey.contains(AT_ENCRYPTION_SHARED_KEY) || + return atKey.contains(AtConstants.atEncryptionSharedKey) || atKey.startsWith('public:') || - atKey.contains(AT_PKAM_SIGNATURE) || - atKey.contains(AT_SIGNING_PRIVATE_KEY); + atKey.contains(AtConstants.atPkamSignature) || + atKey.contains(AtConstants.atSigningPrivateKey); } /// Returns the latest commitEntry of the key. diff --git a/packages/at_persistence_secondary_server/lib/src/model/at_meta_data.dart b/packages/at_persistence_secondary_server/lib/src/model/at_meta_data.dart index 9e3f2cd58..d4224ac66 100644 --- a/packages/at_persistence_secondary_server/lib/src/model/at_meta_data.dart +++ b/packages/at_persistence_secondary_server/lib/src/model/at_meta_data.dart @@ -136,21 +136,21 @@ class AtMetaData extends HiveObject { map['refreshAt'] = refreshAt?.toUtc().toString(); map['status'] = status; map['version'] = version; - map[AT_TTL] = ttl; - map[AT_TTB] = ttb; - map[AT_TTR] = ttr; - map[CCD] = isCascade; - map[IS_BINARY] = isBinary; - map[IS_ENCRYPTED] = isEncrypted; - map[PUBLIC_DATA_SIGNATURE] = dataSignature; - map[SHARED_KEY_ENCRYPTED] = sharedKeyEnc; - map[SHARED_WITH_PUBLIC_KEY_CHECK_SUM] = pubKeyCS; - map[ENCODING] = encoding; - map[ENCRYPTING_KEY_NAME] = encKeyName; - map[ENCRYPTING_ALGO] = encAlgo; - map[IV_OR_NONCE] = ivNonce; - map[SHARED_KEY_ENCRYPTED_ENCRYPTING_KEY_NAME] = skeEncKeyName; - map[SHARED_KEY_ENCRYPTED_ENCRYPTING_ALGO] = skeEncAlgo; + map[AtConstants.ttl] = ttl; + map[AtConstants.ttb] = ttb; + map[AtConstants.ttr] = ttr; + map[AtConstants.ccd] = isCascade; + map[AtConstants.isBinary] = isBinary; + map[AtConstants.isEncrypted] = isEncrypted; + map[AtConstants.publicDataSignature] = dataSignature; + map[AtConstants.sharedKeyEncrypted] = sharedKeyEnc; + map[AtConstants.sharedWithPublicKeyCheckSum] = pubKeyCS; + map[AtConstants.encoding] = encoding; + map[AtConstants.encryptingKeyName] = encKeyName; + map[AtConstants.encryptingAlgo] = encAlgo; + map[AtConstants.ivOrNonce] = ivNonce; + map[AtConstants.sharedKeyEncryptedEncryptingKeyName] = skeEncKeyName; + map[AtConstants.sharedKeyEncryptedEncryptingAlgo] = skeEncAlgo; return map; } @@ -178,33 +178,33 @@ class AtMetaData extends HiveObject { : (json['version'] == null) ? 0 : json['version']; - ttl = (json[AT_TTL] is String) - ? int.parse(json[AT_TTL]) - : (json[AT_TTL] == null) + ttl = (json[AtConstants.ttl] is String) + ? int.parse(json[AtConstants.ttl]) + : (json[AtConstants.ttl] == null) ? null - : json[AT_TTL]; - ttb = (json[AT_TTB] is String) - ? int.parse(json[AT_TTB]) - : (json[AT_TTB] == null) + : json[AtConstants.ttl]; + ttb = (json[AtConstants.ttb] is String) + ? int.parse(json[AtConstants.ttb]) + : (json[AtConstants.ttb] == null) ? null - : json[AT_TTB]; - ttr = (json[AT_TTR] is String) - ? int.parse(json[AT_TTR]) - : (json[AT_TTR] == null) + : json[AtConstants.ttb]; + ttr = (json[AtConstants.ttr] is String) + ? int.parse(json[AtConstants.ttr]) + : (json[AtConstants.ttr] == null) ? null - : json[AT_TTR]; - isCascade = json[CCD]; - isBinary = json[IS_BINARY]; - isEncrypted = json[IS_ENCRYPTED]; - dataSignature = json[PUBLIC_DATA_SIGNATURE]; - sharedKeyEnc = json[SHARED_KEY_ENCRYPTED]; - pubKeyCS = json[SHARED_WITH_PUBLIC_KEY_CHECK_SUM]; - encoding = json[ENCODING]; - encKeyName = json[ENCRYPTING_KEY_NAME]; - encAlgo = json[ENCRYPTING_ALGO]; - ivNonce = json[IV_OR_NONCE]; - skeEncKeyName = json[SHARED_KEY_ENCRYPTED_ENCRYPTING_KEY_NAME]; - skeEncAlgo = json[SHARED_KEY_ENCRYPTED_ENCRYPTING_ALGO]; + : json[AtConstants.ttr]; + isCascade = json[AtConstants.ccd]; + isBinary = json[AtConstants.isBinary]; + isEncrypted = json[AtConstants.isEncrypted]; + dataSignature = json[AtConstants.publicDataSignature]; + sharedKeyEnc = json[AtConstants.sharedKeyEncrypted]; + pubKeyCS = json[AtConstants.sharedWithPublicKeyCheckSum]; + encoding = json[AtConstants.encoding]; + encKeyName = json[AtConstants.encryptingKeyName]; + encAlgo = json[AtConstants.encryptingAlgo]; + ivNonce = json[AtConstants.ivOrNonce]; + skeEncKeyName = json[AtConstants.sharedKeyEncryptedEncryptingKeyName]; + skeEncAlgo = json[AtConstants.sharedKeyEncryptedEncryptingAlgo]; return this; } diff --git a/packages/at_persistence_secondary_server/pubspec.yaml b/packages/at_persistence_secondary_server/pubspec.yaml index 46bf959b4..5b57518c0 100644 --- a/packages/at_persistence_secondary_server/pubspec.yaml +++ b/packages/at_persistence_secondary_server/pubspec.yaml @@ -1,6 +1,6 @@ name: at_persistence_secondary_server description: A Dart library with the implementation classes for the persistence layer of the secondary server. -version: 3.0.57 +version: 3.0.58 repository: https://github.com/atsign-foundation/at_server homepage: https://docs.atsign.com/ diff --git a/packages/at_persistence_secondary_server/test/at_compaction_stats_test.dart b/packages/at_persistence_secondary_server/test/at_compaction_stats_test.dart index cf90ce779..a4958076e 100644 --- a/packages/at_persistence_secondary_server/test/at_compaction_stats_test.dart +++ b/packages/at_persistence_secondary_server/test/at_compaction_stats_test.dart @@ -76,7 +76,7 @@ Future main() async { // Get Compaction Stats AtData? atData = await secondaryPersistenceStore! .getSecondaryKeyStore() - ?.get(at_commons.commitLogCompactionKey); + ?.get(at_commons.AtConstants.commitLogCompactionKey); // Assert Compaction Stats var decodedData = jsonDecode(atData!.data!) as Map; @@ -110,7 +110,7 @@ Future main() async { await atCompactionStatsServiceImpl.handleStats(atCompactionStats); AtData? atData = await secondaryPersistenceStore! .getSecondaryKeyStore() - ?.get(at_commons.accessLogCompactionKey); + ?.get(at_commons.AtConstants.accessLogCompactionKey); var data = (atData?.data); var decodedData = jsonDecode(data!) as Map; expect(decodedData["deletedKeysCount"], '3'); diff --git a/packages/at_persistence_secondary_server/test/commit_log_test.dart b/packages/at_persistence_secondary_server/test/commit_log_test.dart index 9b8fb42fa..5fef444a3 100644 --- a/packages/at_persistence_secondary_server/test/commit_log_test.dart +++ b/packages/at_persistence_secondary_server/test/commit_log_test.dart @@ -133,10 +133,12 @@ void main() async { for (int i = 0; i < 10; i++) { if (i % 2 == 0) { await commitLogKeystore.getBox().add(CommitEntry( - 'test_key_false_$i', CommitOp.UPDATE, DateTime.now())); + 'test_key_false_$i.wavi@alice', + CommitOp.UPDATE, + DateTime.now())); } else { await commitLogKeystore.getBox().add(CommitEntry( - 'test_key_false_$i', CommitOp.UPDATE, DateTime.now()) + 'test_key_false_$i.wavi@alice', CommitOp.UPDATE, DateTime.now()) ..commitId = i); } } diff --git a/packages/at_persistence_secondary_server/test/hive_keystore_impl_test.dart b/packages/at_persistence_secondary_server/test/hive_keystore_impl_test.dart index 21ac071ab..994a8d8ed 100644 --- a/packages/at_persistence_secondary_server/test/hive_keystore_impl_test.dart +++ b/packages/at_persistence_secondary_server/test/hive_keystore_impl_test.dart @@ -238,7 +238,7 @@ void main() async { var keyStore = keyStoreManager.getSecondaryKeyStore()!; var atData = AtData(); atData.data = '123'; - final result = await keyStore.put(AT_PKAM_PRIVATE_KEY, atData); + final result = await keyStore.put(AtConstants.atPkamPrivateKey, atData); expect(result, isA()); });