|
@@ -89,6 +89,7 @@ type AuthenticateParamIndex struct{}
|
|
|
// AuthenticateParamSimpleTokenPrefix is used for a key of context in the parameters of Authenticate()
|
|
// AuthenticateParamSimpleTokenPrefix is used for a key of context in the parameters of Authenticate()
|
|
|
type AuthenticateParamSimpleTokenPrefix struct{}
|
|
type AuthenticateParamSimpleTokenPrefix struct{}
|
|
|
|
|
|
|
|
|
|
+// AuthStore defines auth storage interface.
|
|
|
type AuthStore interface {
|
|
type AuthStore interface {
|
|
|
// AuthEnable turns on the authentication feature
|
|
// AuthEnable turns on the authentication feature
|
|
|
AuthEnable() error
|
|
AuthEnable() error
|
|
@@ -196,6 +197,7 @@ type authStore struct {
|
|
|
// atomic operations; need 64-bit align, or 32-bit tests will crash
|
|
// atomic operations; need 64-bit align, or 32-bit tests will crash
|
|
|
revision uint64
|
|
revision uint64
|
|
|
|
|
|
|
|
|
|
+ lg *zap.Logger
|
|
|
be backend.Backend
|
|
be backend.Backend
|
|
|
enabled bool
|
|
enabled bool
|
|
|
enabledMu sync.RWMutex
|
|
enabledMu sync.RWMutex
|
|
@@ -209,7 +211,11 @@ func (as *authStore) AuthEnable() error {
|
|
|
as.enabledMu.Lock()
|
|
as.enabledMu.Lock()
|
|
|
defer as.enabledMu.Unlock()
|
|
defer as.enabledMu.Unlock()
|
|
|
if as.enabled {
|
|
if as.enabled {
|
|
|
- plog.Noticef("Authentication already enabled")
|
|
|
|
|
|
|
+ if as.lg != nil {
|
|
|
|
|
+ as.lg.Info("authentication is already enabled; ignored auth enable request")
|
|
|
|
|
+ } else {
|
|
|
|
|
+ plog.Noticef("Authentication already enabled")
|
|
|
|
|
+ }
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
|
b := as.be
|
|
b := as.be
|
|
@@ -220,7 +226,7 @@ func (as *authStore) AuthEnable() error {
|
|
|
b.ForceCommit()
|
|
b.ForceCommit()
|
|
|
}()
|
|
}()
|
|
|
|
|
|
|
|
- u := getUser(tx, rootUser)
|
|
|
|
|
|
|
+ u := getUser(as.lg, tx, rootUser)
|
|
|
if u == nil {
|
|
if u == nil {
|
|
|
return ErrRootUserNotExist
|
|
return ErrRootUserNotExist
|
|
|
}
|
|
}
|
|
@@ -238,8 +244,11 @@ func (as *authStore) AuthEnable() error {
|
|
|
|
|
|
|
|
as.setRevision(getRevision(tx))
|
|
as.setRevision(getRevision(tx))
|
|
|
|
|
|
|
|
- plog.Noticef("Authentication enabled")
|
|
|
|
|
-
|
|
|
|
|
|
|
+ if as.lg != nil {
|
|
|
|
|
+ as.lg.Info("enabled authentication")
|
|
|
|
|
+ } else {
|
|
|
|
|
+ plog.Noticef("Authentication enabled")
|
|
|
|
|
+ }
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -260,7 +269,11 @@ func (as *authStore) AuthDisable() {
|
|
|
as.enabled = false
|
|
as.enabled = false
|
|
|
as.tokenProvider.disable()
|
|
as.tokenProvider.disable()
|
|
|
|
|
|
|
|
- plog.Noticef("Authentication disabled")
|
|
|
|
|
|
|
+ if as.lg != nil {
|
|
|
|
|
+ as.lg.Info("disabled authentication")
|
|
|
|
|
+ } else {
|
|
|
|
|
+ plog.Noticef("Authentication disabled")
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func (as *authStore) Close() error {
|
|
func (as *authStore) Close() error {
|
|
@@ -282,7 +295,7 @@ func (as *authStore) Authenticate(ctx context.Context, username, password string
|
|
|
tx.Lock()
|
|
tx.Lock()
|
|
|
defer tx.Unlock()
|
|
defer tx.Unlock()
|
|
|
|
|
|
|
|
- user := getUser(tx, username)
|
|
|
|
|
|
|
+ user := getUser(as.lg, tx, username)
|
|
|
if user == nil {
|
|
if user == nil {
|
|
|
return nil, ErrAuthFailed
|
|
return nil, ErrAuthFailed
|
|
|
}
|
|
}
|
|
@@ -295,7 +308,15 @@ func (as *authStore) Authenticate(ctx context.Context, username, password string
|
|
|
return nil, err
|
|
return nil, err
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- plog.Debugf("authorized %s, token is %s", username, token)
|
|
|
|
|
|
|
+ if as.lg != nil {
|
|
|
|
|
+ as.lg.Debug(
|
|
|
|
|
+ "authenticated a user",
|
|
|
|
|
+ zap.String("user-name", username),
|
|
|
|
|
+ zap.String("token", token),
|
|
|
|
|
+ )
|
|
|
|
|
+ } else {
|
|
|
|
|
+ plog.Debugf("authorized %s, token is %s", username, token)
|
|
|
|
|
+ }
|
|
|
return &pb.AuthenticateResponse{Token: token}, nil
|
|
return &pb.AuthenticateResponse{Token: token}, nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -308,16 +329,19 @@ func (as *authStore) CheckPassword(username, password string) (uint64, error) {
|
|
|
tx.Lock()
|
|
tx.Lock()
|
|
|
defer tx.Unlock()
|
|
defer tx.Unlock()
|
|
|
|
|
|
|
|
- user := getUser(tx, username)
|
|
|
|
|
|
|
+ user := getUser(as.lg, tx, username)
|
|
|
if user == nil {
|
|
if user == nil {
|
|
|
return 0, ErrAuthFailed
|
|
return 0, ErrAuthFailed
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if bcrypt.CompareHashAndPassword(user.Password, []byte(password)) != nil {
|
|
if bcrypt.CompareHashAndPassword(user.Password, []byte(password)) != nil {
|
|
|
- plog.Noticef("authentication failed, invalid password for user %s", username)
|
|
|
|
|
|
|
+ if as.lg != nil {
|
|
|
|
|
+ as.lg.Info("invalid password", zap.String("user-name", username))
|
|
|
|
|
+ } else {
|
|
|
|
|
+ plog.Noticef("authentication failed, invalid password for user %s", username)
|
|
|
|
|
+ }
|
|
|
return 0, ErrAuthFailed
|
|
return 0, ErrAuthFailed
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
return getRevision(tx), nil
|
|
return getRevision(tx), nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -349,7 +373,15 @@ func (as *authStore) UserAdd(r *pb.AuthUserAddRequest) (*pb.AuthUserAddResponse,
|
|
|
|
|
|
|
|
hashed, err := bcrypt.GenerateFromPassword([]byte(r.Password), BcryptCost)
|
|
hashed, err := bcrypt.GenerateFromPassword([]byte(r.Password), BcryptCost)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
- plog.Errorf("failed to hash password: %s", err)
|
|
|
|
|
|
|
+ if as.lg != nil {
|
|
|
|
|
+ as.lg.Warn(
|
|
|
|
|
+ "failed to bcrypt hash password",
|
|
|
|
|
+ zap.String("user-name", r.Name),
|
|
|
|
|
+ zap.Error(err),
|
|
|
|
|
+ )
|
|
|
|
|
+ } else {
|
|
|
|
|
+ plog.Errorf("failed to hash password: %s", err)
|
|
|
|
|
+ }
|
|
|
return nil, err
|
|
return nil, err
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -357,7 +389,7 @@ func (as *authStore) UserAdd(r *pb.AuthUserAddRequest) (*pb.AuthUserAddResponse,
|
|
|
tx.Lock()
|
|
tx.Lock()
|
|
|
defer tx.Unlock()
|
|
defer tx.Unlock()
|
|
|
|
|
|
|
|
- user := getUser(tx, r.Name)
|
|
|
|
|
|
|
+ user := getUser(as.lg, tx, r.Name)
|
|
|
if user != nil {
|
|
if user != nil {
|
|
|
return nil, ErrUserAlreadyExist
|
|
return nil, ErrUserAlreadyExist
|
|
|
}
|
|
}
|
|
@@ -367,18 +399,25 @@ func (as *authStore) UserAdd(r *pb.AuthUserAddRequest) (*pb.AuthUserAddResponse,
|
|
|
Password: hashed,
|
|
Password: hashed,
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- putUser(tx, newUser)
|
|
|
|
|
|
|
+ putUser(as.lg, tx, newUser)
|
|
|
|
|
|
|
|
as.commitRevision(tx)
|
|
as.commitRevision(tx)
|
|
|
|
|
|
|
|
- plog.Noticef("added a new user: %s", r.Name)
|
|
|
|
|
-
|
|
|
|
|
|
|
+ if as.lg != nil {
|
|
|
|
|
+ as.lg.Info("added a user", zap.String("user-name", r.Name))
|
|
|
|
|
+ } else {
|
|
|
|
|
+ plog.Noticef("added a new user: %s", r.Name)
|
|
|
|
|
+ }
|
|
|
return &pb.AuthUserAddResponse{}, nil
|
|
return &pb.AuthUserAddResponse{}, nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func (as *authStore) UserDelete(r *pb.AuthUserDeleteRequest) (*pb.AuthUserDeleteResponse, error) {
|
|
func (as *authStore) UserDelete(r *pb.AuthUserDeleteRequest) (*pb.AuthUserDeleteResponse, error) {
|
|
|
if as.enabled && strings.Compare(r.Name, rootUser) == 0 {
|
|
if as.enabled && strings.Compare(r.Name, rootUser) == 0 {
|
|
|
- plog.Errorf("the user root must not be deleted")
|
|
|
|
|
|
|
+ if as.lg != nil {
|
|
|
|
|
+ as.lg.Warn("cannot delete 'root' user", zap.String("user-name", r.Name))
|
|
|
|
|
+ } else {
|
|
|
|
|
+ plog.Errorf("the user root must not be deleted")
|
|
|
|
|
+ }
|
|
|
return nil, ErrInvalidAuthMgmt
|
|
return nil, ErrInvalidAuthMgmt
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -386,7 +425,7 @@ func (as *authStore) UserDelete(r *pb.AuthUserDeleteRequest) (*pb.AuthUserDelete
|
|
|
tx.Lock()
|
|
tx.Lock()
|
|
|
defer tx.Unlock()
|
|
defer tx.Unlock()
|
|
|
|
|
|
|
|
- user := getUser(tx, r.Name)
|
|
|
|
|
|
|
+ user := getUser(as.lg, tx, r.Name)
|
|
|
if user == nil {
|
|
if user == nil {
|
|
|
return nil, ErrUserNotFound
|
|
return nil, ErrUserNotFound
|
|
|
}
|
|
}
|
|
@@ -398,8 +437,15 @@ func (as *authStore) UserDelete(r *pb.AuthUserDeleteRequest) (*pb.AuthUserDelete
|
|
|
as.invalidateCachedPerm(r.Name)
|
|
as.invalidateCachedPerm(r.Name)
|
|
|
as.tokenProvider.invalidateUser(r.Name)
|
|
as.tokenProvider.invalidateUser(r.Name)
|
|
|
|
|
|
|
|
- plog.Noticef("deleted a user: %s", r.Name)
|
|
|
|
|
-
|
|
|
|
|
|
|
+ if as.lg != nil {
|
|
|
|
|
+ as.lg.Info(
|
|
|
|
|
+ "deleted a user",
|
|
|
|
|
+ zap.String("user-name", r.Name),
|
|
|
|
|
+ zap.Strings("user-roles", user.Roles),
|
|
|
|
|
+ )
|
|
|
|
|
+ } else {
|
|
|
|
|
+ plog.Noticef("deleted a user: %s", r.Name)
|
|
|
|
|
+ }
|
|
|
return &pb.AuthUserDeleteResponse{}, nil
|
|
return &pb.AuthUserDeleteResponse{}, nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -408,7 +454,15 @@ func (as *authStore) UserChangePassword(r *pb.AuthUserChangePasswordRequest) (*p
|
|
|
// If the cost is too high, we should move the encryption to outside of the raft
|
|
// If the cost is too high, we should move the encryption to outside of the raft
|
|
|
hashed, err := bcrypt.GenerateFromPassword([]byte(r.Password), BcryptCost)
|
|
hashed, err := bcrypt.GenerateFromPassword([]byte(r.Password), BcryptCost)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
- plog.Errorf("failed to hash password: %s", err)
|
|
|
|
|
|
|
+ if as.lg != nil {
|
|
|
|
|
+ as.lg.Warn(
|
|
|
|
|
+ "failed to bcrypt hash password",
|
|
|
|
|
+ zap.String("user-name", r.Name),
|
|
|
|
|
+ zap.Error(err),
|
|
|
|
|
+ )
|
|
|
|
|
+ } else {
|
|
|
|
|
+ plog.Errorf("failed to hash password: %s", err)
|
|
|
|
|
+ }
|
|
|
return nil, err
|
|
return nil, err
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -416,7 +470,7 @@ func (as *authStore) UserChangePassword(r *pb.AuthUserChangePasswordRequest) (*p
|
|
|
tx.Lock()
|
|
tx.Lock()
|
|
|
defer tx.Unlock()
|
|
defer tx.Unlock()
|
|
|
|
|
|
|
|
- user := getUser(tx, r.Name)
|
|
|
|
|
|
|
+ user := getUser(as.lg, tx, r.Name)
|
|
|
if user == nil {
|
|
if user == nil {
|
|
|
return nil, ErrUserNotFound
|
|
return nil, ErrUserNotFound
|
|
|
}
|
|
}
|
|
@@ -427,15 +481,22 @@ func (as *authStore) UserChangePassword(r *pb.AuthUserChangePasswordRequest) (*p
|
|
|
Password: hashed,
|
|
Password: hashed,
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- putUser(tx, updatedUser)
|
|
|
|
|
|
|
+ putUser(as.lg, tx, updatedUser)
|
|
|
|
|
|
|
|
as.commitRevision(tx)
|
|
as.commitRevision(tx)
|
|
|
|
|
|
|
|
as.invalidateCachedPerm(r.Name)
|
|
as.invalidateCachedPerm(r.Name)
|
|
|
as.tokenProvider.invalidateUser(r.Name)
|
|
as.tokenProvider.invalidateUser(r.Name)
|
|
|
|
|
|
|
|
- plog.Noticef("changed a password of a user: %s", r.Name)
|
|
|
|
|
-
|
|
|
|
|
|
|
+ if as.lg != nil {
|
|
|
|
|
+ as.lg.Info(
|
|
|
|
|
+ "changed a password of a user",
|
|
|
|
|
+ zap.String("user-name", r.Name),
|
|
|
|
|
+ zap.Strings("user-roles", user.Roles),
|
|
|
|
|
+ )
|
|
|
|
|
+ } else {
|
|
|
|
|
+ plog.Noticef("changed a password of a user: %s", r.Name)
|
|
|
|
|
+ }
|
|
|
return &pb.AuthUserChangePasswordResponse{}, nil
|
|
return &pb.AuthUserChangePasswordResponse{}, nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -444,7 +505,7 @@ func (as *authStore) UserGrantRole(r *pb.AuthUserGrantRoleRequest) (*pb.AuthUser
|
|
|
tx.Lock()
|
|
tx.Lock()
|
|
|
defer tx.Unlock()
|
|
defer tx.Unlock()
|
|
|
|
|
|
|
|
- user := getUser(tx, r.User)
|
|
|
|
|
|
|
+ user := getUser(as.lg, tx, r.User)
|
|
|
if user == nil {
|
|
if user == nil {
|
|
|
return nil, ErrUserNotFound
|
|
return nil, ErrUserNotFound
|
|
|
}
|
|
}
|
|
@@ -458,27 +519,45 @@ func (as *authStore) UserGrantRole(r *pb.AuthUserGrantRoleRequest) (*pb.AuthUser
|
|
|
|
|
|
|
|
idx := sort.SearchStrings(user.Roles, r.Role)
|
|
idx := sort.SearchStrings(user.Roles, r.Role)
|
|
|
if idx < len(user.Roles) && strings.Compare(user.Roles[idx], r.Role) == 0 {
|
|
if idx < len(user.Roles) && strings.Compare(user.Roles[idx], r.Role) == 0 {
|
|
|
- plog.Warningf("user %s is already granted role %s", r.User, r.Role)
|
|
|
|
|
|
|
+ if as.lg != nil {
|
|
|
|
|
+ as.lg.Warn(
|
|
|
|
|
+ "ignored grant role request to a user",
|
|
|
|
|
+ zap.String("user-name", r.User),
|
|
|
|
|
+ zap.Strings("user-roles", user.Roles),
|
|
|
|
|
+ zap.String("duplicate-role-name", r.Role),
|
|
|
|
|
+ )
|
|
|
|
|
+ } else {
|
|
|
|
|
+ plog.Warningf("user %s is already granted role %s", r.User, r.Role)
|
|
|
|
|
+ }
|
|
|
return &pb.AuthUserGrantRoleResponse{}, nil
|
|
return &pb.AuthUserGrantRoleResponse{}, nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
user.Roles = append(user.Roles, r.Role)
|
|
user.Roles = append(user.Roles, r.Role)
|
|
|
sort.Strings(user.Roles)
|
|
sort.Strings(user.Roles)
|
|
|
|
|
|
|
|
- putUser(tx, user)
|
|
|
|
|
|
|
+ putUser(as.lg, tx, user)
|
|
|
|
|
|
|
|
as.invalidateCachedPerm(r.User)
|
|
as.invalidateCachedPerm(r.User)
|
|
|
|
|
|
|
|
as.commitRevision(tx)
|
|
as.commitRevision(tx)
|
|
|
|
|
|
|
|
- plog.Noticef("granted role %s to user %s", r.Role, r.User)
|
|
|
|
|
|
|
+ if as.lg != nil {
|
|
|
|
|
+ as.lg.Info(
|
|
|
|
|
+ "granted a role to a user",
|
|
|
|
|
+ zap.String("user-name", r.User),
|
|
|
|
|
+ zap.Strings("user-roles", user.Roles),
|
|
|
|
|
+ zap.String("added-role-name", r.Role),
|
|
|
|
|
+ )
|
|
|
|
|
+ } else {
|
|
|
|
|
+ plog.Noticef("granted role %s to user %s", r.Role, r.User)
|
|
|
|
|
+ }
|
|
|
return &pb.AuthUserGrantRoleResponse{}, nil
|
|
return &pb.AuthUserGrantRoleResponse{}, nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func (as *authStore) UserGet(r *pb.AuthUserGetRequest) (*pb.AuthUserGetResponse, error) {
|
|
func (as *authStore) UserGet(r *pb.AuthUserGetRequest) (*pb.AuthUserGetResponse, error) {
|
|
|
tx := as.be.BatchTx()
|
|
tx := as.be.BatchTx()
|
|
|
tx.Lock()
|
|
tx.Lock()
|
|
|
- user := getUser(tx, r.Name)
|
|
|
|
|
|
|
+ user := getUser(as.lg, tx, r.Name)
|
|
|
tx.Unlock()
|
|
tx.Unlock()
|
|
|
|
|
|
|
|
if user == nil {
|
|
if user == nil {
|
|
@@ -493,7 +572,7 @@ func (as *authStore) UserGet(r *pb.AuthUserGetRequest) (*pb.AuthUserGetResponse,
|
|
|
func (as *authStore) UserList(r *pb.AuthUserListRequest) (*pb.AuthUserListResponse, error) {
|
|
func (as *authStore) UserList(r *pb.AuthUserListRequest) (*pb.AuthUserListResponse, error) {
|
|
|
tx := as.be.BatchTx()
|
|
tx := as.be.BatchTx()
|
|
|
tx.Lock()
|
|
tx.Lock()
|
|
|
- users := getAllUsers(tx)
|
|
|
|
|
|
|
+ users := getAllUsers(as.lg, tx)
|
|
|
tx.Unlock()
|
|
tx.Unlock()
|
|
|
|
|
|
|
|
resp := &pb.AuthUserListResponse{Users: make([]string, len(users))}
|
|
resp := &pb.AuthUserListResponse{Users: make([]string, len(users))}
|
|
@@ -505,7 +584,15 @@ func (as *authStore) UserList(r *pb.AuthUserListRequest) (*pb.AuthUserListRespon
|
|
|
|
|
|
|
|
func (as *authStore) UserRevokeRole(r *pb.AuthUserRevokeRoleRequest) (*pb.AuthUserRevokeRoleResponse, error) {
|
|
func (as *authStore) UserRevokeRole(r *pb.AuthUserRevokeRoleRequest) (*pb.AuthUserRevokeRoleResponse, error) {
|
|
|
if as.enabled && strings.Compare(r.Name, rootUser) == 0 && strings.Compare(r.Role, rootRole) == 0 {
|
|
if as.enabled && strings.Compare(r.Name, rootUser) == 0 && strings.Compare(r.Role, rootRole) == 0 {
|
|
|
- plog.Errorf("the role root must not be revoked from the user root")
|
|
|
|
|
|
|
+ if as.lg != nil {
|
|
|
|
|
+ as.lg.Warn(
|
|
|
|
|
+ "'root' user cannot revoke 'root' role",
|
|
|
|
|
+ zap.String("user-name", r.Name),
|
|
|
|
|
+ zap.String("role-name", r.Role),
|
|
|
|
|
+ )
|
|
|
|
|
+ } else {
|
|
|
|
|
+ plog.Errorf("the role root must not be revoked from the user root")
|
|
|
|
|
+ }
|
|
|
return nil, ErrInvalidAuthMgmt
|
|
return nil, ErrInvalidAuthMgmt
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -513,7 +600,7 @@ func (as *authStore) UserRevokeRole(r *pb.AuthUserRevokeRoleRequest) (*pb.AuthUs
|
|
|
tx.Lock()
|
|
tx.Lock()
|
|
|
defer tx.Unlock()
|
|
defer tx.Unlock()
|
|
|
|
|
|
|
|
- user := getUser(tx, r.Name)
|
|
|
|
|
|
|
+ user := getUser(as.lg, tx, r.Name)
|
|
|
if user == nil {
|
|
if user == nil {
|
|
|
return nil, ErrUserNotFound
|
|
return nil, ErrUserNotFound
|
|
|
}
|
|
}
|
|
@@ -533,13 +620,23 @@ func (as *authStore) UserRevokeRole(r *pb.AuthUserRevokeRoleRequest) (*pb.AuthUs
|
|
|
return nil, ErrRoleNotGranted
|
|
return nil, ErrRoleNotGranted
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- putUser(tx, updatedUser)
|
|
|
|
|
|
|
+ putUser(as.lg, tx, updatedUser)
|
|
|
|
|
|
|
|
as.invalidateCachedPerm(r.Name)
|
|
as.invalidateCachedPerm(r.Name)
|
|
|
|
|
|
|
|
as.commitRevision(tx)
|
|
as.commitRevision(tx)
|
|
|
|
|
|
|
|
- plog.Noticef("revoked role %s from user %s", r.Role, r.Name)
|
|
|
|
|
|
|
+ if as.lg != nil {
|
|
|
|
|
+ as.lg.Info(
|
|
|
|
|
+ "revoked a role from a user",
|
|
|
|
|
+ zap.String("user-name", r.Name),
|
|
|
|
|
+ zap.Strings("old-user-roles", user.Roles),
|
|
|
|
|
+ zap.Strings("new-user-roles", updatedUser.Roles),
|
|
|
|
|
+ zap.String("revoked-role-name", r.Role),
|
|
|
|
|
+ )
|
|
|
|
|
+ } else {
|
|
|
|
|
+ plog.Noticef("revoked role %s from user %s", r.Role, r.Name)
|
|
|
|
|
+ }
|
|
|
return &pb.AuthUserRevokeRoleResponse{}, nil
|
|
return &pb.AuthUserRevokeRoleResponse{}, nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -561,7 +658,7 @@ func (as *authStore) RoleGet(r *pb.AuthRoleGetRequest) (*pb.AuthRoleGetResponse,
|
|
|
func (as *authStore) RoleList(r *pb.AuthRoleListRequest) (*pb.AuthRoleListResponse, error) {
|
|
func (as *authStore) RoleList(r *pb.AuthRoleListRequest) (*pb.AuthRoleListResponse, error) {
|
|
|
tx := as.be.BatchTx()
|
|
tx := as.be.BatchTx()
|
|
|
tx.Lock()
|
|
tx.Lock()
|
|
|
- roles := getAllRoles(tx)
|
|
|
|
|
|
|
+ roles := getAllRoles(as.lg, tx)
|
|
|
tx.Unlock()
|
|
tx.Unlock()
|
|
|
|
|
|
|
|
resp := &pb.AuthRoleListResponse{Roles: make([]string, len(roles))}
|
|
resp := &pb.AuthRoleListResponse{Roles: make([]string, len(roles))}
|
|
@@ -595,7 +692,7 @@ func (as *authStore) RoleRevokePermission(r *pb.AuthRoleRevokePermissionRequest)
|
|
|
return nil, ErrPermissionNotGranted
|
|
return nil, ErrPermissionNotGranted
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- putRole(tx, updatedRole)
|
|
|
|
|
|
|
+ putRole(as.lg, tx, updatedRole)
|
|
|
|
|
|
|
|
// TODO(mitake): currently single role update invalidates every cache
|
|
// TODO(mitake): currently single role update invalidates every cache
|
|
|
// It should be optimized.
|
|
// It should be optimized.
|
|
@@ -603,13 +700,26 @@ func (as *authStore) RoleRevokePermission(r *pb.AuthRoleRevokePermissionRequest)
|
|
|
|
|
|
|
|
as.commitRevision(tx)
|
|
as.commitRevision(tx)
|
|
|
|
|
|
|
|
- plog.Noticef("revoked key %s from role %s", r.Key, r.Role)
|
|
|
|
|
|
|
+ if as.lg != nil {
|
|
|
|
|
+ as.lg.Info(
|
|
|
|
|
+ "revoked a permission on range",
|
|
|
|
|
+ zap.String("role-name", r.Role),
|
|
|
|
|
+ zap.String("key", string(r.Key)),
|
|
|
|
|
+ zap.String("range-end", string(r.RangeEnd)),
|
|
|
|
|
+ )
|
|
|
|
|
+ } else {
|
|
|
|
|
+ plog.Noticef("revoked key %s from role %s", r.Key, r.Role)
|
|
|
|
|
+ }
|
|
|
return &pb.AuthRoleRevokePermissionResponse{}, nil
|
|
return &pb.AuthRoleRevokePermissionResponse{}, nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func (as *authStore) RoleDelete(r *pb.AuthRoleDeleteRequest) (*pb.AuthRoleDeleteResponse, error) {
|
|
func (as *authStore) RoleDelete(r *pb.AuthRoleDeleteRequest) (*pb.AuthRoleDeleteResponse, error) {
|
|
|
if as.enabled && strings.Compare(r.Role, rootRole) == 0 {
|
|
if as.enabled && strings.Compare(r.Role, rootRole) == 0 {
|
|
|
- plog.Errorf("the role root must not be deleted")
|
|
|
|
|
|
|
+ if as.lg != nil {
|
|
|
|
|
+ as.lg.Warn("cannot delete 'root' role", zap.String("role-name", r.Role))
|
|
|
|
|
+ } else {
|
|
|
|
|
+ plog.Errorf("the role root must not be deleted")
|
|
|
|
|
+ }
|
|
|
return nil, ErrInvalidAuthMgmt
|
|
return nil, ErrInvalidAuthMgmt
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -624,7 +734,7 @@ func (as *authStore) RoleDelete(r *pb.AuthRoleDeleteRequest) (*pb.AuthRoleDelete
|
|
|
|
|
|
|
|
delRole(tx, r.Role)
|
|
delRole(tx, r.Role)
|
|
|
|
|
|
|
|
- users := getAllUsers(tx)
|
|
|
|
|
|
|
+ users := getAllUsers(as.lg, tx)
|
|
|
for _, user := range users {
|
|
for _, user := range users {
|
|
|
updatedUser := &authpb.User{
|
|
updatedUser := &authpb.User{
|
|
|
Name: user.Name,
|
|
Name: user.Name,
|
|
@@ -641,14 +751,18 @@ func (as *authStore) RoleDelete(r *pb.AuthRoleDeleteRequest) (*pb.AuthRoleDelete
|
|
|
continue
|
|
continue
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- putUser(tx, updatedUser)
|
|
|
|
|
|
|
+ putUser(as.lg, tx, updatedUser)
|
|
|
|
|
|
|
|
as.invalidateCachedPerm(string(user.Name))
|
|
as.invalidateCachedPerm(string(user.Name))
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
as.commitRevision(tx)
|
|
as.commitRevision(tx)
|
|
|
|
|
|
|
|
- plog.Noticef("deleted role %s", r.Role)
|
|
|
|
|
|
|
+ if as.lg != nil {
|
|
|
|
|
+ as.lg.Info("deleted a role", zap.String("role-name", r.Role))
|
|
|
|
|
+ } else {
|
|
|
|
|
+ plog.Noticef("deleted role %s", r.Role)
|
|
|
|
|
+ }
|
|
|
return &pb.AuthRoleDeleteResponse{}, nil
|
|
return &pb.AuthRoleDeleteResponse{}, nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -666,12 +780,15 @@ func (as *authStore) RoleAdd(r *pb.AuthRoleAddRequest) (*pb.AuthRoleAddResponse,
|
|
|
Name: []byte(r.Name),
|
|
Name: []byte(r.Name),
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- putRole(tx, newRole)
|
|
|
|
|
|
|
+ putRole(as.lg, tx, newRole)
|
|
|
|
|
|
|
|
as.commitRevision(tx)
|
|
as.commitRevision(tx)
|
|
|
|
|
|
|
|
- plog.Noticef("Role %s is created", r.Name)
|
|
|
|
|
-
|
|
|
|
|
|
|
+ if as.lg != nil {
|
|
|
|
|
+ as.lg.Info("created a role", zap.String("role-name", r.Name))
|
|
|
|
|
+ } else {
|
|
|
|
|
+ plog.Noticef("Role %s is created", r.Name)
|
|
|
|
|
+ }
|
|
|
return &pb.AuthRoleAddResponse{}, nil
|
|
return &pb.AuthRoleAddResponse{}, nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -722,7 +839,7 @@ func (as *authStore) RoleGrantPermission(r *pb.AuthRoleGrantPermissionRequest) (
|
|
|
sort.Sort(permSlice(role.KeyPermission))
|
|
sort.Sort(permSlice(role.KeyPermission))
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- putRole(tx, role)
|
|
|
|
|
|
|
+ putRole(as.lg, tx, role)
|
|
|
|
|
|
|
|
// TODO(mitake): currently single role update invalidates every cache
|
|
// TODO(mitake): currently single role update invalidates every cache
|
|
|
// It should be optimized.
|
|
// It should be optimized.
|
|
@@ -730,8 +847,15 @@ func (as *authStore) RoleGrantPermission(r *pb.AuthRoleGrantPermissionRequest) (
|
|
|
|
|
|
|
|
as.commitRevision(tx)
|
|
as.commitRevision(tx)
|
|
|
|
|
|
|
|
- plog.Noticef("role %s's permission of key %s is updated as %s", r.Name, r.Perm.Key, authpb.Permission_Type_name[int32(r.Perm.PermType)])
|
|
|
|
|
-
|
|
|
|
|
|
|
+ if as.lg != nil {
|
|
|
|
|
+ as.lg.Info(
|
|
|
|
|
+ "granted/updated a permission to a user",
|
|
|
|
|
+ zap.String("user-name", r.Name),
|
|
|
|
|
+ zap.String("permission-name", authpb.Permission_Type_name[int32(r.Perm.PermType)]),
|
|
|
|
|
+ )
|
|
|
|
|
+ } else {
|
|
|
|
|
+ plog.Noticef("role %s's permission of key %s is updated as %s", r.Name, r.Perm.Key, authpb.Permission_Type_name[int32(r.Perm.PermType)])
|
|
|
|
|
+ }
|
|
|
return &pb.AuthRoleGrantPermissionResponse{}, nil
|
|
return &pb.AuthRoleGrantPermissionResponse{}, nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -754,9 +878,13 @@ func (as *authStore) isOpPermitted(userName string, revision uint64, key, rangeE
|
|
|
tx.Lock()
|
|
tx.Lock()
|
|
|
defer tx.Unlock()
|
|
defer tx.Unlock()
|
|
|
|
|
|
|
|
- user := getUser(tx, userName)
|
|
|
|
|
|
|
+ user := getUser(as.lg, tx, userName)
|
|
|
if user == nil {
|
|
if user == nil {
|
|
|
- plog.Errorf("invalid user name %s for permission checking", userName)
|
|
|
|
|
|
|
+ if as.lg != nil {
|
|
|
|
|
+ as.lg.Warn("cannot find a user for permission check", zap.String("user-name", userName))
|
|
|
|
|
+ } else {
|
|
|
|
|
+ plog.Errorf("invalid user name %s for permission checking", userName)
|
|
|
|
|
+ }
|
|
|
return ErrPermissionDenied
|
|
return ErrPermissionDenied
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -794,7 +922,7 @@ func (as *authStore) IsAdminPermitted(authInfo *AuthInfo) error {
|
|
|
|
|
|
|
|
tx := as.be.BatchTx()
|
|
tx := as.be.BatchTx()
|
|
|
tx.Lock()
|
|
tx.Lock()
|
|
|
- u := getUser(tx, authInfo.Username)
|
|
|
|
|
|
|
+ u := getUser(as.lg, tx, authInfo.Username)
|
|
|
tx.Unlock()
|
|
tx.Unlock()
|
|
|
|
|
|
|
|
if u == nil {
|
|
if u == nil {
|
|
@@ -808,7 +936,7 @@ func (as *authStore) IsAdminPermitted(authInfo *AuthInfo) error {
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func getUser(tx backend.BatchTx, username string) *authpb.User {
|
|
|
|
|
|
|
+func getUser(lg *zap.Logger, tx backend.BatchTx, username string) *authpb.User {
|
|
|
_, vs := tx.UnsafeRange(authUsersBucketName, []byte(username), nil, 0)
|
|
_, vs := tx.UnsafeRange(authUsersBucketName, []byte(username), nil, 0)
|
|
|
if len(vs) == 0 {
|
|
if len(vs) == 0 {
|
|
|
return nil
|
|
return nil
|
|
@@ -817,12 +945,20 @@ func getUser(tx backend.BatchTx, username string) *authpb.User {
|
|
|
user := &authpb.User{}
|
|
user := &authpb.User{}
|
|
|
err := user.Unmarshal(vs[0])
|
|
err := user.Unmarshal(vs[0])
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
- plog.Panicf("failed to unmarshal user struct (name: %s): %s", username, err)
|
|
|
|
|
|
|
+ if lg != nil {
|
|
|
|
|
+ lg.Panic(
|
|
|
|
|
+ "failed to unmarshal 'authpb.User'",
|
|
|
|
|
+ zap.String("user-name", username),
|
|
|
|
|
+ zap.Error(err),
|
|
|
|
|
+ )
|
|
|
|
|
+ } else {
|
|
|
|
|
+ plog.Panicf("failed to unmarshal user struct (name: %s): %s", username, err)
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
return user
|
|
return user
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func getAllUsers(tx backend.BatchTx) []*authpb.User {
|
|
|
|
|
|
|
+func getAllUsers(lg *zap.Logger, tx backend.BatchTx) []*authpb.User {
|
|
|
_, vs := tx.UnsafeRange(authUsersBucketName, []byte{0}, []byte{0xff}, -1)
|
|
_, vs := tx.UnsafeRange(authUsersBucketName, []byte{0}, []byte{0xff}, -1)
|
|
|
if len(vs) == 0 {
|
|
if len(vs) == 0 {
|
|
|
return nil
|
|
return nil
|
|
@@ -833,17 +969,25 @@ func getAllUsers(tx backend.BatchTx) []*authpb.User {
|
|
|
user := &authpb.User{}
|
|
user := &authpb.User{}
|
|
|
err := user.Unmarshal(vs[i])
|
|
err := user.Unmarshal(vs[i])
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
- plog.Panicf("failed to unmarshal user struct: %s", err)
|
|
|
|
|
|
|
+ if lg != nil {
|
|
|
|
|
+ lg.Panic("failed to unmarshal 'authpb.User'", zap.Error(err))
|
|
|
|
|
+ } else {
|
|
|
|
|
+ plog.Panicf("failed to unmarshal user struct: %s", err)
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
users[i] = user
|
|
users[i] = user
|
|
|
}
|
|
}
|
|
|
return users
|
|
return users
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func putUser(tx backend.BatchTx, user *authpb.User) {
|
|
|
|
|
|
|
+func putUser(lg *zap.Logger, tx backend.BatchTx, user *authpb.User) {
|
|
|
b, err := user.Marshal()
|
|
b, err := user.Marshal()
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
- plog.Panicf("failed to marshal user struct (name: %s): %s", user.Name, err)
|
|
|
|
|
|
|
+ if lg != nil {
|
|
|
|
|
+ lg.Panic("failed to unmarshal 'authpb.User'", zap.Error(err))
|
|
|
|
|
+ } else {
|
|
|
|
|
+ plog.Panicf("failed to marshal user struct (name: %s): %s", user.Name, err)
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
tx.UnsafePut(authUsersBucketName, user.Name, b)
|
|
tx.UnsafePut(authUsersBucketName, user.Name, b)
|
|
|
}
|
|
}
|
|
@@ -866,7 +1010,7 @@ func getRole(tx backend.BatchTx, rolename string) *authpb.Role {
|
|
|
return role
|
|
return role
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func getAllRoles(tx backend.BatchTx) []*authpb.Role {
|
|
|
|
|
|
|
+func getAllRoles(lg *zap.Logger, tx backend.BatchTx) []*authpb.Role {
|
|
|
_, vs := tx.UnsafeRange(authRolesBucketName, []byte{0}, []byte{0xff}, -1)
|
|
_, vs := tx.UnsafeRange(authRolesBucketName, []byte{0}, []byte{0xff}, -1)
|
|
|
if len(vs) == 0 {
|
|
if len(vs) == 0 {
|
|
|
return nil
|
|
return nil
|
|
@@ -877,17 +1021,29 @@ func getAllRoles(tx backend.BatchTx) []*authpb.Role {
|
|
|
role := &authpb.Role{}
|
|
role := &authpb.Role{}
|
|
|
err := role.Unmarshal(vs[i])
|
|
err := role.Unmarshal(vs[i])
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
- plog.Panicf("failed to unmarshal role struct: %s", err)
|
|
|
|
|
|
|
+ if lg != nil {
|
|
|
|
|
+ lg.Panic("failed to unmarshal 'authpb.Role'", zap.Error(err))
|
|
|
|
|
+ } else {
|
|
|
|
|
+ plog.Panicf("failed to unmarshal role struct: %s", err)
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
roles[i] = role
|
|
roles[i] = role
|
|
|
}
|
|
}
|
|
|
return roles
|
|
return roles
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func putRole(tx backend.BatchTx, role *authpb.Role) {
|
|
|
|
|
|
|
+func putRole(lg *zap.Logger, tx backend.BatchTx, role *authpb.Role) {
|
|
|
b, err := role.Marshal()
|
|
b, err := role.Marshal()
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
- plog.Panicf("failed to marshal role struct (name: %s): %s", role.Name, err)
|
|
|
|
|
|
|
+ if lg != nil {
|
|
|
|
|
+ lg.Panic(
|
|
|
|
|
+ "failed to marshal 'authpb.Role'",
|
|
|
|
|
+ zap.String("role-name", string(role.Name)),
|
|
|
|
|
+ zap.Error(err),
|
|
|
|
|
+ )
|
|
|
|
|
+ } else {
|
|
|
|
|
+ plog.Panicf("failed to marshal role struct (name: %s): %s", role.Name, err)
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
tx.UnsafePut(authRolesBucketName, []byte(role.Name), b)
|
|
tx.UnsafePut(authRolesBucketName, []byte(role.Name), b)
|
|
@@ -903,7 +1059,8 @@ func (as *authStore) IsAuthEnabled() bool {
|
|
|
return as.enabled
|
|
return as.enabled
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func NewAuthStore(be backend.Backend, tp TokenProvider) *authStore {
|
|
|
|
|
|
|
+// NewAuthStore creates a new AuthStore.
|
|
|
|
|
+func NewAuthStore(lg *zap.Logger, be backend.Backend, tp TokenProvider) *authStore {
|
|
|
tx := be.BatchTx()
|
|
tx := be.BatchTx()
|
|
|
tx.Lock()
|
|
tx.Lock()
|
|
|
|
|
|
|
@@ -920,8 +1077,9 @@ func NewAuthStore(be backend.Backend, tp TokenProvider) *authStore {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
as := &authStore{
|
|
as := &authStore{
|
|
|
- be: be,
|
|
|
|
|
revision: getRevision(tx),
|
|
revision: getRevision(tx),
|
|
|
|
|
+ lg: lg,
|
|
|
|
|
+ be: be,
|
|
|
enabled: enabled,
|
|
enabled: enabled,
|
|
|
rangePermCache: make(map[string]*unifiedRangePermissions),
|
|
rangePermCache: make(map[string]*unifiedRangePermissions),
|
|
|
tokenProvider: tp,
|
|
tokenProvider: tp,
|
|
@@ -960,7 +1118,6 @@ func getRevision(tx backend.BatchTx) uint64 {
|
|
|
// this can happen in the initialization phase
|
|
// this can happen in the initialization phase
|
|
|
return 0
|
|
return 0
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
return binary.BigEndian.Uint64(vs[0])
|
|
return binary.BigEndian.Uint64(vs[0])
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -982,12 +1139,12 @@ func (as *authStore) AuthInfoFromTLS(ctx context.Context) *AuthInfo {
|
|
|
for _, chains := range tlsInfo.State.VerifiedChains {
|
|
for _, chains := range tlsInfo.State.VerifiedChains {
|
|
|
for _, chain := range chains {
|
|
for _, chain := range chains {
|
|
|
cn := chain.Subject.CommonName
|
|
cn := chain.Subject.CommonName
|
|
|
- plog.Debugf("found common name %s", cn)
|
|
|
|
|
-
|
|
|
|
|
- return &AuthInfo{
|
|
|
|
|
- Username: cn,
|
|
|
|
|
- Revision: as.Revision(),
|
|
|
|
|
|
|
+ if as.lg != nil {
|
|
|
|
|
+ as.lg.Debug("found command name", zap.String("common-name", cn))
|
|
|
|
|
+ } else {
|
|
|
|
|
+ plog.Debugf("found common name %s", cn)
|
|
|
}
|
|
}
|
|
|
|
|
+ return &AuthInfo{Username: cn, Revision: as.Revision()}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -1012,7 +1169,11 @@ func (as *authStore) AuthInfoFromCtx(ctx context.Context) (*AuthInfo, error) {
|
|
|
token := ts[0]
|
|
token := ts[0]
|
|
|
authInfo, uok := as.authInfoFromToken(ctx, token)
|
|
authInfo, uok := as.authInfoFromToken(ctx, token)
|
|
|
if !uok {
|
|
if !uok {
|
|
|
- plog.Warningf("invalid auth token: %s", token)
|
|
|
|
|
|
|
+ if as.lg != nil {
|
|
|
|
|
+ as.lg.Warn("invalid auth token", zap.String("token", token))
|
|
|
|
|
+ } else {
|
|
|
|
|
+ plog.Warningf("invalid auth token: %s", token)
|
|
|
|
|
+ }
|
|
|
return nil, ErrInvalidAuthToken
|
|
return nil, ErrInvalidAuthToken
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -1023,7 +1184,7 @@ func (as *authStore) GenTokenPrefix() (string, error) {
|
|
|
return as.tokenProvider.genTokenPrefix()
|
|
return as.tokenProvider.genTokenPrefix()
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func decomposeOpts(optstr string) (string, map[string]string, error) {
|
|
|
|
|
|
|
+func decomposeOpts(lg *zap.Logger, optstr string) (string, map[string]string, error) {
|
|
|
opts := strings.Split(optstr, ",")
|
|
opts := strings.Split(optstr, ",")
|
|
|
tokenType := opts[0]
|
|
tokenType := opts[0]
|
|
|
|
|
|
|
@@ -1032,12 +1193,24 @@ func decomposeOpts(optstr string) (string, map[string]string, error) {
|
|
|
pair := strings.Split(opts[i], "=")
|
|
pair := strings.Split(opts[i], "=")
|
|
|
|
|
|
|
|
if len(pair) != 2 {
|
|
if len(pair) != 2 {
|
|
|
- plog.Errorf("invalid token specific option: %s", optstr)
|
|
|
|
|
|
|
+ if lg != nil {
|
|
|
|
|
+ lg.Warn("invalid token option", zap.String("option", optstr))
|
|
|
|
|
+ } else {
|
|
|
|
|
+ plog.Errorf("invalid token specific option: %s", optstr)
|
|
|
|
|
+ }
|
|
|
return "", nil, ErrInvalidAuthOpts
|
|
return "", nil, ErrInvalidAuthOpts
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if _, ok := typeSpecificOpts[pair[0]]; ok {
|
|
if _, ok := typeSpecificOpts[pair[0]]; ok {
|
|
|
- plog.Errorf("invalid token specific option, duplicated parameters (%s): %s", pair[0], optstr)
|
|
|
|
|
|
|
+ if lg != nil {
|
|
|
|
|
+ lg.Warn(
|
|
|
|
|
+ "invalid token option",
|
|
|
|
|
+ zap.String("option", optstr),
|
|
|
|
|
+ zap.String("duplicate-parameter", pair[0]),
|
|
|
|
|
+ )
|
|
|
|
|
+ } else {
|
|
|
|
|
+ plog.Errorf("invalid token specific option, duplicated parameters (%s): %s", pair[0], optstr)
|
|
|
|
|
+ }
|
|
|
return "", nil, ErrInvalidAuthOpts
|
|
return "", nil, ErrInvalidAuthOpts
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -1048,8 +1221,12 @@ func decomposeOpts(optstr string) (string, map[string]string, error) {
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func NewTokenProvider(lg *zap.Logger, tokenOpts string, indexWaiter func(uint64) <-chan struct{}) (TokenProvider, error) {
|
|
|
|
|
- tokenType, typeSpecificOpts, err := decomposeOpts(tokenOpts)
|
|
|
|
|
|
|
+// NewTokenProvider creates a new token provider.
|
|
|
|
|
+func NewTokenProvider(
|
|
|
|
|
+ lg *zap.Logger,
|
|
|
|
|
+ tokenOpts string,
|
|
|
|
|
+ indexWaiter func(uint64) <-chan struct{}) (TokenProvider, error) {
|
|
|
|
|
+ tokenType, typeSpecificOpts, err := decomposeOpts(lg, tokenOpts)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
return nil, ErrInvalidAuthOpts
|
|
return nil, ErrInvalidAuthOpts
|
|
|
}
|
|
}
|
|
@@ -1061,14 +1238,21 @@ func NewTokenProvider(lg *zap.Logger, tokenOpts string, indexWaiter func(uint64)
|
|
|
} else {
|
|
} else {
|
|
|
plog.Warningf("simple token is not cryptographically signed")
|
|
plog.Warningf("simple token is not cryptographically signed")
|
|
|
}
|
|
}
|
|
|
- return newTokenProviderSimple(indexWaiter), nil
|
|
|
|
|
|
|
+ return newTokenProviderSimple(lg, indexWaiter), nil
|
|
|
|
|
+
|
|
|
case "jwt":
|
|
case "jwt":
|
|
|
- return newTokenProviderJWT(typeSpecificOpts)
|
|
|
|
|
|
|
+ return newTokenProviderJWT(lg, typeSpecificOpts)
|
|
|
|
|
+
|
|
|
case "":
|
|
case "":
|
|
|
return newTokenProviderNop()
|
|
return newTokenProviderNop()
|
|
|
|
|
+
|
|
|
default:
|
|
default:
|
|
|
if lg != nil {
|
|
if lg != nil {
|
|
|
- lg.Warn("unknown token type", zap.String("type", tokenType), zap.Error(ErrInvalidAuthOpts))
|
|
|
|
|
|
|
+ lg.Warn(
|
|
|
|
|
+ "unknown token type",
|
|
|
|
|
+ zap.String("type", tokenType),
|
|
|
|
|
+ zap.Error(ErrInvalidAuthOpts),
|
|
|
|
|
+ )
|
|
|
} else {
|
|
} else {
|
|
|
plog.Errorf("unknown token type: %s", tokenType)
|
|
plog.Errorf("unknown token type: %s", tokenType)
|
|
|
}
|
|
}
|
|
@@ -1086,7 +1270,14 @@ func (as *authStore) WithRoot(ctx context.Context) context.Context {
|
|
|
ctx1 := context.WithValue(ctx, AuthenticateParamIndex{}, uint64(0))
|
|
ctx1 := context.WithValue(ctx, AuthenticateParamIndex{}, uint64(0))
|
|
|
prefix, err := ts.genTokenPrefix()
|
|
prefix, err := ts.genTokenPrefix()
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
- plog.Errorf("failed to generate prefix of internally used token")
|
|
|
|
|
|
|
+ if as.lg != nil {
|
|
|
|
|
+ as.lg.Warn(
|
|
|
|
|
+ "failed to generate prefix of internally used token",
|
|
|
|
|
+ zap.Error(err),
|
|
|
|
|
+ )
|
|
|
|
|
+ } else {
|
|
|
|
|
+ plog.Errorf("failed to generate prefix of internally used token")
|
|
|
|
|
+ }
|
|
|
return ctx
|
|
return ctx
|
|
|
}
|
|
}
|
|
|
ctxForAssign = context.WithValue(ctx1, AuthenticateParamSimpleTokenPrefix{}, prefix)
|
|
ctxForAssign = context.WithValue(ctx1, AuthenticateParamSimpleTokenPrefix{}, prefix)
|
|
@@ -1097,7 +1288,14 @@ func (as *authStore) WithRoot(ctx context.Context) context.Context {
|
|
|
token, err := as.tokenProvider.assign(ctxForAssign, "root", as.Revision())
|
|
token, err := as.tokenProvider.assign(ctxForAssign, "root", as.Revision())
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
// this must not happen
|
|
// this must not happen
|
|
|
- plog.Errorf("failed to assign token for lease revoking: %s", err)
|
|
|
|
|
|
|
+ if as.lg != nil {
|
|
|
|
|
+ as.lg.Warn(
|
|
|
|
|
+ "failed to assign token for lease revoking",
|
|
|
|
|
+ zap.Error(err),
|
|
|
|
|
+ )
|
|
|
|
|
+ } else {
|
|
|
|
|
+ plog.Errorf("failed to assign token for lease revoking: %s", err)
|
|
|
|
|
+ }
|
|
|
return ctx
|
|
return ctx
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -1113,11 +1311,19 @@ func (as *authStore) WithRoot(ctx context.Context) context.Context {
|
|
|
func (as *authStore) HasRole(user, role string) bool {
|
|
func (as *authStore) HasRole(user, role string) bool {
|
|
|
tx := as.be.BatchTx()
|
|
tx := as.be.BatchTx()
|
|
|
tx.Lock()
|
|
tx.Lock()
|
|
|
- u := getUser(tx, user)
|
|
|
|
|
|
|
+ u := getUser(as.lg, tx, user)
|
|
|
tx.Unlock()
|
|
tx.Unlock()
|
|
|
|
|
|
|
|
if u == nil {
|
|
if u == nil {
|
|
|
- plog.Warningf("tried to check user %s has role %s, but user %s doesn't exist", user, role, user)
|
|
|
|
|
|
|
+ if as.lg != nil {
|
|
|
|
|
+ as.lg.Warn(
|
|
|
|
|
+ "'has-role' requested for non-existing user",
|
|
|
|
|
+ zap.String("user-name", user),
|
|
|
|
|
+ zap.String("role-name", role),
|
|
|
|
|
+ )
|
|
|
|
|
+ } else {
|
|
|
|
|
+ plog.Warningf("tried to check user %s has role %s, but user %s doesn't exist", user, role, user)
|
|
|
|
|
+ }
|
|
|
return false
|
|
return false
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -1126,6 +1332,5 @@ func (as *authStore) HasRole(user, role string) bool {
|
|
|
return true
|
|
return true
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
return false
|
|
return false
|
|
|
}
|
|
}
|