From f7fa548160ba9d5394b2bac6fad2427c8b803a33 Mon Sep 17 00:00:00 2001 From: Eti Ijeoma Date: Fri, 11 Oct 2024 16:22:38 +0100 Subject: [PATCH 1/3] Add check to verify email address providedd for snapshot Signed-off-by: Eti Ijeoma --- cmd/kanvas-snapshot/main.go | 7 +++++++ internal/errors/error.go | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/cmd/kanvas-snapshot/main.go b/cmd/kanvas-snapshot/main.go index ed87547d81..9fe592ce48 100644 --- a/cmd/kanvas-snapshot/main.go +++ b/cmd/kanvas-snapshot/main.go @@ -9,6 +9,7 @@ import ( "net/url" "os" "path/filepath" + "regexp" "strings" "time" @@ -31,6 +32,8 @@ var ( designName string ) +var emailRegex = regexp.MustCompile(`^[a-z0-9._%+\-]+@[a-z0-9.\-]+\.[a-z]{2,}$`) + var generateKanvasSnapshotCmd = &cobra.Command{ Use: "kanvas", Short: "Generate a Kanvas snapshot using a Helm chart", @@ -255,6 +258,10 @@ func GenerateSnapshot(designID, chartURI, email, assetLocation string) error { return nil } +func isValidEmail(email string) bool { + return emailRegex.MatchString(email) +} + func main() { generateKanvasSnapshotCmd.Flags().StringVarP(&chartURI, "file", "f", "", "URI to Helm chart (required)") diff --git a/internal/errors/error.go b/internal/errors/error.go index 001a987f7c..2d313b2e79 100644 --- a/internal/errors/error.go +++ b/internal/errors/error.go @@ -7,6 +7,7 @@ import ( ) var ( +<<<<<<< Updated upstream ErrInvalidChartURICode = "kanvas-snapshot-900" ErrCreatingMesheryDesignCode = "kanvas-snapshot-901" ErrGeneratingSnapshotCode = "kanvas-snapshot-902" @@ -14,6 +15,15 @@ var ( ErrDecodingAPICode = "kanvas-snapshot-905" ErrUnexpectedResponseCodeCode = "kanvas-snapshot-906" ErrRequiredFieldNotProvidedCode = "kanvas-snapshot-907" +======= + ErrInvalidChartURICode = "kanvas-snapshot-900" + ErrCreatingMesheryDesignCode = "kanvas-snapshot-901" + ErrGeneratingSnapshotCode = "kanvas-snapshot-902" + ErrHTTPPostRequestCode = "kanvas-snapshot-903" + ErrDecodingAPICode = "kanvas-snapshot-905" + ErrUnexpectedResponseCodeCode = "kanvas-snapshot-906" + ErrInvalidEmailFormatCode = "kanvas-snapshot-907" +>>>>>>> Stashed changes ) func ErrInvalidChartURI(err error) error { @@ -70,11 +80,20 @@ func ErrUnexpectedResponseCode(statusCode int, body string) error { ) } +<<<<<<< Updated upstream func ErrRequiredFieldNotProvided(err error, field string) error { return errors.New(ErrRequiredFieldNotProvidedCode, errors.Alert, []string{"All required flags are not passed."}, []string{err.Error()}, []string{"Required flag \"%s\" is not passed."}, []string{"Ensure value for flag \"%s\" is correctly provided."}, +======= +func ErrInvalidEmailFormat(email string) error { + return errors.New(ErrInvalidEmailFormatCode, errors.Alert, + []string{"Invalid email format provided."}, + []string{fmt.Sprintf("The provided email '%s' is not a valid email format.", email)}, + []string{"The email provided for the Kanvas snapshot request is not in the correct format."}, + []string{"Ensure the email address follows the correct format (e.g., user@example.com)."}, +>>>>>>> Stashed changes ) } From 598f120200e2a0fbfe67dc2e18236e2299438a4c Mon Sep 17 00:00:00 2001 From: Eti Ijeoma Date: Fri, 11 Oct 2024 16:26:33 +0100 Subject: [PATCH 2/3] pull latest changes Signed-off-by: Eti Ijeoma --- internal/errors/error.go | 20 +------------------- 1 file changed, 1 insertion(+), 19 deletions(-) diff --git a/internal/errors/error.go b/internal/errors/error.go index 2d313b2e79..bd0cfd93e3 100644 --- a/internal/errors/error.go +++ b/internal/errors/error.go @@ -7,7 +7,6 @@ import ( ) var ( -<<<<<<< Updated upstream ErrInvalidChartURICode = "kanvas-snapshot-900" ErrCreatingMesheryDesignCode = "kanvas-snapshot-901" ErrGeneratingSnapshotCode = "kanvas-snapshot-902" @@ -15,15 +14,7 @@ var ( ErrDecodingAPICode = "kanvas-snapshot-905" ErrUnexpectedResponseCodeCode = "kanvas-snapshot-906" ErrRequiredFieldNotProvidedCode = "kanvas-snapshot-907" -======= - ErrInvalidChartURICode = "kanvas-snapshot-900" - ErrCreatingMesheryDesignCode = "kanvas-snapshot-901" - ErrGeneratingSnapshotCode = "kanvas-snapshot-902" - ErrHTTPPostRequestCode = "kanvas-snapshot-903" - ErrDecodingAPICode = "kanvas-snapshot-905" - ErrUnexpectedResponseCodeCode = "kanvas-snapshot-906" - ErrInvalidEmailFormatCode = "kanvas-snapshot-907" ->>>>>>> Stashed changes + ErrInvalidEmailFormatCode = "kanvas-snapshot-908" ) func ErrInvalidChartURI(err error) error { @@ -80,20 +71,11 @@ func ErrUnexpectedResponseCode(statusCode int, body string) error { ) } -<<<<<<< Updated upstream -func ErrRequiredFieldNotProvided(err error, field string) error { - return errors.New(ErrRequiredFieldNotProvidedCode, errors.Alert, - []string{"All required flags are not passed."}, - []string{err.Error()}, - []string{"Required flag \"%s\" is not passed."}, - []string{"Ensure value for flag \"%s\" is correctly provided."}, -======= func ErrInvalidEmailFormat(email string) error { return errors.New(ErrInvalidEmailFormatCode, errors.Alert, []string{"Invalid email format provided."}, []string{fmt.Sprintf("The provided email '%s' is not a valid email format.", email)}, []string{"The email provided for the Kanvas snapshot request is not in the correct format."}, []string{"Ensure the email address follows the correct format (e.g., user@example.com)."}, ->>>>>>> Stashed changes ) } From a617ad4152d37564fba34ffe34625dc9de016486 Mon Sep 17 00:00:00 2001 From: Eti Ijeoma Date: Fri, 11 Oct 2024 16:34:05 +0100 Subject: [PATCH 3/3] pull latest changes Signed-off-by: Eti Ijeoma --- cmd/kanvas-snapshot/main.go | 4 +++- internal/errors/error.go | 9 +++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/cmd/kanvas-snapshot/main.go b/cmd/kanvas-snapshot/main.go index 9fe592ce48..5c72ec80ad 100644 --- a/cmd/kanvas-snapshot/main.go +++ b/cmd/kanvas-snapshot/main.go @@ -57,7 +57,9 @@ var generateKanvasSnapshotCmd = &cobra.Command{ designName = ExtractNameFromURI(chartURI) Log.Warnf("No design name provided. Using extracted name: %s", designName) } - + if email != "" && !isValidEmail(email) { + handleError(errors.ErrInvalidEmailFormat(email)) + } // Create Meshery Snapshot designID, err := CreateMesheryDesign(chartURI, designName, email) if err != nil { diff --git a/internal/errors/error.go b/internal/errors/error.go index bd0cfd93e3..19cbba28bb 100644 --- a/internal/errors/error.go +++ b/internal/errors/error.go @@ -71,6 +71,15 @@ func ErrUnexpectedResponseCode(statusCode int, body string) error { ) } +func ErrRequiredFieldNotProvided(err error, field string) error { + return errors.New(ErrRequiredFieldNotProvidedCode, errors.Alert, + []string{"All required flags are not passed."}, + []string{err.Error()}, + []string{"Required flag \"%s\" is not passed."}, + []string{"Ensure value for flag \"%s\" is correctly provided."}, + ) +} + func ErrInvalidEmailFormat(email string) error { return errors.New(ErrInvalidEmailFormatCode, errors.Alert, []string{"Invalid email format provided."},