|
| 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 | +} |
0 commit comments