Skip to content
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
18 changes: 18 additions & 0 deletions internal/cli/kraft/cloud/certificate/certerr/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2024, Unikraft GmbH and The KraftKit Authors.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Copyright (c) 2024, Unikraft GmbH and The KraftKit Authors.
// Copyright (c) 2026, Unikraft GmbH and The KraftKit Authors.

// Licensed under the BSD-3-Clause License (the "License").
// You may not use this file except in compliance with the License.

package certerr
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not look well, you would have a different named sub-package in every package: insterr/certerr/svcerr and so on

Try something generic like errortypes or something else generic


import "errors"

var (
ErrInvalidOutputFormat = errors.New("invalid output format")
ErrCertificateIdentifierRequired = errors.New("either specify a certificate name or UUID, or use the --all flag")
ErrCouldNotParsePEM = errors.New("could not parse PEM")
ErrInvalidPrivateKeyFormat = errors.New("could not parse private key in PKCS1 or PKCS8 format")
ErrCommonNameRequired = errors.New("common name (CN) is required")
ErrPrivateKeyRequired = errors.New("private key is required")
ErrChainRequired = errors.New("chain is required")
Comment on lines +11 to +17
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you start the names with Err, then you have a clash with the package in the style: ...err.Err...

)
13 changes: 7 additions & 6 deletions internal/cli/kraft/cloud/certificate/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

"kraftkit.sh/cmdfactory"
"kraftkit.sh/config"
"kraftkit.sh/internal/cli/kraft/cloud/certificate/certerr"
"kraftkit.sh/internal/cli/kraft/cloud/utils"
"kraftkit.sh/log"
)
Expand All @@ -42,7 +43,7 @@ func isValidChain(chain []byte) error {
block, rest := pem.Decode(chain)
if block == nil {
if len(rest) > 0 {
return fmt.Errorf("could not parse PEM")
return certerr.ErrCouldNotParsePEM
}
break
}
Expand All @@ -57,7 +58,7 @@ func isValidChain(chain []byte) error {
func isValidPrivateKey(pkey []byte) error {
block, _ := pem.Decode(pkey)
if block == nil {
return fmt.Errorf("could not parse PEM")
return certerr.ErrCouldNotParsePEM
}

if _, err := x509.ParsePKCS1PrivateKey(block.Bytes); err == nil {
Expand All @@ -68,7 +69,7 @@ func isValidPrivateKey(pkey []byte) error {
return nil
}

return fmt.Errorf("could not parse private key in PKCS1 or PKCS8 format")
return certerr.ErrInvalidPrivateKeyFormat
}

// Create a KraftCloud certificate.
Expand Down Expand Up @@ -172,15 +173,15 @@ func NewCmd() *cobra.Command {

func (opts *CreateOptions) Pre(cmd *cobra.Command, _ []string) error {
if opts.CN == "" {
return fmt.Errorf("common name (CN) is required")
return certerr.ErrCommonNameRequired
}

if opts.PKey == "" {
return fmt.Errorf("private key is required")
return certerr.ErrPrivateKeyRequired
}

if opts.Chain == "" {
return fmt.Errorf("chain is required")
return certerr.ErrChainRequired
}

err := utils.PopulateMetroToken(cmd, &opts.Metro, &opts.Token, &opts.allowInsecure)
Expand Down
3 changes: 2 additions & 1 deletion internal/cli/kraft/cloud/certificate/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

"kraftkit.sh/cmdfactory"
"kraftkit.sh/config"
"kraftkit.sh/internal/cli/kraft/cloud/certificate/certerr"
"kraftkit.sh/internal/cli/kraft/cloud/utils"
)

Expand Down Expand Up @@ -70,7 +71,7 @@ func (opts *GetOptions) Pre(cmd *cobra.Command, _ []string) error {
}

if !utils.IsValidOutputFormat(opts.Output) {
return fmt.Errorf("invalid output format: %s", opts.Output)
return fmt.Errorf("%w: %s", certerr.ErrInvalidOutputFormat, opts.Output)
}

return nil
Expand Down
3 changes: 2 additions & 1 deletion internal/cli/kraft/cloud/certificate/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"kraftkit.sh/cmdfactory"
"kraftkit.sh/config"
"kraftkit.sh/internal/cli/kraft/cloud/certificate/certerr"
"kraftkit.sh/internal/cli/kraft/cloud/utils"
)

Expand Down Expand Up @@ -57,7 +58,7 @@ func (opts *ListOptions) Pre(cmd *cobra.Command, _ []string) error {
}

if !utils.IsValidOutputFormat(opts.Output) {
return fmt.Errorf("invalid output format: %s", opts.Output)
return fmt.Errorf("%w: %s", certerr.ErrInvalidOutputFormat, opts.Output)
}

return nil
Expand Down
5 changes: 3 additions & 2 deletions internal/cli/kraft/cloud/certificate/remove/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

"kraftkit.sh/cmdfactory"
"kraftkit.sh/config"
"kraftkit.sh/internal/cli/kraft/cloud/certificate/certerr"
"kraftkit.sh/internal/cli/kraft/cloud/utils"
"kraftkit.sh/log"
)
Expand Down Expand Up @@ -70,7 +71,7 @@ func NewCmd() *cobra.Command {

func (opts *RemoveOptions) Pre(cmd *cobra.Command, args []string) error {
if !opts.All && len(args) == 0 {
return fmt.Errorf("either specify a certificate name or UUID, or use the --all flag")
return certerr.ErrCertificateIdentifierRequired
}

err := utils.PopulateMetroToken(cmd, &opts.metro, &opts.token, &opts.allowInsecure)
Expand All @@ -79,7 +80,7 @@ func (opts *RemoveOptions) Pre(cmd *cobra.Command, args []string) error {
}

if !utils.IsValidOutputFormat(opts.Output) {
return fmt.Errorf("invalid output format: %s", opts.Output)
return fmt.Errorf("%w: %s", certerr.ErrInvalidOutputFormat, opts.Output)
}

return nil
Expand Down