forked from couchbase/gocb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GOCBC-90: Implement cluster level authentication.
Change-Id: I6a829294b4bdedf9cecdb14aed97b3bc42dfa8aa Reviewed-on: http://review.couchbase.org/61667 Reviewed-by: Mark Nunberg <mark.nunberg@couchbase.com> Tested-by: Brett Lawson <brett19@gmail.com>
- Loading branch information
Showing
5 changed files
with
162 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package gocb | ||
|
||
type Authenticator interface { | ||
clusterMgmt() userPassPair | ||
clusterN1ql() []userPassPair | ||
bucketMemd(bucket string) string | ||
bucketMgmt(bucket string) userPassPair | ||
bucketViews(bucket string) userPassPair | ||
bucketN1ql(bucket string) []userPassPair | ||
} | ||
|
||
type BucketAuthenticator struct { | ||
Password string | ||
} | ||
|
||
type userPassPair struct { | ||
Username string `json:"user"` | ||
Password string `json:"pass"` | ||
} | ||
|
||
type BucketAuthenticatorMap map[string]BucketAuthenticator | ||
|
||
type ClusterAuthenticator struct { | ||
Buckets BucketAuthenticatorMap | ||
Username string | ||
Password string | ||
} | ||
|
||
func (ca ClusterAuthenticator) clusterMgmt() userPassPair { | ||
return userPassPair{ca.Username, ca.Password} | ||
} | ||
|
||
func (ca ClusterAuthenticator) clusterN1ql() []userPassPair { | ||
userPassList := make([]userPassPair, 0) | ||
for bucket, auth := range ca.Buckets { | ||
userPassList = append(userPassList, userPassPair{ | ||
Username: bucket, | ||
Password: auth.Password, | ||
}) | ||
} | ||
return userPassList | ||
} | ||
|
||
func (ca ClusterAuthenticator) bucketAll(bucket string) userPassPair { | ||
if bucketAuth, ok := ca.Buckets[bucket]; ok { | ||
return userPassPair{bucket, bucketAuth.Password} | ||
} | ||
return userPassPair{"", ""} | ||
} | ||
|
||
func (ca ClusterAuthenticator) bucketMemd(bucket string) string { | ||
return ca.bucketAll(bucket).Password | ||
} | ||
|
||
func (ca ClusterAuthenticator) bucketMgmt(bucket string) userPassPair { | ||
return ca.bucketAll(bucket) | ||
} | ||
|
||
func (ca ClusterAuthenticator) bucketViews(bucket string) userPassPair { | ||
return ca.bucketAll(bucket) | ||
} | ||
|
||
func (ca ClusterAuthenticator) bucketN1ql(bucket string) []userPassPair { | ||
return []userPassPair{ | ||
ca.bucketAll(bucket), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package gocb | ||
|
||
import ( | ||
"net/http" | ||
"time" | ||
) | ||
|
||
// Wrapper around net.http.Client.Do(). | ||
// This allows a per-request timeout without setting the timeout on the Client object | ||
// directly. | ||
// The third parameter is the duration for the request itself. | ||
func doHttpWithTimeout(cli *http.Client, req *http.Request, timeout time.Duration) (resp *http.Response, err error) { | ||
if timeout.Seconds() == 0 { | ||
// No timeout | ||
resp, err = cli.Do(req) | ||
return | ||
} | ||
|
||
tmoch := make(chan struct{}) | ||
timer := time.AfterFunc(timeout, func() { | ||
tmoch <- struct{}{} | ||
}) | ||
|
||
req.Cancel = tmoch | ||
resp, err = cli.Do(req) | ||
timer.Stop() | ||
return | ||
} |