-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
258 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}, | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.