Skip to content

Commit 180af97

Browse files
authored
feat:add command to manage user passwords (#524)
1 parent a949fc3 commit 180af97

File tree

15 files changed

+488
-2
lines changed

15 files changed

+488
-2
lines changed

cmd/harbor/root/cmd.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,10 @@ harbor help
151151
cmd.GroupID = "access"
152152
root.AddCommand(cmd)
153153

154+
cmd = PasswordCommand()
155+
cmd.GroupID = "access"
156+
root.AddCommand(cmd)
157+
154158
// System
155159
cmd = context.Context()
156160
cmd.GroupID = "system"

cmd/harbor/root/password.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// Copyright Project Harbor Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
package root
15+
16+
import (
17+
"fmt"
18+
19+
"github.com/goharbor/go-client/pkg/sdk/v2.0/client/user"
20+
"github.com/goharbor/go-client/pkg/sdk/v2.0/models"
21+
"github.com/goharbor/harbor-cli/pkg/utils"
22+
"github.com/goharbor/harbor-cli/pkg/views/password/change"
23+
"github.com/spf13/cobra"
24+
)
25+
26+
func PasswordCommand() *cobra.Command {
27+
var opts change.PasswordChangeView
28+
29+
cmd := &cobra.Command{
30+
Use: "password",
31+
Short: "Change your password",
32+
Args: cobra.MinimumNArgs(0),
33+
RunE: func(cmd *cobra.Command, args []string) error {
34+
change.ChangePasswordView(&opts)
35+
36+
err := UpdatePassword(&opts)
37+
if err != nil {
38+
return fmt.Errorf("error changing password:%v", err)
39+
}
40+
return nil
41+
},
42+
}
43+
44+
return cmd
45+
}
46+
47+
func UpdatePassword(opts *change.PasswordChangeView) error {
48+
ctx, client, err := utils.ContextWithClient()
49+
if err != nil {
50+
return err
51+
}
52+
53+
userResp, err := client.User.GetCurrentUserInfo(ctx, &user.GetCurrentUserInfoParams{})
54+
if err != nil {
55+
return err
56+
}
57+
58+
_, err = client.User.UpdateUserPassword(ctx, &user.UpdateUserPasswordParams{
59+
Password: &models.PasswordReq{
60+
OldPassword: opts.OldPassword,
61+
NewPassword: opts.NewPassword,
62+
},
63+
UserID: userResp.Payload.UserID,
64+
})
65+
if err != nil {
66+
return err
67+
}
68+
if err := utils.GenerateEncryptionKey(); err != nil {
69+
fmt.Println("Encryption key already exists or could not be created:", err)
70+
}
71+
72+
key, err := utils.GetEncryptionKey()
73+
if err != nil {
74+
return fmt.Errorf("failed to get encryption key: %s", err)
75+
}
76+
77+
encryptedPassword, err := utils.Encrypt(key, []byte(opts.NewPassword))
78+
if err != nil {
79+
return fmt.Errorf("failed to encrypt password: %s", err)
80+
}
81+
82+
config, err := utils.GetCurrentHarborConfig()
83+
if err != nil {
84+
return fmt.Errorf("failed to get current Harbor config: %v", err)
85+
}
86+
87+
credentialName := config.CurrentCredentialName
88+
89+
existingCred, _ := utils.GetCredentials(credentialName)
90+
cred := utils.Credential{
91+
Name: existingCred.Name,
92+
Username: existingCred.Username,
93+
Password: encryptedPassword,
94+
ServerAddress: existingCred.ServerAddress,
95+
}
96+
harborData, err := utils.GetCurrentHarborData()
97+
if err != nil {
98+
return fmt.Errorf("failed to get current harbor data: %s", err)
99+
}
100+
configPath := harborData.ConfigPath
101+
102+
if err = utils.UpdateCredentialsInConfigFile(cred, configPath); err != nil {
103+
return fmt.Errorf("failed to update credentials: %s", err)
104+
}
105+
return nil
106+
}

cmd/harbor/root/user/cmd.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ func User() *cobra.Command {
3030
UserCreateCmd(),
3131
UserDeleteCmd(),
3232
ElevateUserCmd(),
33+
UserPasswordChangeCmd(),
3334
)
3435

3536
return cmd

cmd/harbor/root/user/password.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright Project Harbor Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package user
16+
17+
import (
18+
"github.com/goharbor/harbor-cli/pkg/api"
19+
"github.com/goharbor/harbor-cli/pkg/prompt"
20+
"github.com/goharbor/harbor-cli/pkg/views/password/reset"
21+
log "github.com/sirupsen/logrus"
22+
"github.com/spf13/cobra"
23+
)
24+
25+
func UserPasswordChangeCmd() *cobra.Command {
26+
var opts reset.PasswordChangeView
27+
28+
cmd := &cobra.Command{
29+
Use: "password",
30+
Short: "Reset user password by name or id",
31+
Long: "Allows admin to reset the password for a specified user or select interactively if no username is provided.",
32+
Args: cobra.MinimumNArgs(0),
33+
Run: func(cmd *cobra.Command, args []string) {
34+
var userId int64
35+
var err error
36+
resetView := &reset.PasswordChangeView{
37+
NewPassword: opts.NewPassword,
38+
ConfirmPassword: opts.ConfirmPassword,
39+
}
40+
41+
if len(args) > 0 {
42+
userId, err = api.GetUsersIdByName(args[0])
43+
if err != nil {
44+
log.Errorf("failed to get user id for '%s': %v", args[0], err)
45+
return
46+
}
47+
if userId == 0 {
48+
log.Errorf("User with name '%s' not found", args[0])
49+
return
50+
}
51+
} else {
52+
userId = prompt.GetUserIdFromUser()
53+
}
54+
55+
reset.ChangePasswordView(resetView)
56+
57+
err = api.ResetPassword(userId, opts)
58+
if err != nil {
59+
if isUnauthorizedError(err) {
60+
log.Error("Permission denied: Admin privileges are required to execute this command.")
61+
} else {
62+
log.Errorf("failed to reset user password: %v", err)
63+
}
64+
}
65+
},
66+
}
67+
return cmd
68+
}

doc/cli-docs/harbor-password.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
title: harbor password
3+
weight: 55
4+
---
5+
## harbor password
6+
7+
### Description
8+
9+
##### Change your password
10+
11+
```sh
12+
harbor password [flags]
13+
```
14+
15+
### Options
16+
17+
```sh
18+
-h, --help help for password
19+
```
20+
21+
### Options inherited from parent commands
22+
23+
```sh
24+
-c, --config string config file (default is $HOME/.config/harbor-cli/config.yaml)
25+
-o, --output-format string Output format. One of: json|yaml
26+
-v, --verbose verbose output
27+
```
28+
29+
### SEE ALSO
30+
31+
* [harbor](harbor.md) - Official Harbor CLI
32+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
title: harbor user password
3+
weight: 30
4+
---
5+
## harbor user password
6+
7+
### Description
8+
9+
##### Reset user password by name or id
10+
11+
### Synopsis
12+
13+
Allows admin to reset the password for a specified user or select interactively if no username is provided.
14+
15+
```sh
16+
harbor user password [flags]
17+
```
18+
19+
### Options
20+
21+
```sh
22+
-h, --help help for password
23+
```
24+
25+
### Options inherited from parent commands
26+
27+
```sh
28+
-c, --config string config file (default is $HOME/.config/harbor-cli/config.yaml)
29+
-o, --output-format string Output format. One of: json|yaml
30+
-v, --verbose verbose output
31+
```
32+
33+
### SEE ALSO
34+
35+
* [harbor user](harbor-user.md) - Manage users
36+

doc/cli-docs/harbor-user.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,5 @@ Manage users in Harbor
3939
* [harbor user delete](harbor-user-delete.md) - delete user by name or id
4040
* [harbor user elevate](harbor-user-elevate.md) - elevate user
4141
* [harbor user list](harbor-user-list.md) - List users
42+
* [harbor user password](harbor-user-password.md) - Reset user password by name or id
4243

doc/cli-docs/harbor.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ harbor help
4444
* [harbor label](harbor-label.md) - Manage labels in Harbor
4545
* [harbor login](harbor-login.md) - Log in to Harbor registry
4646
* [harbor logs](harbor-logs.md) - Get recent logs of the projects which the user is a member of
47+
* [harbor password](harbor-password.md) - Change your password
4748
* [harbor project](harbor-project.md) - Manage projects and assign resources to them
4849
* [harbor quota](harbor-quota.md) - Manage quotas
4950
* [harbor registry](harbor-registry.md) - Manage registries
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
.nh
2+
.TH "HARBOR" "1" "Harbor Community" "Harbor User Manuals"
3+
4+
.SH NAME
5+
harbor-password - Change your password
6+
7+
8+
.SH SYNOPSIS
9+
\fBharbor password [flags]\fP
10+
11+
12+
.SH DESCRIPTION
13+
Change your password
14+
15+
16+
.SH OPTIONS
17+
\fB-h\fP, \fB--help\fP[=false]
18+
help for password
19+
20+
21+
.SH OPTIONS INHERITED FROM PARENT COMMANDS
22+
\fB-c\fP, \fB--config\fP=""
23+
config file (default is $HOME/.config/harbor-cli/config.yaml)
24+
25+
.PP
26+
\fB-o\fP, \fB--output-format\fP=""
27+
Output format. One of: json|yaml
28+
29+
.PP
30+
\fB-v\fP, \fB--verbose\fP[=false]
31+
verbose output
32+
33+
34+
.SH SEE ALSO
35+
\fBharbor(1)\fP
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
.nh
2+
.TH "HARBOR" "1" "Harbor Community" "Harbor User Manuals"
3+
4+
.SH NAME
5+
harbor-user-password - Reset user password by name or id
6+
7+
8+
.SH SYNOPSIS
9+
\fBharbor user password [flags]\fP
10+
11+
12+
.SH DESCRIPTION
13+
Allows admin to reset the password for a specified user or select interactively if no username is provided.
14+
15+
16+
.SH OPTIONS
17+
\fB-h\fP, \fB--help\fP[=false]
18+
help for password
19+
20+
21+
.SH OPTIONS INHERITED FROM PARENT COMMANDS
22+
\fB-c\fP, \fB--config\fP=""
23+
config file (default is $HOME/.config/harbor-cli/config.yaml)
24+
25+
.PP
26+
\fB-o\fP, \fB--output-format\fP=""
27+
Output format. One of: json|yaml
28+
29+
.PP
30+
\fB-v\fP, \fB--verbose\fP[=false]
31+
verbose output
32+
33+
34+
.SH SEE ALSO
35+
\fBharbor-user(1)\fP

0 commit comments

Comments
 (0)