-
Notifications
You must be signed in to change notification settings - Fork 7.4k
create User Api #411
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
create User Api #411
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| package commands | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
|
|
||
| "github.com/argoproj/argo-cd/errors" | ||
| argocdclient "github.com/argoproj/argo-cd/pkg/apiclient" | ||
| "github.com/argoproj/argo-cd/server/users" | ||
| "github.com/argoproj/argo-cd/util" | ||
| "github.com/argoproj/argo-cd/util/settings" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| func NewUsersCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command { | ||
| var command = &cobra.Command{ | ||
| Use: "users", | ||
| Short: "Manage users", | ||
| Run: func(c *cobra.Command, args []string) { | ||
| c.HelpFunc()(c, args) | ||
| os.Exit(1) | ||
| }, | ||
| } | ||
| command.AddCommand(NewUsersChangePasswordCommand(clientOpts)) | ||
| return command | ||
| } | ||
|
|
||
| func NewUsersChangePasswordCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command { | ||
| var ( | ||
| CurrentPassword string | ||
| NewPassword string | ||
| ) | ||
| var command = &cobra.Command{ | ||
| Use: "change-password USERNAME", | ||
| Short: "Change User Password", | ||
| Run: func(c *cobra.Command, args []string) { | ||
| if len(args) == 0 { | ||
| c.HelpFunc()(c, args) | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| if CurrentPassword == "" { | ||
| CurrentPassword = settings.ReadAndConfirmPassword("Current Password") | ||
| } | ||
| if NewPassword == "" { | ||
| NewPassword = settings.ReadAndConfirmPassword("New Password") | ||
| } | ||
|
|
||
| userName := args[0] | ||
| body := users.Body{ | ||
| CurrentPassword: CurrentPassword, | ||
| NewPassword: NewPassword, | ||
| } | ||
| userPasswordRequest := users.UsersPasswordRequest{ | ||
| Name: userName, | ||
| Body: &body, | ||
| } | ||
|
|
||
| conn, usrIf := argocdclient.NewClientOrDie(clientOpts).NewUsersClientOrDie() | ||
| defer util.Close(conn) | ||
| _, err := usrIf.UpdatePassword(context.Background(), &userPasswordRequest) | ||
| errors.CheckError(err) | ||
| fmt.Printf("password for user %s updated to %s \n", userName, NewPassword) | ||
| }, | ||
| } | ||
|
|
||
| command.Flags().StringVar(&CurrentPassword, "current-password", "", "current password you wish to change") | ||
| command.Flags().StringVar(&NewPassword, "new-password", "", "new password you want to update to") | ||
| return command | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| package users | ||
|
|
||
| import ( | ||
| "github.com/argoproj/argo-cd/util/password" | ||
| "github.com/argoproj/argo-cd/util/session" | ||
| "github.com/argoproj/argo-cd/util/settings" | ||
| "golang.org/x/net/context" | ||
| ) | ||
|
|
||
| // Server provides a Session service | ||
| type Server struct { | ||
| sessionMgr *session.SessionManager | ||
| settingsMgr *settings.SettingsManager | ||
| } | ||
|
|
||
| // NewServer returns a new instance of the Session service | ||
| func NewServer(sessionMgr *session.SessionManager, settingsMgr *settings.SettingsManager) *Server { | ||
| return &Server{ | ||
| sessionMgr: sessionMgr, | ||
| settingsMgr: settingsMgr, | ||
| } | ||
|
|
||
| } | ||
|
|
||
| //This Function is used to Update User Passwords \ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Go doc convention is to repeat the name of the function in the comment. e.g.: // UpdatePassword updates a user's password |
||
| func (s *Server) UpdatePassword(ctx context.Context, q *UsersPasswordRequest) (*UserResponse, error) { | ||
|
|
||
| cdSettings, err := s.settingsMgr.GetSettings() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| err = s.sessionMgr.VerifyUsernamePassword(q.Name, q.Body.GetCurrentPassword()) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| hashedPassword, err := password.HashPassword(q.Body.GetNewPassword()) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| cdSettings.LocalUsers[q.Name] = hashedPassword | ||
|
|
||
| err = s.settingsMgr.SaveSettings(cdSettings) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return nil, nil | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should not require that the user type in their current password twice. Password confirmation should be done only for the new one.