clever-tools icon indicating copy to clipboard operation
clever-tools copied to clipboard

Prevent a user from joining a networkgroup twice

Open RemiBardon opened this issue 4 years ago • 1 comments

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

  1. 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.
  2. 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..

RemiBardon avatar Feb 15 '21 10:02 RemiBardon

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;
}

RemiBardon avatar Feb 15 '21 12:02 RemiBardon