From da51df7a35f9db6b02539141fa0e1319b539d9a6 Mon Sep 17 00:00:00 2001 From: Lars Kajes Date: Thu, 11 Apr 2024 13:32:56 +0200 Subject: [PATCH 1/4] Support adding der certificates --- pkg/configuration/configuration.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pkg/configuration/configuration.go b/pkg/configuration/configuration.go index 3f222d8e..a0396d9a 100644 --- a/pkg/configuration/configuration.go +++ b/pkg/configuration/configuration.go @@ -383,19 +383,19 @@ func ReadPemFile(path string) (*x509.Certificate, error) { if info.IsDir() { return nil, fmt.Errorf("path %s is a directory, not a file", path) } - pemData, err := os.ReadFile(path) + b, err := os.ReadFile(path) if err != nil { return nil, fmt.Errorf("not a file %s %s", path, err) } - block, _ := pem.Decode(pemData) - if block == nil { - return nil, fmt.Errorf("expected a pem file, could not decode %s", path) + pemData, certBytes := pem.Decode(b) + if pemData != nil { + certBytes = pemData.Bytes } // See if we can parse the certificate - cert, err := x509.ParseCertificate(block.Bytes) + cert, err := x509.ParseCertificate(certBytes) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to parse certificate: %w", err) } return cert, nil } From 513d40bbf2aae917aefa30528ea148b5d02faffb Mon Sep 17 00:00:00 2001 From: Lars Kajes Date: Thu, 11 Apr 2024 14:04:23 +0200 Subject: [PATCH 2/4] make sure confighome is set correctly --- pkg/filesystem/filesystem.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/pkg/filesystem/filesystem.go b/pkg/filesystem/filesystem.go index 083d4174..623f6ac7 100644 --- a/pkg/filesystem/filesystem.go +++ b/pkg/filesystem/filesystem.go @@ -43,7 +43,14 @@ func ConfigDir() string { if configDir, ok := ud["CONFIG"]; ok { return configDir } - return filepath.Join(xdg.Home, ".config", "sdpctl") + path := filepath.Join(xdg.ConfigHome, "sdpctl") + if len(xdg.ConfigHome) <= 0 { + path = filepath.Join(xdg.Home, ".config", "sdpctl") + } + if _, err := os.Stat(path); os.IsNotExist(err) { + os.MkdirAll(path, 0700) + } + return path } func DataDir() string { From ef4b0eecb289701cb7bf1f705198b7ccb2daa946 Mon Sep 17 00:00:00 2001 From: Lars Kajes Date: Fri, 12 Apr 2024 09:41:07 +0200 Subject: [PATCH 3/4] config dir is os dependent --- pkg/filesystem/filesystem.go | 5 ++--- pkg/filesystem/vars.go | 8 ++++++++ pkg/filesystem/vars_windows.go | 8 ++++++++ 3 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 pkg/filesystem/vars.go create mode 100644 pkg/filesystem/vars_windows.go diff --git a/pkg/filesystem/filesystem.go b/pkg/filesystem/filesystem.go index 623f6ac7..cdacf88c 100644 --- a/pkg/filesystem/filesystem.go +++ b/pkg/filesystem/filesystem.go @@ -15,7 +15,6 @@ const ( AgDataDir = "SDPCTL_DATA_DIR" AgDownloadDir = "SDPCTL_DOWNLOAD_DIR" XdgConfigHome = "XDG_CONFIG_HOME" - AppData = "AppData" ) func AbsolutePath(s string) string { @@ -45,7 +44,7 @@ func ConfigDir() string { } path := filepath.Join(xdg.ConfigHome, "sdpctl") if len(xdg.ConfigHome) <= 0 { - path = filepath.Join(xdg.Home, ".config", "sdpctl") + path = filepath.Join(xdg.Home, ConfigSubDir, "sdpctl") } if _, err := os.Stat(path); os.IsNotExist(err) { os.MkdirAll(path, 0700) @@ -87,7 +86,7 @@ func BackupDir() string { func parseUsersDirs() (map[string]string, error) { res := map[string]string{} - configHome := filepath.Join(xdg.Home, ".config") + configHome := filepath.Join(xdg.Home, ConfigSubDir) if len(xdg.ConfigHome) > 0 { configHome = xdg.ConfigHome } diff --git a/pkg/filesystem/vars.go b/pkg/filesystem/vars.go new file mode 100644 index 00000000..d83789b8 --- /dev/null +++ b/pkg/filesystem/vars.go @@ -0,0 +1,8 @@ +//go:build !windows && !darwin +// +build !windows,!darwin + +package filesystem + +const ( + ConfigSubDir string = ".config" +) diff --git a/pkg/filesystem/vars_windows.go b/pkg/filesystem/vars_windows.go new file mode 100644 index 00000000..9c30b1f3 --- /dev/null +++ b/pkg/filesystem/vars_windows.go @@ -0,0 +1,8 @@ +//go:build windows +// +build windows + +package filesystem + +const ( + ConfigSubDir string = "AppData/Local" +) From 101fc792da4285182b32d641de6420b3628ec8b4 Mon Sep 17 00:00:00 2001 From: Lars Kajes Date: Fri, 12 Apr 2024 10:07:26 +0200 Subject: [PATCH 4/4] remove unused vars --- cmd/admin_messages.go | 4 ++-- cmd/configure/configure.go | 4 ++-- cmd/configure/signin.go | 4 ++-- cmd/privileges.go | 4 ++-- cmd/root.go | 4 ++-- pkg/configuration/configuration.go | 3 --- 6 files changed, 10 insertions(+), 13 deletions(-) diff --git a/cmd/admin_messages.go b/cmd/admin_messages.go index 22117e42..312e29ce 100644 --- a/cmd/admin_messages.go +++ b/cmd/admin_messages.go @@ -31,7 +31,7 @@ func NewAdminMessageCmd(f *factory.Factory) *cobra.Command { Use: "admin-messages", Short: docs.AdminMessagesRootDoc.Short, RunE: func(c *cobra.Command, args []string) error { - return adminMessagesRun(c, args, &opts) + return adminMessagesRun(&opts) }, } cmd.Flags().BoolVar(&opts.json, "json", false, "Display in JSON format") @@ -47,7 +47,7 @@ const messageTemplate = `Messages: {{ end }} ` -func adminMessagesRun(cmd *cobra.Command, args []string, opts *adminMessageOpts) error { +func adminMessagesRun(opts *adminMessageOpts) error { cfg := opts.config client, err := opts.factory.APIClient(cfg) if err != nil { diff --git a/cmd/configure/configure.go b/cmd/configure/configure.go index 0aa42b4b..641254e1 100644 --- a/cmd/configure/configure.go +++ b/cmd/configure/configure.go @@ -74,7 +74,7 @@ func NewCmdConfigure(f *factory.Factory) *cobra.Command { return nil }, RunE: func(c *cobra.Command, args []string) error { - return configRun(c, args, &opts) + return configRun(&opts) }, } @@ -85,7 +85,7 @@ func NewCmdConfigure(f *factory.Factory) *cobra.Command { return cmd } -func configRun(cmd *cobra.Command, args []string, opts *configureOptions) error { +func configRun(opts *configureOptions) error { if len(opts.URL) < 1 { return errors.New("Missing URL for the Controller") } diff --git a/cmd/configure/signin.go b/cmd/configure/signin.go index 1cc6fd9a..3d8ce5ba 100644 --- a/cmd/configure/signin.go +++ b/cmd/configure/signin.go @@ -38,14 +38,14 @@ func NewSigninCmd(f *factory.Factory) *cobra.Command { Long: docs.ConfigureSigninDocs.Long, Example: docs.ConfigureSigninDocs.ExampleString(), RunE: func(c *cobra.Command, args []string) error { - return signinRun(c, args, &opts) + return signinRun(c, &opts) }, } return signinCmd } -func signinRun(cmd *cobra.Command, args []string, opts *signinOptions) error { +func signinRun(cmd *cobra.Command, opts *signinOptions) error { if len(os.Getenv("SDPCTL_NO_KEYRING")) > 0 { fmt.Fprintf(opts.StdErr, "the %s command has no effect when using SDPCTL_NO_KEYRING\n", cmd.CalledAs()) fmt.Fprintln(opts.StdErr, "When the keyring integration is disabled, you must provide credentials for each command call.") diff --git a/cmd/privileges.go b/cmd/privileges.go index 8f47ed8b..ccf5d5f8 100644 --- a/cmd/privileges.go +++ b/cmd/privileges.go @@ -31,7 +31,7 @@ func NewPrivilegesCmd(f *factory.Factory) *cobra.Command { Use: "privileges", Short: docs.PrivilegesDocs.Short, RunE: func(c *cobra.Command, args []string) error { - return privilegeRun(c, args, &opts) + return privilegeRun(&opts) }, } cmd.Flags().BoolVar(&opts.json, "json", false, "Display in JSON format") @@ -39,7 +39,7 @@ func NewPrivilegesCmd(f *factory.Factory) *cobra.Command { return cmd } -func privilegeRun(cmd *cobra.Command, args []string, opts *privilegeOption) error { +func privilegeRun(opts *privilegeOption) error { cfg := opts.config client, err := opts.factory.APIClient(cfg) if err != nil { diff --git a/cmd/root.go b/cmd/root.go index 07e4e776..2ddb1c11 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -244,7 +244,7 @@ func Execute() cmdutil.ExitCode { // logOutput defaults to logfile in $XDG_DATA_HOME or $HOME/.local/share // if no TTY is available, stdout will be used -func logOutput(cmd *cobra.Command, f *factory.Factory, cfg *configuration.Config) io.Writer { +func logOutput(cmd *cobra.Command, f *factory.Factory) io.Writer { log.SetFormatter(&log.TextFormatter{ FullTimestamp: true, TimestampFormat: time.RFC3339, @@ -303,7 +303,7 @@ func rootPersistentPreRunEFunc(f *factory.Factory, cfg *configuration.Config) fu log.SetLevel(log.DebugLevel) } - log.SetOutput(logOutput(cmd, f, cfg)) + log.SetOutput(logOutput(cmd, f)) if v, err := cmd.Flags().GetString("events-path"); err == nil && len(v) > 0 { if err := util.AddSocketLogHook(v); err != nil { return fmt.Errorf("failed to initialize events-path: %w", err) diff --git a/pkg/configuration/configuration.go b/pkg/configuration/configuration.go index a0396d9a..0ef591dc 100644 --- a/pkg/configuration/configuration.go +++ b/pkg/configuration/configuration.go @@ -320,9 +320,6 @@ func (c *Config) CheckForUpdate(out io.Writer, client *http.Client, current stri // Write new check time to config after request is made c.LastVersionCheck = time.Now().Format(time.RFC3339Nano) viper.Set("last_version_check", c.LastVersionCheck) - if err != nil { - return c, err - } req.Header.Add("Accept", "application/vnd.github+json") res, err := api.RequestRetry(client, req) if err != nil {