Skip to content
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

feat/Add: Configuration command to Manage System Configuration #114

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/harbor/root/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ harbor help
root.AddCommand(
versionCommand(),
LoginCommand(),
ConfigCommand(),
project.Project(),
registry.Registry(),
repositry.Repository(),
Expand Down
70 changes: 70 additions & 0 deletions cmd/harbor/root/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package root

import (
"github.com/goharbor/harbor-cli/pkg/api"
"github.com/goharbor/harbor-cli/pkg/utils"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

func ConfigCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "config",
Short: "Manage system configurations",
Long: "Manage system configurations",
Example: `harbor config get`,
}

cmd.AddCommand(
GetConfigCmd(),
UpdateConfigCmd(),
)

return cmd
}

// Get System configuration command
func GetConfigCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "get",
Short: "get system configurations",
Example: `harbor config get`,
Run: func(cmd *cobra.Command, args []string) {
res, err := api.GetConfiguration()
if err != nil {
log.Fatalf("Error getting configuration: %v", err)
}

if err = utils.AddConfigToConfigFile(res.Payload, utils.DefaultConfigPath); err != nil {
log.Fatalf("failed to store the configuration: %v", err)
}

utils.PrintPayloadInJSONFormat(res.Payload)
},
}

return cmd
}

func UpdateConfigCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "update",
Short: "update system configurations",
Example: `harbor config update`,
Run: func(cmd *cobra.Command, args []string) {
config, err := utils.GetConfigurations()
if err != nil {
log.Fatalf("failed to get config from file: %v", err)
}

err = api.UpdateConfiguration(config)
if err != nil {
log.Fatalf("failed to update config: %v", err)
}

log.Infof("Configuration updated successfully.")
},
}

return cmd
}
43 changes: 43 additions & 0 deletions pkg/api/config_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package api

import (
"github.com/goharbor/go-client/pkg/sdk/v2.0/client/configure"
"github.com/goharbor/go-client/pkg/sdk/v2.0/models"
"github.com/goharbor/harbor-cli/pkg/utils"
)

// GetConfigurationResponse of the system
func GetConfiguration() (*configure.GetConfigurationsOK, error) {
ctx, client, err := utils.ContextWithClient()
if err != nil {
return nil, err
}

response, err := client.Configure.GetConfigurations(
ctx,
&configure.GetConfigurationsParams{},
)
if err != nil {
return nil, err
}

return response, nil
}

// Update Configuration of the system
func UpdateConfiguration(config *models.Configurations) error {
ctx, client, err := utils.ContextWithClient()
if err != nil {
return err
}

_, err = client.Configure.UpdateConfigurations(
ctx,
&configure.UpdateConfigurationsParams{Configurations: config},
)
if err != nil {
return err
}

return nil
}
52 changes: 50 additions & 2 deletions pkg/utils/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"os"
"path/filepath"

"github.com/goharbor/go-client/pkg/sdk/v2.0/models"
"github.com/goharbor/harbor-cli/pkg/views/config/update"
"github.com/spf13/viper"
)

Expand All @@ -16,8 +18,9 @@ type Credential struct {
}

type HarborConfig struct {
CurrentCredentialName string `yaml:"current-credential-name"`
Credentials []Credential `yaml:"credentials"`
CurrentCredentialName string `yaml:"current-credential-name"`
Credentials []Credential `yaml:"credentials"`
Configurations models.Configurations `yaml:"config"`
}

var (
Expand Down Expand Up @@ -78,7 +81,52 @@ func AddCredentialsToConfigFile(credential Credential, configPath string) error
return err
}
return nil
}

func AddConfigToConfigFile(config *models.ConfigurationsResponse, configPath string) error {
if _, err := os.Stat(configPath); os.IsNotExist(err) {
return err
}

viper.SetConfigFile(configPath)
err := viper.ReadInConfig()
if err != nil {
return err
}

c := HarborConfig{}
err = viper.Unmarshal(&c)
if err != nil {
return err
}

// convert ConfigurationsResponse to Configurations
configurations := update.ConvertToConfigurations(config, "", "")

c.Configurations = *configurations

viper.Set("config", c.Configurations)
err = viper.WriteConfig()
if err != nil {
return err
}

return nil
}

func GetConfigurations() (*models.Configurations, error) {
err := viper.ReadInConfig()
if err != nil {
return &models.Configurations{}, err
}

c := HarborConfig{}
err = viper.Unmarshal(&c)
if err != nil {
return &models.Configurations{}, err
}

return &c.Configurations, nil
}

func GetCredentials(credentialName string) (Credential, error) {
Expand Down
89 changes: 89 additions & 0 deletions pkg/views/config/update/view.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package update

import "github.com/goharbor/go-client/pkg/sdk/v2.0/models"

// Function to convert ConfigurationsResponse to Configurations
func ConvertToConfigurations(
resp *models.ConfigurationsResponse,
ldapSearchPassword string,
oidcClientSecret string,
) *models.Configurations {
return &models.Configurations{
AuditLogForwardEndpoint: getStringValue(resp.AuditLogForwardEndpoint),
AuthMode: getStringValue(resp.AuthMode),
BannerMessage: getStringValue(resp.BannerMessage),
HTTPAuthproxyAdminGroups: getStringValue(resp.HTTPAuthproxyAdminGroups),
HTTPAuthproxyAdminUsernames: getStringValue(resp.HTTPAuthproxyAdminUsernames),
HTTPAuthproxyEndpoint: getStringValue(resp.HTTPAuthproxyEndpoint),
HTTPAuthproxyServerCertificate: getStringValue(resp.HTTPAuthproxyServerCertificate),
HTTPAuthproxySkipSearch: getBoolValue(resp.HTTPAuthproxySkipSearch),
HTTPAuthproxyTokenreviewEndpoint: getStringValue(resp.HTTPAuthproxyTokenreviewEndpoint),
HTTPAuthproxyVerifyCert: getBoolValue(resp.HTTPAuthproxyVerifyCert),
LdapBaseDn: getStringValue(resp.LdapBaseDn),
LdapFilter: getStringValue(resp.LdapFilter),
LdapGroupAdminDn: getStringValue(resp.LdapGroupAdminDn),
LdapGroupAttributeName: getStringValue(resp.LdapGroupAttributeName),
LdapGroupBaseDn: getStringValue(resp.LdapGroupBaseDn),
LdapGroupMembershipAttribute: getStringValue(resp.LdapGroupMembershipAttribute),
LdapGroupSearchFilter: getStringValue(resp.LdapGroupSearchFilter),
LdapGroupSearchScope: getInt64Value(resp.LdapGroupSearchScope),
LdapScope: getInt64Value(resp.LdapScope),
LdapSearchDn: getStringValue(resp.LdapSearchDn),
LdapSearchPassword: &ldapSearchPassword,
LdapTimeout: getInt64Value(resp.LdapTimeout),
LdapUID: getStringValue(resp.LdapUID),
LdapURL: getStringValue(resp.LdapURL),
LdapVerifyCert: getBoolValue(resp.LdapVerifyCert),
NotificationEnable: getBoolValue(resp.NotificationEnable),
OIDCAdminGroup: getStringValue(resp.OIDCAdminGroup),
OIDCAutoOnboard: getBoolValue(resp.OIDCAutoOnboard),
OIDCClientID: getStringValue(resp.OIDCClientID),
OIDCClientSecret: &oidcClientSecret,
OIDCEndpoint: getStringValue(resp.OIDCEndpoint),
OIDCExtraRedirectParms: getStringValue(resp.OIDCExtraRedirectParms),
OIDCGroupFilter: getStringValue(resp.OIDCGroupFilter),
OIDCGroupsClaim: getStringValue(resp.OIDCGroupsClaim),
OIDCName: getStringValue(resp.OIDCName),
OIDCScope: getStringValue(resp.OIDCScope),
OIDCUserClaim: getStringValue(resp.OIDCUserClaim),
OIDCVerifyCert: getBoolValue(resp.OIDCVerifyCert),
PrimaryAuthMode: getBoolValue(resp.PrimaryAuthMode),
ProjectCreationRestriction: getStringValue(resp.ProjectCreationRestriction),
QuotaPerProjectEnable: getBoolValue(resp.QuotaPerProjectEnable),
ReadOnly: getBoolValue(resp.ReadOnly),
RobotNamePrefix: getStringValue(resp.RobotNamePrefix),
RobotTokenDuration: getInt64Value(resp.RobotTokenDuration),
ScannerSkipUpdatePulltime: getBoolValue(resp.ScannerSkipUpdatePulltime),
SelfRegistration: getBoolValue(resp.SelfRegistration),
SessionTimeout: getInt64Value(resp.SessionTimeout),
SkipAuditLogDatabase: getBoolValue(resp.SkipAuditLogDatabase),
StoragePerProject: getInt64Value(resp.StoragePerProject),
TokenExpiration: getInt64Value(resp.TokenExpiration),
UaaClientID: getStringValue(resp.UaaClientID),
UaaClientSecret: getStringValue(resp.UaaClientSecret),
UaaEndpoint: getStringValue(resp.UaaEndpoint),
UaaVerifyCert: getBoolValue(resp.UaaVerifyCert),
}
}

// Helper functions to extract values from configuration items
func getStringValue(item *models.StringConfigItem) *string {
if item != nil {
return &item.Value
}
return nil
}

func getBoolValue(item *models.BoolConfigItem) *bool {
if item != nil {
return &item.Value
}
return nil
}

func getInt64Value(item *models.IntegerConfigItem) *int64 {
if item != nil {
return &item.Value
}
return nil
}