Skip to content

Commit

Permalink
Prevent hash error bcrypt.ErrPasswordTooLong for passwords > 72 byt…
Browse files Browse the repository at this point in the history
…es in length (#397)

* Truncating passwords that are longer than 72 bytes (#396)

* Adding changelog entry (#396)

* Updating changelog entry and docs (#396)

* Updating changelog entry (#396)

* updated comment msg
  • Loading branch information
bendbennett authored Apr 12, 2023
1 parent 800bbd5 commit f1c7ffa
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 13 deletions.
6 changes: 6 additions & 0 deletions .changes/unreleased/BUG FIXES-20230412-132501.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: BUG FIXES
body: 'resource/random_password: Prevent error with `bcrypt` by truncating the bytes
that are hashed to a maximum length of 72'
time: 2023-04-12T13:25:01.113462+01:00
custom:
Issue: "397"
2 changes: 1 addition & 1 deletion docs/resources/password.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ resource "aws_db_instance" "example" {

### Read-Only

- `bcrypt_hash` (String, Sensitive) A bcrypt hash of the generated random string.
- `bcrypt_hash` (String, Sensitive) A bcrypt hash of the generated random string. **NOTE**: If the generated random string is greater than 72 bytes in length, `bcrypt_hash` will contain a hash of the first 72 bytes.
- `id` (String) A static value used internally by Terraform, this should not be referenced in configurations.
- `result` (String, Sensitive) The generated random string.

Expand Down
36 changes: 26 additions & 10 deletions internal/provider/resource_password.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,8 +535,18 @@ func upgradePasswordStateV2toV3(ctx context.Context, req resource.UpgradeStateRe
resp.Diagnostics.Append(resp.State.Set(ctx, passwordDataV3)...)
}

// generateHash truncates strings that are longer than 72 bytes in
// order to avoid the error returned from bcrypt.GenerateFromPassword
// in versions v0.5.0 and above: https://pkg.go.dev/golang.org/x/crypto@v0.8.0/bcrypt#GenerateFromPassword
func generateHash(toHash string) (string, error) {
hash, err := bcrypt.GenerateFromPassword([]byte(toHash), bcrypt.DefaultCost)
bytesHash := []byte(toHash)
bytesToHash := bytesHash

if len(bytesHash) > 72 {
bytesToHash = bytesHash[:72]
}

hash, err := bcrypt.GenerateFromPassword(bytesToHash, bcrypt.DefaultCost)

return string(hash), err
}
Expand Down Expand Up @@ -693,9 +703,11 @@ func passwordSchemaV3() schema.Schema {
},

"bcrypt_hash": schema.StringAttribute{
Description: "A bcrypt hash of the generated random string.",
Computed: true,
Sensitive: true,
Description: "A bcrypt hash of the generated random string. " +
"**NOTE**: If the generated random string is greater than 72 bytes in length, " +
"`bcrypt_hash` will contain a hash of the first 72 bytes.",
Computed: true,
Sensitive: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
Expand Down Expand Up @@ -805,9 +817,11 @@ func passwordSchemaV2() schema.Schema {
},

"bcrypt_hash": schema.StringAttribute{
Description: "A bcrypt hash of the generated random string.",
Computed: true,
Sensitive: true,
Description: "A bcrypt hash of the generated random string. " +
"**NOTE**: If the generated random string is greater than 72 bytes in length, " +
"`bcrypt_hash` will contain a hash of the first 72 bytes.",
Computed: true,
Sensitive: true,
},

"id": schema.StringAttribute{
Expand Down Expand Up @@ -903,9 +917,11 @@ func passwordSchemaV1() schema.Schema {
},

"bcrypt_hash": schema.StringAttribute{
Description: "A bcrypt hash of the generated random string.",
Computed: true,
Sensitive: true,
Description: "A bcrypt hash of the generated random string. " +
"**NOTE**: If the generated random string is greater than 72 bytes in length, " +
"`bcrypt_hash` will contain a hash of the first 72 bytes.",
Computed: true,
Sensitive: true,
},

"id": schema.StringAttribute{
Expand Down
4 changes: 2 additions & 2 deletions internal/provider/resource_password_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestGenerateHash(t *testing.T) {
}{
"defaults": {
input: random.StringParams{
Length: 32, // Required
Length: 73, // Required
Lower: true,
Numeric: true,
Special: true,
Expand Down Expand Up @@ -111,7 +111,7 @@ func TestAccResourcePassword_BcryptHash(t *testing.T) {
Steps: []resource.TestStep{
{
Config: `resource "random_password" "test" {
length = 12
length = 73
}`,
Check: resource.ComposeTestCheckFunc(
testExtractResourceAttr("random_password.test", "bcrypt_hash", &bcryptHash),
Expand Down

0 comments on commit f1c7ffa

Please sign in to comment.