update auth test interface

Signed-off-by: Chao Chen <chaochn@amazon.com>
This commit is contained in:
Chao Chen
2022-10-25 18:18:54 -04:00
parent 5550041633
commit f3c47d760c
11 changed files with 76 additions and 44 deletions

View File

@@ -88,8 +88,8 @@ type e2eCluster struct {
}
func (c *e2eCluster) Client(opts ...config.ClientOption) (Client, error) {
etcdctl := e2e.NewEtcdctl(c.Cfg, c.EndpointsV3(), opts...)
return e2eClient{etcdctl}, nil
etcdctl, err := e2e.NewEtcdctl(c.Cfg, c.EndpointsV3(), opts...)
return e2eClient{etcdctl}, err
}
func (c *e2eCluster) Endpoints() []string {
@@ -179,7 +179,11 @@ type e2eMember struct {
}
func (m e2eMember) Client() Client {
return e2eClient{e2e.NewEtcdctl(m.Cfg, m.EndpointsV3())}
etcdctl, err := e2e.NewEtcdctl(m.Cfg, m.EndpointsV3())
if err != nil {
panic(err)
}
return e2eClient{etcdctl}
}
func (m e2eMember) Start(ctx context.Context) error {

View File

@@ -613,7 +613,11 @@ func (epc *EtcdProcessCluster) Stop() (err error) {
}
func (epc *EtcdProcessCluster) Client() *EtcdctlV3 {
return NewEtcdctl(epc.Cfg, epc.EndpointsV3())
etcdctl, err := NewEtcdctl(epc.Cfg, epc.EndpointsV3())
if err != nil {
panic(err)
}
return etcdctl
}
func (epc *EtcdProcessCluster) Close() error {

View File

@@ -21,21 +21,22 @@ import (
"io"
"strconv"
"strings"
"time"
"go.etcd.io/etcd/api/v3/authpb"
"go.etcd.io/etcd/api/v3/etcdserverpb"
clientv3 "go.etcd.io/etcd/client/v3"
"go.etcd.io/etcd/tests/v3/framework/config"
"google.golang.org/grpc"
)
type EtcdctlV3 struct {
cfg *EtcdProcessClusterConfig
endpoints []string
userName string
password string
cfg *EtcdProcessClusterConfig
endpoints []string
authConfig clientv3.AuthConfig
}
func NewEtcdctl(cfg *EtcdProcessClusterConfig, endpoints []string, opts ...config.ClientOption) *EtcdctlV3 {
func NewEtcdctl(cfg *EtcdProcessClusterConfig, endpoints []string, opts ...config.ClientOption) (*EtcdctlV3, error) {
ctl := &EtcdctlV3{
cfg: cfg,
endpoints: endpoints,
@@ -45,14 +46,28 @@ func NewEtcdctl(cfg *EtcdProcessClusterConfig, endpoints []string, opts ...confi
opt(ctl)
}
return ctl
if !ctl.authConfig.Empty() {
client, err := clientv3.New(clientv3.Config{
Endpoints: ctl.endpoints,
DialTimeout: 5 * time.Second,
DialOptions: []grpc.DialOption{grpc.WithBlock()},
Username: ctl.authConfig.Username,
Password: ctl.authConfig.Password,
})
if err != nil {
return nil, err
}
client.Close()
}
return ctl, nil
}
func WithAuth(userName, password string) config.ClientOption {
return func(c any) {
ctl := c.(*EtcdctlV3)
ctl.userName = userName
ctl.password = password
ctl.authConfig.Username = userName
ctl.authConfig.Password = password
}
}
@@ -300,8 +315,8 @@ func (ctl *EtcdctlV3) flags() map[string]string {
}
}
fmap["endpoints"] = strings.Join(ctl.endpoints, ",")
if ctl.userName != "" && ctl.password != "" {
fmap["user"] = ctl.userName + ":" + ctl.password
if !ctl.authConfig.Empty() {
fmap["user"] = ctl.authConfig.Username + ":" + ctl.authConfig.Password
}
return fmap
}
@@ -473,34 +488,28 @@ func (ctl *EtcdctlV3) AlarmDisarm(ctx context.Context, _ *clientv3.AlarmMember)
return &resp, err
}
func (ctl *EtcdctlV3) AuthEnable(ctx context.Context) (*clientv3.AuthEnableResponse, error) {
func (ctl *EtcdctlV3) AuthEnable(ctx context.Context) error {
args := []string{"auth", "enable"}
cmd, err := SpawnCmd(ctl.cmdArgs(args...), nil)
if err != nil {
return nil, err
return err
}
defer cmd.Close()
_, err = cmd.ExpectWithContext(ctx, "Authentication Enabled")
if err != nil {
return nil, err
}
return &clientv3.AuthEnableResponse{}, nil
return err
}
func (ctl *EtcdctlV3) AuthDisable(ctx context.Context) (*clientv3.AuthDisableResponse, error) {
func (ctl *EtcdctlV3) AuthDisable(ctx context.Context) error {
args := []string{"auth", "disable"}
cmd, err := SpawnCmd(ctl.cmdArgs(args...), nil)
if err != nil {
return nil, err
return err
}
defer cmd.Close()
_, err = cmd.ExpectWithContext(ctx, "Authentication Disabled")
if err != nil {
return nil, err
}
return &clientv3.AuthDisableResponse{}, nil
return err
}
func (ctl *EtcdctlV3) AuthStatus(ctx context.Context) (*clientv3.AuthStatusResponse, error) {

View File

@@ -274,12 +274,14 @@ func (c integrationClient) Revoke(ctx context.Context, id clientv3.LeaseID) (*cl
return c.Client.Revoke(ctx, id)
}
func (c integrationClient) AuthEnable(ctx context.Context) (*clientv3.AuthEnableResponse, error) {
return c.Client.AuthEnable(ctx)
func (c integrationClient) AuthEnable(ctx context.Context) error {
_, err := c.Client.AuthEnable(ctx)
return err
}
func (c integrationClient) AuthDisable(ctx context.Context) (*clientv3.AuthDisableResponse, error) {
return c.Client.AuthDisable(ctx)
func (c integrationClient) AuthDisable(ctx context.Context) error {
_, err := c.Client.AuthDisable(ctx)
return err
}
func (c integrationClient) AuthStatus(ctx context.Context) (*clientv3.AuthStatusResponse, error) {

View File

@@ -1457,9 +1457,9 @@ func (c *Cluster) ClusterClient(t testing.TB, opts ...framecfg.ClientOption) (cl
func WithAuth(userName, password string) framecfg.ClientOption {
return func(c any) {
client := c.(*clientv3.Client)
client.Username = userName
client.Password = password
cfg := c.(*clientv3.Config)
cfg.Username = userName
cfg.Password = password
}
}

View File

@@ -59,8 +59,8 @@ type Client interface {
KeepAliveOnce(context context.Context, id clientv3.LeaseID) (*clientv3.LeaseKeepAliveResponse, error)
Revoke(context context.Context, id clientv3.LeaseID) (*clientv3.LeaseRevokeResponse, error)
AuthEnable(context context.Context) (*clientv3.AuthEnableResponse, error)
AuthDisable(context context.Context) (*clientv3.AuthDisableResponse, error)
AuthEnable(context context.Context) error
AuthDisable(context context.Context) error
AuthStatus(context context.Context) (*clientv3.AuthStatusResponse, error)
UserAdd(context context.Context, name, password string, opts config.UserAddOptions) (*clientv3.AuthUserAddResponse, error)
UserGet(context context.Context, name string) (*clientv3.AuthUserGetResponse, error)
@@ -75,6 +75,7 @@ type Client interface {
RoleList(context context.Context) (*clientv3.AuthRoleListResponse, error)
RoleRevokePermission(context context.Context, role string, key, rangeEnd string) (*clientv3.AuthRoleRevokePermissionResponse, error)
RoleDelete(context context.Context, role string) (*clientv3.AuthRoleDeleteResponse, error)
Txn(context context.Context, compares, ifSucess, ifFail []string, o config.TxnOptions) (*clientv3.TxnResponse, error)
MemberList(context context.Context) (*clientv3.MemberListResponse, error)