2016-05-13 06:50:58 +03:00
|
|
|
// Copyright 2016 The etcd Authors
|
2016-03-02 08:57:40 +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 clientv3
|
|
|
|
|
|
|
|
import (
|
2017-09-07 00:57:25 +03:00
|
|
|
"context"
|
2016-04-05 09:42:38 +03:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2019-05-29 00:59:02 +03:00
|
|
|
"go.etcd.io/etcd/auth/authpb"
|
|
|
|
pb "go.etcd.io/etcd/etcdserver/etcdserverpb"
|
2016-03-23 03:10:28 +03:00
|
|
|
"google.golang.org/grpc"
|
2016-03-02 08:57:40 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2016-06-05 10:57:23 +03:00
|
|
|
AuthEnableResponse pb.AuthEnableResponse
|
|
|
|
AuthDisableResponse pb.AuthDisableResponse
|
|
|
|
AuthenticateResponse pb.AuthenticateResponse
|
|
|
|
AuthUserAddResponse pb.AuthUserAddResponse
|
|
|
|
AuthUserDeleteResponse pb.AuthUserDeleteResponse
|
|
|
|
AuthUserChangePasswordResponse pb.AuthUserChangePasswordResponse
|
2016-06-07 06:17:28 +03:00
|
|
|
AuthUserGrantRoleResponse pb.AuthUserGrantRoleResponse
|
2016-06-05 10:57:23 +03:00
|
|
|
AuthUserGetResponse pb.AuthUserGetResponse
|
|
|
|
AuthUserRevokeRoleResponse pb.AuthUserRevokeRoleResponse
|
|
|
|
AuthRoleAddResponse pb.AuthRoleAddResponse
|
2016-06-07 06:17:28 +03:00
|
|
|
AuthRoleGrantPermissionResponse pb.AuthRoleGrantPermissionResponse
|
2016-06-05 10:57:23 +03:00
|
|
|
AuthRoleGetResponse pb.AuthRoleGetResponse
|
|
|
|
AuthRoleRevokePermissionResponse pb.AuthRoleRevokePermissionResponse
|
|
|
|
AuthRoleDeleteResponse pb.AuthRoleDeleteResponse
|
2016-06-15 09:11:27 +03:00
|
|
|
AuthUserListResponse pb.AuthUserListResponse
|
|
|
|
AuthRoleListResponse pb.AuthRoleListResponse
|
2016-04-05 09:42:38 +03:00
|
|
|
|
|
|
|
PermissionType authpb.Permission_Type
|
2016-09-14 07:57:38 +03:00
|
|
|
Permission authpb.Permission
|
2016-04-05 09:42:38 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
PermRead = authpb.READ
|
|
|
|
PermWrite = authpb.WRITE
|
|
|
|
PermReadWrite = authpb.READWRITE
|
2016-03-02 08:57:40 +03:00
|
|
|
)
|
|
|
|
|
2018-06-06 10:18:28 +03:00
|
|
|
type UserAddOptions authpb.UserAddOptions
|
|
|
|
|
2016-03-02 08:57:40 +03:00
|
|
|
type Auth interface {
|
2016-03-23 07:25:48 +03:00
|
|
|
// AuthEnable enables auth of an etcd cluster.
|
2016-03-02 08:57:40 +03:00
|
|
|
AuthEnable(ctx context.Context) (*AuthEnableResponse, error)
|
2016-03-23 07:25:48 +03:00
|
|
|
|
2016-05-07 21:24:43 +03:00
|
|
|
// AuthDisable disables auth of an etcd cluster.
|
|
|
|
AuthDisable(ctx context.Context) (*AuthDisableResponse, error)
|
|
|
|
|
2016-03-23 07:25:48 +03:00
|
|
|
// UserAdd adds a new user to an etcd cluster.
|
2016-03-29 08:32:19 +03:00
|
|
|
UserAdd(ctx context.Context, name string, password string) (*AuthUserAddResponse, error)
|
2016-03-31 05:29:47 +03:00
|
|
|
|
2018-06-06 10:18:28 +03:00
|
|
|
// UserAddWithOptions adds a new user to an etcd cluster with some options.
|
|
|
|
UserAddWithOptions(ctx context.Context, name string, password string, opt *UserAddOptions) (*AuthUserAddResponse, error)
|
|
|
|
|
2016-03-31 05:29:47 +03:00
|
|
|
// UserDelete deletes a user from an etcd cluster.
|
|
|
|
UserDelete(ctx context.Context, name string) (*AuthUserDeleteResponse, error)
|
2016-03-31 08:31:07 +03:00
|
|
|
|
|
|
|
// UserChangePassword changes a password of a user.
|
|
|
|
UserChangePassword(ctx context.Context, name string, password string) (*AuthUserChangePasswordResponse, error)
|
2016-04-01 05:07:43 +03:00
|
|
|
|
2016-06-07 06:17:28 +03:00
|
|
|
// UserGrantRole grants a role to a user.
|
|
|
|
UserGrantRole(ctx context.Context, user string, role string) (*AuthUserGrantRoleResponse, error)
|
2016-04-11 08:34:13 +03:00
|
|
|
|
2016-05-30 11:21:11 +03:00
|
|
|
// UserGet gets a detailed information of a user.
|
|
|
|
UserGet(ctx context.Context, name string) (*AuthUserGetResponse, error)
|
|
|
|
|
2016-06-15 09:11:27 +03:00
|
|
|
// UserList gets a list of all users.
|
|
|
|
UserList(ctx context.Context) (*AuthUserListResponse, error)
|
|
|
|
|
2016-06-05 10:57:23 +03:00
|
|
|
// UserRevokeRole revokes a role of a user.
|
|
|
|
UserRevokeRole(ctx context.Context, name string, role string) (*AuthUserRevokeRoleResponse, error)
|
2016-06-03 10:38:59 +03:00
|
|
|
|
2016-04-05 09:42:38 +03:00
|
|
|
// RoleAdd adds a new role to an etcd cluster.
|
2016-04-01 05:07:43 +03:00
|
|
|
RoleAdd(ctx context.Context, name string) (*AuthRoleAddResponse, error)
|
2016-04-05 09:42:38 +03:00
|
|
|
|
2016-06-07 06:17:28 +03:00
|
|
|
// RoleGrantPermission grants a permission to a role.
|
2016-06-08 21:52:57 +03:00
|
|
|
RoleGrantPermission(ctx context.Context, name string, key, rangeEnd string, permType PermissionType) (*AuthRoleGrantPermissionResponse, error)
|
2016-06-02 17:25:15 +03:00
|
|
|
|
|
|
|
// RoleGet gets a detailed information of a role.
|
|
|
|
RoleGet(ctx context.Context, role string) (*AuthRoleGetResponse, error)
|
2016-06-03 11:10:08 +03:00
|
|
|
|
2016-06-15 09:11:27 +03:00
|
|
|
// RoleList gets a list of all roles.
|
|
|
|
RoleList(ctx context.Context) (*AuthRoleListResponse, error)
|
|
|
|
|
2016-06-08 21:52:57 +03:00
|
|
|
// RoleRevokePermission revokes a permission from a role.
|
|
|
|
RoleRevokePermission(ctx context.Context, role string, key, rangeEnd string) (*AuthRoleRevokePermissionResponse, error)
|
2016-06-03 11:33:05 +03:00
|
|
|
|
|
|
|
// RoleDelete deletes a role.
|
|
|
|
RoleDelete(ctx context.Context, role string) (*AuthRoleDeleteResponse, error)
|
2016-03-02 08:57:40 +03:00
|
|
|
}
|
|
|
|
|
2017-10-11 11:03:24 +03:00
|
|
|
type authClient struct {
|
2017-12-20 02:07:32 +03:00
|
|
|
remote pb.AuthClient
|
|
|
|
callOpts []grpc.CallOption
|
2016-03-02 08:57:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewAuth(c *Client) Auth {
|
2017-10-11 11:03:24 +03:00
|
|
|
api := &authClient{remote: RetryAuthClient(c)}
|
2017-12-20 02:07:32 +03:00
|
|
|
if c != nil {
|
|
|
|
api.callOpts = c.callOpts
|
|
|
|
}
|
|
|
|
return api
|
2016-03-02 08:57:40 +03:00
|
|
|
}
|
|
|
|
|
2017-10-11 11:03:24 +03:00
|
|
|
func (auth *authClient) AuthEnable(ctx context.Context) (*AuthEnableResponse, error) {
|
2017-12-20 02:07:32 +03:00
|
|
|
resp, err := auth.remote.AuthEnable(ctx, &pb.AuthEnableRequest{}, auth.callOpts...)
|
2016-06-08 03:03:48 +03:00
|
|
|
return (*AuthEnableResponse)(resp), toErr(ctx, err)
|
2016-03-02 08:57:40 +03:00
|
|
|
}
|
2016-03-23 07:25:48 +03:00
|
|
|
|
2017-10-11 11:03:24 +03:00
|
|
|
func (auth *authClient) AuthDisable(ctx context.Context) (*AuthDisableResponse, error) {
|
2017-12-20 02:07:32 +03:00
|
|
|
resp, err := auth.remote.AuthDisable(ctx, &pb.AuthDisableRequest{}, auth.callOpts...)
|
2016-06-08 03:03:48 +03:00
|
|
|
return (*AuthDisableResponse)(resp), toErr(ctx, err)
|
2016-05-07 21:24:43 +03:00
|
|
|
}
|
|
|
|
|
2017-10-11 11:03:24 +03:00
|
|
|
func (auth *authClient) UserAdd(ctx context.Context, name string, password string) (*AuthUserAddResponse, error) {
|
2018-06-06 10:18:28 +03:00
|
|
|
resp, err := auth.remote.UserAdd(ctx, &pb.AuthUserAddRequest{Name: name, Password: password, Options: &authpb.UserAddOptions{NoPassword: false}}, auth.callOpts...)
|
|
|
|
return (*AuthUserAddResponse)(resp), toErr(ctx, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (auth *authClient) UserAddWithOptions(ctx context.Context, name string, password string, options *UserAddOptions) (*AuthUserAddResponse, error) {
|
|
|
|
resp, err := auth.remote.UserAdd(ctx, &pb.AuthUserAddRequest{Name: name, Password: password, Options: (*authpb.UserAddOptions)(options)}, auth.callOpts...)
|
2016-06-08 03:03:48 +03:00
|
|
|
return (*AuthUserAddResponse)(resp), toErr(ctx, err)
|
2016-03-23 07:25:48 +03:00
|
|
|
}
|
2016-03-31 05:29:47 +03:00
|
|
|
|
2017-10-11 11:03:24 +03:00
|
|
|
func (auth *authClient) UserDelete(ctx context.Context, name string) (*AuthUserDeleteResponse, error) {
|
2017-12-20 02:07:32 +03:00
|
|
|
resp, err := auth.remote.UserDelete(ctx, &pb.AuthUserDeleteRequest{Name: name}, auth.callOpts...)
|
2016-06-08 03:03:48 +03:00
|
|
|
return (*AuthUserDeleteResponse)(resp), toErr(ctx, err)
|
2016-03-31 05:29:47 +03:00
|
|
|
}
|
2016-03-31 08:31:07 +03:00
|
|
|
|
2017-10-11 11:03:24 +03:00
|
|
|
func (auth *authClient) UserChangePassword(ctx context.Context, name string, password string) (*AuthUserChangePasswordResponse, error) {
|
2017-12-20 02:07:32 +03:00
|
|
|
resp, err := auth.remote.UserChangePassword(ctx, &pb.AuthUserChangePasswordRequest{Name: name, Password: password}, auth.callOpts...)
|
2016-06-08 03:03:48 +03:00
|
|
|
return (*AuthUserChangePasswordResponse)(resp), toErr(ctx, err)
|
2016-03-31 08:31:07 +03:00
|
|
|
}
|
2016-04-01 05:07:43 +03:00
|
|
|
|
2017-10-11 11:03:24 +03:00
|
|
|
func (auth *authClient) UserGrantRole(ctx context.Context, user string, role string) (*AuthUserGrantRoleResponse, error) {
|
2017-12-20 02:07:32 +03:00
|
|
|
resp, err := auth.remote.UserGrantRole(ctx, &pb.AuthUserGrantRoleRequest{User: user, Role: role}, auth.callOpts...)
|
2016-06-08 03:03:48 +03:00
|
|
|
return (*AuthUserGrantRoleResponse)(resp), toErr(ctx, err)
|
2016-04-11 08:34:13 +03:00
|
|
|
}
|
|
|
|
|
2017-10-11 11:03:24 +03:00
|
|
|
func (auth *authClient) UserGet(ctx context.Context, name string) (*AuthUserGetResponse, error) {
|
2017-12-20 02:07:32 +03:00
|
|
|
resp, err := auth.remote.UserGet(ctx, &pb.AuthUserGetRequest{Name: name}, auth.callOpts...)
|
2016-06-08 03:03:48 +03:00
|
|
|
return (*AuthUserGetResponse)(resp), toErr(ctx, err)
|
2016-05-30 11:21:11 +03:00
|
|
|
}
|
|
|
|
|
2017-10-11 11:03:24 +03:00
|
|
|
func (auth *authClient) UserList(ctx context.Context) (*AuthUserListResponse, error) {
|
2017-12-20 02:07:32 +03:00
|
|
|
resp, err := auth.remote.UserList(ctx, &pb.AuthUserListRequest{}, auth.callOpts...)
|
2016-06-15 09:11:27 +03:00
|
|
|
return (*AuthUserListResponse)(resp), toErr(ctx, err)
|
|
|
|
}
|
|
|
|
|
2017-10-11 11:03:24 +03:00
|
|
|
func (auth *authClient) UserRevokeRole(ctx context.Context, name string, role string) (*AuthUserRevokeRoleResponse, error) {
|
2017-12-20 02:07:32 +03:00
|
|
|
resp, err := auth.remote.UserRevokeRole(ctx, &pb.AuthUserRevokeRoleRequest{Name: name, Role: role}, auth.callOpts...)
|
2016-06-08 03:03:48 +03:00
|
|
|
return (*AuthUserRevokeRoleResponse)(resp), toErr(ctx, err)
|
2016-06-03 10:38:59 +03:00
|
|
|
}
|
|
|
|
|
2017-10-11 11:03:24 +03:00
|
|
|
func (auth *authClient) RoleAdd(ctx context.Context, name string) (*AuthRoleAddResponse, error) {
|
2017-12-20 02:07:32 +03:00
|
|
|
resp, err := auth.remote.RoleAdd(ctx, &pb.AuthRoleAddRequest{Name: name}, auth.callOpts...)
|
2016-06-08 03:03:48 +03:00
|
|
|
return (*AuthRoleAddResponse)(resp), toErr(ctx, err)
|
2016-04-01 05:07:43 +03:00
|
|
|
}
|
2016-04-05 09:42:38 +03:00
|
|
|
|
2017-10-11 11:03:24 +03:00
|
|
|
func (auth *authClient) RoleGrantPermission(ctx context.Context, name string, key, rangeEnd string, permType PermissionType) (*AuthRoleGrantPermissionResponse, error) {
|
2016-04-05 09:42:38 +03:00
|
|
|
perm := &authpb.Permission{
|
|
|
|
Key: []byte(key),
|
2016-06-08 21:52:57 +03:00
|
|
|
RangeEnd: []byte(rangeEnd),
|
2016-04-05 09:42:38 +03:00
|
|
|
PermType: authpb.Permission_Type(permType),
|
|
|
|
}
|
2017-12-20 02:07:32 +03:00
|
|
|
resp, err := auth.remote.RoleGrantPermission(ctx, &pb.AuthRoleGrantPermissionRequest{Name: name, Perm: perm}, auth.callOpts...)
|
2016-06-08 03:03:48 +03:00
|
|
|
return (*AuthRoleGrantPermissionResponse)(resp), toErr(ctx, err)
|
2016-04-05 09:42:38 +03:00
|
|
|
}
|
|
|
|
|
2017-10-11 11:03:24 +03:00
|
|
|
func (auth *authClient) RoleGet(ctx context.Context, role string) (*AuthRoleGetResponse, error) {
|
2017-12-20 02:07:32 +03:00
|
|
|
resp, err := auth.remote.RoleGet(ctx, &pb.AuthRoleGetRequest{Role: role}, auth.callOpts...)
|
2016-06-08 03:03:48 +03:00
|
|
|
return (*AuthRoleGetResponse)(resp), toErr(ctx, err)
|
2016-06-02 17:25:15 +03:00
|
|
|
}
|
|
|
|
|
2017-10-11 11:03:24 +03:00
|
|
|
func (auth *authClient) RoleList(ctx context.Context) (*AuthRoleListResponse, error) {
|
2017-12-20 02:07:32 +03:00
|
|
|
resp, err := auth.remote.RoleList(ctx, &pb.AuthRoleListRequest{}, auth.callOpts...)
|
2016-06-15 09:11:27 +03:00
|
|
|
return (*AuthRoleListResponse)(resp), toErr(ctx, err)
|
|
|
|
}
|
|
|
|
|
2017-10-11 11:03:24 +03:00
|
|
|
func (auth *authClient) RoleRevokePermission(ctx context.Context, role string, key, rangeEnd string) (*AuthRoleRevokePermissionResponse, error) {
|
2018-03-14 08:06:28 +03:00
|
|
|
resp, err := auth.remote.RoleRevokePermission(ctx, &pb.AuthRoleRevokePermissionRequest{Role: role, Key: []byte(key), RangeEnd: []byte(rangeEnd)}, auth.callOpts...)
|
2016-06-08 03:03:48 +03:00
|
|
|
return (*AuthRoleRevokePermissionResponse)(resp), toErr(ctx, err)
|
2016-06-03 11:10:08 +03:00
|
|
|
}
|
|
|
|
|
2017-10-11 11:03:24 +03:00
|
|
|
func (auth *authClient) RoleDelete(ctx context.Context, role string) (*AuthRoleDeleteResponse, error) {
|
2017-12-20 02:07:32 +03:00
|
|
|
resp, err := auth.remote.RoleDelete(ctx, &pb.AuthRoleDeleteRequest{Role: role}, auth.callOpts...)
|
2016-06-08 03:03:48 +03:00
|
|
|
return (*AuthRoleDeleteResponse)(resp), toErr(ctx, err)
|
2016-06-03 11:33:05 +03:00
|
|
|
}
|
|
|
|
|
2016-04-05 09:42:38 +03:00
|
|
|
func StrToPermissionType(s string) (PermissionType, error) {
|
|
|
|
val, ok := authpb.Permission_Type_value[strings.ToUpper(s)]
|
|
|
|
if ok {
|
|
|
|
return PermissionType(val), nil
|
|
|
|
}
|
|
|
|
return PermissionType(-1), fmt.Errorf("invalid permission type: %s", s)
|
|
|
|
}
|
2016-05-13 10:43:42 +03:00
|
|
|
|
|
|
|
type authenticator struct {
|
2017-12-20 02:07:32 +03:00
|
|
|
conn *grpc.ClientConn // conn in-use
|
|
|
|
remote pb.AuthClient
|
|
|
|
callOpts []grpc.CallOption
|
2016-05-13 10:43:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (auth *authenticator) authenticate(ctx context.Context, name string, password string) (*AuthenticateResponse, error) {
|
2017-12-20 02:07:32 +03:00
|
|
|
resp, err := auth.remote.Authenticate(ctx, &pb.AuthenticateRequest{Name: name, Password: password}, auth.callOpts...)
|
2016-06-08 03:03:48 +03:00
|
|
|
return (*AuthenticateResponse)(resp), toErr(ctx, err)
|
2016-05-13 10:43:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (auth *authenticator) close() {
|
|
|
|
auth.conn.Close()
|
|
|
|
}
|
|
|
|
|
2018-04-27 00:54:07 +03:00
|
|
|
func newAuthenticator(ctx context.Context, target string, opts []grpc.DialOption, c *Client) (*authenticator, error) {
|
|
|
|
conn, err := grpc.DialContext(ctx, target, opts...)
|
2016-05-13 10:43:42 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-12-20 02:07:32 +03:00
|
|
|
api := &authenticator{
|
2016-05-13 10:43:42 +03:00
|
|
|
conn: conn,
|
|
|
|
remote: pb.NewAuthClient(conn),
|
2017-12-20 02:07:32 +03:00
|
|
|
}
|
|
|
|
if c != nil {
|
|
|
|
api.callOpts = c.callOpts
|
|
|
|
}
|
|
|
|
return api, nil
|
2016-05-13 10:43:42 +03:00
|
|
|
}
|