-
Notifications
You must be signed in to change notification settings - Fork 2
/
resource_quantum_password_test.go
82 lines (73 loc) · 1.94 KB
/
resource_quantum_password_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"fmt"
"regexp"
"testing"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"golang.org/x/crypto/bcrypt"
)
func testQuantumPasswordBasic(t *testing.T) {
passwordRegex12 := regexp.MustCompile(`^.{12}$`)
passwordRegex10 := regexp.MustCompile(`^.{10}$`)
bcryptRegex := regexp.MustCompile(`^\$2[ayb]\$.{56}$`)
resource.Test(t, resource.TestCase{
Steps: []resource.TestStep{
{
Config: testAccQuantumPasswordResource(12),
Check: resource.ComposeTestCheckFunc(
resource.TestMatchResourceAttr(
"quantum_password.test",
"password",
passwordRegex12,
),
resource.TestMatchResourceAttr(
"quantum_password.test",
"bcrypt",
bcryptRegex,
),
testAccQuantumPasswordBcrypt("quantum_password.test"),
),
},
// Force a rotation of password
{
Config: testAccQuantumPasswordResource(10),
Check: resource.ComposeTestCheckFunc(
resource.TestMatchResourceAttr(
"quantum_password.test",
"password",
passwordRegex10,
),
resource.TestMatchResourceAttr(
"quantum_password.test",
"bcrypt",
bcryptRegex,
),
testAccQuantumPasswordBcrypt("quantum_password.test"),
),
},
},
})
}
func testAccQuantumPasswordResource(length int) string {
return fmt.Sprintf(`
resource "quantum_password" "test" {
special_chars = ""
length = %d
}
`, length)
}
func testAccQuantumPasswordBcrypt(resourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
// retrieve the resource by name from state
rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("Not found: %s", resourceName)
}
err := bcrypt.CompareHashAndPassword([]byte(rs.Primary.Attributes["bcrypt"]), []byte(rs.Primary.Attributes["password"]))
if err != nil {
return fmt.Errorf("Bcrypt does not match: %s (%s)", resourceName, err)
}
return nil
}
}