Skip to content

Commit

Permalink
chore: changed results
Browse files Browse the repository at this point in the history
  • Loading branch information
exu committed Nov 19, 2024
1 parent 63fb836 commit c3823d1
Show file tree
Hide file tree
Showing 17 changed files with 258 additions and 135 deletions.
28 changes: 18 additions & 10 deletions cmd/kubectl-testkube/commands/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func NewDiagnosticsCmd() *cobra.Command {

var validators common.CommaList
var groups common.CommaList
var key string
var key, file string

cmd := &cobra.Command{
Use: "diagnostics",
Expand All @@ -30,6 +30,7 @@ func NewDiagnosticsCmd() *cobra.Command {
cmd.Flags().VarP(&groups, "groups", "g", "Comma-separated list of groups, one of: "+allGroupsStr+", defaults to all")

cmd.Flags().StringVarP(&key, "key", "k", "", "License key")
cmd.Flags().StringVarP(&file, "file", "f", "", "License file")

return cmd
}
Expand All @@ -40,24 +41,31 @@ func NewRunDiagnosticsCmdFunc(key string, commands, groups *common.CommaList) fu
// Fetch current setup:
offlineActivation := true
key := cmd.Flag("key").Value.String()
file := cmd.Flag("file").Value.String()

// Run single "diagnostic"

// Run multiple

// Run predefined group

// Run all

// Compose diagnostics validators
d := diagnostics.New()

licenseKeyGroup := d.AddValidatorGroup("license.key", key)
if offlineActivation {
licenseKeyGroup.AddValidator(license.NewOfflineLicenseKeyValidator())

} else {
licenseKeyGroup.AddValidator(license.NewOnlineLicenseKeyValidator())
}
licenseKeyGroup.AddValidator(license.NewOnlineLicenseKeyValidator())
// common validator for both keys
licenseKeyGroup.AddValidator(license.NewKeygenShValidator())

licenseFileGroup := d.AddValidatorGroup("license.file", file)
licenseFileGroup.AddValidator(license.NewFileValidator())

// Run single "diagnostic"

// Run multiple

// Run predefined group

// Run all
d.Run()

}
Expand Down
11 changes: 8 additions & 3 deletions pkg/diagnostics/renderer/cli.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package renderer

import (
"strings"

"github.com/kubeshop/testkube/pkg/diagnostics/validators"
"github.com/kubeshop/testkube/pkg/ui"
)
Expand All @@ -15,20 +17,23 @@ type CLIRenderer struct {
}

func (r CLIRenderer) RenderGroupStart(message string) {
ui.Printf("%s", message)
message = strings.Replace(message, ".", " ", 1)
ui.H2(message)
}

func (r CLIRenderer) RenderProgress(message string) {
ui.Printf("%s", message)
}

func (r CLIRenderer) RenderResult(res validators.ValidationResult) {
ui.Print(res.Validator + " validator status: " + res.Message)
if res.Message != "" {
ui.Warn(res.Validator + " validator status: " + res.Message)
}

if len(res.Errors) > 0 {
for _, err := range res.Errors {
ui.NL()
ui.Err(err.Error)
ui.Errf(err.Message)

Check failure on line 36 in pkg/diagnostics/renderer/cli.go

View workflow job for this annotation

GitHub Actions / Lint Go

printf: non-constant format string in call to github.com/kubeshop/testkube/pkg/ui.Errf (govet)
ui.NL()
ui.Info("Consider following suggestions before proceeding: ")
for _, s := range err.Suggestions {
Expand Down
37 changes: 35 additions & 2 deletions pkg/diagnostics/validators/error.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,41 @@
package validators

type ErrorWithSuggesstion struct {
Error error
import "errors"

func IsError(err error) bool {
return errors.Is(err, &Error{})
}

func Err(e string, kind ErrorKind, suggestions ...string) Error {
err := Error{Message: e, Suggestions: suggestions}
return err
}

type ErrorKind string

type Error struct {
Kind ErrorKind
Message string
Details string
Suggestions []string
DocsURI string
}

func (e Error) Error() string {
return e.Message
}

func (e Error) WithSuggestion(s string) Error {
e.Suggestions = append(e.Suggestions, s)
return e
}

func (e Error) WithDetails(d string) Error {
e.Details = d
return e
}

func (e Error) WithDocsURI(d string) Error {
e.DocsURI = d
return e
}
2 changes: 2 additions & 0 deletions pkg/diagnostics/validators/interface.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package validators

// Validator interface defines the Validate method for validation logic
type Validator interface {
// Validate runs validation logic against subject
Validate(subject any) ValidationResult
}
41 changes: 41 additions & 0 deletions pkg/diagnostics/validators/kinds.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package validators

const (
ErrorKindFileNotFound ErrorKind = "file not found"
ErrorKindKeyNotFound ErrorKind = "key not found"
ErrorKindInvalidFileContent ErrorKind = "invalid file content"
ErrorKindInvalidKeyContent ErrorKind = "invalid key content"
ErrorKindBadWhitespaces ErrorKind = "bad whitespaces"
)

var (
// Suggestions map
Suggestions = map[ErrorKind][]string{
ErrorKindKeyNotFound: {
"please provide valid file for your control plane installation",
"you can pass file as environment variable details here https://docs.testkube.io/installation",
"make sure valid environment variables are set in pods",
},
ErrorKindFileNotFound: {
"please provide valid file for your control plane installation",
"you can pass file as environment variable details here https://docs.testkube.io/blabalbalabl",
"make sure valid environment variables are set in pods, you can use `kubectl describe pods ....`",
},
ErrorKindInvalidKeyContent: {
"please make sure your key is in valid format",
"please make sure given key was not modified in any editor",
"check if provided value was not changed by accident",
"check if additional whitespases were not added on the beggining and the end of the key",
},
ErrorKindInvalidFileContent: {
"please make sure your key is in valid format",
"please make sure given key was not modified in any editor",
"check if provided value was not changed by accident",
"check if additional whitespases were not added on the beggining and the end of the key",
},
ErrorKindBadWhitespaces: {
"please make sure given key was not modified in any editor",
"check if additional whitespases were not added on the beggining and the end of the key",
},
}
)
44 changes: 24 additions & 20 deletions pkg/diagnostics/validators/license/errors.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
package license

import "errors"
import (
v "github.com/kubeshop/testkube/pkg/diagnostics/validators"
)

// Errors definitions for license based logic
var (
ErrLicenseFileNotFound = Err("license file not found")
ErrLicenseKeyNotFound = Err("license key not found")
ErrLicenseKeyInvalid = Err("license key invalid")
ErrLicenseKeyInvalidFormat = Err("license key invalid format")
ErrLicenseKeyInvalidLength = Err("license key invalid length")
ErrWhitespacesAdded = Err("license key contains additional whitespaces")
)
ErrLicenseFileNotFound = v.Err("license file not found", v.ErrorKindFileNotFound).
WithSuggestion("Make sure license key was correctly provided in for the testkube-cloud-api deployment").
WithSuggestion("You can grab deployment detail with kubectl command - `kubectl get deployment testkube-cloud-api -n testkube`, check for ENTERPRISE_LICENSE_FILE value")

func IsLicenseError(err error) bool {
return errors.Is(err, &LicenseError{})
}
ErrLicenseKeyNotFound = v.Err("license key not found", v.ErrorKindKeyNotFound).
WithSuggestion("Make sure license key was correctly provided in for the testkube-cloud-api deployment").
WithSuggestion("You can grab deployment detail with kubectl command - `kubectl get deployment testkube-cloud-api -n testkube`, check for ENTERPRISE_LICENSE_KEY value").
WithSuggestion("Check your Helm chart installation values")

func Err(e string) error {
return &LicenseError{msg: e}
}
ErrLicenseKeyInvalidFormat = v.Err("license key invalid format", v.ErrorKindInvalidKeyContent)

type LicenseError struct {
msg string
}
ErrLicenseKeyInvalidLength = v.Err("license key invalid length", v.ErrorKindInvalidKeyContent).
WithDetails("License key should be in form XXXXXX-XXXXXX-XXXXXX-XXXXXX-XXXXXX-XX - 29 chars in length").
WithSuggestion("Make sure license key is in valid format").
WithSuggestion("Make sure there is no whitespaces on the begining and the end of the key")

func (e *LicenseError) Error() string {
return e.msg
}
ErrOfflineLicenseKeyInvalidPrefix = v.Err("license key has invalid prefix", v.ErrorKindInvalidKeyContent).
WithDetails("License key should start with 'key/' string").
WithSuggestion("Make sure license key is in valid format").
WithSuggestion("Make sure there is no whitespaces on the begining and the end of the key")

ErrWhitespacesAdded = v.Err("license key contains additional whitespaces", v.ErrorKindBadWhitespaces).
WithSuggestion("Make sure there is no whitespaces on the begining and the end of the key")
)
37 changes: 37 additions & 0 deletions pkg/diagnostics/validators/license/file_validator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package license

import (
"github.com/kubeshop/testkube/pkg/diagnostics/validators"
)

func NewFileValidator() FileValidator {
return FileValidator{}
}

type FileValidator struct {
}

func (v FileValidator) Requireds() bool {
return true
}

// Validate validates a given license file for format / length correctness without calling external services
func (v FileValidator) Validate(subject any) validators.ValidationResult {
// get file
file, ok := subject.(string)
if !ok {
return ErrInvalidLicenseFormat
}

if file == "" {
return validators.ValidationResult{
Status: validators.StatusInvalid,
Errors: []validators.Error{
ErrLicenseFileNotFound,
},
}

}

return validators.NewValidResponse()
}
12 changes: 4 additions & 8 deletions pkg/diagnostics/validators/license/keygensh_validator.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package license

import (
"errors"
"fmt"

"github.com/kubeshop/testkube/pkg/diagnostics/validators"
Expand All @@ -13,11 +12,8 @@ var (
ErrInvalidLicenseFormat = validators.ValidationResult{
Status: validators.StatusInvalid,
Message: "Invalid license format",
Errors: []validators.ErrorWithSuggesstion{
{
Error: ErrLicenseKeyInvalidFormat,
Suggestions: []string{"Please provide a valid license string"},
},
Errors: []validators.Error{
ErrLicenseKeyInvalidFormat,
},
}
)
Expand Down Expand Up @@ -52,9 +48,9 @@ func (v KeygenShValidator) Validate(subject any) validators.ValidationResult {
return validators.ValidationResult{
Status: validators.StatusInvalid,
Message: fmt.Sprintf("License key is not valid: '%s'", key),
Errors: []validators.ErrorWithSuggesstion{
Errors: []validators.Error{
{
Error: errors.New(resp.Message),
Message: resp.Message,
DocsURI: "https://docs.testkube.io/articles/migrate-from-oss#license",
},
},
Expand Down
38 changes: 5 additions & 33 deletions pkg/diagnostics/validators/license/offline_key_validator.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package license

import (
"regexp"
"strings"

"github.com/kubeshop/testkube/pkg/diagnostics/validators"
Expand All @@ -15,47 +14,20 @@ type OfflineLicenseKeyValidator struct {
}

// Validate validates a given license key for format / length correctness without calling external services
func (v OfflineLicenseKeyValidator) Validate(subject any) validators.ValidationResult {
func (v OfflineLicenseKeyValidator) Validate(subject any) (r validators.ValidationResult) {
// get key
key, ok := subject.(string)
if !ok {
return ErrInvalidLicenseFormat
return r.WithError(ErrLicenseKeyInvalidFormat)
}

if key == "" {
return validators.ValidationResult{
Status: validators.StatusInvalid,
Errors: []validators.ErrorWithSuggesstion{
{
Error: ErrLicenseKeyNotFound,
Suggestions: Suggestions[ErrLicenseKeyNotFound],
},
},
}

}

// Check if the license key is the correct length and validate
if len(key) == 29 {
// Check if the license key matches the expected format
match, _ := regexp.MatchString(`^[A-Z0-9]{6}-[A-Z0-9]{6}-[A-Z0-9]{6}-[A-Z0-9]{6}-[A-Z0-9]{6}-[A-Z0-9]{1-2}$`, key)
if !match {
println(match)
return validators.ValidationResult{
Status: validators.StatusInvalid,
Errors: []validators.ErrorWithSuggesstion{
{
Error: ErrLicenseKeyInvalidFormat,
Suggestions: Suggestions[ErrLicenseKeyInvalidFormat],
},
},
}
}
return r.WithError(ErrLicenseKeyNotFound)
}

// key can be in enrypted format
if strings.HasPrefix(key, "key/") {
// TODO validate air gapped key
if !strings.HasPrefix(key, "key/") {
return r.WithError(ErrLicenseKeyInvalidLength)

}

Expand Down
Loading

0 comments on commit c3823d1

Please sign in to comment.