2016-05-13 20:42:09 +00:00
|
|
|
package mongodb
|
|
|
|
|
|
|
|
import (
|
2018-01-08 18:31:38 +00:00
|
|
|
"context"
|
2016-05-13 20:42:09 +00:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2016-07-05 13:32:38 +00:00
|
|
|
"strings"
|
|
|
|
"sync"
|
2016-05-13 20:42:09 +00:00
|
|
|
"testing"
|
|
|
|
|
2019-04-12 21:54:35 +00:00
|
|
|
logicaltest "github.com/hashicorp/vault/helper/testhelpers/logical"
|
2019-04-22 16:26:10 +00:00
|
|
|
"github.com/hashicorp/vault/helper/testhelpers/mongodb"
|
2019-04-13 07:44:06 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2016-05-13 20:42:09 +00:00
|
|
|
"github.com/mitchellh/mapstructure"
|
2016-07-05 13:32:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
testImagePull sync.Once
|
2016-05-13 20:42:09 +00:00
|
|
|
)
|
|
|
|
|
2016-07-05 13:32:38 +00:00
|
|
|
func TestBackend_config_connection(t *testing.T) {
|
|
|
|
var resp *logical.Response
|
|
|
|
var err error
|
|
|
|
config := logical.TestBackendConfig()
|
|
|
|
config.StorageView = &logical.InmemStorage{}
|
2018-01-19 06:44:44 +00:00
|
|
|
b, err := Factory(context.Background(), config)
|
2016-07-05 13:32:38 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
configData := map[string]interface{}{
|
|
|
|
"uri": "sample_connection_uri",
|
|
|
|
"verify_connection": false,
|
|
|
|
}
|
|
|
|
|
|
|
|
configReq := &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "config/connection",
|
|
|
|
Storage: config.StorageView,
|
|
|
|
Data: configData,
|
|
|
|
}
|
2018-01-08 18:31:38 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), configReq)
|
2016-07-05 13:32:38 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%s resp:%#v\n", err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
configReq.Operation = logical.ReadOperation
|
2018-01-08 18:31:38 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), configReq)
|
2016-07-05 13:32:38 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%s resp:%#v\n", err, resp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-13 20:42:09 +00:00
|
|
|
func TestBackend_basic(t *testing.T) {
|
2016-07-05 13:32:38 +00:00
|
|
|
config := logical.TestBackendConfig()
|
|
|
|
config.StorageView = &logical.InmemStorage{}
|
2018-01-19 06:44:44 +00:00
|
|
|
b, err := Factory(context.Background(), config)
|
2016-07-05 13:32:38 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-04-22 16:26:10 +00:00
|
|
|
cleanup, connURI := mongodb.PrepareTestContainer(t, "latest")
|
|
|
|
defer cleanup()
|
2016-07-05 13:32:38 +00:00
|
|
|
connData := map[string]interface{}{
|
|
|
|
"uri": connURI,
|
|
|
|
}
|
2016-05-13 20:42:09 +00:00
|
|
|
|
|
|
|
logicaltest.Test(t, logicaltest.TestCase{
|
2018-11-07 01:21:24 +00:00
|
|
|
LogicalBackend: b,
|
2016-05-13 20:42:09 +00:00
|
|
|
Steps: []logicaltest.TestStep{
|
2016-07-19 16:26:23 +00:00
|
|
|
testAccStepConfig(connData, false),
|
|
|
|
testAccStepRole(),
|
|
|
|
testAccStepReadCreds("web"),
|
2016-05-13 20:42:09 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBackend_roleCrud(t *testing.T) {
|
2016-07-05 13:32:38 +00:00
|
|
|
config := logical.TestBackendConfig()
|
|
|
|
config.StorageView = &logical.InmemStorage{}
|
2018-01-19 06:44:44 +00:00
|
|
|
b, err := Factory(context.Background(), config)
|
2016-07-05 13:32:38 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-04-22 16:26:10 +00:00
|
|
|
cleanup, connURI := mongodb.PrepareTestContainer(t, "latest")
|
|
|
|
defer cleanup()
|
2016-07-05 13:32:38 +00:00
|
|
|
connData := map[string]interface{}{
|
|
|
|
"uri": connURI,
|
|
|
|
}
|
2016-05-13 20:42:09 +00:00
|
|
|
|
|
|
|
logicaltest.Test(t, logicaltest.TestCase{
|
2018-11-07 01:21:24 +00:00
|
|
|
LogicalBackend: b,
|
2016-05-13 20:42:09 +00:00
|
|
|
Steps: []logicaltest.TestStep{
|
2016-07-19 16:26:23 +00:00
|
|
|
testAccStepConfig(connData, false),
|
|
|
|
testAccStepRole(),
|
|
|
|
testAccStepReadRole("web", testDb, testMongoDBRoles),
|
|
|
|
testAccStepDeleteRole("web"),
|
|
|
|
testAccStepReadRole("web", "", ""),
|
2016-05-13 20:42:09 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBackend_leaseWriteRead(t *testing.T) {
|
2016-07-05 13:32:38 +00:00
|
|
|
config := logical.TestBackendConfig()
|
|
|
|
config.StorageView = &logical.InmemStorage{}
|
2018-01-19 06:44:44 +00:00
|
|
|
b, err := Factory(context.Background(), config)
|
2016-07-05 13:32:38 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-04-22 16:26:10 +00:00
|
|
|
cleanup, connURI := mongodb.PrepareTestContainer(t, "latest")
|
|
|
|
defer cleanup()
|
2016-07-05 13:32:38 +00:00
|
|
|
connData := map[string]interface{}{
|
|
|
|
"uri": connURI,
|
|
|
|
}
|
2016-05-13 20:42:09 +00:00
|
|
|
|
|
|
|
logicaltest.Test(t, logicaltest.TestCase{
|
2018-11-07 01:21:24 +00:00
|
|
|
LogicalBackend: b,
|
2016-05-13 20:42:09 +00:00
|
|
|
Steps: []logicaltest.TestStep{
|
2016-07-19 16:26:23 +00:00
|
|
|
testAccStepConfig(connData, false),
|
|
|
|
testAccStepWriteLease(),
|
|
|
|
testAccStepReadLease(),
|
2016-05-13 20:42:09 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-07-19 16:26:23 +00:00
|
|
|
func testAccStepConfig(d map[string]interface{}, expectError bool) logicaltest.TestStep {
|
2016-05-13 20:42:09 +00:00
|
|
|
return logicaltest.TestStep{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "config/connection",
|
2016-07-05 13:32:38 +00:00
|
|
|
Data: d,
|
|
|
|
ErrorOk: true,
|
|
|
|
Check: func(resp *logical.Response) error {
|
|
|
|
if expectError {
|
|
|
|
if resp.Data == nil {
|
|
|
|
return fmt.Errorf("data is nil")
|
|
|
|
}
|
|
|
|
var e struct {
|
|
|
|
Error string `mapstructure:"error"`
|
|
|
|
}
|
|
|
|
if err := mapstructure.Decode(resp.Data, &e); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(e.Error) == 0 {
|
2018-04-09 18:35:21 +00:00
|
|
|
return fmt.Errorf("expected error, but write succeeded")
|
2016-07-05 13:32:38 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
} else if resp != nil && resp.IsError() {
|
|
|
|
return fmt.Errorf("got an error response: %v", resp.Error())
|
|
|
|
}
|
|
|
|
return nil
|
2016-05-13 20:42:09 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-19 16:26:23 +00:00
|
|
|
func testAccStepRole() logicaltest.TestStep {
|
2016-05-13 20:42:09 +00:00
|
|
|
return logicaltest.TestStep{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "roles/web",
|
|
|
|
Data: map[string]interface{}{
|
2016-07-08 03:16:11 +00:00
|
|
|
"db": testDb,
|
2016-05-13 20:42:09 +00:00
|
|
|
"roles": testMongoDBRoles,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-19 16:26:23 +00:00
|
|
|
func testAccStepDeleteRole(n string) logicaltest.TestStep {
|
2016-05-13 20:42:09 +00:00
|
|
|
return logicaltest.TestStep{
|
|
|
|
Operation: logical.DeleteOperation,
|
|
|
|
Path: "roles/" + n,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-19 16:26:23 +00:00
|
|
|
func testAccStepReadCreds(name string) logicaltest.TestStep {
|
2016-05-13 20:42:09 +00:00
|
|
|
return logicaltest.TestStep{
|
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: "creds/" + name,
|
|
|
|
Check: func(resp *logical.Response) error {
|
|
|
|
var d struct {
|
|
|
|
DB string `mapstructure:"db"`
|
|
|
|
Username string `mapstructure:"username"`
|
|
|
|
Password string `mapstructure:"password"`
|
|
|
|
}
|
|
|
|
if err := mapstructure.Decode(resp.Data, &d); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.DB == "" {
|
|
|
|
return fmt.Errorf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
if d.Username == "" {
|
|
|
|
return fmt.Errorf("bad: %#v", resp)
|
|
|
|
}
|
2016-07-01 01:11:45 +00:00
|
|
|
if !strings.HasPrefix(d.Username, "vault-root-") {
|
|
|
|
return fmt.Errorf("bad: %#v", resp)
|
|
|
|
}
|
2016-05-13 20:42:09 +00:00
|
|
|
if d.Password == "" {
|
|
|
|
return fmt.Errorf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[WARN] Generated credentials: %v", d)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-19 16:26:23 +00:00
|
|
|
func testAccStepReadRole(name, db, mongoDBRoles string) logicaltest.TestStep {
|
2016-05-13 20:42:09 +00:00
|
|
|
return logicaltest.TestStep{
|
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: "roles/" + name,
|
|
|
|
Check: func(resp *logical.Response) error {
|
|
|
|
if resp == nil {
|
|
|
|
if db == "" && mongoDBRoles == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
var d struct {
|
|
|
|
DB string `mapstructure:"db"`
|
|
|
|
MongoDBRoles string `mapstructure:"roles"`
|
|
|
|
}
|
|
|
|
if err := mapstructure.Decode(resp.Data, &d); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.DB != db {
|
|
|
|
return fmt.Errorf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
if d.MongoDBRoles != mongoDBRoles {
|
|
|
|
return fmt.Errorf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-19 16:26:23 +00:00
|
|
|
func testAccStepWriteLease() logicaltest.TestStep {
|
2016-05-13 20:42:09 +00:00
|
|
|
return logicaltest.TestStep{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "config/lease",
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"ttl": "1h5m",
|
2016-06-30 13:57:43 +00:00
|
|
|
"max_ttl": "24h",
|
2016-05-13 20:42:09 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-19 16:26:23 +00:00
|
|
|
func testAccStepReadLease() logicaltest.TestStep {
|
2016-05-13 20:42:09 +00:00
|
|
|
return logicaltest.TestStep{
|
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: "config/lease",
|
|
|
|
Check: func(resp *logical.Response) error {
|
2016-07-19 16:26:23 +00:00
|
|
|
if resp.Data["ttl"].(float64) != 3900 || resp.Data["max_ttl"].(float64) != 86400 {
|
2016-05-13 20:42:09 +00:00
|
|
|
return fmt.Errorf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const testDb = "foo"
|
2016-07-10 01:12:42 +00:00
|
|
|
const testMongoDBRoles = `["readWrite",{"role":"read","db":"bar"}]`
|