Aaryamann Challani e7b2b88f5b
fix(rln-relay): modify keystore credentials logic (#1956)
* fix(rln-relay): modify keystore credentials logic

fix: bump version

* Update waku/waku_rln_relay/group_manager/on_chain/group_manager.nim

Co-authored-by: Ivan Folgueira Bande <128452529+Ivansete-status@users.noreply.github.com>

* Update tests/waku_rln_relay/test_waku_rln_relay.nim

Co-authored-by: Ivan Folgueira Bande <128452529+Ivansete-status@users.noreply.github.com>

* Update waku/waku_keystore/protocol_types.nim

Co-authored-by: Ivan Folgueira Bande <128452529+Ivansete-status@users.noreply.github.com>

* fix: greatly improve error handling

* fix: display proc and appropriate assert

---------

Co-authored-by: Ivan Folgueira Bande <128452529+Ivansete-status@users.noreply.github.com>
2023-08-29 17:46:21 +05:30

65 lines
2.4 KiB
Nim

when (NimMajor, NimMinor) < (1, 4):
{.push raises: [Defect].}
else:
{.push raises: [].}
import
json,
std/[os, sequtils]
import
./keyfile,
./protocol_types
# Checks if a JsonNode has all keys contained in "keys"
proc hasKeys*(data: JsonNode, keys: openArray[string]): bool =
return all(keys, proc (key: string): bool = return data.hasKey(key))
# Safely saves a Keystore's JsonNode to disk.
# If exists, the destination file is renamed with extension .bkp; the file is written at its destination and the .bkp file is removed if write is successful, otherwise is restored
proc save*(json: JsonNode, path: string, separator: string): KeystoreResult[void] =
# We first backup the current keystore
if fileExists(path):
try:
moveFile(path, path & ".bkp")
except: # TODO: Fix "BareExcept" warning
return err(AppKeystoreError(kind: KeystoreOsError,
msg: "could not backup keystore: " & getCurrentExceptionMsg()))
# We save the updated json
var f: File
if not f.open(path, fmAppend):
return err(AppKeystoreError(kind: KeystoreOsError,
msg: getCurrentExceptionMsg()))
try:
# To avoid other users/attackers to be able to read keyfiles, we make the file readable/writable only by the running user
setFilePermissions(path, {fpUserWrite, fpUserRead})
f.write($json)
# We store a keyfile per line
f.write(separator)
except CatchableError:
# We got some KeystoreOsError writing to disk. We attempt to restore the previous keystore backup
if fileExists(path & ".bkp"):
try:
f.close()
removeFile(path)
moveFile(path & ".bkp", path)
except: # TODO: Fix "BareExcept" warning
# Unlucky, we just fail
return err(AppKeystoreError(kind: KeystoreOsError,
msg: "could not restore keystore backup: " & getCurrentExceptionMsg()))
return err(AppKeystoreError(kind: KeystoreOsError,
msg: "could not write keystore: " & getCurrentExceptionMsg()))
finally:
f.close()
# The write went fine, so we can remove the backup keystore
if fileExists(path & ".bkp"):
try:
removeFile(path & ".bkp")
except CatchableError:
return err(AppKeystoreError(kind: KeystoreOsError,
msg: "could not remove keystore backup: " & getCurrentExceptionMsg()))
return ok()