Browse Source

Merge pull request #3604 from gyuho/replace_netutil_BasicAuth

etcdhttp/auth: BasicAuth method in standard pkg
Xiang Li 10 years ago
parent
commit
6c05a01ec6
3 changed files with 3 additions and 39 deletions
  1. 1 0
      README.md
  2. 2 3
      etcdserver/etcdhttp/client_auth.go
  3. 0 36
      pkg/netutil/netutil.go

+ 1 - 0
README.md

@@ -31,6 +31,7 @@ If you're considering etcd for production use, please see: [production-ready.md]
 The easiest way to get etcd is to install one of the pre-built binaries from the tagged releases: instructions are available on [GitHub][github-release].
 The easiest way to get etcd is to install one of the pre-built binaries from the tagged releases: instructions are available on [GitHub][github-release].
 
 
 For those wanting to try the very latest version, you can build the latest version of etcd from the `master` branch.
 For those wanting to try the very latest version, you can build the latest version of etcd from the `master` branch.
+You will first need [*Go*](https://golang.org/) installed on your machine (version 1.4+ is required).
 All development occurs on `master`, including new features and bug fixes.
 All development occurs on `master`, including new features and bug fixes.
 Bug fixes are first targeted at `master` and subsequently ported to release branches, as described in the [branch management][branch-management] guide.
 Bug fixes are first targeted at `master` and subsequently ported to release branches, as described in the [branch management][branch-management] guide.
 
 

+ 2 - 3
etcdserver/etcdhttp/client_auth.go

@@ -23,7 +23,6 @@ import (
 	"github.com/coreos/etcd/etcdserver"
 	"github.com/coreos/etcd/etcdserver"
 	"github.com/coreos/etcd/etcdserver/auth"
 	"github.com/coreos/etcd/etcdserver/auth"
 	"github.com/coreos/etcd/etcdserver/etcdhttp/httptypes"
 	"github.com/coreos/etcd/etcdserver/etcdhttp/httptypes"
-	"github.com/coreos/etcd/pkg/netutil"
 )
 )
 
 
 type authHandler struct {
 type authHandler struct {
@@ -46,7 +45,7 @@ func hasRootAccess(sec auth.Store, r *http.Request) bool {
 	if !sec.AuthEnabled() {
 	if !sec.AuthEnabled() {
 		return true
 		return true
 	}
 	}
-	username, password, ok := netutil.BasicAuth(r)
+	username, password, ok := r.BasicAuth()
 	if !ok {
 	if !ok {
 		return false
 		return false
 	}
 	}
@@ -80,7 +79,7 @@ func hasKeyPrefixAccess(sec auth.Store, r *http.Request, key string, recursive b
 		plog.Warningf("auth: no authorization provided, checking guest access")
 		plog.Warningf("auth: no authorization provided, checking guest access")
 		return hasGuestAccess(sec, r, key)
 		return hasGuestAccess(sec, r, key)
 	}
 	}
-	username, password, ok := netutil.BasicAuth(r)
+	username, password, ok := r.BasicAuth()
 	if !ok {
 	if !ok {
 		plog.Warningf("auth: malformed basic auth encoding")
 		plog.Warningf("auth: malformed basic auth encoding")
 		return false
 		return false

+ 0 - 36
pkg/netutil/netutil.go

@@ -15,13 +15,10 @@
 package netutil
 package netutil
 
 
 import (
 import (
-	"encoding/base64"
 	"net"
 	"net"
-	"net/http"
 	"net/url"
 	"net/url"
 	"reflect"
 	"reflect"
 	"sort"
 	"sort"
-	"strings"
 
 
 	"github.com/coreos/etcd/Godeps/_workspace/src/github.com/coreos/pkg/capnslog"
 	"github.com/coreos/etcd/Godeps/_workspace/src/github.com/coreos/pkg/capnslog"
 	"github.com/coreos/etcd/pkg/types"
 	"github.com/coreos/etcd/pkg/types"
@@ -118,36 +115,3 @@ func URLStringsEqual(a []string, b []string) bool {
 
 
 	return urlsEqual(urlsA, urlsB)
 	return urlsEqual(urlsA, urlsB)
 }
 }
-
-// BasicAuth returns the username and password provided in the request's
-// Authorization header, if the request uses HTTP Basic Authentication.
-// See RFC 2617, Section 2.
-// Based on the BasicAuth method from the Golang standard lib.
-// TODO: use the standard lib BasicAuth method when we move to Go 1.4.
-func BasicAuth(r *http.Request) (username, password string, ok bool) {
-	auth := r.Header.Get("Authorization")
-	if auth == "" {
-		return
-	}
-	return parseBasicAuth(auth)
-}
-
-// parseBasicAuth parses an HTTP Basic Authentication string.
-// "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==" returns ("Aladdin", "open sesame", true).
-// Taken from the Golang standard lib.
-// TODO: use the standard lib BasicAuth method when we move to Go 1.4.
-func parseBasicAuth(auth string) (username, password string, ok bool) {
-	if !strings.HasPrefix(auth, "Basic ") {
-		return
-	}
-	c, err := base64.StdEncoding.DecodeString(strings.TrimPrefix(auth, "Basic "))
-	if err != nil {
-		return
-	}
-	cs := string(c)
-	s := strings.IndexByte(cs, ':')
-	if s < 0 {
-		return
-	}
-	return cs[:s], cs[s+1:], true
-}