|
|
@@ -23,25 +23,32 @@ import (
|
|
|
"github.com/coreos/etcd/etcdserver/api"
|
|
|
"github.com/coreos/etcd/etcdserver/api/v2http/httptypes"
|
|
|
"github.com/coreos/etcd/etcdserver/v2auth"
|
|
|
+
|
|
|
+ "go.uber.org/zap"
|
|
|
)
|
|
|
|
|
|
type authHandler struct {
|
|
|
+ lg *zap.Logger
|
|
|
sec v2auth.Store
|
|
|
cluster api.Cluster
|
|
|
clientCertAuthEnabled bool
|
|
|
}
|
|
|
|
|
|
-func hasWriteRootAccess(sec v2auth.Store, r *http.Request, clientCertAuthEnabled bool) bool {
|
|
|
+func hasWriteRootAccess(lg *zap.Logger, sec v2auth.Store, r *http.Request, clientCertAuthEnabled bool) bool {
|
|
|
if r.Method == "GET" || r.Method == "HEAD" {
|
|
|
return true
|
|
|
}
|
|
|
- return hasRootAccess(sec, r, clientCertAuthEnabled)
|
|
|
+ return hasRootAccess(lg, sec, r, clientCertAuthEnabled)
|
|
|
}
|
|
|
|
|
|
-func userFromBasicAuth(sec v2auth.Store, r *http.Request) *v2auth.User {
|
|
|
+func userFromBasicAuth(lg *zap.Logger, sec v2auth.Store, r *http.Request) *v2auth.User {
|
|
|
username, password, ok := r.BasicAuth()
|
|
|
if !ok {
|
|
|
- plog.Warningf("auth: malformed basic auth encoding")
|
|
|
+ if lg != nil {
|
|
|
+ lg.Warn("malformed basic auth encoding")
|
|
|
+ } else {
|
|
|
+ plog.Warningf("auth: malformed basic auth encoding")
|
|
|
+ }
|
|
|
return nil
|
|
|
}
|
|
|
user, err := sec.GetUser(username)
|
|
|
@@ -51,23 +58,39 @@ func userFromBasicAuth(sec v2auth.Store, r *http.Request) *v2auth.User {
|
|
|
|
|
|
ok = sec.CheckPassword(user, password)
|
|
|
if !ok {
|
|
|
- plog.Warningf("auth: incorrect password for user: %s", username)
|
|
|
+ if lg != nil {
|
|
|
+ lg.Warn("incorrect password", zap.String("user-name", username))
|
|
|
+ } else {
|
|
|
+ plog.Warningf("auth: incorrect password for user: %s", username)
|
|
|
+ }
|
|
|
return nil
|
|
|
}
|
|
|
return &user
|
|
|
}
|
|
|
|
|
|
-func userFromClientCertificate(sec v2auth.Store, r *http.Request) *v2auth.User {
|
|
|
+func userFromClientCertificate(lg *zap.Logger, sec v2auth.Store, r *http.Request) *v2auth.User {
|
|
|
if r.TLS == nil {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
for _, chains := range r.TLS.VerifiedChains {
|
|
|
for _, chain := range chains {
|
|
|
- plog.Debugf("auth: found common name %s.\n", chain.Subject.CommonName)
|
|
|
+ if lg != nil {
|
|
|
+ lg.Debug("found common name", zap.String("common-name", chain.Subject.CommonName))
|
|
|
+ } else {
|
|
|
+ plog.Debugf("auth: found common name %s.\n", chain.Subject.CommonName)
|
|
|
+ }
|
|
|
user, err := sec.GetUser(chain.Subject.CommonName)
|
|
|
if err == nil {
|
|
|
- plog.Debugf("auth: authenticated user %s by cert common name.", user.User)
|
|
|
+ if lg != nil {
|
|
|
+ lg.Debug(
|
|
|
+ "authenticated a user via common name",
|
|
|
+ zap.String("user-name", user.User),
|
|
|
+ zap.String("common-name", chain.Subject.CommonName),
|
|
|
+ )
|
|
|
+ } else {
|
|
|
+ plog.Debugf("auth: authenticated user %s by cert common name.", user.User)
|
|
|
+ }
|
|
|
return &user
|
|
|
}
|
|
|
}
|
|
|
@@ -75,7 +98,7 @@ func userFromClientCertificate(sec v2auth.Store, r *http.Request) *v2auth.User {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-func hasRootAccess(sec v2auth.Store, r *http.Request, clientCertAuthEnabled bool) bool {
|
|
|
+func hasRootAccess(lg *zap.Logger, sec v2auth.Store, r *http.Request, clientCertAuthEnabled bool) bool {
|
|
|
if sec == nil {
|
|
|
// No store means no auth available, eg, tests.
|
|
|
return true
|
|
|
@@ -86,12 +109,12 @@ func hasRootAccess(sec v2auth.Store, r *http.Request, clientCertAuthEnabled bool
|
|
|
|
|
|
var rootUser *v2auth.User
|
|
|
if r.Header.Get("Authorization") == "" && clientCertAuthEnabled {
|
|
|
- rootUser = userFromClientCertificate(sec, r)
|
|
|
+ rootUser = userFromClientCertificate(lg, sec, r)
|
|
|
if rootUser == nil {
|
|
|
return false
|
|
|
}
|
|
|
} else {
|
|
|
- rootUser = userFromBasicAuth(sec, r)
|
|
|
+ rootUser = userFromBasicAuth(lg, sec, r)
|
|
|
if rootUser == nil {
|
|
|
return false
|
|
|
}
|
|
|
@@ -102,11 +125,21 @@ func hasRootAccess(sec v2auth.Store, r *http.Request, clientCertAuthEnabled bool
|
|
|
return true
|
|
|
}
|
|
|
}
|
|
|
- plog.Warningf("auth: user %s does not have the %s role for resource %s.", rootUser.User, v2auth.RootRoleName, r.URL.Path)
|
|
|
+
|
|
|
+ if lg != nil {
|
|
|
+ lg.Warn(
|
|
|
+ "a user does not have root role for resource",
|
|
|
+ zap.String("root-user", rootUser.User),
|
|
|
+ zap.String("root-role-name", v2auth.RootRoleName),
|
|
|
+ zap.String("resource-path", r.URL.Path),
|
|
|
+ )
|
|
|
+ } else {
|
|
|
+ plog.Warningf("auth: user %s does not have the %s role for resource %s.", rootUser.User, v2auth.RootRoleName, r.URL.Path)
|
|
|
+ }
|
|
|
return false
|
|
|
}
|
|
|
|
|
|
-func hasKeyPrefixAccess(sec v2auth.Store, r *http.Request, key string, recursive, clientCertAuthEnabled bool) bool {
|
|
|
+func hasKeyPrefixAccess(lg *zap.Logger, sec v2auth.Store, r *http.Request, key string, recursive, clientCertAuthEnabled bool) bool {
|
|
|
if sec == nil {
|
|
|
// No store means no auth available, eg, tests.
|
|
|
return true
|
|
|
@@ -118,13 +151,13 @@ func hasKeyPrefixAccess(sec v2auth.Store, r *http.Request, key string, recursive
|
|
|
var user *v2auth.User
|
|
|
if r.Header.Get("Authorization") == "" {
|
|
|
if clientCertAuthEnabled {
|
|
|
- user = userFromClientCertificate(sec, r)
|
|
|
+ user = userFromClientCertificate(lg, sec, r)
|
|
|
}
|
|
|
if user == nil {
|
|
|
- return hasGuestAccess(sec, r, key)
|
|
|
+ return hasGuestAccess(lg, sec, r, key)
|
|
|
}
|
|
|
} else {
|
|
|
- user = userFromBasicAuth(sec, r)
|
|
|
+ user = userFromBasicAuth(lg, sec, r)
|
|
|
if user == nil {
|
|
|
return false
|
|
|
}
|
|
|
@@ -144,11 +177,20 @@ func hasKeyPrefixAccess(sec v2auth.Store, r *http.Request, key string, recursive
|
|
|
return true
|
|
|
}
|
|
|
}
|
|
|
- plog.Warningf("auth: invalid access for user %s on key %s.", user.User, key)
|
|
|
+
|
|
|
+ if lg != nil {
|
|
|
+ lg.Warn(
|
|
|
+ "invalid access for user on key",
|
|
|
+ zap.String("user-name", user.User),
|
|
|
+ zap.String("key", key),
|
|
|
+ )
|
|
|
+ } else {
|
|
|
+ plog.Warningf("auth: invalid access for user %s on key %s.", user.User, key)
|
|
|
+ }
|
|
|
return false
|
|
|
}
|
|
|
|
|
|
-func hasGuestAccess(sec v2auth.Store, r *http.Request, key string) bool {
|
|
|
+func hasGuestAccess(lg *zap.Logger, sec v2auth.Store, r *http.Request, key string) bool {
|
|
|
writeAccess := r.Method != "GET" && r.Method != "HEAD"
|
|
|
role, err := sec.GetRole(v2auth.GuestRoleName)
|
|
|
if err != nil {
|
|
|
@@ -157,14 +199,31 @@ func hasGuestAccess(sec v2auth.Store, r *http.Request, key string) bool {
|
|
|
if role.HasKeyAccess(key, writeAccess) {
|
|
|
return true
|
|
|
}
|
|
|
- plog.Warningf("auth: invalid access for unauthenticated user on resource %s.", key)
|
|
|
+
|
|
|
+ if lg != nil {
|
|
|
+ lg.Warn(
|
|
|
+ "invalid access for a guest role on key",
|
|
|
+ zap.String("role-name", v2auth.GuestRoleName),
|
|
|
+ zap.String("key", key),
|
|
|
+ )
|
|
|
+ } else {
|
|
|
+ plog.Warningf("auth: invalid access for unauthenticated user on resource %s.", key)
|
|
|
+ }
|
|
|
return false
|
|
|
}
|
|
|
|
|
|
-func writeNoAuth(w http.ResponseWriter, r *http.Request) {
|
|
|
+func writeNoAuth(lg *zap.Logger, w http.ResponseWriter, r *http.Request) {
|
|
|
herr := httptypes.NewHTTPError(http.StatusUnauthorized, "Insufficient credentials")
|
|
|
if err := herr.WriteTo(w); err != nil {
|
|
|
- plog.Debugf("error writing HTTPError (%v) to %s", err, r.RemoteAddr)
|
|
|
+ if lg != nil {
|
|
|
+ lg.Debug(
|
|
|
+ "failed to write v2 HTTP error",
|
|
|
+ zap.String("remote-addr", r.RemoteAddr),
|
|
|
+ zap.Error(err),
|
|
|
+ )
|
|
|
+ } else {
|
|
|
+ plog.Debugf("error writing HTTPError (%v) to %s", err, r.RemoteAddr)
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -180,8 +239,8 @@ func (sh *authHandler) baseRoles(w http.ResponseWriter, r *http.Request) {
|
|
|
if !allowMethod(w, r.Method, "GET") {
|
|
|
return
|
|
|
}
|
|
|
- if !hasRootAccess(sh.sec, r, sh.clientCertAuthEnabled) {
|
|
|
- writeNoAuth(w, r)
|
|
|
+ if !hasRootAccess(sh.lg, sh.sec, r, sh.clientCertAuthEnabled) {
|
|
|
+ writeNoAuth(sh.lg, w, r)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
@@ -190,7 +249,7 @@ func (sh *authHandler) baseRoles(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
roles, err := sh.sec.AllRoles()
|
|
|
if err != nil {
|
|
|
- writeError(w, r, err)
|
|
|
+ writeError(sh.lg, w, r, err)
|
|
|
return
|
|
|
}
|
|
|
if roles == nil {
|
|
|
@@ -199,7 +258,7 @@ func (sh *authHandler) baseRoles(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
err = r.ParseForm()
|
|
|
if err != nil {
|
|
|
- writeError(w, r, err)
|
|
|
+ writeError(sh.lg, w, r, err)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
@@ -210,7 +269,7 @@ func (sh *authHandler) baseRoles(w http.ResponseWriter, r *http.Request) {
|
|
|
var role v2auth.Role
|
|
|
role, err = sh.sec.GetRole(roleName)
|
|
|
if err != nil {
|
|
|
- writeError(w, r, err)
|
|
|
+ writeError(sh.lg, w, r, err)
|
|
|
return
|
|
|
}
|
|
|
rolesCollections.Roles = append(rolesCollections.Roles, role)
|
|
|
@@ -218,8 +277,16 @@ func (sh *authHandler) baseRoles(w http.ResponseWriter, r *http.Request) {
|
|
|
err = json.NewEncoder(w).Encode(rolesCollections)
|
|
|
|
|
|
if err != nil {
|
|
|
- plog.Warningf("baseRoles error encoding on %s", r.URL)
|
|
|
- writeError(w, r, err)
|
|
|
+ if sh.lg != nil {
|
|
|
+ sh.lg.Warn(
|
|
|
+ "failed to encode base roles",
|
|
|
+ zap.String("url", r.URL.String()),
|
|
|
+ zap.Error(err),
|
|
|
+ )
|
|
|
+ } else {
|
|
|
+ plog.Warningf("baseRoles error encoding on %s", r.URL)
|
|
|
+ }
|
|
|
+ writeError(sh.lg, w, r, err)
|
|
|
return
|
|
|
}
|
|
|
}
|
|
|
@@ -234,7 +301,7 @@ func (sh *authHandler) handleRoles(w http.ResponseWriter, r *http.Request) {
|
|
|
return
|
|
|
}
|
|
|
if len(pieces) != 3 {
|
|
|
- writeError(w, r, httptypes.NewHTTPError(http.StatusBadRequest, "Invalid path"))
|
|
|
+ writeError(sh.lg, w, r, httptypes.NewHTTPError(http.StatusBadRequest, "Invalid path"))
|
|
|
return
|
|
|
}
|
|
|
sh.forRole(w, r, pieces[2])
|
|
|
@@ -244,8 +311,8 @@ func (sh *authHandler) forRole(w http.ResponseWriter, r *http.Request, role stri
|
|
|
if !allowMethod(w, r.Method, "GET", "PUT", "DELETE") {
|
|
|
return
|
|
|
}
|
|
|
- if !hasRootAccess(sh.sec, r, sh.clientCertAuthEnabled) {
|
|
|
- writeNoAuth(w, r)
|
|
|
+ if !hasRootAccess(sh.lg, sh.sec, r, sh.clientCertAuthEnabled) {
|
|
|
+ writeNoAuth(sh.lg, w, r)
|
|
|
return
|
|
|
}
|
|
|
w.Header().Set("X-Etcd-Cluster-ID", sh.cluster.ID().String())
|
|
|
@@ -255,24 +322,33 @@ func (sh *authHandler) forRole(w http.ResponseWriter, r *http.Request, role stri
|
|
|
case "GET":
|
|
|
data, err := sh.sec.GetRole(role)
|
|
|
if err != nil {
|
|
|
- writeError(w, r, err)
|
|
|
+ writeError(sh.lg, w, r, err)
|
|
|
return
|
|
|
}
|
|
|
err = json.NewEncoder(w).Encode(data)
|
|
|
if err != nil {
|
|
|
- plog.Warningf("forRole error encoding on %s", r.URL)
|
|
|
+ if sh.lg != nil {
|
|
|
+ sh.lg.Warn(
|
|
|
+ "failed to encode a role",
|
|
|
+ zap.String("url", r.URL.String()),
|
|
|
+ zap.Error(err),
|
|
|
+ )
|
|
|
+ } else {
|
|
|
+ plog.Warningf("forRole error encoding on %s", r.URL)
|
|
|
+ }
|
|
|
return
|
|
|
}
|
|
|
return
|
|
|
+
|
|
|
case "PUT":
|
|
|
var in v2auth.Role
|
|
|
err := json.NewDecoder(r.Body).Decode(&in)
|
|
|
if err != nil {
|
|
|
- writeError(w, r, httptypes.NewHTTPError(http.StatusBadRequest, "Invalid JSON in request body."))
|
|
|
+ writeError(sh.lg, w, r, httptypes.NewHTTPError(http.StatusBadRequest, "Invalid JSON in request body."))
|
|
|
return
|
|
|
}
|
|
|
if in.Role != role {
|
|
|
- writeError(w, r, httptypes.NewHTTPError(http.StatusBadRequest, "Role JSON name does not match the name in the URL"))
|
|
|
+ writeError(sh.lg, w, r, httptypes.NewHTTPError(http.StatusBadRequest, "Role JSON name does not match the name in the URL"))
|
|
|
return
|
|
|
}
|
|
|
|
|
|
@@ -282,19 +358,19 @@ func (sh *authHandler) forRole(w http.ResponseWriter, r *http.Request, role stri
|
|
|
if in.Grant.IsEmpty() && in.Revoke.IsEmpty() {
|
|
|
err = sh.sec.CreateRole(in)
|
|
|
if err != nil {
|
|
|
- writeError(w, r, err)
|
|
|
+ writeError(sh.lg, w, r, err)
|
|
|
return
|
|
|
}
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
|
out = in
|
|
|
} else {
|
|
|
if !in.Permissions.IsEmpty() {
|
|
|
- writeError(w, r, httptypes.NewHTTPError(http.StatusBadRequest, "Role JSON contains both permissions and grant/revoke"))
|
|
|
+ writeError(sh.lg, w, r, httptypes.NewHTTPError(http.StatusBadRequest, "Role JSON contains both permissions and grant/revoke"))
|
|
|
return
|
|
|
}
|
|
|
out, err = sh.sec.UpdateRole(in)
|
|
|
if err != nil {
|
|
|
- writeError(w, r, err)
|
|
|
+ writeError(sh.lg, w, r, err)
|
|
|
return
|
|
|
}
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
@@ -302,14 +378,23 @@ func (sh *authHandler) forRole(w http.ResponseWriter, r *http.Request, role stri
|
|
|
|
|
|
err = json.NewEncoder(w).Encode(out)
|
|
|
if err != nil {
|
|
|
- plog.Warningf("forRole error encoding on %s", r.URL)
|
|
|
+ if sh.lg != nil {
|
|
|
+ sh.lg.Warn(
|
|
|
+ "failed to encode a role",
|
|
|
+ zap.String("url", r.URL.String()),
|
|
|
+ zap.Error(err),
|
|
|
+ )
|
|
|
+ } else {
|
|
|
+ plog.Warningf("forRole error encoding on %s", r.URL)
|
|
|
+ }
|
|
|
return
|
|
|
}
|
|
|
return
|
|
|
+
|
|
|
case "DELETE":
|
|
|
err := sh.sec.DeleteRole(role)
|
|
|
if err != nil {
|
|
|
- writeError(w, r, err)
|
|
|
+ writeError(sh.lg, w, r, err)
|
|
|
return
|
|
|
}
|
|
|
}
|
|
|
@@ -328,8 +413,8 @@ func (sh *authHandler) baseUsers(w http.ResponseWriter, r *http.Request) {
|
|
|
if !allowMethod(w, r.Method, "GET") {
|
|
|
return
|
|
|
}
|
|
|
- if !hasRootAccess(sh.sec, r, sh.clientCertAuthEnabled) {
|
|
|
- writeNoAuth(w, r)
|
|
|
+ if !hasRootAccess(sh.lg, sh.sec, r, sh.clientCertAuthEnabled) {
|
|
|
+ writeNoAuth(sh.lg, w, r)
|
|
|
return
|
|
|
}
|
|
|
w.Header().Set("X-Etcd-Cluster-ID", sh.cluster.ID().String())
|
|
|
@@ -337,7 +422,7 @@ func (sh *authHandler) baseUsers(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
users, err := sh.sec.AllUsers()
|
|
|
if err != nil {
|
|
|
- writeError(w, r, err)
|
|
|
+ writeError(sh.lg, w, r, err)
|
|
|
return
|
|
|
}
|
|
|
if users == nil {
|
|
|
@@ -346,7 +431,7 @@ func (sh *authHandler) baseUsers(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
err = r.ParseForm()
|
|
|
if err != nil {
|
|
|
- writeError(w, r, err)
|
|
|
+ writeError(sh.lg, w, r, err)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
@@ -355,7 +440,7 @@ func (sh *authHandler) baseUsers(w http.ResponseWriter, r *http.Request) {
|
|
|
var user v2auth.User
|
|
|
user, err = sh.sec.GetUser(userName)
|
|
|
if err != nil {
|
|
|
- writeError(w, r, err)
|
|
|
+ writeError(sh.lg, w, r, err)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
@@ -374,8 +459,16 @@ func (sh *authHandler) baseUsers(w http.ResponseWriter, r *http.Request) {
|
|
|
err = json.NewEncoder(w).Encode(ucs)
|
|
|
|
|
|
if err != nil {
|
|
|
- plog.Warningf("baseUsers error encoding on %s", r.URL)
|
|
|
- writeError(w, r, err)
|
|
|
+ if sh.lg != nil {
|
|
|
+ sh.lg.Warn(
|
|
|
+ "failed to encode users",
|
|
|
+ zap.String("url", r.URL.String()),
|
|
|
+ zap.Error(err),
|
|
|
+ )
|
|
|
+ } else {
|
|
|
+ plog.Warningf("baseUsers error encoding on %s", r.URL)
|
|
|
+ }
|
|
|
+ writeError(sh.lg, w, r, err)
|
|
|
return
|
|
|
}
|
|
|
}
|
|
|
@@ -390,7 +483,7 @@ func (sh *authHandler) handleUsers(w http.ResponseWriter, r *http.Request) {
|
|
|
return
|
|
|
}
|
|
|
if len(pieces) != 3 {
|
|
|
- writeError(w, r, httptypes.NewHTTPError(http.StatusBadRequest, "Invalid path"))
|
|
|
+ writeError(sh.lg, w, r, httptypes.NewHTTPError(http.StatusBadRequest, "Invalid path"))
|
|
|
return
|
|
|
}
|
|
|
sh.forUser(w, r, pieces[2])
|
|
|
@@ -400,8 +493,8 @@ func (sh *authHandler) forUser(w http.ResponseWriter, r *http.Request, user stri
|
|
|
if !allowMethod(w, r.Method, "GET", "PUT", "DELETE") {
|
|
|
return
|
|
|
}
|
|
|
- if !hasRootAccess(sh.sec, r, sh.clientCertAuthEnabled) {
|
|
|
- writeNoAuth(w, r)
|
|
|
+ if !hasRootAccess(sh.lg, sh.sec, r, sh.clientCertAuthEnabled) {
|
|
|
+ writeNoAuth(sh.lg, w, r)
|
|
|
return
|
|
|
}
|
|
|
w.Header().Set("X-Etcd-Cluster-ID", sh.cluster.ID().String())
|
|
|
@@ -411,13 +504,13 @@ func (sh *authHandler) forUser(w http.ResponseWriter, r *http.Request, user stri
|
|
|
case "GET":
|
|
|
u, err := sh.sec.GetUser(user)
|
|
|
if err != nil {
|
|
|
- writeError(w, r, err)
|
|
|
+ writeError(sh.lg, w, r, err)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
err = r.ParseForm()
|
|
|
if err != nil {
|
|
|
- writeError(w, r, err)
|
|
|
+ writeError(sh.lg, w, r, err)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
@@ -426,7 +519,7 @@ func (sh *authHandler) forUser(w http.ResponseWriter, r *http.Request, user stri
|
|
|
var role v2auth.Role
|
|
|
role, err = sh.sec.GetRole(roleName)
|
|
|
if err != nil {
|
|
|
- writeError(w, r, err)
|
|
|
+ writeError(sh.lg, w, r, err)
|
|
|
return
|
|
|
}
|
|
|
uwr.Roles = append(uwr.Roles, role)
|
|
|
@@ -434,19 +527,28 @@ func (sh *authHandler) forUser(w http.ResponseWriter, r *http.Request, user stri
|
|
|
err = json.NewEncoder(w).Encode(uwr)
|
|
|
|
|
|
if err != nil {
|
|
|
- plog.Warningf("forUser error encoding on %s", r.URL)
|
|
|
+ if sh.lg != nil {
|
|
|
+ sh.lg.Warn(
|
|
|
+ "failed to encode roles",
|
|
|
+ zap.String("url", r.URL.String()),
|
|
|
+ zap.Error(err),
|
|
|
+ )
|
|
|
+ } else {
|
|
|
+ plog.Warningf("forUser error encoding on %s", r.URL)
|
|
|
+ }
|
|
|
return
|
|
|
}
|
|
|
return
|
|
|
+
|
|
|
case "PUT":
|
|
|
var u v2auth.User
|
|
|
err := json.NewDecoder(r.Body).Decode(&u)
|
|
|
if err != nil {
|
|
|
- writeError(w, r, httptypes.NewHTTPError(http.StatusBadRequest, "Invalid JSON in request body."))
|
|
|
+ writeError(sh.lg, w, r, httptypes.NewHTTPError(http.StatusBadRequest, "Invalid JSON in request body."))
|
|
|
return
|
|
|
}
|
|
|
if u.User != user {
|
|
|
- writeError(w, r, httptypes.NewHTTPError(http.StatusBadRequest, "User JSON name does not match the name in the URL"))
|
|
|
+ writeError(sh.lg, w, r, httptypes.NewHTTPError(http.StatusBadRequest, "User JSON name does not match the name in the URL"))
|
|
|
return
|
|
|
}
|
|
|
|
|
|
@@ -466,18 +568,18 @@ func (sh *authHandler) forUser(w http.ResponseWriter, r *http.Request, user stri
|
|
|
}
|
|
|
|
|
|
if err != nil {
|
|
|
- writeError(w, r, err)
|
|
|
+ writeError(sh.lg, w, r, err)
|
|
|
return
|
|
|
}
|
|
|
} else {
|
|
|
// update case
|
|
|
if len(u.Roles) != 0 {
|
|
|
- writeError(w, r, httptypes.NewHTTPError(http.StatusBadRequest, "User JSON contains both roles and grant/revoke"))
|
|
|
+ writeError(sh.lg, w, r, httptypes.NewHTTPError(http.StatusBadRequest, "User JSON contains both roles and grant/revoke"))
|
|
|
return
|
|
|
}
|
|
|
out, err = sh.sec.UpdateUser(u)
|
|
|
if err != nil {
|
|
|
- writeError(w, r, err)
|
|
|
+ writeError(sh.lg, w, r, err)
|
|
|
return
|
|
|
}
|
|
|
}
|
|
|
@@ -492,14 +594,23 @@ func (sh *authHandler) forUser(w http.ResponseWriter, r *http.Request, user stri
|
|
|
|
|
|
err = json.NewEncoder(w).Encode(out)
|
|
|
if err != nil {
|
|
|
- plog.Warningf("forUser error encoding on %s", r.URL)
|
|
|
+ if sh.lg != nil {
|
|
|
+ sh.lg.Warn(
|
|
|
+ "failed to encode a user",
|
|
|
+ zap.String("url", r.URL.String()),
|
|
|
+ zap.Error(err),
|
|
|
+ )
|
|
|
+ } else {
|
|
|
+ plog.Warningf("forUser error encoding on %s", r.URL)
|
|
|
+ }
|
|
|
return
|
|
|
}
|
|
|
return
|
|
|
+
|
|
|
case "DELETE":
|
|
|
err := sh.sec.DeleteUser(user)
|
|
|
if err != nil {
|
|
|
- writeError(w, r, err)
|
|
|
+ writeError(sh.lg, w, r, err)
|
|
|
return
|
|
|
}
|
|
|
}
|
|
|
@@ -513,8 +624,8 @@ func (sh *authHandler) enableDisable(w http.ResponseWriter, r *http.Request) {
|
|
|
if !allowMethod(w, r.Method, "GET", "PUT", "DELETE") {
|
|
|
return
|
|
|
}
|
|
|
- if !hasWriteRootAccess(sh.sec, r, sh.clientCertAuthEnabled) {
|
|
|
- writeNoAuth(w, r)
|
|
|
+ if !hasWriteRootAccess(sh.lg, sh.sec, r, sh.clientCertAuthEnabled) {
|
|
|
+ writeNoAuth(sh.lg, w, r)
|
|
|
return
|
|
|
}
|
|
|
w.Header().Set("X-Etcd-Cluster-ID", sh.cluster.ID().String())
|
|
|
@@ -525,18 +636,28 @@ func (sh *authHandler) enableDisable(w http.ResponseWriter, r *http.Request) {
|
|
|
jsonDict := enabled{isEnabled}
|
|
|
err := json.NewEncoder(w).Encode(jsonDict)
|
|
|
if err != nil {
|
|
|
- plog.Warningf("error encoding auth state on %s", r.URL)
|
|
|
+ if sh.lg != nil {
|
|
|
+ sh.lg.Warn(
|
|
|
+ "failed to encode a auth state",
|
|
|
+ zap.String("url", r.URL.String()),
|
|
|
+ zap.Error(err),
|
|
|
+ )
|
|
|
+ } else {
|
|
|
+ plog.Warningf("error encoding auth state on %s", r.URL)
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
case "PUT":
|
|
|
err := sh.sec.EnableAuth()
|
|
|
if err != nil {
|
|
|
- writeError(w, r, err)
|
|
|
+ writeError(sh.lg, w, r, err)
|
|
|
return
|
|
|
}
|
|
|
+
|
|
|
case "DELETE":
|
|
|
err := sh.sec.DisableAuth()
|
|
|
if err != nil {
|
|
|
- writeError(w, r, err)
|
|
|
+ writeError(sh.lg, w, r, err)
|
|
|
return
|
|
|
}
|
|
|
}
|