Skip to content

Commit

Permalink
添加账户用户时增加校验规则
Browse files Browse the repository at this point in the history
  • Loading branch information
yangjie727 committed Dec 14, 2023
1 parent c671cb2 commit 10eac10
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 73 deletions.
126 changes: 63 additions & 63 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,13 @@ func (s *serverUser) AddUserToAccount(ctx context.Context, in *pb.AddUserToAccou
defaultQos := configValue.Slurm.DefaultQOS

// 检查用户名、账户名是否包含大写字母
resultAcct := utils.ContainsUppercase(in.AccountName)
resultUser := utils.ContainsUppercase(in.UserId)
if resultAcct || resultUser {
resultAcct := utils.CheckAccountOrUserStrings(in.AccountName) // 重新判断一下
resultUser := utils.CheckAccountOrUserStrings(in.UserId) // 重新判断一下
if !resultAcct || !resultUser {
errInfo := &errdetails.ErrorInfo{
Reason: "ACCOUNT_USER_CONTAIN_UPPER_LETTER",
Reason: "ACCOUNT_USER_CONTAIN_ILLEGAL_CHARACTERS",
}
st := status.New(codes.Internal, "The account or username contains uppercase letters.")
st := status.New(codes.Internal, "The account or username contains illegal characters.")
st, _ = st.WithDetails(errInfo)
return nil, st.Err()
}
Expand Down Expand Up @@ -234,13 +234,13 @@ func (s *serverUser) RemoveUserFromAccount(ctx context.Context, in *pb.RemoveUse
clusterName := configValue.MySQLConfig.ClusterName

// 检查账户名、用户名是否包含大写字母
resultAcct := utils.ContainsUppercase(in.AccountName)
resultUser := utils.ContainsUppercase(in.UserId)
if resultAcct || resultUser {
resultAcct := utils.CheckAccountOrUserStrings(in.AccountName)
resultUser := utils.CheckAccountOrUserStrings(in.UserId)
if !resultAcct || !resultUser {
errInfo := &errdetails.ErrorInfo{
Reason: "ACCOUNT_USER_CONTAIN_UPPER_LETTER",
Reason: "ACCOUNT_USER_CONTAIN_ILLEGAL_CHARACTERS",
}
st := status.New(codes.Internal, "The account or username contains uppercase letters.")
st := status.New(codes.Internal, "The account or username contains illegal characters.")
st, _ = st.WithDetails(errInfo)
return nil, st.Err()
}
Expand Down Expand Up @@ -415,13 +415,13 @@ func (s *serverUser) BlockUserInAccount(ctx context.Context, in *pb.BlockUserInA
logger.Infof("Received request BlockUserInAccount: %v", in)

// 检查账户名、用户名是否包含大写字母
resultAcct := utils.ContainsUppercase(in.AccountName)
resultUser := utils.ContainsUppercase(in.UserId)
if resultAcct || resultUser {
resultAcct := utils.CheckAccountOrUserStrings(in.AccountName)
resultUser := utils.CheckAccountOrUserStrings(in.UserId)
if !resultAcct || !resultUser {
errInfo := &errdetails.ErrorInfo{
Reason: "ACCOUNT_USER_CONTAIN_UPPER_LETTER",
Reason: "ACCOUNT_USER_CONTAIN_ILLEGAL_CHARACTERS",
}
st := status.New(codes.Internal, "The account or username contains uppercase letters.")
st := status.New(codes.Internal, "The account or username contains illegal characters.")
st, _ = st.WithDetails(errInfo)
return nil, st.Err()
}
Expand Down Expand Up @@ -488,13 +488,13 @@ func (s *serverUser) UnblockUserInAccount(ctx context.Context, in *pb.UnblockUse
// 记录日志
logger.Infof("Received request UnblockUserInAccount: %v", in)
// 检查账户名、用户名是否包含大写字母
resultAcct := utils.ContainsUppercase(in.AccountName)
resultUser := utils.ContainsUppercase(in.UserId)
if resultAcct || resultUser {
resultAcct := utils.CheckAccountOrUserStrings(in.AccountName)
resultUser := utils.CheckAccountOrUserStrings(in.UserId)
if !resultAcct || !resultUser {
errInfo := &errdetails.ErrorInfo{
Reason: "ACCOUNT_USER_CONTAIN_UPPER_LETTER",
Reason: "ACCOUNT_USER_CONTAIN_ILLEGAL_CHARACTERS",
}
st := status.New(codes.Internal, "The account or username contains uppercase letters.")
st := status.New(codes.Internal, "The account or username contains illegal characters.")
st, _ = st.WithDetails(errInfo)
return nil, st.Err()
}
Expand Down Expand Up @@ -567,13 +567,13 @@ func (s *serverUser) QueryUserInAccountBlockStatus(ctx context.Context, in *pb.Q
// 记录日志
logger.Infof("Received request QueryUserInAccountBlockStatus: %v", in)
// 检查账户名、用户名是否包含大写字母
resultAcct := utils.ContainsUppercase(in.AccountName)
resultUser := utils.ContainsUppercase(in.UserId)
if resultAcct || resultUser {
resultAcct := utils.CheckAccountOrUserStrings(in.AccountName)
resultUser := utils.CheckAccountOrUserStrings(in.UserId)
if !resultAcct || !resultUser {
errInfo := &errdetails.ErrorInfo{
Reason: "ACCOUNT_USER_CONTAIN_UPPER_LETTER",
Reason: "ACCOUNT_USER_CONTAIN_ILLEGAL_CHARACTERS",
}
st := status.New(codes.Internal, "The account or username contains uppercase letters.")
st := status.New(codes.Internal, "The account or username contains illegal characters.")
st, _ = st.WithDetails(errInfo)
return nil, st.Err()
}
Expand Down Expand Up @@ -635,12 +635,12 @@ func (s *serverAccount) ListAccounts(ctx context.Context, in *pb.ListAccountsReq
// 记录日志
logger.Infof("Received request ListAccounts: %v", in)
// 检查用户名中是否包含大写字母
resultUser := utils.ContainsUppercase(in.UserId)
if resultUser {
resultUser := utils.CheckAccountOrUserStrings(in.UserId)
if !resultUser {
errInfo := &errdetails.ErrorInfo{
Reason: "USER_CONTAIN_UPPER_LETTER",
Reason: "USER_CONTAIN_ILLEGAL_CHARACTERS",
}
st := status.New(codes.Internal, "The username contains uppercase letters.")
st := status.New(codes.Internal, "The username contains illegal characters.")
st, _ = st.WithDetails(errInfo)
return nil, st.Err()
}
Expand Down Expand Up @@ -704,13 +704,13 @@ func (s *serverAccount) CreateAccount(ctx context.Context, in *pb.CreateAccountR
// 记录日志
logger.Infof("Received request CreateAccount: %v", in)
// 检查账户名、用户名是否包含大写字母
resultAcct := utils.ContainsUppercase(in.AccountName)
resultUser := utils.ContainsUppercase(in.OwnerUserId)
if resultAcct || resultUser {
resultAcct := utils.CheckAccountOrUserStrings(in.AccountName)
resultUser := utils.CheckAccountOrUserStrings(in.OwnerUserId)
if !resultAcct || !resultUser {
errInfo := &errdetails.ErrorInfo{
Reason: "ACCOUNT_USER_CONTAIN_UPPER_LETTER",
Reason: "ACCOUNT_USER_CONTAIN_ILLEGAL_CHARACTERS",
}
st := status.New(codes.Internal, "The account or username contains uppercase letters.")
st := status.New(codes.Internal, "The account or username contains illegal characters.")
st, _ = st.WithDetails(errInfo)
return nil, st.Err()
}
Expand Down Expand Up @@ -785,12 +785,12 @@ func (s *serverAccount) BlockAccount(ctx context.Context, in *pb.BlockAccountReq
// 记录日志
logger.Infof("Received request BlockAccount: %v", in)
// 检查账户名中是否包含大写字母
resultAcct := utils.ContainsUppercase(in.AccountName)
if resultAcct {
resultAcct := utils.CheckAccountOrUserStrings(in.AccountName)
if !resultAcct {
errInfo := &errdetails.ErrorInfo{
Reason: "ACCOUNT_CONTAIN_UPPER_LETTER",
Reason: "ACCOUNT_CONTAIN_ILLEGAL_CHARACTERS",
}
st := status.New(codes.Internal, "The account contains uppercase letters.")
st := status.New(codes.Internal, "The account contains illegal characters.")
st, _ = st.WithDetails(errInfo)
return nil, st.Err()
}
Expand Down Expand Up @@ -876,12 +876,12 @@ func (s *serverAccount) UnblockAccount(ctx context.Context, in *pb.UnblockAccoun
// 记录日志
logger.Infof("Received request UnblockAccount: %v", in)
// 检查用户名中是否包含大写字母
resultAcct := utils.ContainsUppercase(in.AccountName)
if resultAcct {
resultAcct := utils.CheckAccountOrUserStrings(in.AccountName)
if !resultAcct {
errInfo := &errdetails.ErrorInfo{
Reason: "ACCOUNT_CONTAIN_UPPER_LETTER",
Reason: "ACCOUNT_CONTAIN_ILLEGAL_CHARACTERS",
}
st := status.New(codes.Internal, "The account contains uppercase letters.")
st := status.New(codes.Internal, "The account contains illegal characters.")
st, _ = st.WithDetails(errInfo)
return nil, st.Err()
}
Expand Down Expand Up @@ -1045,12 +1045,12 @@ func (s *serverAccount) QueryAccountBlockStatus(ctx context.Context, in *pb.Quer
// 记录日志
logger.Infof("Received request QueryAccountBlockStatus: %v", in)
// 检查用户名中是否包含大写字母
resultAcct := utils.ContainsUppercase(in.AccountName)
if resultAcct {
resultAcct := utils.CheckAccountOrUserStrings(in.AccountName)
if !resultAcct {
errInfo := &errdetails.ErrorInfo{
Reason: "ACCOUNT_CONTAIN_UPPER_LETTER",
Reason: "ACCOUNT_CONTAIN_ILLEGAL_CHARACTERS",
}
st := status.New(codes.Internal, "The account contains uppercase letters.")
st := status.New(codes.Internal, "The account contains illegal characters.")
st, _ = st.WithDetails(errInfo)
return nil, st.Err()
}
Expand Down Expand Up @@ -1299,12 +1299,12 @@ func (s *serverConfig) GetAvailablePartitions(ctx context.Context, in *pb.GetAva
// 记录日志
logger.Infof("Received request GetAvailablePartitions: %v", in)
// 检查用户名中是否包含大写字母
resultUser := utils.ContainsUppercase(in.UserId)
if resultUser {
resultUser := utils.CheckAccountOrUserStrings(in.UserId)
if !resultUser {
errInfo := &errdetails.ErrorInfo{
Reason: "USER_CONTAIN_UPPER_LETTER",
Reason: "USER_CONTAIN_ILLEGAL_CHARACTERS",
}
st := status.New(codes.Internal, "The username contains uppercase letters.")
st := status.New(codes.Internal, "The username contains illegal characters.")
st, _ = st.WithDetails(errInfo)
return nil, st.Err()
}
Expand Down Expand Up @@ -1836,12 +1836,12 @@ func (s *serverJob) CancelJob(ctx context.Context, in *pb.CancelJobRequest) (*pb
// 记录日志
logger.Infof("Received request CancelJob: %v", in)
// 检查用户名中是否包含大写字母
resultUser := utils.ContainsUppercase(in.UserId)
if resultUser {
resultUser := utils.CheckAccountOrUserStrings(in.UserId)
if !resultUser {
errInfo := &errdetails.ErrorInfo{
Reason: "USER_CONTAIN_UPPER_LETTER",
Reason: "USER_CONTAIN_ILLEGAL_CHARACTERS",
}
st := status.New(codes.Internal, "The username contains uppercase letters.")
st := status.New(codes.Internal, "The username contains illegal characters.")
st, _ = st.WithDetails(errInfo)
return nil, st.Err()
}
Expand Down Expand Up @@ -2913,13 +2913,13 @@ func (s *serverJob) SubmitJob(ctx context.Context, in *pb.SubmitJobRequest) (*pb
// 记录日志
logger.Infof("Received request SubmitJob: %v", in)
// 判断账户名、用户名是否包含大写字母
resultAcct := utils.ContainsUppercase(in.Account)
resultUser := utils.ContainsUppercase(in.UserId)
if resultAcct || resultUser {
resultAcct := utils.CheckAccountOrUserStrings(in.Account)
resultUser := utils.CheckAccountOrUserStrings(in.UserId)
if !resultAcct || !resultUser {
errInfo := &errdetails.ErrorInfo{
Reason: "ACCOUNT_USER_CONTAIN_UPPER_LETTER",
Reason: "ACCOUNT_USER_CONTAIN_ILLEGAL_CHARACTERS",
}
st := status.New(codes.Internal, "The account or username contains uppercase letters.")
st := status.New(codes.Internal, "The account or username contains illegal characters.")
st, _ = st.WithDetails(errInfo)
return nil, st.Err()
}
Expand Down Expand Up @@ -3017,12 +3017,12 @@ func (s *serverJob) SubmitScriptAsJob(ctx context.Context, in *pb.SubmitScriptAs
)
// 记录日志
logger.Infof("Received request SubmitFileAsJob: %v", in)
resultUser := utils.ContainsUppercase(in.UserId)
if resultUser {
resultUser := utils.CheckAccountOrUserStrings(in.UserId)
if !resultUser {
errInfo := &errdetails.ErrorInfo{
Reason: "USER_CONTAIN_UPPER_LETTER",
Reason: "USER_CONTAIN_ILLEGAL_CHARACTERS",
}
st := status.New(codes.Internal, "The username contains uppercase letters.")
st := status.New(codes.Internal, "The username contains illegal characters.")
st, _ = st.WithDetails(errInfo)
return nil, st.Err()
}
Expand Down
2 changes: 1 addition & 1 deletion tests/account/CreateAccount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestCreateAccount(t *testing.T) {

// Call the Add RPC with test data
req := &pb.CreateAccountRequest{
AccountName: "this11",
AccountName: "t1his11jjj",
OwnerUserId: "test16",
}
_, err = client.CreateAccount(context.Background(), req)
Expand Down
4 changes: 2 additions & 2 deletions tests/user/AddUserToAccount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ func TestAddUserToAccount(t *testing.T) {

// Call the Add RPC with test data
req := &pb.AddUserToAccountRequest{
UserId: "test06",
AccountName: "x_admin",
UserId: "test06666",
AccountName: "t1his11jjj",
}
_, err = client.AddUserToAccount(context.Background(), req)
if err != nil {
Expand Down
27 changes: 20 additions & 7 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"io/ioutil"
"log"
"reflect"
"regexp"
"sort"
"strconv"
"unicode"

"os/exec"
"os/user"
Expand Down Expand Up @@ -338,13 +338,26 @@ func GetUserNameByUid(uid int) (string, error) {
}

// 判断字符串中是否包含大写字母
func ContainsUppercase(s string) bool {
for _, char := range s {
if unicode.IsUpper(char) {
return true
}
func CheckAccountOrUserStrings(s string) bool {

pattern := "^[a-z0-9_]+$"
// 编译正则表达式
reg := regexp.MustCompile(pattern)
// 使用正则表达式判断字符串是否符合模式
if reg.MatchString(s) {
return true
// fmt.Println("字符串符合要求")
} else {
// fmt.Println("字符串不符合要求")
return false
}
return false

// for _, char := range s {
// if unicode.IsUpper(char) {
// return true
// }
// }
// return false
}

// 本地提交作业函数
Expand Down

0 comments on commit 10eac10

Please sign in to comment.