2016-05-13 06:50:58 +03:00
|
|
|
// Copyright 2016 The etcd Authors
|
2016-04-28 22:18:24 +03:00
|
|
|
//
|
|
|
|
// 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 integration
|
|
|
|
|
|
|
|
import (
|
2017-09-07 00:57:25 +03:00
|
|
|
"context"
|
2016-04-28 22:18:24 +03:00
|
|
|
"testing"
|
2018-04-27 00:54:07 +03:00
|
|
|
"time"
|
2016-04-28 22:18:24 +03:00
|
|
|
|
2019-05-29 00:59:02 +03:00
|
|
|
"go.etcd.io/etcd/clientv3"
|
|
|
|
"go.etcd.io/etcd/etcdserver/api/v3rpc/rpctypes"
|
|
|
|
"go.etcd.io/etcd/integration"
|
|
|
|
"go.etcd.io/etcd/pkg/testutil"
|
2018-04-27 00:54:07 +03:00
|
|
|
"google.golang.org/grpc"
|
2016-04-28 22:18:24 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestUserError(t *testing.T) {
|
|
|
|
defer testutil.AfterTest(t)
|
|
|
|
|
|
|
|
clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
|
|
|
|
defer clus.Terminate(t)
|
|
|
|
|
2017-02-07 02:22:48 +03:00
|
|
|
authapi := clus.RandClient()
|
2016-04-28 22:18:24 +03:00
|
|
|
|
|
|
|
_, err := authapi.UserAdd(context.TODO(), "foo", "bar")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = authapi.UserAdd(context.TODO(), "foo", "bar")
|
|
|
|
if err != rpctypes.ErrUserAlreadyExist {
|
|
|
|
t.Fatalf("expected %v, got %v", rpctypes.ErrUserAlreadyExist, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = authapi.UserDelete(context.TODO(), "not-exist-user")
|
|
|
|
if err != rpctypes.ErrUserNotFound {
|
|
|
|
t.Fatalf("expected %v, got %v", rpctypes.ErrUserNotFound, err)
|
|
|
|
}
|
|
|
|
|
2016-06-07 06:17:28 +03:00
|
|
|
_, err = authapi.UserGrantRole(context.TODO(), "foo", "test-role-does-not-exist")
|
2016-04-28 22:18:24 +03:00
|
|
|
if err != rpctypes.ErrRoleNotFound {
|
|
|
|
t.Fatalf("expected %v, got %v", rpctypes.ErrRoleNotFound, err)
|
|
|
|
}
|
|
|
|
}
|
2017-04-17 22:46:03 +03:00
|
|
|
|
|
|
|
func TestUserErrorAuth(t *testing.T) {
|
|
|
|
defer testutil.AfterTest(t)
|
|
|
|
|
|
|
|
clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
|
|
|
|
defer clus.Terminate(t)
|
|
|
|
|
|
|
|
authapi := clus.RandClient()
|
|
|
|
authSetupRoot(t, authapi.Auth)
|
|
|
|
|
2017-09-04 13:09:32 +03:00
|
|
|
// unauthenticated client
|
2017-04-17 22:46:03 +03:00
|
|
|
if _, err := authapi.UserAdd(context.TODO(), "foo", "bar"); err != rpctypes.ErrUserNotFound {
|
|
|
|
t.Fatalf("expected %v, got %v", rpctypes.ErrUserNotFound, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// wrong id or password
|
2018-04-27 00:54:07 +03:00
|
|
|
cfg := clientv3.Config{
|
|
|
|
Endpoints: authapi.Endpoints(),
|
|
|
|
DialTimeout: 5 * time.Second,
|
|
|
|
DialOptions: []grpc.DialOption{grpc.WithBlock()},
|
|
|
|
}
|
2017-04-17 22:46:03 +03:00
|
|
|
cfg.Username, cfg.Password = "wrong-id", "123"
|
|
|
|
if _, err := clientv3.New(cfg); err != rpctypes.ErrAuthFailed {
|
|
|
|
t.Fatalf("expected %v, got %v", rpctypes.ErrAuthFailed, err)
|
|
|
|
}
|
|
|
|
cfg.Username, cfg.Password = "root", "wrong-pass"
|
|
|
|
if _, err := clientv3.New(cfg); err != rpctypes.ErrAuthFailed {
|
|
|
|
t.Fatalf("expected %v, got %v", rpctypes.ErrAuthFailed, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg.Username, cfg.Password = "root", "123"
|
|
|
|
authed, err := clientv3.New(cfg)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer authed.Close()
|
|
|
|
|
|
|
|
if _, err := authed.UserList(context.TODO()); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func authSetupRoot(t *testing.T, auth clientv3.Auth) {
|
|
|
|
if _, err := auth.UserAdd(context.TODO(), "root", "123"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := auth.RoleAdd(context.TODO(), "root"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := auth.UserGrantRole(context.TODO(), "root", "root"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := auth.AuthEnable(context.TODO()); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2019-02-02 09:06:54 +03:00
|
|
|
|
|
|
|
func TestGetTokenWithoutAuth(t *testing.T) {
|
|
|
|
defer testutil.AfterTest(t)
|
|
|
|
|
|
|
|
clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 10})
|
|
|
|
defer clus.Terminate(t)
|
|
|
|
|
|
|
|
authapi := clus.RandClient()
|
|
|
|
|
|
|
|
var err error
|
|
|
|
var client *clientv3.Client
|
|
|
|
|
|
|
|
// make sure "auth" was disabled
|
|
|
|
if _, err = authapi.AuthDisable(context.TODO()); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// "Username" and "Password" must be used
|
|
|
|
cfg := clientv3.Config{
|
|
|
|
Endpoints: authapi.Endpoints(),
|
|
|
|
DialTimeout: 1 * time.Second, // make sure all connection time of connect all endpoint must be more DialTimeout
|
|
|
|
Username: "root",
|
|
|
|
Password: "123",
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err = clientv3.New(cfg)
|
|
|
|
if err == nil {
|
|
|
|
defer client.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
t.Log("passes as expected, but may be connection time less than DialTimeout")
|
|
|
|
case context.DeadlineExceeded:
|
|
|
|
t.Errorf("not expected result:%v with endpoint:%s", err, authapi.Endpoints())
|
|
|
|
case rpctypes.ErrAuthNotEnabled:
|
|
|
|
t.Logf("passes as expected:%v", err)
|
|
|
|
default:
|
|
|
|
t.Errorf("other errors:%v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|