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

Add support for der certificates #546

Merged
merged 4 commits into from
Apr 12, 2024
Merged
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
4 changes: 2 additions & 2 deletions cmd/admin_messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions cmd/configure/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}

Expand All @@ -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")
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/configure/signin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down
4 changes: 2 additions & 2 deletions cmd/privileges.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ 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")

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 {
Expand Down
4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
15 changes: 6 additions & 9 deletions pkg/configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -383,19 +380,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
}
Expand Down
12 changes: 9 additions & 3 deletions pkg/filesystem/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const (
AgDataDir = "SDPCTL_DATA_DIR"
AgDownloadDir = "SDPCTL_DOWNLOAD_DIR"
XdgConfigHome = "XDG_CONFIG_HOME"
AppData = "AppData"
)

func AbsolutePath(s string) string {
Expand Down Expand Up @@ -43,7 +42,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, ConfigSubDir, "sdpctl")
}
if _, err := os.Stat(path); os.IsNotExist(err) {
os.MkdirAll(path, 0700)
}
return path
}

func DataDir() string {
Expand Down Expand Up @@ -80,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
}
Expand Down
8 changes: 8 additions & 0 deletions pkg/filesystem/vars.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//go:build !windows && !darwin
// +build !windows,!darwin

package filesystem

const (
ConfigSubDir string = ".config"
)
8 changes: 8 additions & 0 deletions pkg/filesystem/vars_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//go:build windows
// +build windows

package filesystem

const (
ConfigSubDir string = "AppData/Local"
)
Loading