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

rpcclient: Add cookie auth #1460

Merged
merged 4 commits into from
Jun 8, 2020
Merged

Conversation

JeremyRand
Copy link
Contributor

Fixes #1054

Copy link
Contributor

@Rjected Rjected left a comment

Choose a reason for hiding this comment

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

I think it's a good idea to try to replicate Core's behavior here - it is a useful feature and would be even more useful if it worked the same way and were a little more defined.


// retrieveCookie is a function that returns the cookie username and
// passphrase.
retrieveCookie func() (username, passphrase string, err error)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there any reasoning why this is a member closure? It seems like this is just keeping track of lastCheckTime and lastModTime.
Could this somehow be replaced with

func (config *ConnConfig) retrieveCookie() (username, passphrase string, err error) {
    // access config.lastModTime and config.lastCheckTime ...
}

that way the path does not need to be passed in and config.CookiePath can be accessed instead?

// this connection. This will be the result of checking the cookie if a cookie
// path is configured; if not, it will be the user-configured username and
// passphrase.
func (config *ConnConfig) getAuth() (username, passphrase string, err error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Bitcoin Core falls back to cookie authentication if a password is not found, we might want to replicate that functionality rather than fall back to username/password authentication if a cookie is not found.

Additionally, there is no example of cookie creation - this is a relevant feature, and it is currently undefined.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Additionally, there is no example of cookie creation - this is a relevant feature, and it is currently undefined.

Hmm, what functionality are you looking for exactly? Making the btcd daemon support creating a cookie file?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yup - something like in https://github.com/bitcoin/bitcoin/blob/master/src/rpc/request.cpp
Just so users don't have to generate using core first, and this can be a little more feature complete.

Copy link
Contributor

Choose a reason for hiding this comment

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

Out of scope for this PR though

return
}

s := strings.TrimSpace(string(b))
Copy link
Contributor

Choose a reason for hiding this comment

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

Considering Core uses base64 encoding for their client cookie authentication, those libraries could be used instead of a binary => string cast

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're talking about https://github.com/bitcoin/bitcoin/blob/f4a0d27e85754d60804ffa36e415b67c263180b9/src/bitcoin-cli.cpp#L345 , right? AFAICT from looking at the Bitcoin Core source code, while it's true that the Core cookie reader doesn't try to parse out a separate username and password (and instead simply submits the line extracted from the cookie file verbatim), the code that actually creates the cookie file does distinguish between the username and password components.

I think it's generally a good thing to try to parse out the username and password separately, for 2 reasons:

  1. It simplifies the integration with existing code that expects authentication to use a username+password rather than a combined blob.
  2. It will better detect cases where the cookie file doesn't look like a standard cookie file, which are probably an indication of some kind of error.

Copy link
Contributor

Choose a reason for hiding this comment

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

That makes sense, I'm cool with the user/pass separation. I still have an issue with this line though - why are the spaces being trimmed? I just added a space in a cookie file with Core and it worked.
Parsing only the first line of the file makes sense, and it is what core does in https://github.com/bitcoin/bitcoin/blob/42d0eca725a83c999e2b67e33dfc7bcc96288dc3/src/rpc/request.cpp#L108, but spaces in general seem a little arbitrary

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Replaced TrimSpace with reading the first line.

@JeremyRand
Copy link
Contributor Author

Thanks for the feedback @Rjected , I'll address the feedback ASAP. Things are a bit busy here at the moment, so I might take a little while.

@JeremyRand
Copy link
Contributor Author

Updated PR to address feedback.

Copy link
Contributor

@Rjected Rjected left a comment

Choose a reason for hiding this comment

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

Cookie generation could be a separate PR, this PR looks good to me once TrimSpaces is sorted out

Copy link
Contributor

@Rjected Rjected left a comment

Choose a reason for hiding this comment

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

LGTM

JeremyRand added a commit to namecoin/ncdns that referenced this pull request Oct 27, 2019
2b1eed8 Switch to new rpcclient based on latest upstream Conformal (JeremyRand)

Pull request description:

  Namecoin's fork of Conformal's RPC client library is ancient and unmaintained; switching to upstream eliminates a major source of potential bugs (and known bugs, e.g. the ConsensusJ and Electrum compatibility bugs that we had to patch).

  Fixes #9

  TODO:

  - [ ] Wait for Conformal to merge btcsuite/btcd#1457
  - [ ] Wait for Conformal to merge btcsuite/btcd#1460
  - [x] Push `name_show` support for `ncjson` and `ncrpcclient`
  - [x] Implement cookie authentication
  - [x] Test cookie authentication

ACKs for commit 2b1eed:

Tree-SHA512: 157780613661af240d83a78d66386c66fcfeed0700088d263a20389c45b200db1b651ea985f345de2ac2f250bdafcc4cd5901579e9f5a97ddc13e77e7a7bcf39
@torkelrogstad
Copy link
Contributor

Tested this locally worked like a charm.

LGTM

@jakesylvestre
Copy link
Collaborator

@jcvernaleo (as per #1530)

  • Low priority
  • Enhancement

Well tested, mergable with minimal review

Based on Hugo Landau's cookie auth implementation for Namecoin's ncdns.

Fixes btcsuite#1054
@JeremyRand
Copy link
Contributor Author

Rebased and fixed merge conflicts.

Copy link
Member

@jcvernaleo jcvernaleo left a comment

Choose a reason for hiding this comment

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

OK

@jcvernaleo jcvernaleo merged commit 6d521ff into btcsuite:master Jun 8, 2020
@onyb onyb mentioned this pull request Jul 10, 2020
@JeremyRand JeremyRand deleted the cookie-auth branch October 2, 2021 17:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

rpcclient: Support cookie authentication
5 participants