123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874 |
- // Copyright 2016 The etcd Authors
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- package auth
- import (
- "context"
- "fmt"
- "os"
- "reflect"
- "strings"
- "sync"
- "testing"
- "time"
- "go.etcd.io/etcd/auth/authpb"
- "go.etcd.io/etcd/etcdserver/api/v3rpc/rpctypes"
- pb "go.etcd.io/etcd/etcdserver/etcdserverpb"
- "go.etcd.io/etcd/mvcc/backend"
- "go.uber.org/zap"
- "golang.org/x/crypto/bcrypt"
- "google.golang.org/grpc/metadata"
- )
- func dummyIndexWaiter(index uint64) <-chan struct{} {
- ch := make(chan struct{})
- go func() {
- ch <- struct{}{}
- }()
- return ch
- }
- // TestNewAuthStoreRevision ensures newly auth store
- // keeps the old revision when there are no changes.
- func TestNewAuthStoreRevision(t *testing.T) {
- b, tPath := backend.NewDefaultTmpBackend()
- defer os.Remove(tPath)
- tp, err := NewTokenProvider(zap.NewExample(), tokenTypeSimple, dummyIndexWaiter)
- if err != nil {
- t.Fatal(err)
- }
- as := NewAuthStore(zap.NewExample(), b, tp, bcrypt.MinCost)
- err = enableAuthAndCreateRoot(as)
- if err != nil {
- t.Fatal(err)
- }
- old := as.Revision()
- as.Close()
- b.Close()
- // no changes to commit
- b2 := backend.NewDefaultBackend(tPath)
- as = NewAuthStore(zap.NewExample(), b2, tp, bcrypt.MinCost)
- new := as.Revision()
- as.Close()
- b2.Close()
- if old != new {
- t.Fatalf("expected revision %d, got %d", old, new)
- }
- }
- // TestNewAuthStoreBryptCost ensures that NewAuthStore uses default when given bcrypt-cost is invalid
- func TestNewAuthStoreBcryptCost(t *testing.T) {
- b, tPath := backend.NewDefaultTmpBackend()
- defer os.Remove(tPath)
- tp, err := NewTokenProvider(zap.NewExample(), tokenTypeSimple, dummyIndexWaiter)
- if err != nil {
- t.Fatal(err)
- }
- invalidCosts := [2]int{bcrypt.MinCost - 1, bcrypt.MaxCost + 1}
- for _, invalidCost := range invalidCosts {
- as := NewAuthStore(zap.NewExample(), b, tp, invalidCost)
- if as.BcryptCost() != bcrypt.DefaultCost {
- t.Fatalf("expected DefaultCost when bcryptcost is invalid")
- }
- as.Close()
- }
- b.Close()
- }
- func setupAuthStore(t *testing.T) (store *authStore, teardownfunc func(t *testing.T)) {
- b, tPath := backend.NewDefaultTmpBackend()
- tp, err := NewTokenProvider(zap.NewExample(), tokenTypeSimple, dummyIndexWaiter)
- if err != nil {
- t.Fatal(err)
- }
- as := NewAuthStore(zap.NewExample(), b, tp, bcrypt.MinCost)
- err = enableAuthAndCreateRoot(as)
- if err != nil {
- t.Fatal(err)
- }
- // adds a new role
- _, err = as.RoleAdd(&pb.AuthRoleAddRequest{Name: "role-test"})
- if err != nil {
- t.Fatal(err)
- }
- ua := &pb.AuthUserAddRequest{Name: "foo", Password: "bar", Options: &authpb.UserAddOptions{NoPassword: false}}
- _, err = as.UserAdd(ua) // add a non-existing user
- if err != nil {
- t.Fatal(err)
- }
- tearDown := func(_ *testing.T) {
- b.Close()
- os.Remove(tPath)
- as.Close()
- }
- return as, tearDown
- }
- func enableAuthAndCreateRoot(as *authStore) error {
- _, err := as.UserAdd(&pb.AuthUserAddRequest{Name: "root", Password: "root", Options: &authpb.UserAddOptions{NoPassword: false}})
- if err != nil {
- return err
- }
- _, err = as.RoleAdd(&pb.AuthRoleAddRequest{Name: "root"})
- if err != nil {
- return err
- }
- _, err = as.UserGrantRole(&pb.AuthUserGrantRoleRequest{User: "root", Role: "root"})
- if err != nil {
- return err
- }
- return as.AuthEnable()
- }
- func TestUserAdd(t *testing.T) {
- as, tearDown := setupAuthStore(t)
- defer tearDown(t)
- ua := &pb.AuthUserAddRequest{Name: "foo", Options: &authpb.UserAddOptions{NoPassword: false}}
- _, err := as.UserAdd(ua) // add an existing user
- if err == nil {
- t.Fatalf("expected %v, got %v", ErrUserAlreadyExist, err)
- }
- if err != ErrUserAlreadyExist {
- t.Fatalf("expected %v, got %v", ErrUserAlreadyExist, err)
- }
- ua = &pb.AuthUserAddRequest{Name: "", Options: &authpb.UserAddOptions{NoPassword: false}}
- _, err = as.UserAdd(ua) // add a user with empty name
- if err != ErrUserEmpty {
- t.Fatal(err)
- }
- }
- func TestRecover(t *testing.T) {
- as, tearDown := setupAuthStore(t)
- defer tearDown(t)
- as.enabled = false
- as.Recover(as.be)
- if !as.IsAuthEnabled() {
- t.Fatalf("expected auth enabled got disabled")
- }
- }
- func TestCheckPassword(t *testing.T) {
- as, tearDown := setupAuthStore(t)
- defer tearDown(t)
- // auth a non-existing user
- _, err := as.CheckPassword("foo-test", "bar")
- if err == nil {
- t.Fatalf("expected %v, got %v", ErrAuthFailed, err)
- }
- if err != ErrAuthFailed {
- t.Fatalf("expected %v, got %v", ErrAuthFailed, err)
- }
- // auth an existing user with correct password
- _, err = as.CheckPassword("foo", "bar")
- if err != nil {
- t.Fatal(err)
- }
- // auth an existing user but with wrong password
- _, err = as.CheckPassword("foo", "")
- if err == nil {
- t.Fatalf("expected %v, got %v", ErrAuthFailed, err)
- }
- if err != ErrAuthFailed {
- t.Fatalf("expected %v, got %v", ErrAuthFailed, err)
- }
- }
- func TestUserDelete(t *testing.T) {
- as, tearDown := setupAuthStore(t)
- defer tearDown(t)
- // delete an existing user
- ud := &pb.AuthUserDeleteRequest{Name: "foo"}
- _, err := as.UserDelete(ud)
- if err != nil {
- t.Fatal(err)
- }
- // delete a non-existing user
- _, err = as.UserDelete(ud)
- if err == nil {
- t.Fatalf("expected %v, got %v", ErrUserNotFound, err)
- }
- if err != ErrUserNotFound {
- t.Fatalf("expected %v, got %v", ErrUserNotFound, err)
- }
- }
- func TestUserChangePassword(t *testing.T) {
- as, tearDown := setupAuthStore(t)
- defer tearDown(t)
- ctx1 := context.WithValue(context.WithValue(context.TODO(), AuthenticateParamIndex{}, uint64(1)), AuthenticateParamSimpleTokenPrefix{}, "dummy")
- _, err := as.Authenticate(ctx1, "foo", "bar")
- if err != nil {
- t.Fatal(err)
- }
- _, err = as.UserChangePassword(&pb.AuthUserChangePasswordRequest{Name: "foo", Password: "baz"})
- if err != nil {
- t.Fatal(err)
- }
- ctx2 := context.WithValue(context.WithValue(context.TODO(), AuthenticateParamIndex{}, uint64(2)), AuthenticateParamSimpleTokenPrefix{}, "dummy")
- _, err = as.Authenticate(ctx2, "foo", "baz")
- if err != nil {
- t.Fatal(err)
- }
- // change a non-existing user
- _, err = as.UserChangePassword(&pb.AuthUserChangePasswordRequest{Name: "foo-test", Password: "bar"})
- if err == nil {
- t.Fatalf("expected %v, got %v", ErrUserNotFound, err)
- }
- if err != ErrUserNotFound {
- t.Fatalf("expected %v, got %v", ErrUserNotFound, err)
- }
- }
- func TestRoleAdd(t *testing.T) {
- as, tearDown := setupAuthStore(t)
- defer tearDown(t)
- // adds a new role
- _, err := as.RoleAdd(&pb.AuthRoleAddRequest{Name: "role-test-1"})
- if err != nil {
- t.Fatal(err)
- }
- // add a role with empty name
- _, err = as.RoleAdd(&pb.AuthRoleAddRequest{Name: ""})
- if err != ErrRoleEmpty {
- t.Fatal(err)
- }
- }
- func TestUserGrant(t *testing.T) {
- as, tearDown := setupAuthStore(t)
- defer tearDown(t)
- // grants a role to the user
- _, err := as.UserGrantRole(&pb.AuthUserGrantRoleRequest{User: "foo", Role: "role-test"})
- if err != nil {
- t.Fatal(err)
- }
- // grants a role to a non-existing user
- _, err = as.UserGrantRole(&pb.AuthUserGrantRoleRequest{User: "foo-test", Role: "role-test"})
- if err == nil {
- t.Errorf("expected %v, got %v", ErrUserNotFound, err)
- }
- if err != ErrUserNotFound {
- t.Errorf("expected %v, got %v", ErrUserNotFound, err)
- }
- }
- func TestHasRole(t *testing.T) {
- as, tearDown := setupAuthStore(t)
- defer tearDown(t)
- // grants a role to the user
- _, err := as.UserGrantRole(&pb.AuthUserGrantRoleRequest{User: "foo", Role: "role-test"})
- if err != nil {
- t.Fatal(err)
- }
- // checks role reflects correctly
- hr := as.HasRole("foo", "role-test")
- if !hr {
- t.Fatal("expected role granted, got false")
- }
- // checks non existent role
- hr = as.HasRole("foo", "non-existent-role")
- if hr {
- t.Fatal("expected role not found, got true")
- }
- // checks non existent user
- hr = as.HasRole("nouser", "role-test")
- if hr {
- t.Fatal("expected user not found got true")
- }
- }
- func TestIsOpPermitted(t *testing.T) {
- as, tearDown := setupAuthStore(t)
- defer tearDown(t)
- // add new role
- _, err := as.RoleAdd(&pb.AuthRoleAddRequest{Name: "role-test-1"})
- if err != nil {
- t.Fatal(err)
- }
- perm := &authpb.Permission{
- PermType: authpb.WRITE,
- Key: []byte("Keys"),
- RangeEnd: []byte("RangeEnd"),
- }
- _, err = as.RoleGrantPermission(&pb.AuthRoleGrantPermissionRequest{
- Name: "role-test-1",
- Perm: perm,
- })
- if err != nil {
- t.Fatal(err)
- }
- // grants a role to the user
- _, err = as.UserGrantRole(&pb.AuthUserGrantRoleRequest{User: "foo", Role: "role-test-1"})
- if err != nil {
- t.Fatal(err)
- }
- // check permission reflected to user
- err = as.isOpPermitted("foo", as.Revision(), perm.Key, perm.RangeEnd, perm.PermType)
- if err != nil {
- t.Fatal(err)
- }
- }
- func TestGetUser(t *testing.T) {
- as, tearDown := setupAuthStore(t)
- defer tearDown(t)
- _, err := as.UserGrantRole(&pb.AuthUserGrantRoleRequest{User: "foo", Role: "role-test"})
- if err != nil {
- t.Fatal(err)
- }
- u, err := as.UserGet(&pb.AuthUserGetRequest{Name: "foo"})
- if err != nil {
- t.Fatal(err)
- }
- if u == nil {
- t.Fatal("expect user not nil, got nil")
- }
- expected := []string{"role-test"}
- if !reflect.DeepEqual(expected, u.Roles) {
- t.Errorf("expected %v, got %v", expected, u.Roles)
- }
- // check non existent user
- _, err = as.UserGet(&pb.AuthUserGetRequest{Name: "nouser"})
- if err == nil {
- t.Errorf("expected %v, got %v", ErrUserNotFound, err)
- }
- }
- func TestListUsers(t *testing.T) {
- as, tearDown := setupAuthStore(t)
- defer tearDown(t)
- ua := &pb.AuthUserAddRequest{Name: "user1", Password: "pwd1", Options: &authpb.UserAddOptions{NoPassword: false}}
- _, err := as.UserAdd(ua) // add a non-existing user
- if err != nil {
- t.Fatal(err)
- }
- ul, err := as.UserList(&pb.AuthUserListRequest{})
- if err != nil {
- t.Fatal(err)
- }
- if !contains(ul.Users, "root") {
- t.Errorf("expected %v in %v", "root", ul.Users)
- }
- if !contains(ul.Users, "user1") {
- t.Errorf("expected %v in %v", "user1", ul.Users)
- }
- }
- func TestRoleGrantPermission(t *testing.T) {
- as, tearDown := setupAuthStore(t)
- defer tearDown(t)
- _, err := as.RoleAdd(&pb.AuthRoleAddRequest{Name: "role-test-1"})
- if err != nil {
- t.Fatal(err)
- }
- perm := &authpb.Permission{
- PermType: authpb.WRITE,
- Key: []byte("Keys"),
- RangeEnd: []byte("RangeEnd"),
- }
- _, err = as.RoleGrantPermission(&pb.AuthRoleGrantPermissionRequest{
- Name: "role-test-1",
- Perm: perm,
- })
- if err != nil {
- t.Error(err)
- }
- r, err := as.RoleGet(&pb.AuthRoleGetRequest{Role: "role-test-1"})
- if err != nil {
- t.Fatal(err)
- }
- if !reflect.DeepEqual(perm, r.Perm[0]) {
- t.Errorf("expected %v, got %v", perm, r.Perm[0])
- }
- }
- func TestRoleRevokePermission(t *testing.T) {
- as, tearDown := setupAuthStore(t)
- defer tearDown(t)
- _, err := as.RoleAdd(&pb.AuthRoleAddRequest{Name: "role-test-1"})
- if err != nil {
- t.Fatal(err)
- }
- perm := &authpb.Permission{
- PermType: authpb.WRITE,
- Key: []byte("Keys"),
- RangeEnd: []byte("RangeEnd"),
- }
- _, err = as.RoleGrantPermission(&pb.AuthRoleGrantPermissionRequest{
- Name: "role-test-1",
- Perm: perm,
- })
- if err != nil {
- t.Fatal(err)
- }
- _, err = as.RoleGet(&pb.AuthRoleGetRequest{Role: "role-test-1"})
- if err != nil {
- t.Fatal(err)
- }
- _, err = as.RoleRevokePermission(&pb.AuthRoleRevokePermissionRequest{
- Role: "role-test-1",
- Key: []byte("Keys"),
- RangeEnd: []byte("RangeEnd"),
- })
- if err != nil {
- t.Fatal(err)
- }
- var r *pb.AuthRoleGetResponse
- r, err = as.RoleGet(&pb.AuthRoleGetRequest{Role: "role-test-1"})
- if err != nil {
- t.Fatal(err)
- }
- if len(r.Perm) != 0 {
- t.Errorf("expected %v, got %v", 0, len(r.Perm))
- }
- }
- func TestUserRevokePermission(t *testing.T) {
- as, tearDown := setupAuthStore(t)
- defer tearDown(t)
- _, err := as.RoleAdd(&pb.AuthRoleAddRequest{Name: "role-test-1"})
- if err != nil {
- t.Fatal(err)
- }
- _, err = as.UserGrantRole(&pb.AuthUserGrantRoleRequest{User: "foo", Role: "role-test"})
- if err != nil {
- t.Fatal(err)
- }
- _, err = as.UserGrantRole(&pb.AuthUserGrantRoleRequest{User: "foo", Role: "role-test-1"})
- if err != nil {
- t.Fatal(err)
- }
- u, err := as.UserGet(&pb.AuthUserGetRequest{Name: "foo"})
- if err != nil {
- t.Fatal(err)
- }
- expected := []string{"role-test", "role-test-1"}
- if !reflect.DeepEqual(expected, u.Roles) {
- t.Fatalf("expected %v, got %v", expected, u.Roles)
- }
- _, err = as.UserRevokeRole(&pb.AuthUserRevokeRoleRequest{Name: "foo", Role: "role-test-1"})
- if err != nil {
- t.Fatal(err)
- }
- u, err = as.UserGet(&pb.AuthUserGetRequest{Name: "foo"})
- if err != nil {
- t.Fatal(err)
- }
- expected = []string{"role-test"}
- if !reflect.DeepEqual(expected, u.Roles) {
- t.Errorf("expected %v, got %v", expected, u.Roles)
- }
- }
- func TestRoleDelete(t *testing.T) {
- as, tearDown := setupAuthStore(t)
- defer tearDown(t)
- _, err := as.RoleDelete(&pb.AuthRoleDeleteRequest{Role: "role-test"})
- if err != nil {
- t.Fatal(err)
- }
- rl, err := as.RoleList(&pb.AuthRoleListRequest{})
- if err != nil {
- t.Fatal(err)
- }
- expected := []string{"root"}
- if !reflect.DeepEqual(expected, rl.Roles) {
- t.Errorf("expected %v, got %v", expected, rl.Roles)
- }
- }
- func TestAuthInfoFromCtx(t *testing.T) {
- as, tearDown := setupAuthStore(t)
- defer tearDown(t)
- ctx := context.Background()
- ai, err := as.AuthInfoFromCtx(ctx)
- if err != nil && ai != nil {
- t.Errorf("expected (nil, nil), got (%v, %v)", ai, err)
- }
- // as if it came from RPC
- ctx = metadata.NewIncomingContext(context.Background(), metadata.New(map[string]string{"tokens": "dummy"}))
- ai, err = as.AuthInfoFromCtx(ctx)
- if err != nil && ai != nil {
- t.Errorf("expected (nil, nil), got (%v, %v)", ai, err)
- }
- ctx = context.WithValue(context.WithValue(context.TODO(), AuthenticateParamIndex{}, uint64(1)), AuthenticateParamSimpleTokenPrefix{}, "dummy")
- resp, err := as.Authenticate(ctx, "foo", "bar")
- if err != nil {
- t.Error(err)
- }
- ctx = metadata.NewIncomingContext(context.Background(), metadata.New(map[string]string{rpctypes.TokenFieldNameGRPC: "Invalid Token"}))
- _, err = as.AuthInfoFromCtx(ctx)
- if err != ErrInvalidAuthToken {
- t.Errorf("expected %v, got %v", ErrInvalidAuthToken, err)
- }
- ctx = metadata.NewIncomingContext(context.Background(), metadata.New(map[string]string{rpctypes.TokenFieldNameGRPC: "Invalid.Token"}))
- _, err = as.AuthInfoFromCtx(ctx)
- if err != ErrInvalidAuthToken {
- t.Errorf("expected %v, got %v", ErrInvalidAuthToken, err)
- }
- ctx = metadata.NewIncomingContext(context.Background(), metadata.New(map[string]string{rpctypes.TokenFieldNameGRPC: resp.Token}))
- ai, err = as.AuthInfoFromCtx(ctx)
- if err != nil {
- t.Error(err)
- }
- if ai.Username != "foo" {
- t.Errorf("expected %v, got %v", "foo", ai.Username)
- }
- }
- func TestAuthDisable(t *testing.T) {
- as, tearDown := setupAuthStore(t)
- defer tearDown(t)
- as.AuthDisable()
- ctx := context.WithValue(context.WithValue(context.TODO(), AuthenticateParamIndex{}, uint64(2)), AuthenticateParamSimpleTokenPrefix{}, "dummy")
- _, err := as.Authenticate(ctx, "foo", "bar")
- if err != ErrAuthNotEnabled {
- t.Errorf("expected %v, got %v", ErrAuthNotEnabled, err)
- }
- // Disabling disabled auth to make sure it can return safely if store is already disabled.
- as.AuthDisable()
- _, err = as.Authenticate(ctx, "foo", "bar")
- if err != ErrAuthNotEnabled {
- t.Errorf("expected %v, got %v", ErrAuthNotEnabled, err)
- }
- }
- // TestAuthRevisionRace ensures that access to authStore.revision is thread-safe.
- func TestAuthInfoFromCtxRace(t *testing.T) {
- b, tPath := backend.NewDefaultTmpBackend()
- defer os.Remove(tPath)
- tp, err := NewTokenProvider(zap.NewExample(), tokenTypeSimple, dummyIndexWaiter)
- if err != nil {
- t.Fatal(err)
- }
- as := NewAuthStore(zap.NewExample(), b, tp, bcrypt.MinCost)
- defer as.Close()
- donec := make(chan struct{})
- go func() {
- defer close(donec)
- ctx := metadata.NewIncomingContext(context.Background(), metadata.New(map[string]string{rpctypes.TokenFieldNameGRPC: "test"}))
- as.AuthInfoFromCtx(ctx)
- }()
- as.UserAdd(&pb.AuthUserAddRequest{Name: "test", Options: &authpb.UserAddOptions{NoPassword: false}})
- <-donec
- }
- func TestIsAdminPermitted(t *testing.T) {
- as, tearDown := setupAuthStore(t)
- defer tearDown(t)
- err := as.IsAdminPermitted(&AuthInfo{Username: "root", Revision: 1})
- if err != nil {
- t.Errorf("expected nil, got %v", err)
- }
- // invalid user
- err = as.IsAdminPermitted(&AuthInfo{Username: "rooti", Revision: 1})
- if err != ErrUserNotFound {
- t.Errorf("expected %v, got %v", ErrUserNotFound, err)
- }
- // non-admin user
- err = as.IsAdminPermitted(&AuthInfo{Username: "foo", Revision: 1})
- if err != ErrPermissionDenied {
- t.Errorf("expected %v, got %v", ErrPermissionDenied, err)
- }
- // disabled auth should return nil
- as.AuthDisable()
- err = as.IsAdminPermitted(&AuthInfo{Username: "root", Revision: 1})
- if err != nil {
- t.Errorf("expected nil, got %v", err)
- }
- }
- func TestRecoverFromSnapshot(t *testing.T) {
- as, _ := setupAuthStore(t)
- ua := &pb.AuthUserAddRequest{Name: "foo", Options: &authpb.UserAddOptions{NoPassword: false}}
- _, err := as.UserAdd(ua) // add an existing user
- if err == nil {
- t.Fatalf("expected %v, got %v", ErrUserAlreadyExist, err)
- }
- if err != ErrUserAlreadyExist {
- t.Fatalf("expected %v, got %v", ErrUserAlreadyExist, err)
- }
- ua = &pb.AuthUserAddRequest{Name: "", Options: &authpb.UserAddOptions{NoPassword: false}}
- _, err = as.UserAdd(ua) // add a user with empty name
- if err != ErrUserEmpty {
- t.Fatal(err)
- }
- as.Close()
- tp, err := NewTokenProvider(zap.NewExample(), tokenTypeSimple, dummyIndexWaiter)
- if err != nil {
- t.Fatal(err)
- }
- as2 := NewAuthStore(zap.NewExample(), as.be, tp, bcrypt.MinCost)
- defer func(a *authStore) {
- a.Close()
- }(as2)
- if !as2.IsAuthEnabled() {
- t.Fatal("recovering authStore from existing backend failed")
- }
- ul, err := as.UserList(&pb.AuthUserListRequest{})
- if err != nil {
- t.Fatal(err)
- }
- if !contains(ul.Users, "root") {
- t.Errorf("expected %v in %v", "root", ul.Users)
- }
- }
- func contains(array []string, str string) bool {
- for _, s := range array {
- if s == str {
- return true
- }
- }
- return false
- }
- func TestHammerSimpleAuthenticate(t *testing.T) {
- // set TTL values low to try to trigger races
- oldTTL, oldTTLRes := simpleTokenTTL, simpleTokenTTLResolution
- defer func() {
- simpleTokenTTL = oldTTL
- simpleTokenTTLResolution = oldTTLRes
- }()
- simpleTokenTTL = 10 * time.Millisecond
- simpleTokenTTLResolution = simpleTokenTTL
- users := make(map[string]struct{})
- as, tearDown := setupAuthStore(t)
- defer tearDown(t)
- // create lots of users
- for i := 0; i < 50; i++ {
- u := fmt.Sprintf("user-%d", i)
- ua := &pb.AuthUserAddRequest{Name: u, Password: "123", Options: &authpb.UserAddOptions{NoPassword: false}}
- if _, err := as.UserAdd(ua); err != nil {
- t.Fatal(err)
- }
- users[u] = struct{}{}
- }
- // hammer on authenticate with lots of users
- for i := 0; i < 10; i++ {
- var wg sync.WaitGroup
- wg.Add(len(users))
- for u := range users {
- go func(user string) {
- defer wg.Done()
- token := fmt.Sprintf("%s(%d)", user, i)
- ctx := context.WithValue(context.WithValue(context.TODO(), AuthenticateParamIndex{}, uint64(1)), AuthenticateParamSimpleTokenPrefix{}, token)
- if _, err := as.Authenticate(ctx, user, "123"); err != nil {
- t.Error(err)
- }
- if _, err := as.AuthInfoFromCtx(ctx); err != nil {
- t.Error(err)
- }
- }(u)
- }
- time.Sleep(time.Millisecond)
- wg.Wait()
- }
- }
- // TestRolesOrder tests authpb.User.Roles is sorted
- func TestRolesOrder(t *testing.T) {
- b, tPath := backend.NewDefaultTmpBackend()
- defer os.Remove(tPath)
- tp, err := NewTokenProvider(zap.NewExample(), tokenTypeSimple, dummyIndexWaiter)
- if err != nil {
- t.Fatal(err)
- }
- as := NewAuthStore(zap.NewExample(), b, tp, bcrypt.MinCost)
- err = enableAuthAndCreateRoot(as)
- if err != nil {
- t.Fatal(err)
- }
- username := "user"
- _, err = as.UserAdd(&pb.AuthUserAddRequest{Name: username, Password: "pass", Options: &authpb.UserAddOptions{NoPassword: false}})
- if err != nil {
- t.Fatal(err)
- }
- roles := []string{"role1", "role2", "abc", "xyz", "role3"}
- for _, role := range roles {
- _, err = as.RoleAdd(&pb.AuthRoleAddRequest{Name: role})
- if err != nil {
- t.Fatal(err)
- }
- _, err = as.UserGrantRole(&pb.AuthUserGrantRoleRequest{User: username, Role: role})
- if err != nil {
- t.Fatal(err)
- }
- }
- user, err := as.UserGet(&pb.AuthUserGetRequest{Name: username})
- if err != nil {
- t.Fatal(err)
- }
- for i := 1; i < len(user.Roles); i++ {
- if strings.Compare(user.Roles[i-1], user.Roles[i]) != -1 {
- t.Errorf("User.Roles isn't sorted (%s vs %s)", user.Roles[i-1], user.Roles[i])
- }
- }
- }
- func TestAuthInfoFromCtxWithRootSimple(t *testing.T) {
- testAuthInfoFromCtxWithRoot(t, tokenTypeSimple)
- }
- func TestAuthInfoFromCtxWithRootJWT(t *testing.T) {
- opts := testJWTOpts()
- testAuthInfoFromCtxWithRoot(t, opts)
- }
- // testAuthInfoFromCtxWithRoot ensures "WithRoot" properly embeds token in the context.
- func testAuthInfoFromCtxWithRoot(t *testing.T, opts string) {
- b, tPath := backend.NewDefaultTmpBackend()
- defer os.Remove(tPath)
- tp, err := NewTokenProvider(zap.NewExample(), opts, dummyIndexWaiter)
- if err != nil {
- t.Fatal(err)
- }
- as := NewAuthStore(zap.NewExample(), b, tp, bcrypt.MinCost)
- defer as.Close()
- if err = enableAuthAndCreateRoot(as); err != nil {
- t.Fatal(err)
- }
- ctx := context.Background()
- ctx = as.WithRoot(ctx)
- ai, aerr := as.AuthInfoFromCtx(ctx)
- if aerr != nil {
- t.Error(err)
- }
- if ai == nil {
- t.Error("expected non-nil *AuthInfo")
- }
- if ai.Username != "root" {
- t.Errorf("expected user name 'root', got %+v", ai)
- }
- }
- func TestUserNoPasswordAdd(t *testing.T) {
- as, tearDown := setupAuthStore(t)
- defer tearDown(t)
- username := "usernopass"
- ua := &pb.AuthUserAddRequest{Name: username, Options: &authpb.UserAddOptions{NoPassword: true}}
- _, err := as.UserAdd(ua)
- if err != nil {
- t.Fatal(err)
- }
- ctx := context.WithValue(context.WithValue(context.TODO(), AuthenticateParamIndex{}, uint64(1)), AuthenticateParamSimpleTokenPrefix{}, "dummy")
- _, err = as.Authenticate(ctx, username, "")
- if err != ErrAuthFailed {
- t.Fatalf("expected %v, got %v", ErrAuthFailed, err)
- }
- }
|