Skip to content

Commit

Permalink
Merge pull request #480 from atsign-foundation/set_isEncrypted
Browse files Browse the repository at this point in the history
Set isEncrypted to true for cached keys.
  • Loading branch information
sitaram-kalluri authored Jan 24, 2022
2 parents d27c1ef + f0510d6 commit fcaeff9
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 110 deletions.
5 changes: 4 additions & 1 deletion at_end2end_test/test/update_verb_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ void main() {
response = await read();
print(
'llookup verb response without private key in llookup verb: $response');
expect(response, contains('error:AT0015-key not found : country$first_atsign does not exist in keystore'));
expect(
response,
contains(
'error:AT0015-key not found : country$first_atsign does not exist in keystore'));
}, timeout: Timeout(Duration(seconds: 90)));

test('update verb with special characters', () async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@ export 'package:at_persistence_spec/src/keystore/log_keystore.dart';
export 'package:at_persistence_spec/src/keystore/secondary_keystore.dart';
export 'package:at_persistence_spec/src/log/at_access_log_manager.dart';
export 'package:at_persistence_spec/src/log/at_commit_log_manager.dart';

Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ class AtNotificationBuilder {
AtMetaData? atMetaData;

AtNotification build() {
if (ttl != null && expiresAt == null) {
if ((ttl != null && ttl! > 0) && expiresAt == null) {
expiresAt = DateTime.now().toUtc().add(Duration(milliseconds: ttl!));
}
return AtNotification._builder(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,27 +123,32 @@ class AtNotificationKeystore
if (value != null && value.isExpired()) {
expired.add(key);
}
if (value?.expiresAt == null && DateTime.now().toUtc().difference(value!.notificationDateTime!).inHours >= _notificationExpiryInHours) {
if (value?.expiresAt == null &&
DateTime.now()
.toUtc()
.difference(value!.notificationDateTime!)
.inHours >=
_notificationExpiryInHours) {
var newNotification = (AtNotificationBuilder()
..id = value.id
..fromAtSign = value.fromAtSign
..notificationDateTime = value.notificationDateTime
..toAtSign = value.toAtSign
..notification = value.notification
..type = value.type
..opType = value.opType
..messageType = value.messageType
..expiresAt = value.notificationDateTime
..priority = value.priority
..notificationStatus = value.notificationStatus
..retryCount = value.retryCount
..strategy = value.strategy
..notifier = value.notifier
..depth = value.depth
..atValue = value.atValue
..atMetaData = value.atMetadata
..ttl = value.ttl
).build();
..id = value.id
..fromAtSign = value.fromAtSign
..notificationDateTime = value.notificationDateTime
..toAtSign = value.toAtSign
..notification = value.notification
..type = value.type
..opType = value.opType
..messageType = value.messageType
..expiresAt = value.notificationDateTime
..priority = value.priority
..notificationStatus = value.notificationStatus
..retryCount = value.retryCount
..strategy = value.strategy
..notifier = value.notifier
..depth = value.depth
..atValue = value.atValue
..atMetaData = value.atMetadata
..ttl = value.ttl)
.build();
put(key, newNotification);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ class AtSecondaryConfig {

//version
static final String? _secondaryServerVersion =
(ConfigUtil.getPubspecConfig() != null &&
ConfigUtil.getPubspecConfig()!['version'] != null)
? ConfigUtil.getPubspecConfig()!['version']
: null;
(ConfigUtil.getPubspecConfig() != null &&
ConfigUtil.getPubspecConfig()!['version'] != null)
? ConfigUtil.getPubspecConfig()!['version']
: null;

static final Map<String, String> _envVars = Platform.environment;

Expand All @@ -92,9 +92,9 @@ class AtSecondaryConfig {
// TODO: Low priority: Lots of very similar boilerplate code here. Not necessarily bad in this particular case, but
// could be terser as per the logLevel getter
static String get logLevel {
return _getStringEnvVar('logLevel')
?? getStringValueFromYaml(['log', 'level'])
?? _defaultLogLevel;
return _getStringEnvVar('logLevel') ??
getStringValueFromYaml(['log', 'level']) ??
_defaultLogLevel;
}

static bool? get useSSL {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,14 @@ class NotifyVerbHandler extends AbstractVerbHandler {
await NotificationUtil.storeNotification(
fromAtSign, forAtSign, key, NotificationType.received, opType,
ttl_ms: ttln_ms, value: atValue);

// Setting isEncrypted variable to true. By default, value of all the keys are encrypted.
// except for the public keys. So, if key is public set isEncrypted to false.
var isEncrypted = true;
// If key is public, remove forAtSign from key.
if (key!.contains('public:')) {
var index = key.indexOf(':');
key = key.substring(index + 1);
isEncrypted = false;
}
var notifyKey = '$CACHED:$key';
if (operation == 'delete') {
Expand All @@ -184,7 +187,8 @@ class NotifyVerbHandler extends AbstractVerbHandler {
ttl: ttl_ms,
ttb: ttb_ms,
ttr: ttr_ms,
ccd: isCascade)
ccd: isCascade,
isEncrypted: isEncrypted)
.build();
cachedKeyCommitId =
await _storeCachedKeys(key, metadata, atValue: atValue);
Expand All @@ -201,7 +205,8 @@ class NotifyVerbHandler extends AbstractVerbHandler {
ttl: ttl_ms,
ttb: ttb_ms,
ttr: ttr_ms,
ccd: isCascade)
ccd: isCascade,
isEncrypted: isEncrypted)
.build();
cachedKeyCommitId = await _updateMetadata(notifyKey, atMetaData);
//write the latest commit id to the StatsNotificationService
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ class ScanVerbHandler extends AbstractVerbHandler {
return scanResult;
}

/// Returns a filtered list of the
/// keys where the filtering
/// depends on the type of authentication
/// Returns a filtered list of the
/// keys where the filtering
/// depends on the type of authentication
/// on the inbound connection
///
/// **Parameters**
Expand Down
137 changes: 71 additions & 66 deletions at_secondary/at_secondary_server/test/notify_list_verb_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import 'package:at_secondary/src/verb/handler/notify_list_verb_handler.dart';
import 'package:at_server_spec/at_verb_spec.dart';
import 'package:crypto/crypto.dart';
import 'package:test/test.dart';
import 'package:uuid/uuid.dart';

void main() {
var storageDir = Directory.current.path + '/test/hive';
Expand Down Expand Up @@ -232,43 +231,46 @@ void main() {
});
group('A group of tests on expiry ', () {
setUp(() async => keyStoreManager = await setUpFunc(storageDir));
test('A test to verify notify list does not return expired entries - 1 expired entry', () async {
test(
'A test to verify notify list does not return expired entries - 1 expired entry',
() async {
var notifyListVerbHandler =
NotifyListVerbHandler(keyStoreManager.getKeyStore());
NotifyListVerbHandler(keyStoreManager.getKeyStore());
var notification1 = (AtNotificationBuilder()
..id = '122'
..fromAtSign = '@test_user_1'
..notificationDateTime = DateTime.now()
..toAtSign = '@bob'
..notification = 'key-2'
..type = NotificationType.received
..opType = OperationType.update
..messageType = MessageType.key
..expiresAt = null
..priority = NotificationPriority.low
..notificationStatus = NotificationStatus.queued
..retryCount = 0
..strategy = 'latest'
..notifier = 'persona'
..depth = 3..ttl=100)
..id = '122'
..fromAtSign = '@test_user_1'
..notificationDateTime = DateTime.now()
..toAtSign = '@bob'
..notification = 'key-2'
..type = NotificationType.received
..opType = OperationType.update
..messageType = MessageType.key
..expiresAt = null
..priority = NotificationPriority.low
..notificationStatus = NotificationStatus.queued
..retryCount = 0
..strategy = 'latest'
..notifier = 'persona'
..depth = 3
..ttl = 100)
.build();

var notification2 = (AtNotificationBuilder()
..id = '125'
..fromAtSign = '@test_user_1'
..notificationDateTime = DateTime.now()
..toAtSign = '@bob'
..notification = 'key-3'
..type = NotificationType.received
..opType = OperationType.update
..messageType = MessageType.key
..expiresAt = null
..priority = NotificationPriority.low
..notificationStatus = NotificationStatus.queued
..retryCount = 0
..strategy = 'latest'
..notifier = 'persona'
..depth = 3)
..id = '125'
..fromAtSign = '@test_user_1'
..notificationDateTime = DateTime.now()
..toAtSign = '@bob'
..notification = 'key-3'
..type = NotificationType.received
..opType = OperationType.update
..messageType = MessageType.key
..expiresAt = null
..priority = NotificationPriority.low
..notificationStatus = NotificationStatus.queued
..retryCount = 0
..strategy = 'latest'
..notifier = 'persona'
..depth = 3)
.build();

await AtNotificationKeystore.getInstance().put('122', notification1);
Expand All @@ -295,43 +297,46 @@ void main() {
await AtNotificationKeystore.getInstance().remove('125');
});

test('A test to verify notify list expired entries - No expired entry', () async {
test('A test to verify notify list expired entries - No expired entry',
() async {
var notifyListVerbHandler =
NotifyListVerbHandler(keyStoreManager.getKeyStore());
NotifyListVerbHandler(keyStoreManager.getKeyStore());
var notification1 = (AtNotificationBuilder()
..id = '122'
..fromAtSign = '@test_user_1'
..notificationDateTime = DateTime.now()
..toAtSign = '@bob'
..notification = 'key-2'
..type = NotificationType.received
..opType = OperationType.update
..messageType = MessageType.key
..expiresAt = null
..priority = NotificationPriority.low
..notificationStatus = NotificationStatus.queued
..retryCount = 0
..strategy = 'latest'
..notifier = 'persona'
..depth = 3..ttl=60000)
..id = '122'
..fromAtSign = '@test_user_1'
..notificationDateTime = DateTime.now()
..toAtSign = '@bob'
..notification = 'key-2'
..type = NotificationType.received
..opType = OperationType.update
..messageType = MessageType.key
..expiresAt = null
..priority = NotificationPriority.low
..notificationStatus = NotificationStatus.queued
..retryCount = 0
..strategy = 'latest'
..notifier = 'persona'
..depth = 3
..ttl = 60000)
.build();

var notification2 = (AtNotificationBuilder()
..id = '125'
..fromAtSign = '@test_user_1'
..notificationDateTime = DateTime.now()
..toAtSign = '@bob'
..notification = 'key-3'
..type = NotificationType.received
..opType = OperationType.update
..messageType = MessageType.key
..expiresAt = null
..priority = NotificationPriority.low
..notificationStatus = NotificationStatus.queued
..retryCount = 0
..strategy = 'latest'
..notifier = 'persona'
..depth = 3..ttl=70000)
..id = '125'
..fromAtSign = '@test_user_1'
..notificationDateTime = DateTime.now()
..toAtSign = '@bob'
..notification = 'key-3'
..type = NotificationType.received
..opType = OperationType.update
..messageType = MessageType.key
..expiresAt = null
..priority = NotificationPriority.low
..notificationStatus = NotificationStatus.queued
..retryCount = 0
..strategy = 'latest'
..notifier = 'persona'
..depth = 3
..ttl = 70000)
.build();

await AtNotificationKeystore.getInstance().put('122', notification1);
Expand All @@ -353,7 +358,7 @@ void main() {
var result = jsonDecode(response.data!);
print(result);
expect(result.length, 2);
expect(result[0]['id'],'122');
expect(result[0]['id'], '122');
expect(result[1]['id'], '125');
await AtNotificationKeystore.getInstance().remove('122');
await AtNotificationKeystore.getInstance().remove('125');
Expand Down
15 changes: 9 additions & 6 deletions at_secondary/at_secondary_server/test/secondary_config_test.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:test/test.dart';
import 'package:at_secondary/src/server/at_secondary_config.dart';
import 'package:at_utils/at_logger.dart';
import 'package:logging/logging.dart' as logging;
import 'package:test/test.dart';

void main() async {
group('A group of secondary config test', () {
Expand All @@ -13,15 +13,18 @@ void main() async {
expect(AtSecondaryConfig.rootServerPort is int, true);
});

test('Config: check AtSecondaryConfig.logLevel defaults to FINEST', () async {
expect (AtSecondaryConfig.logLevel.trim().toUpperCase(), equals(logging.Level.INFO.name.trim().toUpperCase()));
test('Config: check AtSecondaryConfig.logLevel defaults to FINEST',
() async {
expect(AtSecondaryConfig.logLevel.trim().toUpperCase(),
equals(logging.Level.INFO.name.trim().toUpperCase()));
});

test('Config: check new AtSignLoggers have level set correctly, via setting AtSignLogger.root_level from a string config setting', () async {
test(
'Config: check new AtSignLoggers have level set correctly, via setting AtSignLogger.root_level from a string config setting',
() async {
AtSignLogger.root_level = 'wARNinG';
AtSignLogger atLogger = AtSignLogger('test');
expect (atLogger.logger.level, equals(logging.Level.WARNING));
expect(atLogger.logger.level, equals(logging.Level.WARNING));
});

});
}
4 changes: 2 additions & 2 deletions at_secondary/at_secondary_server/test/update_verb_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ void main() {
SecondaryKeyStore keyStore = keyStoreManager.getKeyStore();
var secretData = AtData();
secretData.data =
'b26455a907582760ebf35bc4847de549bc41c24b25c8b1c58d5964f7b4f8a43bc55b0e9a601c9a9657d9a8b8bbc32f88b4e38ffaca03c8710ebae1b14ca9f364';
'b26455a907582760ebf35bc4847de549bc41c24b25c8b1c58d5964f7b4f8a43bc55b0e9a601c9a9657d9a8b8bbc32f88b4e38ffaca03c8710ebae1b14ca9f364';
await keyStore.put('privatekey:at_secret', secretData);
var fromVerbHandler = FromVerbHandler(keyStoreManager.getKeyStore());
AtSecondaryServerImpl.getInstance().currentAtSign = '@alice';
Expand All @@ -653,7 +653,7 @@ void main() {
await cramVerbHandler.processVerb(
cramResponse, cramVerbParams, atConnection);
var connectionMetadata =
atConnection.getMetaData() as InboundConnectionMetadata;
atConnection.getMetaData() as InboundConnectionMetadata;
expect(connectionMetadata.isAuthenticated, true);
expect(cramResponse.data, 'success');
//Update Verb
Expand Down

0 comments on commit fcaeff9

Please sign in to comment.