Skip to content

Commit

Permalink
Added complexity check for accessKeys before making RPC call
Browse files Browse the repository at this point in the history
Signed-off-by: Aayush Chouhan <[email protected]>
  • Loading branch information
achouhan09 committed Apr 29, 2024
1 parent 816aedf commit 1b749bc
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions pkg/noobaaaccount/noobaaaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package noobaaaccount
import (
"context"
"fmt"
"regexp"
"time"

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

// validating access_keys complexity
ValidateAccessKeys(accessKeys)

if !util.KubeCheck(noobaaAccount) && (name != "[email protected]") {
err := UpdateNonCrdAccountKeys(name, accessKeys)
if err != nil {
Expand Down Expand Up @@ -852,6 +856,59 @@ func UpdateNonCrdAccountKeys(name string, accessKeys nb.S3AccessKeys) error {
return nil
}

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

// Checking complexity for access_key
if !ValidateComplexity(accessKeys.AccessKey, 20, true, false, true, false) {
log.Fatalf(`❌ Account access key length must be 20, and must contain uppercase and numbers`)
}

// Checking complexity for secret_key
if !ValidateComplexity(accessKeys.SecretKey, 40, true, true, true, true) {
log.Fatalf(`❌ Account secret length must be 40, and must contain uppercase, lowercase, numbers and symbols`)
}
}

// ValidateComplexity checks for complexity
func ValidateComplexity(str string, length int, upper bool, lower bool, numbers bool, symbols bool) bool {

if len(str) < length {
return false
}

// Check uppercase
if upper {
if matched, _ := regexp.MatchString("[A-Z]", str); !matched {
return false
}
}

// Check lowercase
if lower {
if matched, _ := regexp.MatchString("[a-z]", str); !matched {
return false
}
}

// Check numbers
if numbers {
if matched, _ := regexp.MatchString("[0-9]", str); !matched {
return false
}
}

// Check symbols
if symbols {
if matched, _ := regexp.MatchString(`[^a-zA-Z0-9]`, str); !matched {
return false
}
}

return true
}

// ResetPassword reset noobaa account password
func ResetPassword(name string, oldPassword string, newPassword string, retypeNewPassword string) error {
sysClient, err := system.Connect(true)
Expand Down

0 comments on commit 1b749bc

Please sign in to comment.