clever-tools
clever-tools copied to clipboard
Prevent a user from joining a networkgroup twice
Context
Currently, when a user joins a networkgroup (with clever ng join), we create temporary files, start fireguard-go and start listening to the configuration SSE of the given networkgroup.
Problem
A user can join a networkgroup from two different terminals. Since files are created or overwritten (fs.writeFile default file system flag is "'w': Open file for writing. The file is created (if it does not exist) or truncated (if it exists)."),
Proposed solutions
- Use "
'wx': Like'w'but fails if the path exists.". Write would throw but an external peer would be created. It would need special handling. - Check if files exist before needing to write the files (but still use
'wx'as a security with no error handling), and show a useful error message. Possible issue: the check could be out of sync with the code (if one day we decide to store another file for example).
I prefer option 2..
I just did option 2., but added the automatic deletion of a possibly created external peer if error was thrown at file-writing time.
try {
storePeerId(peerId, confName);
}
catch (error) {
// If networkgroup already joined, remove freshly created external peer
await removeExternalPeer({ options: { ng: { ng_id: ngId }, 'peer-id': peerId } });
throw error;
}