2020-09-18 21:10:54 +00:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
log "github.com/hashicorp/go-hclog"
|
2020-10-15 19:20:12 +00:00
|
|
|
v5 "github.com/hashicorp/vault/sdk/database/dbplugin/v5"
|
2020-09-18 21:10:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const mockV5Type = "mockv5"
|
|
|
|
|
|
|
|
// MockDatabaseV5 is an implementation of Database interface
|
|
|
|
type MockDatabaseV5 struct {
|
|
|
|
config map[string]interface{}
|
|
|
|
}
|
|
|
|
|
2020-10-15 19:20:12 +00:00
|
|
|
var _ v5.Database = &MockDatabaseV5{}
|
2020-09-18 21:10:54 +00:00
|
|
|
|
|
|
|
// New returns a new in-memory instance
|
|
|
|
func New() (interface{}, error) {
|
|
|
|
db := MockDatabaseV5{}
|
|
|
|
return db, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run instantiates a MongoDB object, and runs the RPC server for the plugin
|
2020-10-22 21:43:19 +00:00
|
|
|
func RunV5() error {
|
2020-09-18 21:10:54 +00:00
|
|
|
dbType, err := New()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-22 21:43:19 +00:00
|
|
|
v5.Serve(dbType.(v5.Database))
|
2020-09-18 21:10:54 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-03 14:40:46 +00:00
|
|
|
// Run instantiates a MongoDB object, and runs the RPC server for the plugin
|
|
|
|
func RunV6Multiplexed() error {
|
|
|
|
v5.ServeMultiplex(New)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-15 19:20:12 +00:00
|
|
|
func (m MockDatabaseV5) Initialize(ctx context.Context, req v5.InitializeRequest) (v5.InitializeResponse, error) {
|
2020-09-18 21:10:54 +00:00
|
|
|
log.Default().Info("Initialize called",
|
|
|
|
"req", req)
|
|
|
|
|
|
|
|
config := req.Config
|
|
|
|
config["from-plugin"] = "this value is from the plugin itself"
|
|
|
|
|
2020-10-15 19:20:12 +00:00
|
|
|
resp := v5.InitializeResponse{
|
2020-09-18 21:10:54 +00:00
|
|
|
Config: req.Config,
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2020-10-15 19:20:12 +00:00
|
|
|
func (m MockDatabaseV5) NewUser(ctx context.Context, req v5.NewUserRequest) (v5.NewUserResponse, error) {
|
2020-09-18 21:10:54 +00:00
|
|
|
log.Default().Info("NewUser called",
|
|
|
|
"req", req)
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
user := fmt.Sprintf("mockv5_user_%s", now.Format(time.RFC3339))
|
2020-10-15 19:20:12 +00:00
|
|
|
resp := v5.NewUserResponse{
|
2020-09-18 21:10:54 +00:00
|
|
|
Username: user,
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2020-10-15 19:20:12 +00:00
|
|
|
func (m MockDatabaseV5) UpdateUser(ctx context.Context, req v5.UpdateUserRequest) (v5.UpdateUserResponse, error) {
|
2020-09-18 21:10:54 +00:00
|
|
|
log.Default().Info("UpdateUser called",
|
|
|
|
"req", req)
|
2020-10-15 19:20:12 +00:00
|
|
|
return v5.UpdateUserResponse{}, nil
|
2020-09-18 21:10:54 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 19:20:12 +00:00
|
|
|
func (m MockDatabaseV5) DeleteUser(ctx context.Context, req v5.DeleteUserRequest) (v5.DeleteUserResponse, error) {
|
2020-09-18 21:10:54 +00:00
|
|
|
log.Default().Info("DeleteUser called",
|
|
|
|
"req", req)
|
2020-10-15 19:20:12 +00:00
|
|
|
return v5.DeleteUserResponse{}, nil
|
2020-09-18 21:10:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m MockDatabaseV5) Type() (string, error) {
|
|
|
|
log.Default().Info("Type called")
|
|
|
|
return mockV5Type, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m MockDatabaseV5) Close() error {
|
|
|
|
log.Default().Info("Close called")
|
|
|
|
return nil
|
|
|
|
}
|