Skip to content

Commit

Permalink
feature: add master api 'getAllUsers'
Browse files Browse the repository at this point in the history
Signed-off-by: wenjia322 <[email protected]>
  • Loading branch information
wenjia322 committed Mar 16, 2020
1 parent c612cd5 commit 09da2ff
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 44 deletions.
33 changes: 29 additions & 4 deletions master/api_service_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (m *Server) addUserPolicy(w http.ResponseWriter, r *http.Request) {
sendErrReply(w, r, &proto.HTTPReply{Code: proto.ErrCodeReadBodyError, Msg: err.Error()})
return
}
userPolicy = &proto.UserPolicy{}
userPolicy = proto.NewUserPolicy()
if err = json.Unmarshal(body, userPolicy); err != nil {
sendErrReply(w, r, &proto.HTTPReply{Code: proto.ErrCodeUnmarshalData, Msg: err.Error()})
return
Expand Down Expand Up @@ -130,7 +130,7 @@ func (m *Server) deleteUserPolicy(w http.ResponseWriter, r *http.Request) {
sendErrReply(w, r, &proto.HTTPReply{Code: proto.ErrCodeReadBodyError, Msg: err.Error()})
return
}
userPolicy = &proto.UserPolicy{}
userPolicy = proto.NewUserPolicy()
if err = json.Unmarshal(body, userPolicy); err != nil {
sendErrReply(w, r, &proto.HTTPReply{Code: proto.ErrCodeUnmarshalData, Msg: err.Error()})
return
Expand Down Expand Up @@ -196,6 +196,20 @@ func (m *Server) transferUserVol(w http.ResponseWriter, r *http.Request) {
sendOkReply(w, r, newSuccessHTTPReply(akPolicy))
}

func (m *Server) getAllUsers(w http.ResponseWriter, r *http.Request) {
var (
keywords string
akPolicies []*proto.AKPolicy
err error
)
if keywords, err = parseKeywords(r); err != nil {
sendErrReply(w, r, &proto.HTTPReply{Code: proto.ErrCodeParamError, Msg: err.Error()})
return
}
akPolicies = m.user.getAllUserInfo(keywords)
sendOkReply(w, r, newSuccessHTTPReply(akPolicies))
}

func parseOwner(r *http.Request) (owner string, err error) {
if err = r.ParseForm(); err != nil {
return
Expand Down Expand Up @@ -262,7 +276,14 @@ func parseVolAndKey(r *http.Request) (name, accessKey, targetKey string, err err
return
}
return
}

func parseKeywords(r *http.Request) (keywords string, err error) {
if err = r.ParseForm(); err != nil {
return
}
keywords = extractKeywords(r)
return
}

func extractAccessKey(r *http.Request) (ak string, err error) {
Expand Down Expand Up @@ -300,9 +321,13 @@ func extractSecretKey(r *http.Request) (sk string, err error) {

func extractPassword(r *http.Request) (password string, err error) {
if password = r.FormValue(passwordKey); password == "" {
err = keyNotFound(passwordKey)
return
password = DefaultUserPassword
}
// todo password regex
return
}

func extractKeywords(r *http.Request) (keywords string) {
keywords = r.FormValue(keywordsKey)
return
}
1 change: 1 addition & 0 deletions master/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const (
akKey = "ak"
skKey = "sk"
targetKey = "targetak"
keywordsKey = "keywords"
passwordKey = "password"
zoneNameKey = "zoneName"
crossZoneKey = "crossZone"
Expand Down
3 changes: 3 additions & 0 deletions master/http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,9 @@ func (m *Server) registerAPIRoutes(router *mux.Router) {
router.NewRoute().Methods(http.MethodGet).
Path(proto.UserGetInfo).
HandlerFunc(m.getUserInfo)
router.NewRoute().Methods(http.MethodGet).
Path(proto.UserList).
HandlerFunc(m.getAllUsers)
router.NewRoute().Methods(http.MethodGet, http.MethodPost).
Path(proto.UserTransferVol).
HandlerFunc(m.transferUserVol)
Expand Down
70 changes: 35 additions & 35 deletions master/master_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,43 +152,43 @@ func (m *Server) clearMetadata() {
}

func (m *Server) refreshUser() (err error) {
var akPolicy *cfsProto.AKPolicy
for volName, vol := range m.cluster.allVols() {
if _, err = m.user.getUserInfo(vol.Owner); err == cfsProto.ErrOSSUserNotExists {
if len(vol.OSSAccessKey) > 0 && len(vol.OSSSecretKey) > 0 {
var param = cfsProto.UserCreateParam{
ID: vol.Owner,
Password: DefaultUserPassword,
AccessKey: vol.OSSAccessKey,
SecretKey: vol.OSSSecretKey,
Type: cfsProto.UserTypeNormal,
}
akPolicy, err = m.user.createKey(&param)
if err != nil && err != cfsProto.ErrDuplicateUserID && err != cfsProto.ErrDuplicateAccessKey {
return err
}
} else {
var param = cfsProto.UserCreateParam{
ID: vol.Owner,
Password: DefaultUserPassword,
Type: cfsProto.UserTypeNormal,
}
akPolicy, err = m.user.createKey(&param)
if err != nil && err != cfsProto.ErrDuplicateUserID {
return err
}
}
if err == nil && akPolicy != nil {
userPolicy := &cfsProto.UserPolicy{OwnVols: []string{volName}}
if _, err = m.user.addPolicy(akPolicy.AccessKey, userPolicy); err != nil {
return err
}
}
}
}
//var akPolicy *cfsProto.AKPolicy
//for volName, vol := range m.cluster.allVols() {
// if _, err = m.user.getUserInfo(vol.Owner); err == cfsProto.ErrOSSUserNotExists {
// if len(vol.OSSAccessKey) > 0 && len(vol.OSSSecretKey) > 0 {
// var param = cfsProto.UserCreateParam{
// ID: vol.Owner,
// Password: DefaultUserPassword,
// AccessKey: vol.OSSAccessKey,
// SecretKey: vol.OSSSecretKey,
// Type: cfsProto.UserTypeNormal,
// }
// akPolicy, err = m.user.createKey(&param)
// if err != nil && err != cfsProto.ErrDuplicateUserID && err != cfsProto.ErrDuplicateAccessKey {
// return err
// }
// } else {
// var param = cfsProto.UserCreateParam{
// ID: vol.Owner,
// Password: DefaultUserPassword,
// Type: cfsProto.UserTypeNormal,
// }
// akPolicy, err = m.user.createKey(&param)
// if err != nil && err != cfsProto.ErrDuplicateUserID {
// return err
// }
// }
// if err == nil && akPolicy != nil {
// userPolicy := &cfsProto.UserPolicy{OwnVols: []string{volName}}
// if _, err = m.user.addPolicy(akPolicy.AccessKey, userPolicy); err != nil {
// return err
// }
// }
// }
//}
if !m.user.rootExist {
var param = cfsProto.UserCreateParam{
ID: cfsProto.RootUserID,
ID: RootUserID,
Password: DefaultRootPasswd,
Type: cfsProto.UserTypeRoot,
}
Expand Down
15 changes: 15 additions & 0 deletions master/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package master
import (
"crypto/sha1"
"io"
"strings"
"sync"
"time"

Expand All @@ -16,6 +17,7 @@ const (
accessKeyLength = 16
secretKeyLength = 32
ALL = "all"
RootUserID = "root"
DefaultRootPasswd = "ChubaoFSRoot"
DefaultUserPassword = "ChubaoFSUser"
)
Expand Down Expand Up @@ -252,6 +254,19 @@ func (u *User) transferVol(volName, ak, targetKey string) (targetAKPolicy *proto
return
}

func (u *User) getAllUserInfo(keywords string) (akPolicies []*proto.AKPolicy) {
akPolicies = make([]*proto.AKPolicy, 0)
u.akStore.Range(func(key, value interface{}) bool { //todo mutex
akPolicy := value.(*proto.AKPolicy)
if strings.Contains(akPolicy.UserID, keywords) {
akPolicies = append(akPolicies, akPolicy)
}
return true
})
log.LogInfof("action[getAllUserInfo], keywords: %v, total numbers: %v", keywords, len(akPolicies))
return
}

func (u *User) loadAKInfo(ak string) (akPolicy *proto.AKPolicy, err error) {
if value, exist := u.akStore.Load(ak); exist {
akPolicy = value.(*proto.AKPolicy)
Expand Down
1 change: 1 addition & 0 deletions proto/admin_proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ const (
UserGetInfo = "/user/info"
UserGetAKInfo = "/user/akInfo"
UserTransferVol = "/user/transferVol"
UserList = "/user/list"
)

const TimeFormat = "2006-01-02 15:04:05"
Expand Down
5 changes: 0 additions & 5 deletions proto/user_proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,6 @@ func UserTypeFromString(name string) UserType {
return UserTypeInvalid
}

const (
RootUserID = "root"
RootPassword = "SuperAdminOfChubaoFS"
)

type UserAK struct {
UserID string `json:"user_id"`
AccessKey string `json:"access_key"`
Expand Down
14 changes: 14 additions & 0 deletions sdk/master/api_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,17 @@ func (api *UserAPI) TransferVol(vol, ak, targetAK string) (akPolicy *proto.AKPol
}
return
}

func (api *UserAPI) GetAllUsers(keywords string) (akPolicies []*proto.AKPolicy, err error) {
var request = newAPIRequest(http.MethodGet, proto.UserList)
request.addParam("keywords", keywords)
var data []byte
if data, err = api.mc.serveRequest(request); err != nil {
return
}
akPolicies = make([]*proto.AKPolicy, 0)
if err = json.Unmarshal(data, akPolicies); err != nil {
return
}
return
}

0 comments on commit 09da2ff

Please sign in to comment.