Skip to content

Commit

Permalink
feat: expose log events via the policy admin API
Browse files Browse the repository at this point in the history
  • Loading branch information
gkc committed Sep 4, 2024
1 parent ce287e4 commit 1be9c68
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 25 deletions.
12 changes: 12 additions & 0 deletions apps/admin/admin_api/lib/src/expose_apis.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ import 'package:at_client/at_client.dart';
import 'package:noports_core/admin.dart';

policy(Alfred app, String pathPrefix, PolicyService api) {
// policy log events
app.get('$pathPrefix/logs', (req, res) async {
stderr.writeln('Fetching policy log events');
final now = DateTime.now();
final r = jsonEncode(await api.getLogEvents(
from: now.subtract(Duration(hours: 24)).millisecondsSinceEpoch,
to: now.millisecondsSinceEpoch,
));
stderr.writeln('Fetched policy log events');
return r;
});

// all groups TODO add query parameters for search, pagination etc
app.get('$pathPrefix/group', (req, res) async {
stderr.writeln('Fetching all groups');
Expand Down
40 changes: 20 additions & 20 deletions apps/admin/admin_api/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ packages:
dependency: transitive
description:
name: at_demo_data
sha256: bbaa979d9e6552472c5f5c755ebd2fef8c9c965140219f1faeaff1c7bb4e6ea7
sha256: "0f59a24b83f0cd6d0e0557021511602ff167ece0ac69f12b8612c03263dff9ea"
url: "https://pub.dev"
source: hosted
version: "1.0.3"
version: "1.1.0"
at_lookup:
dependency: transitive
description:
Expand All @@ -131,10 +131,10 @@ packages:
dependency: transitive
description:
name: at_onboarding_cli
sha256: "6728eccbf2d89a83d872ef82f10ec36b0174699cfc1dadaaf6a9a53b35773115"
sha256: fca7f5d96e83adf50057bccc6ebde3ec6562adfff99031894c781badf6daf623
url: "https://pub.dev"
source: hosted
version: "1.6.1"
version: "1.6.2"
at_persistence_secondary_server:
dependency: transitive
description:
Expand Down Expand Up @@ -307,10 +307,10 @@ packages:
dependency: transitive
description:
name: coverage
sha256: "576aaab8b1abdd452e0f656c3e73da9ead9d7880e15bdc494189d9c1a1baf0db"
sha256: c1fb2dce3c0085f39dc72668e85f8e0210ec7de05345821ff58530567df345a5
url: "https://pub.dev"
source: hosted
version: "1.9.0"
version: "1.9.2"
cron:
dependency: transitive
description:
Expand All @@ -323,10 +323,10 @@ packages:
dependency: transitive
description:
name: crypto
sha256: ff625774173754681d66daaf4a448684fb04b78f902da9cb3d308c19cc5e8bab
sha256: ec30d999af904f33454ba22ed9a86162b35e52b44ac4807d1d93c288041d7d27
url: "https://pub.dev"
source: hosted
version: "3.0.3"
version: "3.0.5"
cryptography:
dependency: transitive
description:
Expand Down Expand Up @@ -363,10 +363,10 @@ packages:
dependency: transitive
description:
name: dartssh2
sha256: "53a230c7dd6f487b704ceef1b29323ad64d19be89e786ccbc81e157a70417a56"
sha256: "9aa21bb23e4ce3b8133637162f8439af4796ee08c207c8c5e777b03c33ba7f10"
url: "https://pub.dev"
source: hosted
version: "2.8.2"
version: "2.10.0"
ecdsa:
dependency: transitive
description:
Expand Down Expand Up @@ -563,18 +563,18 @@ packages:
dependency: transitive
description:
name: mime
sha256: "2e123074287cc9fd6c09de8336dae606d1ddb88d9ac47358826db698c176a1f2"
sha256: "801fd0b26f14a4a58ccb09d5892c3fbdeff209594300a542492cf13fba9d247a"
url: "https://pub.dev"
source: hosted
version: "1.0.5"
version: "1.0.6"
mime_type:
dependency: transitive
description:
name: mime_type
sha256: "2ad6e67d3d2de9ac0f8ef5352d998fd103cb21351ae8c02fb0c78b079b37d275"
sha256: d652b613e84dac1af28030a9fba82c0999be05b98163f9e18a0849c6e63838bb
url: "https://pub.dev"
source: hosted
version: "1.0.0"
version: "1.0.1"
mocktail:
dependency: "direct dev"
description:
Expand Down Expand Up @@ -650,10 +650,10 @@ packages:
dependency: transitive
description:
name: pinenacl
sha256: "3a5503637587d635647c93ea9a8fecf48a420cc7deebe6f1fc85c2a5637ab327"
sha256: "57e907beaacbc3c024a098910b6240758e899674de07d6949a67b52fd984cbdf"
url: "https://pub.dev"
source: hosted
version: "0.5.1"
version: "0.6.0"
platform:
dependency: transitive
description:
Expand Down Expand Up @@ -778,10 +778,10 @@ packages:
dependency: transitive
description:
name: source_map_stack_trace
sha256: "84cf769ad83aa6bb61e0aa5a18e53aea683395f196a6f39c4c881fb90ed4f7ae"
sha256: c0713a43e323c3302c2abe2a1cc89aa057a387101ebd280371d6a6c9fa68516b
url: "https://pub.dev"
source: hosted
version: "2.1.1"
version: "2.1.2"
source_maps:
dependency: transitive
description:
Expand Down Expand Up @@ -898,10 +898,10 @@ packages:
dependency: transitive
description:
name: vm_service
sha256: f652077d0bdf60abe4c1f6377448e8655008eef28f128bc023f7b5e8dfeb48fc
sha256: "5c5f338a667b4c644744b661f309fb8080bb94b18a7e91ef1dbd343bed00ed6d"
url: "https://pub.dev"
source: hosted
version: "14.2.4"
version: "14.2.5"
watcher:
dependency: transitive
description:
Expand Down
30 changes: 29 additions & 1 deletion packages/dart/noports_core/lib/src/admin/impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ import 'dart:convert';
import 'package:at_client/at_client.dart';
import 'package:at_utils/at_logger.dart';
import 'package:noports_core/admin.dart';
import 'package:noports_core/sshnp_foundation.dart';

class PolicyServiceWithAtClient extends PolicyServiceInMem {
class PolicyServiceWithAtClient extends PolicyServiceInMem with AtClientBindings {
@override
final logger = AtSignLogger('PolicyServiceWithAtClient');
@override
final AtClient atClient;

PolicyServiceWithAtClient({
Expand All @@ -30,6 +33,14 @@ class PolicyServiceWithAtClient extends PolicyServiceInMem {
}
});

subscribe(
regex: r'.*\.logs\.policy\.sshnp',
shouldDecrypt: true,
).listen((AtNotification n) {
// TODO Make a PolicyLogEvent and use PolicyLogEvent.fromJson()
onPolicyLogEvent(jsonDecode(n.value!));
});

logger.shout('Loading groups via AtClient');
// Fetch all the groups
List<AtKey> groupKeys = await atClient.getAtKeys(
Expand All @@ -48,6 +59,10 @@ class PolicyServiceWithAtClient extends PolicyServiceInMem {
logger.shout('Load complete');
}

Future<void> onPolicyLogEvent(event) async {
super.logEvents.add(event);
}

String _groupKey(String id) {
return '$id.groups.policy.sshnp${atClient.getCurrentAtSign()!}';
}
Expand Down Expand Up @@ -119,6 +134,9 @@ class PolicyServiceInMem implements PolicyService {
@override
final Map<String, UserGroup> groups = {};

@override
final List<dynamic> logEvents = [];

int _maxGroupId() {
int i = 0;
for (final g in groups.values) {
Expand All @@ -129,6 +147,16 @@ class PolicyServiceInMem implements PolicyService {
}
return i;
}

@override
Future<List<dynamic>> getLogEvents(
{required int from, required int to}) async {
return List.from(logEvents.where((event) {
int ts = event['timestamp'];
return (ts >= from && ts <= to);
}));
}

@override
Future<UserGroup?> getUserGroup(String id) async {
return groups[id];
Expand Down
8 changes: 8 additions & 0 deletions packages/dart/noports_core/lib/src/admin/interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ abstract interface class PolicyService {
@visibleForTesting
Map<String, UserGroup> get groups;

/// The in-memory list of log events. Not for external use.
@visibleForTesting
List<dynamic> get logEvents;

// TODO Use a PolicyLogEvent
/// Fetch some log events
Future<List<dynamic>> getLogEvents({required int from, required int to});

/// Get (some of) the permission groups known to this policy service.
/// Method rather than getter, as we will add query parameters later
Future<List<UserGroup>> getUserGroups();
Expand Down
3 changes: 2 additions & 1 deletion packages/dart/noports_core/lib/src/npa/npa_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class NPAImpl with AtClientBindings implements NPA {
'${jsonPrettyPrinter.convert(request.toJson())}');
// We will send a 'log' notification to the loggingAtsign
var logKey = AtKey()
..key = '${DateTime.now().millisecondsSinceEpoch}.log.policy'
..key = '${DateTime.now().millisecondsSinceEpoch}.logs.policy'
..sharedBy = authorizerAtsign
..sharedWith = loggingAtsign
..namespace = DefaultArgs.namespace
Expand Down Expand Up @@ -141,6 +141,7 @@ class NPAImpl with AtClientBindings implements NPA {
}
await notify(
logKey,
// TODO Make a PolicyLogEvent and use PolicyLogEvent.toJson()
jsonEncode(
{
'daemon': fromAtSign,
Expand Down
5 changes: 2 additions & 3 deletions packages/dart/sshnoports/bin/npp.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ void main(List<String> args) async {
if (p.verbose) {
AtSignLogger.root_level = 'INFO';
}
AtSignLogger.defaultLoggingHandler = AtSignLogger.stdErrLoggingHandler;


logger = AtSignLogger(' npp ');
AtClient atClient = await createAtClientCli(
Expand Down Expand Up @@ -52,9 +54,6 @@ void main(List<String> args) async {
sshnpa.logger.logger.level = Level.INFO;
}

AtSignLogger.root_level = 'SHOUT';
AtSignLogger.defaultLoggingHandler = AtSignLogger.stdErrLoggingHandler;

atClient.notificationService.subscribe(
regex: r'.*\.groups\.policy\.sshnp',
shouldDecrypt: true,
Expand Down

0 comments on commit 1be9c68

Please sign in to comment.