Skip to content

Commit

Permalink
Fixed regexp to compile once and removed complexity term
Browse files Browse the repository at this point in the history
Signed-off-by: Aayush Chouhan <[email protected]>
  • Loading branch information
achouhan09 committed May 7, 2024
1 parent db9b6c1 commit 2b78cec
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
15 changes: 6 additions & 9 deletions pkg/noobaaaccount/noobaaaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package noobaaaccount
import (
"context"
"fmt"
"regexp"
"time"

nbv1 "github.com/noobaa/noobaa-operator/v5/pkg/apis/noobaa/v1alpha1"
Expand Down Expand Up @@ -418,7 +417,7 @@ func RunCredentials(cmd *cobra.Command, args []string) {
log.Fatalf(`❌ access_key and secret_key flags must be provided`)
}

// validating access_keys complexity
// validating access_keys
ValidateAccessKeys(accessKeys)

if !util.KubeCheck(noobaaAccount) && (name != "[email protected]") {
Expand Down Expand Up @@ -862,19 +861,17 @@ func UpdateNonCrdAccountKeys(name string, accessKeys nb.S3AccessKeys) error {
return nil
}

// ValidateAccessKeys checks for complexity of credentials(access_key and secret_key)
// ValidateAccessKeys checks validity of credentials (access_key and secret_key)
func ValidateAccessKeys(accessKeys nb.S3AccessKeys) {
log := util.Logger()

// Checking complexity for access_key
regex := regexp.MustCompile(util.AccessKeyRegexp)
if !regex.MatchString(accessKeys.AccessKey) {
// validations for access_key
if !util.AccessKeyRegexp.MatchString(accessKeys.AccessKey) {
log.Fatalf(`❌ Account access key length must be 20, and must contain only alpha-numeric chars`)
}

// Checking complexity for secret_key
regex = regexp.MustCompile(util.SecretKeyRegexp)
if !regex.MatchString(accessKeys.SecretKey) {
// validations for secret_key
if !util.SecretKeyRegexp.MatchString(accessKeys.SecretKey) {
log.Fatalf(`❌ Account secret length must be 40, and must contain only alpha-numeric chars, "+", "/"`)
}
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ type ValidationError struct {
Msg string
}

// AccessKeyRegex validates access keys, which are 20 characters long and may include alphanumeric characters
const AccessKeyRegexp = `^[a-zA-Z0-9]{20}$`
// AccessKeyRegexp validates access keys, which are 20 characters long and may include alphanumeric characters
var AccessKeyRegexp, _ = regexp.Compile(`^[a-zA-Z0-9]{20}$`)

// SecretKeyRegexp validates secret keys, which are 40 characters long and may include alphanumeric characters '+' and '/'
const SecretKeyRegexp = `^[a-zA-Z0-9+/]{40}$`
var SecretKeyRegexp, _ = regexp.Compile(`^[a-zA-Z0-9+/]{40}$`)

// IsValidationError check if err is of type ValidationError
func IsValidationError(err error) bool {
Expand Down

0 comments on commit 2b78cec

Please sign in to comment.