Skip to content

Commit

Permalink
support password_history and password_reuse_interval take effect sepa…
Browse files Browse the repository at this point in the history
…rately(#2.0-dev) (#19950)

support password_history and password_reuse_interval take effect separately

Approved by: @daviszhen, @heni02, @sukki37
  • Loading branch information
YANGGMM authored Nov 11, 2024
1 parent 04529f1 commit 14644a5
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 24 deletions.
22 changes: 12 additions & 10 deletions pkg/frontend/password_management.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ func whetherSavePasswordHistory(ses *Session) (bool, error) {
return false, err
}

return passwordReuseInfo.PasswordHisoty > 0 && passwordReuseInfo.PasswordReuseInterval > 0, nil
return passwordReuseInfo.PasswordHisoty > 0 || passwordReuseInfo.PasswordReuseInterval > 0, nil
}

func generateSinglePasswordRecod(pwd string) ([]byte, error) {
Expand Down Expand Up @@ -426,7 +426,7 @@ func checkPasswordIntervalRule(ctx context.Context, reuseInfo *passwordReuseInfo
return false, err
}

if passwordTime.AddDate(0, 0, int(reuseInfo.PasswordReuseInterval)).After(time.Now()) {
if passwordTime.AddDate(0, 0, int(reuseInfo.PasswordReuseInterval)).After(time.Now().UTC()) {
return false, moerr.NewInvalidInputf(ctx, "The password has been used before, please change another one")
}
}
Expand Down Expand Up @@ -501,7 +501,7 @@ func checkPasswordReusePolicy(ctx context.Context, ses *Session, bh BackgroundEx
return err
}

if reuseInfo.PasswordHisoty <= 0 || reuseInfo.PasswordReuseInterval <= 0 {
if reuseInfo.PasswordHisoty <= 0 && reuseInfo.PasswordReuseInterval <= 0 {
return nil
}

Expand All @@ -525,13 +525,7 @@ func checkPasswordReusePolicy(ctx context.Context, ses *Session, bh BackgroundEx
if canDeleteNum > 0 {
for i := 0; i < int(canDeleteNum); i++ {
// if password time exceeds the password history, delete the password record
var passwordTime time.Time
passwordTime, err = time.ParseInLocation("2006-01-02 15:04:05", userPasswords[i].PasswordTimestamp, time.UTC)
if err != nil {
return err
}

if passwordTime.AddDate(0, 0, int(reuseInfo.PasswordHisoty)).Before(time.Now()) {
if passwordIntervalExpired(userPasswords[i].PasswordTimestamp, reuseInfo.PasswordReuseInterval) {
deleteNum++
} else {
break
Expand Down Expand Up @@ -673,3 +667,11 @@ func checkValidIpInInvitedNodes(ctx context.Context, invitedNodes string, ip str
}
return moerr.NewInvalidInputf(ctx, "IP %s is not in the invited nodes", ip)
}

func passwordIntervalExpired(timeStr string, interVal int64) bool {
passwordTime, err := time.ParseInLocation("2006-01-02 15:04:05", timeStr, time.UTC)
if err != nil {
return false
}
return passwordTime.AddDate(0, 0, int(interVal)).Before(time.Now().UTC())
}
20 changes: 20 additions & 0 deletions pkg/frontend/password_management_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,3 +306,23 @@ func TestCheckValidIpInInvitedNodes(t *testing.T) {
}
}
}

func TestPasswordTimeIntervalCheck(t *testing.T) {
tests := []struct {
timeStr string
interval int64
want bool
}{
{"2024-11-11 06:45:56", 0, true},
{"2024-11-11 06:46:01", 0, true},
{"2024-11-11 06:48:30", 0, true},
}

for _, tt := range tests {
t.Run(tt.timeStr, func(t *testing.T) {
if got := passwordIntervalExpired(tt.timeStr, tt.interval); got != tt.want {
t.Errorf("passwordTimeIntervalCheck(%v) = %v, want %v", tt.timeStr, got, tt.want)
}
})
}
}
Loading

0 comments on commit 14644a5

Please sign in to comment.