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

Issue #121: BasicAuth REST API #147

Merged
merged 1 commit into from Oct 16, 2017
Merged

Issue #121: BasicAuth REST API #147

merged 1 commit into from Oct 16, 2017

Conversation

dgrisham
Copy link
Collaborator

@dgrisham dgrisham commented Sep 7, 2017

@hsanjuan Here's a start to the BasicAuth implementation. Todos:

  • Server-side
    • Default to HTTPS when BasicAuth active
    • Better response to bad credentials.
  • CLI flags for server
    • (De?)activate BasicAuth
    • Force BasicAuth w/ HTTP (HTTPS by default)
  • Client-side
    • BasicAuth impl
    • -c <username>:<password> flag
    • Env var input for username/password
      • CLUSTER_CREDENTIALS="<username>:<password>"
    • Default to HTTPS with BasicAuth (flag to override) -- this mirrors the server behavior
  • Tests
    • Sharness

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.1%) to 73.707% when pulling b010c40 on feat/api-basic-auth into af8a385 on master.

Copy link
Collaborator

@hsanjuan hsanjuan left a comment

Choose a reason for hiding this comment

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

Hey, looks very good so far!

func NewTLSRESTAPI(apiMAddr ma.Multiaddr, tlsCfg *tls.Config) (*RESTAPI, error) {
// NewRESTAPI creates a new REST API component. It receives
// the multiaddress on which the API listens.
func NewRESTAPIWithConfig(apiMAddr ma.Multiaddr, cfg *Config) (*RESTAPI, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should include the apiMaddr as part of the configuration I guess

Copy link
Collaborator Author

@dgrisham dgrisham Sep 20, 2017

Choose a reason for hiding this comment

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

Sounds good -- the only difference that I see so far is that the apiMAddr is required, while everything in the cfg is optional. Not sure if that difference matters much, but for now I'm combining them into a single config (so we'll have NewRESTAPI(cfg *Config)).

@@ -56,6 +56,11 @@ type RESTAPI struct {
wg sync.WaitGroup
}

type Config struct {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Don't forget to document public types

@dgrisham
Copy link
Collaborator Author

dgrisham commented Oct 4, 2017

@hsanjuan @ZenGround0 Just made a push, the current state should be consistent with the Todo list in the OP.

@dgrisham dgrisham force-pushed the feat/api-basic-auth branch 2 times, most recently from 2bf672a to 9d000a1 Compare October 7, 2017 21:23

test_expect_success "BasicAuth fails without credentials" '
id=`cluster_id`
ipfs-cluster-ctl id | grep -q Unauthorized
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm not convinced checking for 'Unauthorized' in the response is the best approach to this -- e.g. we could accidentally be leaking info in some case but the presence of the word 'Unauthorized' would allow this test to pass anyway.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Copy link
Collaborator

Choose a reason for hiding this comment

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

@dgrisham check that the command did not return 0 for exit code and then check for Unauthorized (should be ok for the moment).

@dgrisham
Copy link
Collaborator Author

dgrisham commented Oct 9, 2017

@hsanjuan Two questions on the remaining Todos I have listed:

  • Config file to hold cluster-ctl config -- Should I leave that to Configuration file with sections per component #162 (or some other PR) and just support CLI/env var input for now?
  • Go tests -- sharness seems sufficient/the best way to test this (think we did the same for TLS), but lmk if you think we need Go tests here too

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.4%) to 73.409% when pulling ca6cf9a on feat/api-basic-auth into 72d0d22 on master.

@@ -105,11 +107,30 @@ func main() {
Name: "debug, d",
Usage: "set debug log level",
},
cli.StringFlag{
Name: "credentials, c",
Usage: "specify BasicAuth credentials for server that requires " +
Copy link
Collaborator

Choose a reason for hiding this comment

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

Would need a hint that they are passed as "user:password"

@@ -105,11 +107,30 @@ func main() {
Name: "debug, d",
Usage: "set debug log level",
},
cli.StringFlag{
Name: "credentials, c",
Copy link
Collaborator

Choose a reason for hiding this comment

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

perhaps rename to --basic-auth (or even -u, --user USER[:PASSWORD] Server user and password like curl does) . Not sure if it deserves a short-hand alias. I'd like to keep -c for config (in line with server).

Copy link
Collaborator Author

@dgrisham dgrisham Oct 11, 2017

Choose a reason for hiding this comment

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

Hm, so right now (I think) an empty password would work by passing --basic-auth <username>: (note the : at the end) but you'd get an error if you just passed --basic-auth <username> (no :). Might be nice to support the latter, I'll make that change.

Copy link
Collaborator

Choose a reason for hiding this comment

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

sounds good

@hsanjuan
Copy link
Collaborator

Seems sharness is complaining too. But other than that, should be good to go

@hsanjuan
Copy link
Collaborator

Config file to hold cluster-ctl config -- Should I leave that to #162 (or some other PR) and just support CLI/env var input for now?

Yes, let's leave it like that for the moment

Go tests -- sharness seems sufficient/the best way to test this (think we did the same for TLS), but lmk if you think we need Go tests here too

Sharness is ok

@hsanjuan
Copy link
Collaborator

hsanjuan commented Oct 11, 2017

Implements #121

@hsanjuan hsanjuan changed the title BasicAuth REST API Issue #121: BasicAuth REST API Oct 11, 2017
@dgrisham dgrisham force-pushed the feat/api-basic-auth branch 2 times, most recently from b38b25a to b36b3db Compare October 12, 2017 13:22
@ZenGround0
Copy link
Collaborator

@dgrisham I had the same Travis problem yesterday. Rebasing on top of master should do the trick.

client := &http.Client{Transport: defaultTransport}
resp, err := client.Do(r)
if err == nil && resp.StatusCode >= 400 {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Now that this has been brought to my attention :)

I think the best place to detect error and set exit code in such case is the end of formatResponse. This ensures the received data/error is shown to the user in the desired format. Only then you can check resp.StatusCode and exit with 2 when it was not a successful request.

Copy link
Collaborator Author

@dgrisham dgrisham Oct 14, 2017

Choose a reason for hiding this comment

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

Gotcha, yeah I was definitely planning on discussing/correcting this approach. And out of curiosity, why exit code 2 specifically? Is there a good rule of thumb for the first few exit codes that I can keep in mind?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Nah, I said 2 because 1 is used for the rest of errors, so just to use a different one for errors which are from responses. It's completely arbitrary...

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Gotcha, good to know. We should document that somewhere for any users who might want to distinguish between to the two.

@dgrisham
Copy link
Collaborator Author

@ZenGround0 Ah, dope, thanks for the tip :)

@dgrisham dgrisham force-pushed the feat/api-basic-auth branch 2 times, most recently from 6f320de to 2ac98dd Compare October 14, 2017 19:35
@coveralls
Copy link

Coverage Status

Coverage decreased (-0.4%) to 73.494% when pulling 2ac98dd on feat/api-basic-auth into da0cb5e on master.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.4%) to 73.518% when pulling 25a910f on feat/api-basic-auth into da0cb5e on master.

@dgrisham
Copy link
Collaborator Author

@hsanjuan Tests passing! Let me know if there are any other changes you think I should make.

@dgrisham
Copy link
Collaborator Author

@hsanjuan Don't merge this quite yet, there are a couple of sharness tests I want to add first.

Copy link
Collaborator

@hsanjuan hsanjuan left a comment

Choose a reason for hiding this comment

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

awesome thanks!

@hsanjuan hsanjuan merged commit 6e40610 into master Oct 16, 2017
@ghost ghost removed the status/in-progress In progress label Oct 16, 2017
@hsanjuan hsanjuan deleted the feat/api-basic-auth branch October 16, 2017 11:22
@hsanjuan hsanjuan mentioned this pull request Oct 17, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants