2015-04-19 21:59:30 +00:00
|
|
|
package userpass
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-04-09 22:28:30 +00:00
|
|
|
"reflect"
|
2015-04-19 21:59:30 +00:00
|
|
|
"testing"
|
2015-09-17 20:33:26 +00:00
|
|
|
"time"
|
2015-04-19 21:59:30 +00:00
|
|
|
|
2017-09-13 15:36:52 +00:00
|
|
|
"github.com/hashicorp/vault/helper/policyutil"
|
2015-04-19 21:59:30 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
logicaltest "github.com/hashicorp/vault/logical/testing"
|
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
)
|
|
|
|
|
2016-01-30 01:14:14 +00:00
|
|
|
const (
|
|
|
|
testSysTTL = time.Hour * 10
|
|
|
|
testSysMaxTTL = time.Hour * 20
|
|
|
|
)
|
|
|
|
|
2015-09-17 20:33:26 +00:00
|
|
|
func TestBackend_TTLDurations(t *testing.T) {
|
|
|
|
data1 := map[string]interface{}{
|
|
|
|
"password": "password",
|
|
|
|
"policies": "root",
|
|
|
|
"ttl": "21h",
|
|
|
|
"max_ttl": "11h",
|
|
|
|
}
|
|
|
|
data2 := map[string]interface{}{
|
|
|
|
"password": "password",
|
|
|
|
"policies": "root",
|
|
|
|
"ttl": "10h",
|
|
|
|
"max_ttl": "21h",
|
|
|
|
}
|
|
|
|
data3 := map[string]interface{}{
|
|
|
|
"password": "password",
|
|
|
|
"policies": "root",
|
|
|
|
"ttl": "10h",
|
|
|
|
"max_ttl": "10h",
|
|
|
|
}
|
|
|
|
data4 := map[string]interface{}{
|
|
|
|
"password": "password",
|
|
|
|
"policies": "root",
|
|
|
|
"ttl": "11h",
|
|
|
|
"max_ttl": "5h",
|
|
|
|
}
|
|
|
|
data5 := map[string]interface{}{
|
|
|
|
"password": "password",
|
|
|
|
}
|
|
|
|
b, err := Factory(&logical.BackendConfig{
|
|
|
|
Logger: nil,
|
|
|
|
System: &logical.StaticSystemView{
|
2016-01-30 01:14:14 +00:00
|
|
|
DefaultLeaseTTLVal: testSysTTL,
|
|
|
|
MaxLeaseTTLVal: testSysMaxTTL,
|
2015-09-17 20:33:26 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to create backend: %s", err)
|
|
|
|
}
|
|
|
|
logicaltest.Test(t, logicaltest.TestCase{
|
2016-07-01 15:37:38 +00:00
|
|
|
Backend: b,
|
2015-09-17 20:33:26 +00:00
|
|
|
Steps: []logicaltest.TestStep{
|
|
|
|
testUsersWrite(t, "test", data1, true),
|
|
|
|
testUsersWrite(t, "test", data2, true),
|
|
|
|
testUsersWrite(t, "test", data3, false),
|
|
|
|
testUsersWrite(t, "test", data4, false),
|
|
|
|
testLoginWrite(t, "test", data5, false),
|
|
|
|
testLoginWrite(t, "wrong", data5, true),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-04-19 22:06:29 +00:00
|
|
|
func TestBackend_basic(t *testing.T) {
|
2016-01-30 01:14:14 +00:00
|
|
|
b, err := Factory(&logical.BackendConfig{
|
|
|
|
Logger: nil,
|
|
|
|
System: &logical.StaticSystemView{
|
|
|
|
DefaultLeaseTTLVal: testSysTTL,
|
|
|
|
MaxLeaseTTLVal: testSysMaxTTL,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to create backend: %s", err)
|
|
|
|
}
|
2015-04-19 22:06:29 +00:00
|
|
|
logicaltest.Test(t, logicaltest.TestCase{
|
2016-07-01 15:37:38 +00:00
|
|
|
Backend: b,
|
2015-04-19 22:06:29 +00:00
|
|
|
Steps: []logicaltest.TestStep{
|
|
|
|
testAccStepUser(t, "web", "password", "foo"),
|
2016-04-09 22:28:30 +00:00
|
|
|
testAccStepUser(t, "web2", "password", "foo"),
|
|
|
|
testAccStepUser(t, "web3", "password", "foo"),
|
|
|
|
testAccStepList(t, []string{"web", "web2", "web3"}),
|
2016-03-15 20:09:23 +00:00
|
|
|
testAccStepLogin(t, "web", "password", []string{"default", "foo"}),
|
2015-04-19 22:06:29 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-04-19 21:59:30 +00:00
|
|
|
func TestBackend_userCrud(t *testing.T) {
|
2016-01-30 01:14:14 +00:00
|
|
|
b, err := Factory(&logical.BackendConfig{
|
|
|
|
Logger: nil,
|
|
|
|
System: &logical.StaticSystemView{
|
|
|
|
DefaultLeaseTTLVal: testSysTTL,
|
|
|
|
MaxLeaseTTLVal: testSysMaxTTL,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to create backend: %s", err)
|
|
|
|
}
|
2015-04-19 21:59:30 +00:00
|
|
|
|
|
|
|
logicaltest.Test(t, logicaltest.TestCase{
|
2016-07-01 15:37:38 +00:00
|
|
|
Backend: b,
|
2015-04-19 21:59:30 +00:00
|
|
|
Steps: []logicaltest.TestStep{
|
|
|
|
testAccStepUser(t, "web", "password", "foo"),
|
2017-09-13 15:36:52 +00:00
|
|
|
testAccStepReadUser(t, "web", "foo"),
|
2015-04-19 21:59:30 +00:00
|
|
|
testAccStepDeleteUser(t, "web"),
|
|
|
|
testAccStepReadUser(t, "web", ""),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-03-15 20:46:12 +00:00
|
|
|
func TestBackend_userCreateOperation(t *testing.T) {
|
2016-03-15 20:09:23 +00:00
|
|
|
b, err := Factory(&logical.BackendConfig{
|
|
|
|
Logger: nil,
|
|
|
|
System: &logical.StaticSystemView{
|
|
|
|
DefaultLeaseTTLVal: testSysTTL,
|
|
|
|
MaxLeaseTTLVal: testSysMaxTTL,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to create backend: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
logicaltest.Test(t, logicaltest.TestCase{
|
2016-07-01 15:37:38 +00:00
|
|
|
Backend: b,
|
2016-03-15 20:09:23 +00:00
|
|
|
Steps: []logicaltest.TestStep{
|
|
|
|
testUserCreateOperation(t, "web", "password", "foo"),
|
|
|
|
testAccStepLogin(t, "web", "password", []string{"default", "foo"}),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBackend_passwordUpdate(t *testing.T) {
|
|
|
|
b, err := Factory(&logical.BackendConfig{
|
|
|
|
Logger: nil,
|
|
|
|
System: &logical.StaticSystemView{
|
|
|
|
DefaultLeaseTTLVal: testSysTTL,
|
|
|
|
MaxLeaseTTLVal: testSysMaxTTL,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to create backend: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
logicaltest.Test(t, logicaltest.TestCase{
|
2016-07-01 15:37:38 +00:00
|
|
|
Backend: b,
|
2016-03-15 20:09:23 +00:00
|
|
|
Steps: []logicaltest.TestStep{
|
|
|
|
testAccStepUser(t, "web", "password", "foo"),
|
2017-09-13 15:36:52 +00:00
|
|
|
testAccStepReadUser(t, "web", "foo"),
|
2016-03-15 20:09:23 +00:00
|
|
|
testAccStepLogin(t, "web", "password", []string{"default", "foo"}),
|
|
|
|
testUpdatePassword(t, "web", "newpassword"),
|
|
|
|
testAccStepLogin(t, "web", "newpassword", []string{"default", "foo"}),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBackend_policiesUpdate(t *testing.T) {
|
|
|
|
b, err := Factory(&logical.BackendConfig{
|
|
|
|
Logger: nil,
|
|
|
|
System: &logical.StaticSystemView{
|
|
|
|
DefaultLeaseTTLVal: testSysTTL,
|
|
|
|
MaxLeaseTTLVal: testSysMaxTTL,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to create backend: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
logicaltest.Test(t, logicaltest.TestCase{
|
2016-07-01 15:37:38 +00:00
|
|
|
Backend: b,
|
2016-03-15 20:09:23 +00:00
|
|
|
Steps: []logicaltest.TestStep{
|
|
|
|
testAccStepUser(t, "web", "password", "foo"),
|
2017-09-13 15:36:52 +00:00
|
|
|
testAccStepReadUser(t, "web", "foo"),
|
2016-03-15 20:09:23 +00:00
|
|
|
testAccStepLogin(t, "web", "password", []string{"default", "foo"}),
|
|
|
|
testUpdatePolicies(t, "web", "foo,bar"),
|
2017-09-13 15:36:52 +00:00
|
|
|
testAccStepReadUser(t, "web", "bar,foo"),
|
2016-03-15 20:09:23 +00:00
|
|
|
testAccStepLogin(t, "web", "password", []string{"bar", "default", "foo"}),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func testUpdatePassword(t *testing.T, user, password string) logicaltest.TestStep {
|
|
|
|
return logicaltest.TestStep{
|
|
|
|
Operation: logical.UpdateOperation,
|
2016-03-15 20:46:12 +00:00
|
|
|
Path: "users/" + user + "/password",
|
2016-03-15 20:09:23 +00:00
|
|
|
Data: map[string]interface{}{
|
|
|
|
"password": password,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testUpdatePolicies(t *testing.T, user, policies string) logicaltest.TestStep {
|
|
|
|
return logicaltest.TestStep{
|
|
|
|
Operation: logical.UpdateOperation,
|
2016-03-15 20:46:12 +00:00
|
|
|
Path: "users/" + user + "/policies",
|
2016-03-15 20:09:23 +00:00
|
|
|
Data: map[string]interface{}{
|
|
|
|
"policies": policies,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-17 20:33:26 +00:00
|
|
|
func testUsersWrite(t *testing.T, user string, data map[string]interface{}, expectError bool) logicaltest.TestStep {
|
|
|
|
return logicaltest.TestStep{
|
2016-01-07 15:30:47 +00:00
|
|
|
Operation: logical.UpdateOperation,
|
2015-09-17 20:33:26 +00:00
|
|
|
Path: "users/" + user,
|
|
|
|
Data: data,
|
|
|
|
ErrorOk: true,
|
|
|
|
Check: func(resp *logical.Response) error {
|
|
|
|
if resp == nil && expectError {
|
|
|
|
return fmt.Errorf("Expected error but received nil")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testLoginWrite(t *testing.T, user string, data map[string]interface{}, expectError bool) logicaltest.TestStep {
|
|
|
|
return logicaltest.TestStep{
|
2016-01-07 15:30:47 +00:00
|
|
|
Operation: logical.UpdateOperation,
|
2015-09-17 20:33:26 +00:00
|
|
|
Path: "login/" + user,
|
|
|
|
Data: data,
|
|
|
|
ErrorOk: true,
|
|
|
|
Check: func(resp *logical.Response) error {
|
|
|
|
if resp == nil && expectError {
|
|
|
|
return fmt.Errorf("Expected error but received nil")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-09 22:28:30 +00:00
|
|
|
func testAccStepList(t *testing.T, users []string) logicaltest.TestStep {
|
|
|
|
return logicaltest.TestStep{
|
|
|
|
Operation: logical.ListOperation,
|
|
|
|
Path: "users",
|
|
|
|
Check: func(resp *logical.Response) error {
|
|
|
|
if resp.IsError() {
|
|
|
|
return fmt.Errorf("Got error response: %#v", *resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
exp := []string{"web", "web2", "web3"}
|
|
|
|
if !reflect.DeepEqual(exp, resp.Data["keys"].([]string)) {
|
|
|
|
return fmt.Errorf("expected:\n%#v\ngot:\n%#v\n", exp, resp.Data["keys"])
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-15 20:09:23 +00:00
|
|
|
func testAccStepLogin(t *testing.T, user string, pass string, policies []string) logicaltest.TestStep {
|
2015-04-19 22:06:29 +00:00
|
|
|
return logicaltest.TestStep{
|
2016-01-07 15:30:47 +00:00
|
|
|
Operation: logical.UpdateOperation,
|
2015-04-19 22:06:29 +00:00
|
|
|
Path: "login/" + user,
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"password": pass,
|
|
|
|
},
|
|
|
|
Unauthenticated: true,
|
|
|
|
|
2016-03-15 20:09:23 +00:00
|
|
|
Check: logicaltest.TestCheckAuth(policies),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testUserCreateOperation(
|
|
|
|
t *testing.T, name string, password string, policies string) logicaltest.TestStep {
|
|
|
|
return logicaltest.TestStep{
|
|
|
|
Operation: logical.CreateOperation,
|
|
|
|
Path: "users/" + name,
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"password": password,
|
|
|
|
"policies": policies,
|
|
|
|
},
|
2015-04-19 22:06:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-19 21:59:30 +00:00
|
|
|
func testAccStepUser(
|
|
|
|
t *testing.T, name string, password string, policies string) logicaltest.TestStep {
|
|
|
|
return logicaltest.TestStep{
|
2016-01-07 15:30:47 +00:00
|
|
|
Operation: logical.UpdateOperation,
|
2015-04-19 21:59:30 +00:00
|
|
|
Path: "users/" + name,
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"password": password,
|
|
|
|
"policies": policies,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccStepDeleteUser(t *testing.T, n string) logicaltest.TestStep {
|
|
|
|
return logicaltest.TestStep{
|
|
|
|
Operation: logical.DeleteOperation,
|
|
|
|
Path: "users/" + n,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccStepReadUser(t *testing.T, name string, policies string) logicaltest.TestStep {
|
|
|
|
return logicaltest.TestStep{
|
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: "users/" + name,
|
|
|
|
Check: func(resp *logical.Response) error {
|
|
|
|
if resp == nil {
|
|
|
|
if policies == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
var d struct {
|
2017-09-13 15:36:52 +00:00
|
|
|
Policies []string `mapstructure:"policies"`
|
2015-04-19 21:59:30 +00:00
|
|
|
}
|
|
|
|
if err := mapstructure.Decode(resp.Data, &d); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-09-13 15:36:52 +00:00
|
|
|
if !reflect.DeepEqual(d.Policies, policyutil.ParsePolicies(policies)) {
|
2015-04-19 21:59:30 +00:00
|
|
|
return fmt.Errorf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|