Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add random password feature #34

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions internal/provider/resource_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,25 @@ func resourceUser() *schema.Resource {
func resourceUserCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*model.Client4)

email := d.Get("email").(string)
password := d.Get("password").(string)
isRandomPassword := false
if password == "-" {
isRandomPassword = true
password = genPassword(16, 2, 2, 6)
}

user := &model.User{
Username: d.Get("username").(string),
Password: d.Get("password").(string),
Password: password,
AuthService: d.Get("auth_service").(string),
Email: d.Get("email").(string),
Email: email,
Nickname: d.Get("nickname").(string),
FirstName: d.Get("first_name").(string),
LastName: d.Get("last_name").(string),
Locale: d.Get("locale").(string),
}

if authData, ok := d.GetOk("auth_data"); ok {
ad := authData.(string)
user.AuthData = &ad
Expand All @@ -107,6 +116,12 @@ func resourceUserCreate(ctx context.Context, d *schema.ResourceData, meta interf
if err != nil {
return diag.Errorf("cannot create user: %v", fmtErr(resp, err))
}
if isRandomPassword {
resp, err = c.SendPasswordResetEmail(email)
if err != nil {
return diag.Errorf("cannot reset password for user: %v", fmtErr(resp, err))
}
}

d.SetId(user.Id)

Expand Down
51 changes: 51 additions & 0 deletions internal/provider/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ package provider

import (
"fmt"
"math/rand"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should use crypto/rand for password generation. See my suggestion below for an example

"strings"
"time"

"github.com/mattermost/mattermost-server/v6/model"
)

var random *rand.Rand

func fmtErr(resp *model.Response, err error) error {
if resp == nil {
return err
Expand All @@ -22,3 +27,49 @@ func expandStringMap(m map[string]interface{}) map[string]string {

return r
}

func init() {
random = rand.New(rand.NewSource(time.Now().UnixNano()))
}

var passwordChars = struct {
Lower string
Upper string
Digits string
Specials string
}{
"abcdefghijklmnopqrstuvwxyz",
"ABCDEFGHIJKLMNOPQRSTUVWXYZ",
"0123456789",
"!@#$%^&*()-+",
}

func genPassword(size, numSpecials, numDigits, numUpper int) string {
var password strings.Builder

for i := 0; i < numSpecials; i++ {
password.WriteString(
string(passwordChars.Specials[random.Intn(len(passwordChars.Specials))]),
)
Comment on lines +51 to +53
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
password.WriteString(
string(passwordChars.Specials[random.Intn(len(passwordChars.Specials))]),
)
// Using crypto/rand
i, err := rand.Int(rand.Reader, big.NewInt(len(passwordChars.Specials)))
if err != nil {
return "", err
}
password.WriteString(string(passwordChars.Specials[i.Uint64()]))

}
for i := 0; i < numDigits; i++ {
password.WriteString(
string(passwordChars.Digits[random.Intn(len(passwordChars.Digits))]),
)
}
for i := 0; i < numUpper; i++ {
password.WriteString(
string(passwordChars.Upper[random.Intn(len(passwordChars.Upper))]),
)
}
for i := 0; i < size-numSpecials-numDigits-numUpper; i++ {
password.WriteString(
string(passwordChars.Lower[random.Intn(len(passwordChars.Lower))]),
)
}
runes := []rune(password.String())
random.Shuffle(len(runes), func(i, j int) {
runes[i], runes[j] = runes[j], runes[i]
})
return string(runes)
}