Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
mobile: keep the PeerID when unenrolling
Tomas Tormoe6577d7fb2ff
diff --git a/mobile/sam-node-app/lib/main.dart b/mobile/sam-node-app/lib/main.dartindex c8e71b0b..cd649dd8 100644--- a/mobile/sam-node-app/lib/main.dart+++ b/mobile/sam-node-app/lib/main.dart@@ -54,6 +54,17 @@ Future<String?> _isolatedEnrollBootstrap(String dataDir, String server, String t}});+Future<String?> _isolatedUnenroll(String dataDir) => Isolate.run(() {+ try {+ return SamNodeLib().unenroll(dataDir);+ } catch (e) {+ return e.toString();+ }+});++/// Unenroll keeps the PeerID; resetIdentity deletes the key behind it too.+enum _UnenrollChoice { unenroll, resetIdentity }+void main() async {WidgetsFlutterBinding.ensureInitialized();try {@@ -1086,46 +1097,70 @@ class _NodeControlPageState extends State<NodeControlPage> {}Future<void> _unenroll() async {- final confirm = await showDialog<bool>(+ final choice = await showDialog<_UnenrollChoice>(context: context,builder: (context) => AlertDialog(title: const Text('Unenroll Node'),content: const Text(- 'Are you sure you want to unenroll? This will delete your local identity and disconnect you from the mesh.'),+ 'Unenroll disconnects this device from the mesh and deletes its '+ 'credentials. The PeerID is kept, so re-enrolling brings back the '+ 'same node.\n\n'+ 'Reset device identity also deletes the key behind the PeerID and '+ 'every local setting, so the next enrollment looks like a device '+ 'the mesh has never seen.'),actions: [TextButton(- onPressed: () => Navigator.pop(context, false),+ onPressed: () => Navigator.pop(context),child: const Text('Cancel')),TextButton(- onPressed: () => Navigator.pop(context, true),+ onPressed: () =>+ Navigator.pop(context, _UnenrollChoice.resetIdentity),+ child: const Text('Reset device identity',+ style: TextStyle(color: Colors.red)),+ ),+ TextButton(+ onPressed: () => Navigator.pop(context, _UnenrollChoice.unenroll),child: const Text('Unenroll', style: TextStyle(color: Colors.red)),),],),);- if (confirm == true) {- if (_running) {- _stop();- }- final appDir = await getApplicationDocumentsDirectory();- final dataDir = '${appDir.path}/sam_data';- try {+ if (choice == null) return;++ // Closing the store here is what lets UnenrollNode take its file lock.+ if (_running) {+ _stop();+ }+ final appDir = await getApplicationDocumentsDirectory();+ final dataDir = '${appDir.path}/sam_data';+ try {+ if (choice == _UnenrollChoice.resetIdentity) {final dir = Directory(dataDir);if (await dir.exists()) {await dir.delete(recursive: true);}- setState(() {- _isEnrolled = false;- _joinedWithToken = false;- _status = 'Unenrolled';- _nodeID = '';- });- } catch (e) {- setState(() {- _status = 'Failed to unenroll: $e';- });+ } else {+ final err = await _isolatedUnenroll(dataDir);+ if (err != null) {+ setState(() {+ _status = 'Failed to unenroll: $err';+ });+ return;+ }}+ setState(() {+ _isEnrolled = false;+ _joinedWithToken = false;+ _status = choice == _UnenrollChoice.resetIdentity+ ? 'Device identity reset'+ : 'Unenrolled (PeerID kept)';+ _nodeID = '';+ });+ } catch (e) {+ setState(() {+ _status = 'Failed to unenroll: $e';+ });}}diff --git a/mobile/sam-node-app/lib/sam_ffi.dart b/mobile/sam-node-app/lib/sam_ffi.dartindex a358f80c..d53ae5f9 100644--- a/mobile/sam-node-app/lib/sam_ffi.dart+++ b/mobile/sam-node-app/lib/sam_ffi.dart@@ -46,6 +46,9 @@ typedef EnrollNodeBootstrapDart = ffi.Pointer<Utf8> Function(int allowLoopback,ffi.Pointer<Utf8> labels);+typedef UnenrollNodeC = ffi.Pointer<Utf8> Function(ffi.Pointer<Utf8> dataDir);+typedef UnenrollNodeDart = ffi.Pointer<Utf8> Function(ffi.Pointer<Utf8> dataDir);+typedef FetchControlPlaneInfoJSONC = ffi.Pointer<Utf8> Function(ffi.Pointer<Utf8> controlPlaneURL);typedef FetchControlPlaneInfoJSONDart = ffi.Pointer<Utf8> Function(ffi.Pointer<Utf8> controlPlaneURL);@@ -66,6 +69,7 @@ class SamNodeLib {late EnrollNodeDart _enrollNode;late ReEnrollNodeDart _reEnrollNode;late EnrollNodeBootstrapDart _enrollNodeBootstrap;+ late UnenrollNodeDart _unenrollNode;late FetchControlPlaneInfoJSONDart _fetchControlPlaneInfoJSON;late IsEnrolledDart _isEnrolled;late GetMeshInfoDart _getMeshInfo;@@ -86,6 +90,7 @@ class SamNodeLib {_enrollNode = _dylib.lookupFunction<EnrollNodeC, EnrollNodeDart>('EnrollNode');_reEnrollNode = _dylib.lookupFunction<ReEnrollNodeC, ReEnrollNodeDart>('ReEnrollNode');_enrollNodeBootstrap = _dylib.lookupFunction<EnrollNodeBootstrapC, EnrollNodeBootstrapDart>('EnrollNodeBootstrap');+ _unenrollNode = _dylib.lookupFunction<UnenrollNodeC, UnenrollNodeDart>('UnenrollNode');_fetchControlPlaneInfoJSON = _dylib.lookupFunction<FetchControlPlaneInfoJSONC, FetchControlPlaneInfoJSONDart>('FetchControlPlaneInfoJSON');_isEnrolled = _dylib.lookupFunction<IsEnrolledC, IsEnrolledDart>('IsEnrolled');_getMeshInfo = _dylib.lookupFunction<GetMeshInfoC, GetMeshInfoDart>('GetMeshInfo');@@ -157,6 +162,20 @@ class SamNodeLib {return goErr;}+ /// Clears the stored mesh identity, keeping the key behind the PeerID.+ String? unenroll(String dataDir) {+ final cDataDir = dataDir.toNativeUtf8();++ final cErr = _unenrollNode(cDataDir);++ calloc.free(cDataDir);++ if (cErr.address == 0) return null;+ final goErr = cErr.toDartString();+ _freeString(cErr);+ return goErr;+ }+/// Enrolls with a bootstrap token through POST /enroll; no identity/// provider is involved. A single-use token is burned by this call.String? enrollBootstrap(String dataDir, String controlPlaneURL,diff --git a/mobile/sam-node-ffi/ffi/ffi.go b/mobile/sam-node-ffi/ffi/ffi.goindex 7e5d9f10..62eaefea 100644--- a/mobile/sam-node-ffi/ffi/ffi.go+++ b/mobile/sam-node-ffi/ffi/ffi.go@@ -606,3 +606,21 @@ func ReEnrollNode(dataDir string, labels string) error {}return nil}++// UnenrollNode clears the stored mesh identity and keeps the key behind the+// PeerID, so re-enrolling brings back the same node. Deleting the data+// directory instead is what makes the next enrollment a new device.+func UnenrollNode(dataDir string) error {+ mu.Lock()+ isRunning := activeNode != nil || unauthSrv != nil+ mu.Unlock()+ if isRunning {+ return errors.New("stop the node before unenrolling")+ }+ store, err := node.NewStore(dataDir)+ if err != nil {+ return fmt.Errorf("failed to open store: %w", err)+ }+ defer func() { _ = store.Close() }()+ return store.ResetMeshIdentity()+}diff --git a/mobile/sam-node-ffi/main.go b/mobile/sam-node-ffi/main.goindex d417116c..b7e0e352 100644--- a/mobile/sam-node-ffi/main.go+++ b/mobile/sam-node-ffi/main.go@@ -88,6 +88,15 @@ func EnrollNodeBootstrap(dataDir *C.char, controlPlaneURL *C.char, bootstrapTokereturn nil}+//export UnenrollNode+func UnenrollNode(dataDir *C.char) *C.char {+ err := ffi.UnenrollNode(C.GoString(dataDir))+ if err != nil {+ return C.CString(err.Error())+ }+ return nil+}+//export FetchControlPlaneInfoJSONfunc FetchControlPlaneInfoJSON(controlPlaneURL *C.char) *C.char {goControlPlaneURL := C.GoString(controlPlaneURL)