diff --git a/go.mod b/go.mod index dd715912..6ae85359 100644 --- a/go.mod +++ b/go.mod @@ -54,6 +54,7 @@ require ( github.com/spf13/pflag v1.0.5 // indirect go.uber.org/multierr v1.11.0 // indirect go.uber.org/zap v1.27.0 // indirect + golang.org/x/crypto v0.23.0 golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 // indirect golang.org/x/net v0.25.0 // indirect golang.org/x/oauth2 v0.18.0 // indirect diff --git a/go.sum b/go.sum index 3833f715..4596bc26 100644 --- a/go.sum +++ b/go.sum @@ -126,6 +126,8 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI= +golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8= golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 h1:aAcj0Da7eBAtrTp03QXWvm88pSyOt+UgdZw2BFZ+lEw= golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8/go.mod h1:CQ1k9gNrJ50XIzaKCRR2hssIjF07kZFEiieALBM/ARQ= golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= diff --git a/pkg/common/password/manager.go b/pkg/common/password/manager.go index 214b4eeb..b6ad2985 100644 --- a/pkg/common/password/manager.go +++ b/pkg/common/password/manager.go @@ -3,8 +3,9 @@ package password import ( "crypto/rand" "encoding/hex" - "os/exec" - "strings" + "fmt" + + "golang.org/x/crypto/bcrypt" ) type Manager interface { @@ -24,10 +25,10 @@ func (m SimpleManager) GeneratePassword(length int) (string, error) { } func (m SimpleManager) GenerateHtpasswd(username string, password string) (string, error) { - htpasswd, err := exec.Command("htpasswd", "-bn", username, password).Output() + encryptedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) if err != nil { return "", err } - formattedHtpasswd := strings.TrimSpace(string(htpasswd)) + formattedHtpasswd := fmt.Sprintf("%s:%s", username, string(encryptedPassword)) return formattedHtpasswd, nil }