Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TLS/SSL Security Framework #196

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions cmd/clogin/clogin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright (c) 2023 Peking University and Peking University
* Changsha Institute for Computing and Digital Economy
*
* CraneSched is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of
* the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS,
* WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/

package main

import (
"CraneFrontEnd/internal/clogin"
)

func main() {
clogin.ParseCmdArgs()
}
33 changes: 17 additions & 16 deletions internal/cacctmgr/CmdArgParser.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ var (
Run: func(cmd *cobra.Command, args []string) {
err := util.ErrorSuccess
if cmd.Flags().Changed("description") { //See if a flag was set by the user
err = ModifyAccount(protos.ModifyField_Description, FlagAccount.Description, FlagAccount.Name, protos.OperatorType_Overwrite)
err = ModifyAccount(protos.ModifyField_Description, FlagAccount.Description, FlagAccount.Name, protos.OperationType_Overwrite)
}
if err != util.ErrorSuccess {
os.Exit(err)
Expand All @@ -194,27 +194,27 @@ var (
// ModifyAccount("parent_account", FlagAccount.ParentAccount, FlagName, protos.ModifyEntityRequest_Overwrite)
//}
if cmd.Flags().Changed("set-allowed-partition") {
err = ModifyAccount(protos.ModifyField_Partition, strings.Join(FlagAccount.AllowedPartitions, ","), FlagAccount.Name, protos.OperatorType_Overwrite)
err = ModifyAccount(protos.ModifyField_Partition, strings.Join(FlagAccount.AllowedPartitions, ","), FlagAccount.Name, protos.OperationType_Overwrite)
} else if cmd.Flags().Changed("add-allowed-partition") {
err = ModifyAccount(protos.ModifyField_Partition, FlagSetPartition, FlagAccount.Name, protos.OperatorType_Add)
err = ModifyAccount(protos.ModifyField_Partition, FlagSetPartition, FlagAccount.Name, protos.OperationType_Add)
} else if cmd.Flags().Changed("delete-allowed-partition") {
err = ModifyAccount(protos.ModifyField_Partition, FlagSetPartition, FlagAccount.Name, protos.OperatorType_Delete)
err = ModifyAccount(protos.ModifyField_Partition, FlagSetPartition, FlagAccount.Name, protos.OperationType_Delete)
}
if err != util.ErrorSuccess {
os.Exit(err)
}
if cmd.Flags().Changed("set-allowed-qos-list") {
err = ModifyAccount(protos.ModifyField_Qos, strings.Join(FlagAccount.AllowedQosList, ","), FlagAccount.Name, protos.OperatorType_Overwrite)
err = ModifyAccount(protos.ModifyField_Qos, strings.Join(FlagAccount.AllowedQosList, ","), FlagAccount.Name, protos.OperationType_Overwrite)
} else if cmd.Flags().Changed("add-allowed-qos-list") {
err = ModifyAccount(protos.ModifyField_Qos, FlagQos.Name, FlagAccount.Name, protos.OperatorType_Add)
err = ModifyAccount(protos.ModifyField_Qos, FlagQos.Name, FlagAccount.Name, protos.OperationType_Add)
} else if cmd.Flags().Changed("delete-allowed-qos-list") {
err = ModifyAccount(protos.ModifyField_Qos, FlagQos.Name, FlagAccount.Name, protos.OperatorType_Delete)
err = ModifyAccount(protos.ModifyField_Qos, FlagQos.Name, FlagAccount.Name, protos.OperationType_Delete)
}
if err != util.ErrorSuccess {
os.Exit(err)
}
if cmd.Flags().Changed("default-qos") {
err = ModifyAccount(protos.ModifyField_DefaultQos, FlagAccount.DefaultQos, FlagAccount.Name, protos.OperatorType_Overwrite)
err = ModifyAccount(protos.ModifyField_DefaultQos, FlagAccount.DefaultQos, FlagAccount.Name, protos.OperationType_Overwrite)
}
if err != util.ErrorSuccess {
os.Exit(err)
Expand All @@ -239,33 +239,33 @@ var (
err := util.ErrorSuccess
// Check if a flag was set explicitly
if cmd.Flags().Changed("admin-level") {
err = ModifyUser(protos.ModifyField_AdminLevel, FlagSetLevel, FlagUser.Name, FlagUser.Account, FlagPartition, protos.OperatorType_Overwrite)
err = ModifyUser(protos.ModifyField_AdminLevel, FlagSetLevel, FlagUser.Name, FlagUser.Account, FlagPartition, protos.OperationType_Overwrite)
}
if err != util.ErrorSuccess {
os.Exit(err)
}
if cmd.Flags().Changed("set-allowed-partition") {
err = ModifyUser(protos.ModifyField_Partition, strings.Join(FlagUserPartitions, ","), FlagUser.Name, FlagUser.Account, FlagPartition, protos.OperatorType_Overwrite)
err = ModifyUser(protos.ModifyField_Partition, strings.Join(FlagUserPartitions, ","), FlagUser.Name, FlagUser.Account, FlagPartition, protos.OperationType_Overwrite)
} else if cmd.Flags().Changed("add-allowed-partition") {
err = ModifyUser(protos.ModifyField_Partition, FlagSetPartition, FlagUser.Name, FlagUser.Account, FlagPartition, protos.OperatorType_Add)
err = ModifyUser(protos.ModifyField_Partition, FlagSetPartition, FlagUser.Name, FlagUser.Account, FlagPartition, protos.OperationType_Add)
} else if cmd.Flags().Changed("delete-allowed-partition") {
err = ModifyUser(protos.ModifyField_Partition, FlagSetPartition, FlagUser.Name, FlagUser.Account, FlagPartition, protos.OperatorType_Delete)
err = ModifyUser(protos.ModifyField_Partition, FlagSetPartition, FlagUser.Name, FlagUser.Account, FlagPartition, protos.OperationType_Delete)
}
if err != util.ErrorSuccess {
os.Exit(err)
}
if cmd.Flags().Changed("set-allowed-qos-list") {
err = ModifyUser(protos.ModifyField_Qos, strings.Join(FlagUserQosList, ","), FlagUser.Name, FlagUser.Account, FlagPartition, protos.OperatorType_Overwrite)
err = ModifyUser(protos.ModifyField_Qos, strings.Join(FlagUserQosList, ","), FlagUser.Name, FlagUser.Account, FlagPartition, protos.OperationType_Overwrite)
} else if cmd.Flags().Changed("add-allowed-qos-list") {
err = ModifyUser(protos.ModifyField_Qos, FlagQos.Name, FlagUser.Name, FlagUser.Account, FlagPartition, protos.OperatorType_Add)
err = ModifyUser(protos.ModifyField_Qos, FlagQos.Name, FlagUser.Name, FlagUser.Account, FlagPartition, protos.OperationType_Add)
} else if cmd.Flags().Changed("delete-allowed-qos-list") {
err = ModifyUser(protos.ModifyField_Qos, FlagQos.Name, FlagUser.Name, FlagUser.Account, FlagPartition, protos.OperatorType_Delete)
err = ModifyUser(protos.ModifyField_Qos, FlagQos.Name, FlagUser.Name, FlagUser.Account, FlagPartition, protos.OperationType_Delete)
}
if err != util.ErrorSuccess {
os.Exit(err)
}
if cmd.Flags().Changed("default-qos") {
err = ModifyUser(protos.ModifyField_DefaultQos, FlagUserDefaultQos, FlagUser.Name, FlagUser.Account, FlagPartition, protos.OperatorType_Overwrite)
err = ModifyUser(protos.ModifyField_DefaultQos, FlagUserDefaultQos, FlagUser.Name, FlagUser.Account, FlagPartition, protos.OperationType_Overwrite)
}
if err != util.ErrorSuccess {
os.Exit(err)
Expand Down Expand Up @@ -468,6 +468,7 @@ func init() {
addCmd.AddCommand(addUserCmd)
{
addUserCmd.Flags().StringVarP(&FlagUser.Name, "name", "N", "", "Set the name of the user")
addUserCmd.Flags().StringVarP(&FlagUser.Password, "password", "P", "", "Set the password of the user")
addUserCmd.Flags().StringVarP(&FlagUser.Account, "account", "A", "", "Set the account of the user")
addUserCmd.Flags().StringSliceVarP(&FlagUserPartitions, "partition", "p", nil, "Set allowed partitions of the user (comma seperated list)")
addUserCmd.Flags().StringVarP(&FlagLevel, "level", "L", "none", "Set admin level (none/operator) of the user")
Expand Down
4 changes: 2 additions & 2 deletions internal/cacctmgr/cacctmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ func DeleteQos(name string) util.CraneCmdError {
}
}

func ModifyAccount(modify_field protos.ModifyField, new_value string, name string, requestType protos.OperatorType) util.CraneCmdError {
func ModifyAccount(modify_field protos.ModifyField, new_value string, name string, requestType protos.OperationType) util.CraneCmdError {
req := protos.ModifyAccountRequest{
Uid: userUid,
ModifyField: modify_field,
Expand Down Expand Up @@ -590,7 +590,7 @@ func ModifyAccount(modify_field protos.ModifyField, new_value string, name strin
}
}

func ModifyUser(modify_field protos.ModifyField, new_value string, name string, account string, partition string, requestType protos.OperatorType) util.CraneCmdError {
func ModifyUser(modify_field protos.ModifyField, new_value string, name string, account string, partition string, requestType protos.OperationType) util.CraneCmdError {
if modify_field == protos.ModifyField_AdminLevel {
if new_value != "none" && new_value != "operator" && new_value != "admin" {
log.Errorf("Unknown admin level, valid values: none, operator, admin.")
Expand Down
2 changes: 1 addition & 1 deletion internal/cfored/cfored.go
Original file line number Diff line number Diff line change
Expand Up @@ -979,7 +979,7 @@ func StartCfored() {
var wgAllRoutines sync.WaitGroup

ctldClient := &GrpcCtldClient{
ctldClientStub: util.GetStubToCtldByConfig(config),
ctldClientStub: util.GetStubToCtldForCfored(config),
ctldReplyChannel: make(chan *protos.StreamCtldReply, 8),
}
wgAllRoutines.Add(1)
Expand Down
6 changes: 3 additions & 3 deletions internal/cfored/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
)

type GrpcCtldClient struct {
ctldClientStub protos.CraneCtldClient
ctldClientStub protos.CraneCtldForCforedClient
ctldReplyChannel chan *protos.StreamCtldReply
}

Expand All @@ -42,7 +42,7 @@ const (
GracefulExit StateOfCtldClient = 4
)

func (client *GrpcCtldClient) CtldReplyReceiveRoutine(stream protos.CraneCtld_CforedStreamClient) {
func (client *GrpcCtldClient) CtldReplyReceiveRoutine(stream protos.CraneCtldForCfored_CforedStreamClient) {
for {
m := new(protos.StreamCtldReply)
if err := stream.RecvMsg(m); err != nil {
Expand All @@ -57,7 +57,7 @@ func (client *GrpcCtldClient) CtldReplyReceiveRoutine(stream protos.CraneCtld_Cf

func (client *GrpcCtldClient) StartCtldClientStream(wg *sync.WaitGroup) {
var request *protos.StreamCforedRequest
var stream protos.CraneCtld_CforedStreamClient
var stream protos.CraneCtldForCfored_CforedStreamClient
var err error

state := StartReg
Expand Down
44 changes: 44 additions & 0 deletions internal/clogin/CmdArgParser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package clogin

import (
"CraneFrontEnd/internal/util"
"os"

"github.com/spf13/cobra"
)

var (
FlagPassword string
FlagConfigFilePath string

RootCmd = &cobra.Command{
Use: "login [flags]",
Short: "Login with your password",
Long: "",
Version: util.Version(),
PersistentPreRun: func(cmd *cobra.Command, args []string) {
util.DetectNetworkProxy()
config := util.ParseConfig(FlagConfigFilePath)
stub = util.GetStubToCtldByConfig(config)
userUid = uint32(os.Getuid())
},
Run: func(cmd *cobra.Command, args []string) {
if err := Login(FlagPassword); err != util.ErrorSuccess {
os.Exit(err)
}
},
}
)

func ParseCmdArgs() {
if err := RootCmd.Execute(); err != nil {
os.Exit(util.ErrorGeneric)
}
}

func init() {
RootCmd.SetVersionTemplate(util.VersionTemplate())
RootCmd.PersistentFlags().StringVarP(&FlagConfigFilePath, "config", "C",
util.DefaultConfigPath, "Path to configuration file")
RootCmd.Flags().StringVarP(&FlagPassword, "password", "P", "", "Input the password of the user")
}
40 changes: 40 additions & 0 deletions internal/clogin/clogin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package clogin

import (
"CraneFrontEnd/generated/protos"
"CraneFrontEnd/internal/util"
"context"
"fmt"
)

var (
userUid uint32
stub protos.CraneCtldClient
)

func Login(password string) util.CraneCmdError {
config := util.ParseConfig(FlagConfigFilePath)
stub = util.GetStubToCtldByConfig(config)
req := protos.LoginRequest{Uid: userUid, Password: password}
var reply *protos.LoginReply
var err error

reply, err = stub.Login(context.Background(), &req)
if err != nil {
util.GrpcErrorPrintf(err, "Failed to login")
return util.ErrorNetwork
}

if !reply.GetOk() {
fmt.Printf("Login failed: %s.\n", util.ErrMsg(reply.GetReason()))
return util.ErrorBackend
}

err = util.SaveFileWithPermissions(util.DefaultJwtTokenPath, []byte(reply.GetToken()), 0600)
if err != nil {
fmt.Printf("Failed to save token file: %s. \n", err.Error())
return util.ErrorGeneric
}
fmt.Println("Login succeeded.")
return util.ErrorSuccess
}
2 changes: 2 additions & 0 deletions internal/util/err.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ func ErrMsg(err_code protos.ErrCode) string {
return "The user does not belong to this account"
case protos.ErrCode_ERR_NO_ACCOUNT_SPECIFIED:
return "No account is specified for the user"
case protos.ErrCode_ERR_PASSWORD_MISMATCH:
return "The entered password is incorrect."
default:
break
}
Expand Down
Loading