couchbase / go-couchbase

Couchbase client in Go
https://godoc.org/github.com/couchbase/go-couchbase
MIT License
321 stars 92 forks source link

Auth issue when connecting to default bucket via cbdatasource #62

Closed tleyden closed 9 years ago

tleyden commented 9 years ago

I'm connecting to a default bucket via CBGT, which uses the go-couchbase cbdatasource connection library.

The problem appears to be in this method:

func maybeAddAuth(req *http.Request, ah AuthHandler) error {
    if hah, ok := ah.(HTTPAuthHandler); ok {
        return hah.SetCredsForRequest(req)
    }
    if ah != nil {
        user, pass, _ := ah.GetCredentials()
        req.Header.Set("Authorization", "Basic "+
            base64.StdEncoding.EncodeToString([]byte(user+":"+pass)))
    }
    return nil
}

Even though user and pass are both empty, it tries to add the Basic Authorization header, which ends up failing to connect with 401 errors.

A workaround that appears to work is:

func maybeAddAuth(req *http.Request, ah AuthHandler) error {
    if hah, ok := ah.(HTTPAuthHandler); ok {
        return hah.SetCredsForRequest(req)
    }
    if ah != nil {
        user, pass, _ := ah.GetCredentials()
        if user != "" {
            req.Header.Set("Authorization", "Basic "+
                base64.StdEncoding.EncodeToString([]byte(user+":"+pass)))
        }
    }
    return nil
}

This works around the issue by only adding the Basic Auth header if the user is non-empty.

I'm not sure why it has a non-nil AuthHandler if the username and password are both empty. That might be a bug somewhere up in the callstack of the code that calls this method. Even so, checking for a non-empty user seems like good defensive coding, since it doesn't seem like a valid use case to set a Basic Auth header with an empty user.

steveyen commented 9 years ago

cc'ing @nimishzynga

steveyen commented 9 years ago

Looks like that maybeAddAuth() hasn't changed for awhile. So, it might be some higher-level code that changed related to auth.

https://github.com/couchbase/go-couchbase/commit/89d88b58c124de71421a8ed027c022b932a78bca

https://github.com/couchbaselabs/cbgt/commit/f3d5224aa

https://github.com/couchbaselabs/cbgt/commit/699851441

steveyen commented 9 years ago

Hi Traun, Just tried it with cbft, and it worked. How are you connecting to the default bucket? By passing in "default" or by passing in empty string ("")?

tleyden commented 9 years ago

Closing this issue as it looks like it's probably not a go-couchbase issue.