Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Persisting rln credentials #1037

Merged
merged 33 commits into from
Aug 5, 2022
Merged

Persisting rln credentials #1037

merged 33 commits into from
Aug 5, 2022

Conversation

kgrgpg
Copy link
Contributor

@kgrgpg kgrgpg commented Jul 20, 2022

This code implements the persistence of MembershipKeyPair and MembershipIndex to the RLN membership contract. This is done by storing raw unencrypted JSON text in the files keyPair.txt and rlnIndex.txt when the first registration takes place. At a later stage, these credentials need to be stored in an encrypted format. One of the ways to implement that encryption could be similar to how keystore is implemented in Nimbus. But for now this should serve the purpose of credentials persistence saving user funds.

Copy link
Contributor

@kaiserd kaiserd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 on your first nwaku PR :).

What are the security implications of printing / persisting the membership key in the clear?

Also, I left some nit comments inline.

waku/v2/protocol/waku_rln_relay/waku_rln_relay_utils.nim Outdated Show resolved Hide resolved
waku/v2/protocol/waku_rln_relay/waku_rln_relay_utils.nim Outdated Show resolved Hide resolved
waku/v2/protocol/waku_rln_relay/waku_rln_relay_utils.nim Outdated Show resolved Hide resolved
waku/v2/protocol/waku_rln_relay/waku_rln_relay_utils.nim Outdated Show resolved Hide resolved
waku/v2/protocol/waku_rln_relay/waku_rln_relay_utils.nim Outdated Show resolved Hide resolved
waku/v2/protocol/waku_rln_relay/waku_rln_relay_utils.nim Outdated Show resolved Hide resolved
@kgrgpg
Copy link
Contributor Author

kgrgpg commented Jul 21, 2022

@kaiserd Since the files are stored as a raw text file, it is highly susceptible to theft. The files needs some encryption to resolve this.

With regards to printing the keys, it is purely for debugging purposes so that the user becomes explicitly aware of the current keys in use when nwaku is started. Note that this is only until the RLN contract being used is the one deployed on Goerli testnet. These prints need to omitted once RLN contract is deployed on Ethereum mainnet and using valuable funds for staking.

@kaiserd
Copy link
Contributor

kaiserd commented Jul 21, 2022

Thank you for the clarification :).
I'd add comments in the code explaining the reason for both

  • storing the raw key on disc
  • printing the raw key

@kgrgpg
Copy link
Contributor Author

kgrgpg commented Jul 21, 2022

Security warning incorporated as comments within code base.

Copy link
Contributor

@s1fr0 s1fr0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Congrats for your first nwaku PR! Overall good, I left some comments.

waku/v2/protocol/waku_rln_relay/waku_rln_relay_utils.nim Outdated Show resolved Hide resolved
waku/v2/protocol/waku_rln_relay/waku_rln_relay_utils.nim Outdated Show resolved Hide resolved
.gitignore Outdated Show resolved Hide resolved
waku/v2/protocol/waku_rln_relay/waku_rln_relay_utils.nim Outdated Show resolved Hide resolved
waku/v2/protocol/waku_rln_relay/waku_rln_relay_utils.nim Outdated Show resolved Hide resolved
waku/v2/protocol/waku_rln_relay/waku_rln_relay_utils.nim Outdated Show resolved Hide resolved
waku/v2/protocol/waku_rln_relay/waku_rln_relay_utils.nim Outdated Show resolved Hide resolved
Copy link
Contributor

@jm-clius jm-clius left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @kgrgpg! Left some comments on Nim styling and abstracting the file-specific logic and magic constants - if this is to be short-lived until we have a proper keystore, the reading/writing and file types should not be in line with the main logic of the code but abstracted to modular procs.

waku/v2/protocol/waku_rln_relay/waku_rln_relay_utils.nim Outdated Show resolved Hide resolved
waku/v2/protocol/waku_rln_relay/waku_rln_relay_utils.nim Outdated Show resolved Hide resolved
@@ -818,7 +833,7 @@ proc mountRlnRelayDynamic*(node: WakuNode,
node.wakuRlnRelay = rlnPeer


proc mountRlnRelay*(node: WakuNode, conf: WakuNodeConf) {.raises: [Defect, ValueError, IOError, CatchableError].} =
proc mountRlnRelay*(node: WakuNode, conf: WakuNodeConf) {.raises: [Defect, ValueError, IOError, CatchableError, Exception].} =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is the Exception raised from? Couldn't we find the exact type of Exception being raised and catch it here? Now we have a list of Defect, CatchableError and Exception which just means "everything". I know it's not always easy to catch specific exceptions from submodules. Also see: https://status-im.github.io/nim-style-guide/errors.exceptions.html

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been opened as a new issue. See #1045

So far it seems like an OSError but raising it is still giving error that appropriate exception is not raised.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a few function calls in this proc that can potentially raise an exception, i.e., readFile, parseJson and
to(jsonObject, R andlnMembershipCredentials), have you inspected those to see what they raise?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving all discussions related to Exception handling to the new issue. Review this PR on persistence primarily. @staheri14 Your comment is linked in new issue. #1045 (comment)

@@ -861,8 +876,32 @@ proc mountRlnRelay*(node: WakuNode, conf: WakuNodeConf) {.raises: [Defect, Value
let memKeyPair = keyPair.toMembershipKeyPairs()[0]
# mount the rln relay protocol in the on-chain/dynamic mode
waitFor node.mountRlnRelayDynamic(memContractAddr = ethMemContractAddress, ethClientAddr = ethClientAddr, memKeyPair = some(memKeyPair), memIndex = some(rlnRelayIndex), ethAccAddr = ethAccountAddr, ethAccountPrivKey = ethAccountPrivKey, pubsubTopic = conf.rlnRelayPubsubTopic, contentTopic = conf.rlnRelayContentTopic)
elif fileExists("keyPair.txt") and fileExists("rlnIndex.txt"):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I may not understand the exact context, but if the idea is simply for this "write to cleartext file" to be a temporary stopgap until we have an integrated keystore, the logic should be abstracted to separate functions with no magic references to the .txt files in the main logic.

In other words, instead of having an elif fileExists(... here, it should be something like elif usePersistentKeys() where usePersistentKeys() are implemented elsewhere. And instead of having the logic to read the keys from the files here it should be abstracted to a readKeys() proc, etc.

Copy link
Contributor Author

@kgrgpg kgrgpg Jul 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion incorporated. Refer to 6236dd6

waku/v2/protocol/waku_rln_relay/waku_rln_relay_utils.nim Outdated Show resolved Hide resolved
@status-im-auto
Copy link
Collaborator

status-im-auto commented Jul 22, 2022

Jenkins Builds

Click to see older builds (6)
Commit #️⃣ Finished (UTC) Duration Platform Result
✔️ dc61be0 #1 2022-07-22 04:15:04 ~27 min macos 📦bin
dc61be0 #1 2022-07-22 10:16:51 ~32 min windows 📄log
✔️ dc61be0 #1 2022-07-22 12:10:07 ~19 min linux 📦bin
✔️ 7162bfa #2 2022-07-24 16:13:34 ~26 min macos 📦bin
7162bfa #2 2022-07-24 22:11:03 ~26 min windows 📄log
135b72a #3 2022-07-31 04:08:04 ~20 min macos 📄log
Commit #️⃣ Finished (UTC) Duration Platform Result
✔️ 08ab7a1 #4 2022-08-02 04:08:54 ~21 min macos 📦bin
✔️ 1ad6b4d #2 2022-08-28 15:59:08 ~19 min linux 📦bin

@kgrgpg kgrgpg force-pushed the Persisting-rln-credentials branch from dc61be0 to 5a15930 Compare July 22, 2022 21:03
@kgrgpg kgrgpg force-pushed the Persisting-rln-credentials branch from 5a15930 to b375418 Compare July 22, 2022 22:47
Reading credentials from file abstracted away.
@kgrgpg kgrgpg force-pushed the Persisting-rln-credentials branch from 39a30ec to 6236dd6 Compare July 24, 2022 11:20
@s1fr0
Copy link
Contributor

s1fr0 commented Jul 28, 2022

@kgrgpg Note that tests fail and there exist some conflicts that has to be addressed before merging

/home/runner/work/nwaku/nwaku/waku/v2/protocol/waku_rln_relay/waku_rln_relay_utils.nim(840) readPersistentRlnCredentials
/home/runner/work/nwaku/nwaku/vendor/nimbus-build-system/vendor/Nim/lib/system/io.nim(693) readFile

    Unhandled exception: cannot open: testPath.txt [IOError]
  [FAILED] Read Persistent RLN credentials

Please ping reviewers when ready.

Abstrated writePersistentRlnCredentials usage causing error,
with readPersistentRlnCredentials
@kgrgpg
Copy link
Contributor Author

kgrgpg commented Jul 29, 2022

@s1fr0 Usage of writePersistentRlnCredentials in the test is resulting in an IOError while reading.
Even though usage of this write abstract function works correctly within waku_rln_relay_utils.nim.

When I reverted to a direct call to writeFile within the test, the reading becomes successful. Refer f10d873

Now I am perplexed, why this is the case. I read at https://nim-lang.org/docs/io.html#writeFile%2Cstring%2Cstring that writeFile closes the file after the operation is complete. I am wondering if this is failing for some reason when contained inside the abstracted function writePersistentRlnCredentials as readPersistentRlnCredentials gets called right away in the test. Do you have any clue with regards to this?

Copy link
Contributor

@s1fr0 s1fr0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not push commits related to vendor (so revert this). Regularly run make update to update submodules.

This reverts commit 3f4dba1.
@kgrgpg
Copy link
Contributor Author

kgrgpg commented Jul 29, 2022

Do not push commits related to vendor (so revert this). Regularly run make update to update submodules.

Thanks for this info. Make update resulted in new checkout for dnsclient. Concerned commit reverted.

..persiting credential
@kgrgpg
Copy link
Contributor Author

kgrgpg commented Aug 1, 2022

The abstract proc writePersistentRlnCredentials is reverted in commit 08ab7a1. The reason for this is explained in #1037. Since this abstract proc is not necessary for the "persistence" implementation of the RLN credentials and the test associated with it are valid and pass without this abstraction, this is now opened as a new issue #1053 (comment)

let jsonObject = parseJson(entireRlnCredentialsFile)
let deserializedRlnCredentials = to(jsonObject, RlnMembershipCredentials)

debug "Deserialized Rln credentials", rlnCredentials=deserializedRlnCredentials
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer that we do not dump the content of the credentials through the stdout

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refer to #1037 (comment)

@kgrgpg kgrgpg requested a review from LNSD August 2, 2022 22:25
Copy link
Contributor

@LNSD LNSD left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM ✅

Congrats on you first contribution 😁

@LNSD
Copy link
Contributor

LNSD commented Aug 4, 2022

IMHO We need to do an overall code review for the RLN part before it becomes hard to maintain (e.g. @s1fr0 has already experienced issues with the Zerokit integration).

Right now I am fully focused on the Waku Store issue, but let's see if we can find a slot after the next nwaku's release to improve the code maintainability.

cc @staheri14 @kgrgpg @s1fr0

@kgrgpg
Copy link
Contributor Author

kgrgpg commented Aug 4, 2022

@LNSD Thanks but technically this is my second PR :D
First one is the tutorial at #1031
And agree, always up to improve code readability and maintainability.

Copy link
Contributor

@s1fr0 s1fr0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All comments where addressed except abstraction of writePersistentRlnCredentials and byte serialization/storage. These changes are tracked in #1053 and #1041 (encryption somehow implies byte serialization).

@LNSD
Copy link
Contributor

LNSD commented Aug 5, 2022

@kgrgpg If the work in this PR is finished, could you rebase, solve the conflicts and merge the PR into master branch?

@kgrgpg kgrgpg merged commit 115982f into master Aug 5, 2022
@kgrgpg kgrgpg deleted the Persisting-rln-credentials branch August 5, 2022 10:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
track:rln RLN Track (Secure Messaging/Applied ZK), e.g. relay and applications
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants