2019-01-09 01:26:16 +00:00
|
|
|
package influxdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-09-15 14:01:26 +00:00
|
|
|
"net/url"
|
2019-01-09 01:26:16 +00:00
|
|
|
"os"
|
2020-10-12 21:54:26 +00:00
|
|
|
"reflect"
|
2019-01-09 01:26:16 +00:00
|
|
|
"strconv"
|
2020-09-15 14:01:26 +00:00
|
|
|
"strings"
|
2019-01-09 01:26:16 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-04-22 16:26:10 +00:00
|
|
|
"github.com/hashicorp/vault/helper/testhelpers/docker"
|
2020-10-15 19:20:12 +00:00
|
|
|
dbplugin "github.com/hashicorp/vault/sdk/database/dbplugin/v5"
|
|
|
|
dbtesting "github.com/hashicorp/vault/sdk/database/dbplugin/v5/testing"
|
2019-01-09 01:26:16 +00:00
|
|
|
influx "github.com/influxdata/influxdb/client/v2"
|
2021-06-09 21:08:59 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-01-09 01:26:16 +00:00
|
|
|
)
|
|
|
|
|
2020-10-12 21:54:26 +00:00
|
|
|
const createUserStatements = `CREATE USER "{{username}}" WITH PASSWORD '{{password}}';GRANT ALL ON "vault" TO "{{username}}";`
|
2019-01-09 01:26:16 +00:00
|
|
|
|
2020-09-15 14:01:26 +00:00
|
|
|
type Config struct {
|
|
|
|
docker.ServiceURL
|
|
|
|
Username string
|
|
|
|
Password string
|
|
|
|
}
|
2019-01-09 01:26:16 +00:00
|
|
|
|
2020-09-15 14:01:26 +00:00
|
|
|
var _ docker.ServiceConfig = &Config{}
|
2019-01-09 01:26:16 +00:00
|
|
|
|
2020-09-15 14:01:26 +00:00
|
|
|
func (c *Config) apiConfig() influx.HTTPConfig {
|
|
|
|
return influx.HTTPConfig{
|
|
|
|
Addr: c.URL().String(),
|
|
|
|
Username: c.Username,
|
|
|
|
Password: c.Password,
|
2019-01-09 01:26:16 +00:00
|
|
|
}
|
2020-09-15 14:01:26 +00:00
|
|
|
}
|
2019-01-09 01:26:16 +00:00
|
|
|
|
2020-09-15 14:01:26 +00:00
|
|
|
func (c *Config) connectionParams() map[string]interface{} {
|
|
|
|
pieces := strings.Split(c.Address(), ":")
|
|
|
|
port, _ := strconv.Atoi(pieces[1])
|
|
|
|
return map[string]interface{}{
|
|
|
|
"host": pieces[0],
|
|
|
|
"port": port,
|
|
|
|
"username": c.Username,
|
|
|
|
"password": c.Password,
|
2019-01-09 01:26:16 +00:00
|
|
|
}
|
2020-09-15 14:01:26 +00:00
|
|
|
}
|
2019-01-09 01:26:16 +00:00
|
|
|
|
2020-09-15 14:01:26 +00:00
|
|
|
func prepareInfluxdbTestContainer(t *testing.T) (func(), *Config) {
|
|
|
|
c := &Config{
|
|
|
|
Username: "influx-root",
|
|
|
|
Password: "influx-root",
|
|
|
|
}
|
|
|
|
if host := os.Getenv("INFLUXDB_HOST"); host != "" {
|
|
|
|
c.ServiceURL = *docker.NewServiceURL(url.URL{Scheme: "http", Host: host})
|
|
|
|
return func() {}, c
|
|
|
|
}
|
|
|
|
|
|
|
|
runner, err := docker.NewServiceRunner(docker.RunOptions{
|
|
|
|
ImageRepo: "influxdb",
|
2021-02-25 15:50:14 +00:00
|
|
|
ImageTag: "1.8-alpine",
|
2020-09-15 14:01:26 +00:00
|
|
|
Env: []string{
|
|
|
|
"INFLUXDB_DB=vault",
|
|
|
|
"INFLUXDB_ADMIN_USER=" + c.Username,
|
|
|
|
"INFLUXDB_ADMIN_PASSWORD=" + c.Password,
|
2021-04-08 16:43:39 +00:00
|
|
|
"INFLUXDB_HTTP_AUTH_ENABLED=true",
|
|
|
|
},
|
2020-09-15 14:01:26 +00:00
|
|
|
Ports: []string{"8086/tcp"},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not start docker InfluxDB: %s", err)
|
|
|
|
}
|
|
|
|
svc, err := runner.StartService(context.Background(), func(ctx context.Context, host string, port int) (docker.ServiceConfig, error) {
|
|
|
|
c.ServiceURL = *docker.NewServiceURL(url.URL{
|
|
|
|
Scheme: "http",
|
|
|
|
Host: fmt.Sprintf("%s:%d", host, port),
|
2019-01-09 01:26:16 +00:00
|
|
|
})
|
2020-09-15 14:01:26 +00:00
|
|
|
cli, err := influx.NewHTTPClient(c.apiConfig())
|
2019-01-09 01:26:16 +00:00
|
|
|
if err != nil {
|
2021-05-21 14:22:29 +00:00
|
|
|
return nil, fmt.Errorf("error creating InfluxDB client: %w", err)
|
2019-01-09 01:26:16 +00:00
|
|
|
}
|
|
|
|
defer cli.Close()
|
|
|
|
_, _, err = cli.Ping(1)
|
|
|
|
if err != nil {
|
2021-05-21 14:22:29 +00:00
|
|
|
return nil, fmt.Errorf("error checking cluster status: %w", err)
|
2019-01-09 01:26:16 +00:00
|
|
|
}
|
2020-09-15 14:01:26 +00:00
|
|
|
|
|
|
|
return c, nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not start docker InfluxDB: %s", err)
|
2019-01-09 01:26:16 +00:00
|
|
|
}
|
2020-09-15 14:01:26 +00:00
|
|
|
|
|
|
|
return svc.Cleanup, svc.Config.(*Config)
|
2019-01-09 01:26:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestInfluxdb_Initialize(t *testing.T) {
|
2020-09-15 14:01:26 +00:00
|
|
|
cleanup, config := prepareInfluxdbTestContainer(t)
|
2019-01-09 01:26:16 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
2020-10-12 21:54:26 +00:00
|
|
|
type testCase struct {
|
2020-10-15 19:20:12 +00:00
|
|
|
req dbplugin.InitializeRequest
|
|
|
|
expectedResponse dbplugin.InitializeResponse
|
2020-10-12 21:54:26 +00:00
|
|
|
expectErr bool
|
|
|
|
expectInitialized bool
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := map[string]testCase{
|
|
|
|
"port is an int": {
|
2020-10-15 19:20:12 +00:00
|
|
|
req: dbplugin.InitializeRequest{
|
2020-10-12 21:54:26 +00:00
|
|
|
Config: makeConfig(config.connectionParams()),
|
|
|
|
VerifyConnection: true,
|
|
|
|
},
|
2020-10-15 19:20:12 +00:00
|
|
|
expectedResponse: dbplugin.InitializeResponse{
|
2020-10-12 21:54:26 +00:00
|
|
|
Config: config.connectionParams(),
|
|
|
|
},
|
|
|
|
expectErr: false,
|
|
|
|
expectInitialized: true,
|
|
|
|
},
|
|
|
|
"port is a string": {
|
2020-10-15 19:20:12 +00:00
|
|
|
req: dbplugin.InitializeRequest{
|
2020-10-12 21:54:26 +00:00
|
|
|
Config: makeConfig(config.connectionParams(), "port", strconv.Itoa(config.connectionParams()["port"].(int))),
|
|
|
|
VerifyConnection: true,
|
|
|
|
},
|
2020-10-15 19:20:12 +00:00
|
|
|
expectedResponse: dbplugin.InitializeResponse{
|
2020-10-12 21:54:26 +00:00
|
|
|
Config: makeConfig(config.connectionParams(), "port", strconv.Itoa(config.connectionParams()["port"].(int))),
|
|
|
|
},
|
|
|
|
expectErr: false,
|
|
|
|
expectInitialized: true,
|
|
|
|
},
|
|
|
|
"missing config": {
|
2020-10-15 19:20:12 +00:00
|
|
|
req: dbplugin.InitializeRequest{
|
2020-10-12 21:54:26 +00:00
|
|
|
Config: nil,
|
|
|
|
VerifyConnection: true,
|
|
|
|
},
|
2020-10-15 19:20:12 +00:00
|
|
|
expectedResponse: dbplugin.InitializeResponse{},
|
2020-10-12 21:54:26 +00:00
|
|
|
expectErr: true,
|
|
|
|
expectInitialized: false,
|
|
|
|
},
|
|
|
|
"missing host": {
|
2020-10-15 19:20:12 +00:00
|
|
|
req: dbplugin.InitializeRequest{
|
2020-10-12 21:54:26 +00:00
|
|
|
Config: makeConfig(config.connectionParams(), "host", ""),
|
|
|
|
VerifyConnection: true,
|
|
|
|
},
|
2020-10-15 19:20:12 +00:00
|
|
|
expectedResponse: dbplugin.InitializeResponse{},
|
2020-10-12 21:54:26 +00:00
|
|
|
expectErr: true,
|
|
|
|
expectInitialized: false,
|
|
|
|
},
|
|
|
|
"missing username": {
|
2020-10-15 19:20:12 +00:00
|
|
|
req: dbplugin.InitializeRequest{
|
2020-10-12 21:54:26 +00:00
|
|
|
Config: makeConfig(config.connectionParams(), "username", ""),
|
|
|
|
VerifyConnection: true,
|
|
|
|
},
|
2020-10-15 19:20:12 +00:00
|
|
|
expectedResponse: dbplugin.InitializeResponse{},
|
2020-10-12 21:54:26 +00:00
|
|
|
expectErr: true,
|
|
|
|
expectInitialized: false,
|
|
|
|
},
|
|
|
|
"missing password": {
|
2020-10-15 19:20:12 +00:00
|
|
|
req: dbplugin.InitializeRequest{
|
2020-10-12 21:54:26 +00:00
|
|
|
Config: makeConfig(config.connectionParams(), "password", ""),
|
|
|
|
VerifyConnection: true,
|
|
|
|
},
|
2020-10-15 19:20:12 +00:00
|
|
|
expectedResponse: dbplugin.InitializeResponse{},
|
2020-10-12 21:54:26 +00:00
|
|
|
expectErr: true,
|
|
|
|
expectInitialized: false,
|
|
|
|
},
|
|
|
|
"failed to validate connection": {
|
2020-10-15 19:20:12 +00:00
|
|
|
req: dbplugin.InitializeRequest{
|
2020-10-12 21:54:26 +00:00
|
|
|
// Host exists, but isn't a running instance
|
|
|
|
Config: makeConfig(config.connectionParams(), "host", "foobar://bad_connection"),
|
|
|
|
VerifyConnection: true,
|
|
|
|
},
|
2020-10-15 19:20:12 +00:00
|
|
|
expectedResponse: dbplugin.InitializeResponse{},
|
2020-10-12 21:54:26 +00:00
|
|
|
expectErr: true,
|
|
|
|
expectInitialized: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, test := range tests {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
db := new()
|
|
|
|
defer dbtesting.AssertClose(t, db)
|
|
|
|
|
|
|
|
resp, err := db.Initialize(context.Background(), test.req)
|
|
|
|
if test.expectErr && err == nil {
|
|
|
|
t.Fatalf("err expected, got nil")
|
|
|
|
}
|
|
|
|
if !test.expectErr && err != nil {
|
|
|
|
t.Fatalf("no error expected, got: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(resp, test.expectedResponse) {
|
|
|
|
t.Fatalf("Actual response: %#v\nExpected response: %#v", resp, test.expectedResponse)
|
|
|
|
}
|
|
|
|
|
|
|
|
if test.expectInitialized && !db.Initialized {
|
|
|
|
t.Fatalf("Database should be initialized but wasn't")
|
|
|
|
} else if !test.expectInitialized && db.Initialized {
|
|
|
|
t.Fatalf("Database was initiailized when it shouldn't")
|
|
|
|
}
|
|
|
|
})
|
2019-01-09 01:26:16 +00:00
|
|
|
}
|
2020-10-12 21:54:26 +00:00
|
|
|
}
|
2019-01-09 01:26:16 +00:00
|
|
|
|
2020-10-12 21:54:26 +00:00
|
|
|
func makeConfig(rootConfig map[string]interface{}, keyValues ...interface{}) map[string]interface{} {
|
|
|
|
if len(keyValues)%2 != 0 {
|
|
|
|
panic("makeConfig must be provided with key and value pairs")
|
2019-01-09 01:26:16 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 21:54:26 +00:00
|
|
|
// Make a copy of the map so there isn't a chance of test bleedover between maps
|
|
|
|
config := make(map[string]interface{}, len(rootConfig)+(len(keyValues)/2))
|
|
|
|
for k, v := range rootConfig {
|
|
|
|
config[k] = v
|
2019-01-09 01:26:16 +00:00
|
|
|
}
|
2020-10-12 21:54:26 +00:00
|
|
|
for i := 0; i < len(keyValues); i += 2 {
|
|
|
|
k := keyValues[i].(string) // Will panic if the key field isn't a string and that's fine in a test
|
|
|
|
v := keyValues[i+1]
|
|
|
|
config[k] = v
|
2019-01-09 01:26:16 +00:00
|
|
|
}
|
2020-10-12 21:54:26 +00:00
|
|
|
return config
|
2019-01-09 01:26:16 +00:00
|
|
|
}
|
|
|
|
|
2021-06-09 21:08:59 +00:00
|
|
|
func TestInfluxdb_CreateUser_DefaultUsernameTemplate(t *testing.T) {
|
2020-09-15 14:01:26 +00:00
|
|
|
cleanup, config := prepareInfluxdbTestContainer(t)
|
2019-01-09 01:26:16 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
db := new()
|
2020-10-15 19:20:12 +00:00
|
|
|
req := dbplugin.InitializeRequest{
|
2020-10-12 21:54:26 +00:00
|
|
|
Config: config.connectionParams(),
|
|
|
|
VerifyConnection: true,
|
2019-01-09 01:26:16 +00:00
|
|
|
}
|
2020-10-12 21:54:26 +00:00
|
|
|
dbtesting.AssertInitialize(t, db, req)
|
2019-01-09 01:26:16 +00:00
|
|
|
|
2020-10-12 21:54:26 +00:00
|
|
|
password := "nuozxby98523u89bdfnkjl"
|
2020-10-15 19:20:12 +00:00
|
|
|
newUserReq := dbplugin.NewUserRequest{
|
|
|
|
UsernameConfig: dbplugin.UsernameMetadata{
|
2021-06-09 21:08:59 +00:00
|
|
|
DisplayName: "token",
|
|
|
|
RoleName: "mylongrolenamewithmanycharacters",
|
2020-10-12 21:54:26 +00:00
|
|
|
},
|
2020-10-15 19:20:12 +00:00
|
|
|
Statements: dbplugin.Statements{
|
2020-10-12 21:54:26 +00:00
|
|
|
Commands: []string{createUserStatements},
|
|
|
|
},
|
|
|
|
Password: password,
|
|
|
|
Expiration: time.Now().Add(1 * time.Minute),
|
2019-01-09 01:26:16 +00:00
|
|
|
}
|
2020-10-12 21:54:26 +00:00
|
|
|
resp := dbtesting.AssertNewUser(t, db, newUserReq)
|
2019-01-09 01:26:16 +00:00
|
|
|
|
2020-10-12 21:54:26 +00:00
|
|
|
if resp.Username == "" {
|
|
|
|
t.Fatalf("Missing username")
|
2019-01-09 01:26:16 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 21:54:26 +00:00
|
|
|
assertCredsExist(t, config.URL().String(), resp.Username, password)
|
2021-06-09 21:08:59 +00:00
|
|
|
|
|
|
|
require.Regexp(t, `^v_token_mylongrolenamew_[a-z0-9]{20}_[0-9]{10}$`, resp.Username)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInfluxdb_CreateUser_CustomUsernameTemplate(t *testing.T) {
|
|
|
|
cleanup, config := prepareInfluxdbTestContainer(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
db := new()
|
|
|
|
|
|
|
|
conf := config.connectionParams()
|
|
|
|
conf["username_template"] = "{{.DisplayName}}_{{random 10}}"
|
|
|
|
|
|
|
|
req := dbplugin.InitializeRequest{
|
|
|
|
Config: conf,
|
|
|
|
VerifyConnection: true,
|
|
|
|
}
|
|
|
|
dbtesting.AssertInitialize(t, db, req)
|
|
|
|
|
|
|
|
password := "nuozxby98523u89bdfnkjl"
|
|
|
|
newUserReq := dbplugin.NewUserRequest{
|
|
|
|
UsernameConfig: dbplugin.UsernameMetadata{
|
|
|
|
DisplayName: "token",
|
|
|
|
RoleName: "mylongrolenamewithmanycharacters",
|
|
|
|
},
|
|
|
|
Statements: dbplugin.Statements{
|
|
|
|
Commands: []string{createUserStatements},
|
|
|
|
},
|
|
|
|
Password: password,
|
|
|
|
Expiration: time.Now().Add(1 * time.Minute),
|
|
|
|
}
|
|
|
|
resp := dbtesting.AssertNewUser(t, db, newUserReq)
|
|
|
|
|
|
|
|
if resp.Username == "" {
|
|
|
|
t.Fatalf("Missing username")
|
|
|
|
}
|
|
|
|
|
|
|
|
assertCredsExist(t, config.URL().String(), resp.Username, password)
|
|
|
|
|
|
|
|
require.Regexp(t, `^token_[a-zA-Z0-9]{10}$`, resp.Username)
|
2019-01-09 01:26:16 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 21:54:26 +00:00
|
|
|
func TestUpdateUser_expiration(t *testing.T) {
|
|
|
|
// This test should end up with a no-op since the expiration doesn't do anything in Influx
|
|
|
|
|
2020-09-15 14:01:26 +00:00
|
|
|
cleanup, config := prepareInfluxdbTestContainer(t)
|
2019-01-09 01:26:16 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
db := new()
|
2020-10-15 19:20:12 +00:00
|
|
|
req := dbplugin.InitializeRequest{
|
2020-10-12 21:54:26 +00:00
|
|
|
Config: config.connectionParams(),
|
|
|
|
VerifyConnection: true,
|
|
|
|
}
|
|
|
|
dbtesting.AssertInitialize(t, db, req)
|
|
|
|
|
|
|
|
password := "nuozxby98523u89bdfnkjl"
|
2020-10-15 19:20:12 +00:00
|
|
|
newUserReq := dbplugin.NewUserRequest{
|
|
|
|
UsernameConfig: dbplugin.UsernameMetadata{
|
2020-10-12 21:54:26 +00:00
|
|
|
DisplayName: "test",
|
|
|
|
RoleName: "test",
|
|
|
|
},
|
2020-10-15 19:20:12 +00:00
|
|
|
Statements: dbplugin.Statements{
|
2020-10-12 21:54:26 +00:00
|
|
|
Commands: []string{createUserStatements},
|
|
|
|
},
|
|
|
|
Password: password,
|
|
|
|
Expiration: time.Now().Add(1 * time.Minute),
|
|
|
|
}
|
|
|
|
newUserResp := dbtesting.AssertNewUser(t, db, newUserReq)
|
|
|
|
|
|
|
|
assertCredsExist(t, config.URL().String(), newUserResp.Username, password)
|
|
|
|
|
2020-10-15 19:20:12 +00:00
|
|
|
renewReq := dbplugin.UpdateUserRequest{
|
2020-10-12 21:54:26 +00:00
|
|
|
Username: newUserResp.Username,
|
2020-10-15 19:20:12 +00:00
|
|
|
Expiration: &dbplugin.ChangeExpiration{
|
2020-10-12 21:54:26 +00:00
|
|
|
NewExpiration: time.Now().Add(5 * time.Minute),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
dbtesting.AssertUpdateUser(t, db, renewReq)
|
|
|
|
|
|
|
|
// Make sure the user hasn't changed
|
|
|
|
assertCredsExist(t, config.URL().String(), newUserResp.Username, password)
|
|
|
|
}
|
2019-01-09 01:26:16 +00:00
|
|
|
|
2020-10-12 21:54:26 +00:00
|
|
|
func TestUpdateUser_password(t *testing.T) {
|
|
|
|
cleanup, config := prepareInfluxdbTestContainer(t)
|
|
|
|
defer cleanup()
|
2019-01-09 01:26:16 +00:00
|
|
|
|
2020-10-12 21:54:26 +00:00
|
|
|
db := new()
|
2020-10-15 19:20:12 +00:00
|
|
|
req := dbplugin.InitializeRequest{
|
2020-10-12 21:54:26 +00:00
|
|
|
Config: config.connectionParams(),
|
|
|
|
VerifyConnection: true,
|
|
|
|
}
|
|
|
|
dbtesting.AssertInitialize(t, db, req)
|
|
|
|
|
|
|
|
initialPassword := "nuozxby98523u89bdfnkjl"
|
2020-10-15 19:20:12 +00:00
|
|
|
newUserReq := dbplugin.NewUserRequest{
|
|
|
|
UsernameConfig: dbplugin.UsernameMetadata{
|
2020-10-12 21:54:26 +00:00
|
|
|
DisplayName: "test",
|
|
|
|
RoleName: "test",
|
|
|
|
},
|
2020-10-15 19:20:12 +00:00
|
|
|
Statements: dbplugin.Statements{
|
2020-10-12 21:54:26 +00:00
|
|
|
Commands: []string{createUserStatements},
|
|
|
|
},
|
|
|
|
Password: initialPassword,
|
|
|
|
Expiration: time.Now().Add(1 * time.Minute),
|
|
|
|
}
|
|
|
|
newUserResp := dbtesting.AssertNewUser(t, db, newUserReq)
|
|
|
|
|
|
|
|
assertCredsExist(t, config.URL().String(), newUserResp.Username, initialPassword)
|
|
|
|
|
|
|
|
newPassword := "y89qgmbzadiygry8uazodijnb"
|
2020-10-15 19:20:12 +00:00
|
|
|
newPasswordReq := dbplugin.UpdateUserRequest{
|
2020-10-12 21:54:26 +00:00
|
|
|
Username: newUserResp.Username,
|
2020-10-15 19:20:12 +00:00
|
|
|
Password: &dbplugin.ChangePassword{
|
2020-10-12 21:54:26 +00:00
|
|
|
NewPassword: newPassword,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
dbtesting.AssertUpdateUser(t, db, newPasswordReq)
|
|
|
|
|
|
|
|
assertCredsDoNotExist(t, config.URL().String(), newUserResp.Username, initialPassword)
|
|
|
|
assertCredsExist(t, config.URL().String(), newUserResp.Username, newPassword)
|
2019-01-09 01:26:16 +00:00
|
|
|
}
|
|
|
|
|
2020-02-05 19:49:02 +00:00
|
|
|
// TestInfluxdb_RevokeDeletedUser tests attempting to revoke a user that was
|
|
|
|
// deleted externally. Guards against a panic, see
|
|
|
|
// https://github.com/hashicorp/vault/issues/6734
|
2020-10-12 21:54:26 +00:00
|
|
|
// Updated to attempt to delete a user that never existed to replicate a similar scenario since
|
|
|
|
// the cleanup function from `prepareInfluxdbTestContainer` does not do anything if using an
|
|
|
|
// external InfluxDB instance rather than spinning one up for the test.
|
2020-02-05 19:49:02 +00:00
|
|
|
func TestInfluxdb_RevokeDeletedUser(t *testing.T) {
|
2020-09-15 14:01:26 +00:00
|
|
|
cleanup, config := prepareInfluxdbTestContainer(t)
|
|
|
|
defer cleanup()
|
2020-02-05 19:49:02 +00:00
|
|
|
|
|
|
|
db := new()
|
2020-10-15 19:20:12 +00:00
|
|
|
req := dbplugin.InitializeRequest{
|
2020-10-12 21:54:26 +00:00
|
|
|
Config: config.connectionParams(),
|
|
|
|
VerifyConnection: true,
|
2020-02-05 19:49:02 +00:00
|
|
|
}
|
2020-10-12 21:54:26 +00:00
|
|
|
dbtesting.AssertInitialize(t, db, req)
|
2020-02-05 19:49:02 +00:00
|
|
|
|
2020-10-12 21:54:26 +00:00
|
|
|
// attempt to revoke a user that does not exist
|
2020-10-15 19:20:12 +00:00
|
|
|
delReq := dbplugin.DeleteUserRequest{
|
2020-10-12 21:54:26 +00:00
|
|
|
Username: "someuser",
|
2020-02-05 19:49:02 +00:00
|
|
|
}
|
2020-10-12 21:54:26 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
_, err := db.DeleteUser(ctx, delReq)
|
2020-02-05 19:49:02 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Expected err, got nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-09 01:26:16 +00:00
|
|
|
func TestInfluxdb_RevokeUser(t *testing.T) {
|
2020-09-15 14:01:26 +00:00
|
|
|
cleanup, config := prepareInfluxdbTestContainer(t)
|
2019-01-09 01:26:16 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
db := new()
|
2020-10-15 19:20:12 +00:00
|
|
|
req := dbplugin.InitializeRequest{
|
2020-10-12 21:54:26 +00:00
|
|
|
Config: config.connectionParams(),
|
|
|
|
VerifyConnection: true,
|
2019-01-09 01:26:16 +00:00
|
|
|
}
|
2020-10-12 21:54:26 +00:00
|
|
|
dbtesting.AssertInitialize(t, db, req)
|
2019-01-09 01:26:16 +00:00
|
|
|
|
2020-10-12 21:54:26 +00:00
|
|
|
initialPassword := "nuozxby98523u89bdfnkjl"
|
2020-10-15 19:20:12 +00:00
|
|
|
newUserReq := dbplugin.NewUserRequest{
|
|
|
|
UsernameConfig: dbplugin.UsernameMetadata{
|
2020-10-12 21:54:26 +00:00
|
|
|
DisplayName: "test",
|
|
|
|
RoleName: "test",
|
|
|
|
},
|
2020-10-15 19:20:12 +00:00
|
|
|
Statements: dbplugin.Statements{
|
2020-10-12 21:54:26 +00:00
|
|
|
Commands: []string{createUserStatements},
|
|
|
|
},
|
|
|
|
Password: initialPassword,
|
|
|
|
Expiration: time.Now().Add(1 * time.Minute),
|
2019-01-09 01:26:16 +00:00
|
|
|
}
|
2020-10-12 21:54:26 +00:00
|
|
|
newUserResp := dbtesting.AssertNewUser(t, db, newUserReq)
|
2019-01-09 01:26:16 +00:00
|
|
|
|
2020-10-12 21:54:26 +00:00
|
|
|
assertCredsExist(t, config.URL().String(), newUserResp.Username, initialPassword)
|
2019-01-09 01:26:16 +00:00
|
|
|
|
2020-10-15 19:20:12 +00:00
|
|
|
delReq := dbplugin.DeleteUserRequest{
|
2020-10-12 21:54:26 +00:00
|
|
|
Username: newUserResp.Username,
|
2019-01-09 01:26:16 +00:00
|
|
|
}
|
2020-10-12 21:54:26 +00:00
|
|
|
dbtesting.AssertDeleteUser(t, db, delReq)
|
|
|
|
assertCredsDoNotExist(t, config.URL().String(), newUserResp.Username, initialPassword)
|
2019-01-09 01:26:16 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 21:54:26 +00:00
|
|
|
func assertCredsExist(t testing.TB, address, username, password string) {
|
|
|
|
t.Helper()
|
|
|
|
err := testCredsExist(address, username, password)
|
2019-01-09 01:26:16 +00:00
|
|
|
if err != nil {
|
2020-10-12 21:54:26 +00:00
|
|
|
t.Fatalf("Could not log in as %q", username)
|
2019-01-09 01:26:16 +00:00
|
|
|
}
|
2020-10-12 21:54:26 +00:00
|
|
|
}
|
2019-01-09 01:26:16 +00:00
|
|
|
|
2020-10-12 21:54:26 +00:00
|
|
|
func assertCredsDoNotExist(t testing.TB, address, username, password string) {
|
|
|
|
t.Helper()
|
|
|
|
err := testCredsExist(address, username, password)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Able to log in as %q when it shouldn't", username)
|
2019-01-09 01:26:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-12 21:54:26 +00:00
|
|
|
func testCredsExist(address, username, password string) error {
|
|
|
|
conf := influx.HTTPConfig{
|
|
|
|
Addr: address,
|
|
|
|
Username: username,
|
|
|
|
Password: password,
|
|
|
|
}
|
|
|
|
cli, err := influx.NewHTTPClient(conf)
|
2019-01-09 01:26:16 +00:00
|
|
|
if err != nil {
|
2021-05-21 14:22:29 +00:00
|
|
|
return fmt.Errorf("Error creating InfluxDB Client: %w", err)
|
2019-01-09 01:26:16 +00:00
|
|
|
}
|
|
|
|
defer cli.Close()
|
|
|
|
_, _, err = cli.Ping(1)
|
|
|
|
if err != nil {
|
2021-05-21 14:22:29 +00:00
|
|
|
return fmt.Errorf("error checking server ping: %w", err)
|
2019-01-09 01:26:16 +00:00
|
|
|
}
|
|
|
|
q := influx.NewQuery("SHOW SERIES ON vault", "", "")
|
|
|
|
response, err := cli.Query(q)
|
|
|
|
if err != nil {
|
2021-05-21 14:22:29 +00:00
|
|
|
return fmt.Errorf("error querying influxdb server: %w", err)
|
2019-01-09 01:26:16 +00:00
|
|
|
}
|
2020-02-05 19:49:02 +00:00
|
|
|
if response != nil && response.Error() != nil {
|
2021-05-21 14:22:29 +00:00
|
|
|
return fmt.Errorf("error using the correct influx database: %w", response.Error())
|
2019-01-09 01:26:16 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|