Skip to content

Commit 4a11a99

Browse files
qcserestipydependabot[bot]bupd
authored andcommitted
Finalize PR 114 Configuration command to Manage System Configuration (goharbor#521)
* build(deps): bump golang.org/x/text from 0.23.0 to 0.27.0 (goharbor#519) Bumps [golang.org/x/text](https://github.com/golang/text) from 0.23.0 to 0.27.0. - [Release notes](https://github.com/golang/text/releases) - [Commits](golang/text@v0.23.0...v0.27.0) --- updated-dependencies: - dependency-name: golang.org/x/text dependency-version: 0.27.0 dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Signed-off-by: Patrick Eschenbach <patrickeschenbach96@gmail.com> * add system `config get` command This command fetches and stores the system config in the harbor config file. Signed-off-by: bupd <bupdprasanth@gmail.com> Signed-off-by: Patrick Eschenbach <patrickeschenbach96@gmail.com> * add system config `update` command Signed-off-by: bupd <bupdprasanth@gmail.com> Signed-off-by: Patrick Eschenbach <patrickeschenbach96@gmail.com> * Rebase on main; moved command to its own package; added reflection for api configurationsok types; Signed-off-by: Patrick Eschenbach <patrickeschenbach96@gmail.com> * Updated get function to sort for categories; update api handler for different categories; todo: update from config runs into swagger api errors Signed-off-by: Patrick Eschenbach <patrickeschenbach96@gmail.com> * .. Signed-off-by: Patrick Eschenbach <patrickeschenbach96@gmail.com> * feat(configurations): Added configurations view command, clean up get and update command, extend reflection for view functionality - In this commit the view command is added that supports setting categories aligned with the UI - Reflection is cleaned up to also handle secret values stored in the config file - Reflection is used for table view of configurations Signed-off-by: Patrick Eschenbach <patrickeschenbach96@gmail.com> * Add Support for System-Level Robot Accounts (goharbor#507) Signed-off-by: Patrick Eschenbach <patrickeschenbach96@gmail.com> * Updated github.com/go-viper/mapstructure/v2 to avoid vulnerability Signed-off-by: Patrick Eschenbach <patrickeschenbach96@gmail.com> * Renamed config update to config apply; added -f flag for config apply command to support specifiying configurations in a separate file than the harbor-cli global config Signed-off-by: Patrick Eschenbach <patrickeschenbach96@gmail.com> * feat: Remove get command, refactor view command The get command is removed since not necessary. The view command is updated to make category selection available for piping. The apply command is adapted to not use harbor config but external config file that can be chosen via flag. Signed-off-by: Patrick Eschenbach <patrickeschenbach96@gmail.com> * refactor(harbor-cli): Remove add configurations to harbor config function. This function is not longer needed since the configurations settings will be stored in another file and not in the harbor credentials config file. Signed-off-by: Patrick Eschenbach <patrickeschenbach96@gmail.com> * Added changes according to PR Signed-off-by: qcserestipy <patrickeschenbach96@gmail.com> Signed-off-by: Patrick Eschenbach <patrickeschenbach96@gmail.com> * docs(config): update cli docs Signed-off-by: Patrick Eschenbach <patrickeschenbach96@gmail.com> * fix(mod): Patch error in go.mod missing ) Signed-off-by: Patrick Eschenbach <patrickeschenbach96@gmail.com> * docs(config): update cli docs Signed-off-by: Patrick Eschenbach <patrickeschenbach96@gmail.com> * fix(config): make changes to satisfy vulncheck Signed-off-by: Patrick Eschenbach <patrickeschenbach96@gmail.com> * doc(config): remove messed up cli config directory Signed-off-by: Patrick Eschenbach <patrickeschenbach96@gmail.com> * fix(config): remove obsolete vscode settings folder Signed-off-by: Patrick Eschenbach <patrickeschenbach96@gmail.com> --------- Signed-off-by: dependabot[bot] <support@github.com> Signed-off-by: Patrick Eschenbach <patrickeschenbach96@gmail.com> Signed-off-by: bupd <bupdprasanth@gmail.com> Signed-off-by: qcserestipy <patrickeschenbach96@gmail.com> Signed-off-by: Patrick Eschenbach <45457307+qcserestipy@users.noreply.github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: bupd <bupdprasanth@gmail.com>
1 parent 7c94d98 commit 4a11a99

23 files changed

Lines changed: 1064 additions & 7 deletions

cmd/harbor/root/cmd.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"time"
2020

2121
"github.com/goharbor/harbor-cli/cmd/harbor/root/artifact"
22+
"github.com/goharbor/harbor-cli/cmd/harbor/root/configurations"
2223
"github.com/goharbor/harbor-cli/cmd/harbor/root/context"
2324
"github.com/goharbor/harbor-cli/cmd/harbor/root/cve"
2425
"github.com/goharbor/harbor-cli/cmd/harbor/root/instance"
@@ -160,6 +161,10 @@ harbor help
160161
cmd.GroupID = "system"
161162
root.AddCommand(cmd)
162163

164+
cmd = configurations.ConfigurationsCommand()
165+
cmd.GroupID = "system"
166+
root.AddCommand(cmd)
167+
163168
cmd = HealthCommand()
164169
cmd.GroupID = "system"
165170
root.AddCommand(cmd)
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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 configurations
15+
16+
import (
17+
"encoding/json"
18+
"fmt"
19+
"os"
20+
"path/filepath"
21+
22+
"github.com/goharbor/go-client/pkg/sdk/v2.0/models"
23+
"github.com/goharbor/harbor-cli/pkg/api"
24+
"github.com/spf13/cobra"
25+
"gopkg.in/yaml.v2"
26+
)
27+
28+
func ApplyConfigCmd() *cobra.Command {
29+
var cfgFile string
30+
cmd := &cobra.Command{
31+
Use: "apply",
32+
Short: "Update system configurations from local config file",
33+
Long: `Update Harbor system configurations using the values stored in your local config file.
34+
35+
This will push the configurations from your local config file to the Harbor server.
36+
Make sure to run 'harbor config get' first to populate the local config file with current configurations. Alternatively, you can specify a custom configuration file using the --configurations-file flag. This does not have to be a complete configuration file, only the fields you want to update need to be present under the 'configurations' key. Credentials for the Harbor server can be configured in the local config file or through environment variables or global config flags.`,
37+
Args: cobra.NoArgs,
38+
Example: `harbor config apply -f <config_file>`,
39+
RunE: func(cmd *cobra.Command, args []string) error {
40+
var configurations *models.Configurations
41+
var err error
42+
if cfgFile != "" {
43+
data, err := os.ReadFile(cfgFile)
44+
if err != nil {
45+
return fmt.Errorf("failed to read config file: %v", err)
46+
}
47+
fileType := filepath.Ext(cfgFile)
48+
switch fileType {
49+
case ".yaml", ".yml":
50+
if err := yaml.Unmarshal(data, &configurations); err != nil {
51+
return fmt.Errorf("failed to parse YAML: %v", err)
52+
}
53+
case ".json":
54+
if err := json.Unmarshal(data, &configurations); err != nil {
55+
return fmt.Errorf("failed to parse JSON: %v", err)
56+
}
57+
default:
58+
return fmt.Errorf("unsupported file type: %s, expected '.yaml/.yml' or '.json'", fileType)
59+
}
60+
} else {
61+
return fmt.Errorf("no config file specified")
62+
}
63+
64+
err = api.UpdateConfigurations(configurations)
65+
if err != nil {
66+
return fmt.Errorf("failed to update Harbor configurations: %v", err)
67+
}
68+
69+
fmt.Printf("harbor configurations updated successfully from %s.", cfgFile)
70+
return nil
71+
},
72+
}
73+
flags := cmd.Flags()
74+
flags.StringVarP(&cfgFile, "configurations-file", "f", "", "Harbor configurations file to apply.")
75+
76+
return cmd
77+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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 configurations
15+
16+
import (
17+
"github.com/spf13/cobra"
18+
)
19+
20+
func ConfigurationsCommand() *cobra.Command {
21+
cmd := &cobra.Command{
22+
Use: "config",
23+
Short: "Manage system configurations",
24+
Long: `Manage Harbor system configurations including viewing, exporting, and applying settings.
25+
26+
Configuration management workflow:
27+
1. View configurations in table format or export to files
28+
2. Edit exported configuration files as needed
29+
3. Apply modified configurations back to Harbor
30+
31+
Categories available:
32+
- authentication (auth): LDAP, OIDC, UAA authentication settings
33+
- security (sec): Security policies, certificates, and access control
34+
- system (sys): General system behavior, storage, and operational settings`,
35+
Example: ` # View configurations
36+
harbor config view # Table view of all configs
37+
harbor config view -c auth # View only authentication configs
38+
harbor config view -c sec # View only security configs
39+
40+
# Export configurations to files
41+
harbor config view -o json > config.json # Export all configs as JSON
42+
harbor config view -c auth -o yaml | tee auth-config.yaml # Export auth configs as YAML
43+
harbor config view -c sys -o json > system-config.json # Export system configs as JSON
44+
45+
# Apply configurations from files
46+
harbor config apply -f config.json # Apply complete configuration
47+
harbor config apply -f auth-config.yaml # Apply only authentication settings
48+
49+
# Configuration backup and restore workflow
50+
harbor config view -o yaml > backup.yaml # Create backup
51+
# ... make changes to Harbor via UI or other means ...
52+
harbor config apply -f backup.yaml # Restore from backup`,
53+
}
54+
55+
cmd.AddCommand(
56+
ViewConfigCmd(),
57+
ApplyConfigCmd(),
58+
)
59+
60+
return cmd
61+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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 configurations
15+
16+
import (
17+
"fmt"
18+
"strings"
19+
20+
"github.com/goharbor/harbor-cli/pkg/api"
21+
"github.com/goharbor/harbor-cli/pkg/utils"
22+
"github.com/goharbor/harbor-cli/pkg/views/configurations/view"
23+
"github.com/spf13/cobra"
24+
"github.com/spf13/viper"
25+
)
26+
27+
func ViewConfigCmd() *cobra.Command {
28+
var category string
29+
cmd := &cobra.Command{
30+
Use: "view",
31+
Short: "View Harbor configurations",
32+
Long: `View Harbor system configurations. You can filter by category using full names or shorthand:
33+
34+
Categories:
35+
- authentication (auth): User and service authentication settings (LDAP, OIDC, UAA)
36+
- security (sec): Security policies and certificate settings
37+
- system (sys): General system behavior and storage settings
38+
39+
Examples:
40+
harbor config view # View all configurations
41+
harbor config view --category auth # View authentication configs
42+
harbor config view --cat sec # View security configs (shorthand)
43+
harbor config view --cat sys # View system configs
44+
45+
# Export configurations to files
46+
harbor config view -o json > config.json # Save all configs as JSON
47+
harbor config view --cat auth -o yaml | tee auth-config.yaml # Save auth configs as YAML and display
48+
harbor config view --cat sec -o json > security-config.json # Save security configs as JSON`,
49+
Args: cobra.NoArgs,
50+
RunE: func(cmd *cobra.Command, args []string) error {
51+
// Expand shorthand category names
52+
expandedCategory := expandCategoryShorthand(category)
53+
54+
// Validate category if provided
55+
if expandedCategory != "" {
56+
validCategories := []string{"authentication", "security", "system"}
57+
isValid := false
58+
for _, valid := range validCategories {
59+
if expandedCategory == valid {
60+
isValid = true
61+
break
62+
}
63+
}
64+
if !isValid {
65+
return fmt.Errorf("invalid category '%s'. Valid options: authentication (auth), security (sec), system (sys)", category)
66+
}
67+
}
68+
69+
response, err := api.GetConfigurations()
70+
if err != nil {
71+
return err
72+
}
73+
74+
FormatFlag := viper.GetString("output-format")
75+
if FormatFlag != "" {
76+
configurations := utils.ExtractConfigurationsByCategory(response.Payload, expandedCategory)
77+
err = utils.PrintFormat(configurations, FormatFlag)
78+
if err != nil {
79+
return err
80+
}
81+
} else {
82+
view.ViewConfigurations(response.Payload, expandedCategory)
83+
}
84+
return nil
85+
},
86+
}
87+
88+
cmd.Flags().StringVar(&category, "category", "", "Filter by category: authentication (auth), security (sec), system (sys)")
89+
cmd.Flags().StringVar(&category, "cat", "", "Filter by category (shorthand for --category)")
90+
91+
return cmd
92+
}
93+
94+
func expandCategoryShorthand(category string) string {
95+
switch strings.ToLower(category) {
96+
case "auth":
97+
return "authentication"
98+
case "sec":
99+
return "security"
100+
case "sys":
101+
return "system"
102+
default:
103+
return category
104+
}
105+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
---
2+
title: harbor config apply
3+
weight: 85
4+
---
5+
## harbor config apply
6+
7+
### Description
8+
9+
##### Update system configurations from local config file
10+
11+
### Synopsis
12+
13+
Update Harbor system configurations using the values stored in your local config file.
14+
15+
This will push the configurations from your local config file to the Harbor server.
16+
Make sure to run 'harbor config get' first to populate the local config file with current configurations. Alternatively, you can specify a custom configuration file using the --configurations-file flag. This does not have to be a complete configuration file, only the fields you want to update need to be present under the 'configurations' key. Credentials for the Harbor server can be configured in the local config file or through environment variables or global config flags.
17+
18+
```sh
19+
harbor config apply [flags]
20+
```
21+
22+
### Examples
23+
24+
```sh
25+
harbor config apply -f <config_file>
26+
```
27+
28+
### Options
29+
30+
```sh
31+
-f, --configurations-file string Harbor configurations file to apply.
32+
-h, --help help for apply
33+
```
34+
35+
### Options inherited from parent commands
36+
37+
```sh
38+
-c, --config string config file (default is $HOME/.config/harbor-cli/config.yaml)
39+
-o, --output-format string Output format. One of: json|yaml
40+
-v, --verbose verbose output
41+
```
42+
43+
### SEE ALSO
44+
45+
* [harbor config](harbor-config.md) - Manage system configurations
46+

doc/cli-docs/harbor-config-get.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
title: harbor config get
3+
weight: 5
4+
---
5+
## harbor config get
6+
7+
### Description
8+
9+
##### Get Harbor configurations
10+
11+
### Synopsis
12+
13+
Get Harbor system configurations.
14+
15+
This command retrieves the current configurations from Harbor and stores them in your local config file.
16+
17+
```sh
18+
harbor config get [flags]
19+
```
20+
21+
### Options
22+
23+
```sh
24+
-h, --help help for get
25+
```
26+
27+
### Options inherited from parent commands
28+
29+
```sh
30+
-c, --config string config file (default is $HOME/.config/harbor-cli/config.yaml)
31+
-o, --output-format string Output format. One of: json|yaml
32+
-v, --verbose verbose output
33+
```
34+
35+
### SEE ALSO
36+
37+
* [harbor config](harbor-config.md) - Manage system configurations
38+
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
title: harbor config update
3+
weight: 35
4+
---
5+
## harbor config update
6+
7+
### Description
8+
9+
##### Update system configurations from local config file
10+
11+
### Synopsis
12+
13+
Update Harbor system configurations using the values stored in your local config file.
14+
This will push the configurations from your local config file to the Harbor server.
15+
Make sure to run 'harbor config get' first to populate the local config file with current configurations.
16+
17+
```sh
18+
harbor config update [flags]
19+
```
20+
21+
### Examples
22+
23+
```sh
24+
harbor config update
25+
```
26+
27+
### Options
28+
29+
```sh
30+
-h, --help help for update
31+
```
32+
33+
### Options inherited from parent commands
34+
35+
```sh
36+
-c, --config string config file (default is $HOME/.config/harbor-cli/config.yaml)
37+
-o, --output-format string Output format. One of: json|yaml
38+
-v, --verbose verbose output
39+
```
40+
41+
### SEE ALSO
42+
43+
* [harbor config](harbor-config.md) - Manage system configurations
44+

0 commit comments

Comments
 (0)