Skip to content

Commit

Permalink
Merge pull request #211 from atsign-foundation/jeremy-devicename-fix1
Browse files Browse the repository at this point in the history
fix: sshnp exit if atSign DNE, sshnpd wait for atSign to exist
  • Loading branch information
gkc committed Jun 28, 2023
2 parents 3091959 + 407d4c6 commit 948fb5d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
6 changes: 6 additions & 0 deletions bin/sshnpd.dart
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ Future<void> _main(List<String> args) async {

atClient = AtClientManager.getInstance().atClient;

// check if sshnp atSign exists
while(!(await atSignIsActivated(atClient, managerAtsign))) {
await Future.delayed(Duration(seconds: 5));
logger.warning('Waiting for $managerAtsign to be activated...');
}

NotificationService notificationService = atClient.notificationService;

if (results['username']) {
Expand Down
4 changes: 4 additions & 0 deletions lib/sshnp.dart
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ class SSHNP {
throw StateError('Cannot init() - already initialized');
}

if(!(await atSignIsActivated(atClient, sshnpdAtSign))) {
throw ('sshnpd atSign $sshnpdAtSign is not activated.');
}

logger.info('Subscribing to notifications on $sessionId.$nameSpace@');
// Start listening for response notifications from sshnpd
atClient.notificationService
Expand Down
29 changes: 29 additions & 0 deletions lib/sshnp_utils.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'dart:io';

import 'package:at_client/at_client.dart';

/// Get the home directory or null if unknown.
String? getHomeDirectory({bool throwIfNull = false}) {
String? homeDir;
Expand Down Expand Up @@ -62,3 +64,30 @@ String getDefaultSshDirectory(String homeDirectory) {
return '$homeDirectory/.ssh/'
.replaceAll('/', Platform.pathSeparator);
}

/// Checks if the provided atSign's atServer has been properly activated with a public RSA key.
/// `atClient` must be authenticated
/// `atSign` is the atSign to check
/// Returns `true`, if the atSign's cloud secondary server has an existing `public:publickey@` in their server,
/// Returns `false`, if the atSign's cloud secondary *exists*, but does not have an existing `public:publickey@`
/// Throws [AtClientException] if the cloud secondary is invalid or not reachable
Future<bool> atSignIsActivated(final AtClient atClient, String atSign) async {
final Metadata metadata = Metadata()
..isPublic = true
..namespaceAware = false;

final AtKey publicKey = AtKey()
..sharedBy = atSign
..key = 'publickey'
..metadata = metadata;

try {
await atClient.get(publicKey);
return true;
} catch (e) {
if(e is AtKeyNotFoundException || (e is AtClientException && e.message.contains("public:publickey") && e.message.contains("does not exist in keystore"))) {
return false;
}
rethrow;
}
}

0 comments on commit 948fb5d

Please sign in to comment.