Skip to content

Commit 76100db

Browse files
better authorizaion (#357)
Signed-off-by: Rohan <[email protected]>
1 parent 5f79d02 commit 76100db

File tree

4 files changed

+76
-36
lines changed

4 files changed

+76
-36
lines changed

cmd/harbor/root/user/create.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
package user
1515

1616
import (
17+
"strings"
18+
1719
"github.com/goharbor/harbor-cli/pkg/api"
1820
log "github.com/sirupsen/logrus"
1921

@@ -44,9 +46,16 @@ func UserCreateCmd() *cobra.Command {
4446
err = createUserView(createView)
4547
}
4648

49+
// Check if the error is due to unauthorized access.
50+
4751
if err != nil {
48-
log.Errorf("failed to create user: %v", err)
52+
if isUnauthorizedError(err) {
53+
log.Error("Permission denied: Admin privileges are required to execute this command.")
54+
} else {
55+
log.Errorf("failed to create user: %v", err)
56+
}
4957
}
58+
5059
},
5160
}
5261

@@ -64,3 +73,7 @@ func createUserView(createView *create.CreateView) error {
6473
create.CreateUserView(createView)
6574
return api.CreateUser(*createView)
6675
}
76+
77+
func isUnauthorizedError(err error) bool {
78+
return strings.Contains(err.Error(), "403")
79+
}

cmd/harbor/root/user/delete.go

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,25 @@ import (
2222
"github.com/spf13/cobra"
2323
)
2424

25+
// UserDeleteCmd defines the "delete" command for user deletion.
2526
func UserDeleteCmd() *cobra.Command {
2627
cmd := &cobra.Command{
2728
Use: "delete",
2829
Short: "delete user by name or id",
2930
Args: cobra.MinimumNArgs(0),
3031
Run: func(cmd *cobra.Command, args []string) {
31-
var err error
32-
33-
var wg sync.WaitGroup
34-
errChan := make(chan error, len(args)) // Channel to collect error
35-
32+
// If there are command line arguments, process them concurrently.
3633
if len(args) > 0 {
34+
var wg sync.WaitGroup
35+
errChan := make(chan error, len(args)) // Channel to collect errors
36+
3737
for _, arg := range args {
38-
userID, _ := api.GetUsersIdByName(arg)
38+
// Retrieve user ID by name.
39+
userID, err := api.GetUsersIdByName(arg)
40+
if err != nil {
41+
log.Errorf("failed to get user id for '%s': %v", arg, err)
42+
continue
43+
}
3944
wg.Add(1)
4045
go func(userID int64) {
4146
defer wg.Done()
@@ -44,31 +49,31 @@ func UserDeleteCmd() *cobra.Command {
4449
}
4550
}(userID)
4651
}
47-
} else {
48-
userId := prompt.GetUserIdFromUser()
49-
err = api.DeleteUser(userId)
50-
if err != nil {
51-
log.Errorf("failed to delete user: %v", err)
52-
}
53-
}
5452

55-
// Wait for all goroutines to finish
56-
go func() {
57-
wg.Wait()
58-
close(errChan)
59-
}()
53+
// Wait for all goroutines to finish and then close the error channel.
54+
go func() {
55+
wg.Wait()
56+
close(errChan)
57+
}()
6058

61-
// Collect and handle errors
62-
var finalErr error
63-
for err := range errChan {
64-
if finalErr == nil {
65-
finalErr = err
66-
} else {
67-
log.Errorf("Error: %v", err)
59+
// Process errors from the goroutines.
60+
for err := range errChan {
61+
if isUnauthorizedError(err) {
62+
log.Error("Permission denied: Admin privileges are required to execute this command.")
63+
} else {
64+
log.Errorf("failed to delete user: %v", err)
65+
}
66+
}
67+
} else {
68+
// Interactive mode: get the user ID from the prompt.
69+
userID := prompt.GetUserIdFromUser()
70+
if err := api.DeleteUser(userID); err != nil {
71+
if isUnauthorizedError(err) {
72+
log.Error("Permission denied: Admin privileges are required to execute this command.")
73+
} else {
74+
log.Errorf("failed to delete user: %v", err)
75+
}
6876
}
69-
}
70-
if finalErr != nil {
71-
log.Errorf("failed to delete user: %v", finalErr)
7277
}
7378
},
7479
}

cmd/harbor/root/user/elevate.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,38 @@ func ElevateUserCmd() *cobra.Command {
3131
var err error
3232
var userId int64
3333
if len(args) > 0 {
34-
userId, _ = api.GetUsersIdByName(args[0])
34+
userId, err = api.GetUsersIdByName(args[0])
35+
if err != nil {
36+
log.Errorf("failed to get user id for '%s': %v", args[0], err)
37+
return
38+
}
39+
3540
} else {
3641
userId = prompt.GetUserIdFromUser()
42+
if err != nil {
43+
log.Errorf("failed to get user id: %v", err)
44+
return
45+
}
3746
}
3847

3948
confirm, err := views.ConfirmElevation()
40-
if confirm {
41-
err = api.ElevateUser(userId)
42-
} else {
43-
log.Error("Permission denied for elevate user to admin.")
44-
}
4549
if err != nil {
50+
log.Errorf("failed to confirm elevation: %v", err)
51+
return
52+
}
53+
if !confirm {
54+
log.Error("User did not confirm elevation. Aborting command.")
55+
return
56+
}
57+
58+
err = api.ElevateUser(userId)
59+
if isUnauthorizedError(err) {
60+
log.Error("Permission denied: Admin privileges are required to execute this command.")
61+
} else {
4662
log.Errorf("failed to elevate user: %v", err)
4763
}
64+
65+
return
4866
},
4967
}
5068

cmd/harbor/root/user/list.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ func UserListCmd() *cobra.Command {
3333
Run: func(cmd *cobra.Command, args []string) {
3434
response, err := api.ListUsers(opts)
3535
if err != nil {
36-
log.Errorf("failed to list users: %v", err)
36+
if isUnauthorizedError(err) {
37+
log.Error("Permission denied: Admin privileges are required to execute this command.")
38+
} else {
39+
log.Errorf("failed to list users: %v", err)
40+
}
3741
return
3842
}
3943
FormatFlag := viper.GetString("output-format")

0 commit comments

Comments
 (0)